
    XIgC                         d dl mZmZmZmZmZ ddlmZ ddlm	Z	 ddl
mZmZ eeeeef   Zee   Zerd dlmZ d dlmZ  G d	 d
      Z G d d      Zd
dgZy)    )TYPE_CHECKINGListOptionalUnioncast   )ContributionsAcceptedError   )
StructType)PySparkNotImplementedErrorPySparkTypeError	DataFrame)SparkSessionc            &       r   e Zd ZddZdeddfdZ	 	 	 ddedee   deeee   df   d	ee   ddf
d
Z		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddedee   d	ee   dee   dee   dee   deee
ef      dee   deee
ef      deee
ef      dee   dee   deee
ef      deee
ef      dee   dee   dee   dee   f$dZy)DataFrameWriterc                     || _         y N)	dataframe)selfr   s     e/var/www/html/answerous/venv/lib/python3.12/site-packages/duckdb/experimental/spark/sql/readwriter.py__init__zDataFrameWriter.__init__   s	    "    
table_namereturnNc                 R    | j                   j                  }|j                  |       y r   )r   relationcreate)r   r   r   s      r   saveAsTablezDataFrameWriter.saveAsTable   s    >>**
#r   pathmodepartitionBycompressionc                 v    | j                   j                  }|rt        |rt        |j                  ||       y )N)r#   )r   r   NotImplementedErrorwrite_parquet)r   r    r!   r"   r#   r   s         r   parquetzDataFrameWriter.parquet   s8     >>**%%%%t=r   sepquoteescapeheader	nullValueescapeQuotesquoteAll
dateFormattimestampFormatignoreLeadingWhiteSpaceignoreTrailingWhiteSpacecharToEscapeQuoteEscapingencoding
emptyValuelineSepc                    |dvrt         |	rt         |rt         |rt         |rt         |rt         |rt         | j                  j                  }|j                  ||||||t	        |t
              r|n|dk(  ||
||       y )N)N	overwriteTrue)
r(   na_rep	quotecharr#   
escapecharr+   r4   quotingdate_formattimestamp_format)r%   r   r   	write_csv
isinstancebool)r   r    r!   r#   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r   s                       r   csvzDataFrameWriter.csv(   s    * **%%%%"%%#%%$%%%%%%>>**#'566V;K", 	 	
r   )r   r   NNN)NNNNNNNNNNNNNNNNN)__name__
__module____qualname__r   strr   r   r   r   r'   rB   rC    r   r   r   r      s   #$c $d $ #37%)>> sm> 3S	4/0	>
 c]> 
>$ #%)!# $-1#'37/3$()->B?C37"&$(!%'0
0
 sm0
 c]	0

 c]0
 }0
 0
 tSy)*0
 C=0
 uT3Y/00
 5s+,0
 SM0
 "#0
 "*%c	*:!;0
 #+5s+;"<0
  $,C=!0
" 3-#0
$ SM%0
& #'0
r   r   c            G          e Zd Zd9dZ	 	 	 d:deeeee   f      dee   deeeef      de	ddf
d	Z
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d;deeee   f   deeeef      d
ee   dee   dee   dee   dee   deeeef      deeeef      deeeef      deeeef      dee   dee   dee   dee   dee   dee   deeeef      deeeef      deeeef      dee   dee   deeeef      dee   d eeeef      d!eeeef      d"ee   d#ee   d$ee   d%eeeef      d&eeeef      d'eeeef      d(eeeef      d)ee   ddfFd*Zd+edd,ddfd-Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d<deeee   f   deeeef      d.eeeef      d/eeeef      d0eeeef      d1eeeef      d2eeeef      d3eeeef      d4eeeef      dee   dee   dee   dee   deeeef      d5eeeef      d$ee   d eeeef      d6eeeef      dee   d#ee   d%eeeef      d&eeeef      d'eeeef      d(eeeef      d7eeeef      ddf4d8Zy)=DataFrameReaderc                     || _         y r   )session)r   rM   s     r   r   zDataFrameReader.__init__\   s	    r   Nr    formatschemaoptionsr   r   c                    ddl m} t        |t              st        |rt
        d }|r|j                         }|dk(  s|dk(  r&| j                  j                  j                  |      }n|dk(  r&| j                  j                  j                  |      }nY|dk(  r&| j                  j                  j                  |      }n.t
        | j                  j                  j                  d|       } ||| j                        }|r[t        |t              st
        t        t        |      }|j                         \  }}	|j!                  |      }|j#                  |	      }t$        )Nr   r   rC   tsvjsonr'   zselect * from )'duckdb.experimental.spark.sql.dataframer   rA   rH   ImportErrorr	   lowerrM   connread_csv	read_jsonread_parquetsqlr   r   extract_types_and_names_cast_typestoDFr%   )
r   r    rN   rO   rP   r   reldftypesnamess
             r   loadzDataFrameReader.load_   s(    	F$$,,\\^F&E/ll''0066!ll''11$79$ll''44T:00,,##''.(?@CsDLL)fj100*f-F!99;LE5&BB!!r   r(   r4   r)   r*   commentr+   inferSchemar1   r2   r,   nanValuepositiveInfnegativeInfr/   r0   
maxColumnsmaxCharsPerColumnmaxMalformedLogPerPartitionr!   columnNameOfCorruptRecord	multiLiner3   samplingRatioenforceSchemar5   localer6   pathGlobFilterrecursiveFileLookupmodifiedBeforemodifiedAfterunescapedQuoteHandlingc#                     t        |t              st        |rt        |t              st        |rt        |	rt        |
rt        |rt        |rt
        |rt
        |rt
        |rt
        |rt        |rt        |rt        |rt        |rt        |rt        |rt        |rt        |rt        |rt        |rt        |rt        |rt        | rt        |!rt        |"rt        |rt        d }#d }$|r#t        t        |      }|j                         \  }#}$| j                  j                  j                  |t        |t              r|n|dk(  ||#||||||
      }%ddlm}&  |&|%| j                        }'|$r |'j                  |$ }'|'S )Nr9   )	r+   r(   dtype	na_valuesr;   r<   r4   r>   r?   r   r   )rA   rH   r%   r   r	   ConnectionAbortedErrorr   r\   rM   rW   rX   rB   sql.dataframer   r^   )(r   r    rO   r(   r4   r)   r*   rd   r+   re   r1   r2   r,   rf   rg   rh   r/   r0   ri   rj   rk   r!   rl   rm   r3   rn   ro   r5   rp   r6   rq   rr   rs   rt   ru   rw   rb   r_   r   r`   s(                                           r   rC   zDataFrameReader.csv   s   J $$%%*VZ8,,,,,,",,#,,((((((((,,,,&,,,,$,,,,$,,,,,,,,,,,,,,,,,,!,,%%*f-F!99;LE5ll(('566V;K", ) 
 	.sDLL)%B	r   pathsOptionalPrimitiveTypec                 &   t        |      }t        |      dk7  rt        d      t        |j                               }|dk7  rt	        d      |d   }| j
                  j                  j                  |      }ddlm	}  ||| j
                        }|S )Nr
   z'Only single paths are supported for nowr   zOptions are not supportedr   r   )
listlenr%   keysr	   rM   rW   rZ   rz   r   )	r   r{   rP   inputoption_amountr    r_   r   r`   s	            r   r'   zDataFrameReader.parquet   s    Uu:?%&OPPGLLN+A,-HIIQxll,,T2-sDLL)	r   primitivesAsStringprefersDecimalallowCommentsallowUnquotedFieldNamesallowSingleQuotesallowNumericLeadingZero"allowBackslashEscapingAnyCharacterallowUnquotedControlCharsdropFieldIfAllNullallowNonNumericNumbersc                    |t        d      |t        d      |t        d      |t        d      |t        d      |t        d      |t        d      |	t        d      |
t        d	      |t        d
      |t        d      |t        d      |t        d      |t        d      |t        d      |t        d      |t        d      |t        d      |t        d      |t        d      |t        d      |t        d      |t        d      |t        d      t        |t              r|g}t        |t              r]t	        |      dk(  rC| j
                  j                  j                  |d         }ddlm	}  ||| j
                        }|S t        d      t        ddt        |      j                  d !      )"aW  
        Loads JSON files and returns the results as a :class:`DataFrame`.

        `JSON Lines <http://jsonlines.org/>`_ (newline-delimited JSON) is supported by default.
        For JSON (one record per file), set the ``multiLine`` parameter to ``true``.

        If the ``schema`` parameter is not specified, this function goes
        through the input once to determine the input schema.

        .. versionadded:: 1.4.0

        .. versionchanged:: 3.4.0
            Supports Spark Connect.

        Parameters
        ----------
        path : str, list or :class:`RDD`
            string represents path to the JSON dataset, or a list of paths,
            or RDD of Strings storing JSON objects.
        schema : :class:`pyspark.sql.types.StructType` or str, optional
            an optional :class:`pyspark.sql.types.StructType` for the input schema or
            a DDL-formatted string (For example ``col0 INT, col1 DOUBLE``).

        Other Parameters
        ----------------
        Extra options
            For the extra options, refer to
            `Data Source Option <https://spark.apache.org/docs/latest/sql-data-sources-json.html#data-source-option>`_
            for the version you use.

            .. # noqa

        Examples
        --------
        Write a DataFrame into a JSON file and read it back.

        >>> import tempfile
        >>> with tempfile.TemporaryDirectory() as d:
        ...     # Write a DataFrame into a JSON file
        ...     spark.createDataFrame(
        ...         [{"age": 100, "name": "Hyukjin Kwon"}]
        ...     ).write.mode("overwrite").format("json").save(d)
        ...
        ...     # Read the JSON file as a DataFrame.
        ...     spark.read.json(d).show()
        +---+------------+
        |age|        name|
        +---+------------+
        |100|Hyukjin Kwon|
        +---+------------+
        z$The 'schema' option is not supportedz0The 'primitivesAsString' option is not supportedz,The 'prefersDecimal' option is not supportedz+The 'allowComments' option is not supportedz5The 'allowUnquotedFieldNames' option is not supportedz/The 'allowSingleQuotes' option is not supportedz5The 'allowNumericLeadingZero' option is not supportedz@The 'allowBackslashEscapingAnyCharacter' option is not supportedz"The 'mode' option is not supportedz7The 'columnNameOfCorruptRecord' option is not supportedz(The 'dateFormat' option is not supportedz-The 'timestampFormat' option is not supportedz'The 'multiLine' option is not supportedz7The 'allowUnquotedControlChars' option is not supportedz%The 'lineSep' option is not supportedz+The 'samplingRatio' option is not supportedz0The 'dropFieldIfAllNull' option is not supportedz&The 'encoding' option is not supportedz$The 'locale' option is not supportedz,The 'pathGlobFilter' option is not supportedz1The 'recursiveFileLookup' option is not supportedz,The 'modifiedBefore' option is not supportedz+The 'modifiedAfter' option is not supportedz4The 'allowNonNumericNumbers' option is not supportedr
   r   r   z'Only a single path is supported for now)messageNOT_STR_OR_LIST_OF_RDDr    )arg_namearg_type)error_classmessage_parameters)r	   rA   rH   r~   r   rM   rW   rY   r   r   r   r   typerE   )r   r    rO   r   r   r   r   r   r   r   r!   rl   r/   r0   rm   r   r6   rn   r   r4   rp   rq   rr   rs   rt   r   r_   r   r`   s                                r   rS   zDataFrameReader.json  s   ` ,-STT),B  %,>  $,=  #.,G  (,A  #.,G  .9,R  ,-QRR$0,I  !,-WXX&,?   ,-VWW$0,I  ,-TUU$,=  ),B  ,-UVV,-STT%,>  *,C  %,>  $,=  "-,F  dC 6DtT"4yA~ll''11$q':0sDLL1	,A  #4 & $T
 3 3$ r   )rM   r   rD   )!NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN)NNNNNNNNNNNNNNNNNNNNNNNN)rE   rF   rG   r   r   r   rH   r   r   r|   rc   rB   intfloatrC   r'   rS   rI   r   r   rK   rK   [   s$   
 15 $37	#"uS$s)^,-#" #" z3/0	#"
 )#" 
#"P 48!"&# $!%-126>B?C#'"&%)%)$()-047;AE"3704375948$( $!%59:>594804GtCcN#t z3/0t c]	t
 3-t }t t #t tSy)*t eD#I./t "*%c	*:!;t #+5s+;"<t C=t 3-t c]t  c]!t" SM#t$ "#%t& U38_-'t( $E#s(O4)t* &.eCHo%>+t, sm-t. $,C=/t0 E$),-1t2 $,C=3t4  eSj 125t6  dCi 017t8 SM9t: ;t< #=t> !tSy!12?t@ &eD#I&67AtB !tSy!12CtD  dCi 01EtF !)GtH 
Itlc .E + " 489=5948>B8<>BIM"37$()-04@D!%599="& $59:>5948=A5vCcN#v z3/0v %U49%56	v
 !tSy!12v  dCi 01v "*%c	*:!;v $E$)$45v "*%c	*:!;v -5U495E,Fv smv $,C=v SMv "#v E$),-v  $,E$),<#=!v" ##v$  eSj 12%v& %U49%56'v( 3-)v* +v, !tSy!12-v. &eD#I&67/v0 !tSy!121v2  dCi 013v4 !)tSy)9 :5v6 
7vr   rK   N)typingr   r   r   r   r   	exceptionr	   ra   r   errorsr   r   rB   r   r   rH   PrimitiveTyper|   rT   r   %duckdb.experimental.spark.sql.sessionr   r   rK   __all__rI   r   r   <module>r      se    = = 2  BdE3+, / ABG
 G
Tc cL /
0r   