o
    EDi                     @   s  d dl Z d dlZd dlmZ d dlZd dlmZmZ d dlm	Z	m
Z
mZ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 d dlmZ d d	lmZ d d
lmZ d dlmZ d dl m!Z!m"Z"m#Z#m$Z$ d dl%m&Z&m'Z' d dl(m)Z)m*Z*m+Z+ d dl,m-Z- d dl.m/Z/ d dl0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 d dl9m:Z: d dl;m<Z<m=Z=m>Z> d dl?m@Z@mAZAmBZBmCZCmDZD d dlEmFZF de _GeHeIZJdZKG dd deZLdS )    N)tzinfo)ABCabstractmethod)IterableOptionalAnyUnionSequenceDict	GeneratorBinaryIO)UnknownTimeZoneError)common)versionget_from_name)ClickHouseType)dynamic)tzutil)	dict_copyStreamContext
coerce_intcoerce_bool)CH_VERSION_WITH_PROTOCOLPROTOCOL_VERSION_WITH_LOW_CARD)ProgrammingErrorOperationalError	DataError)ExternalData)InsertContext)check_arrowcheck_pandascheck_numpycheck_polarspdarrowplIS_PANDAS_2)QuerySummary)	ColumnDef
SettingDefSettingStatus)QueryResultto_arrowto_arrow_batchesQueryContextarrow_buffer)quote_identifieri   $output_format_arrow_string_as_stringc                ,   @   sz  e Zd ZU dZdZeed< dZeed< dZe	 Z
e	 ZdZdZdZdZdZded	ed
ededee deeeef  dee dee fddZdeeeef  fddZdeeeef  deeef fddZdedededee fddZdedefddZdefd d!Zdee fd"d#Z e!defd$d%Z"e!d&d' Z#e!dee fd(d)Z$e!d*efd+d,Z%																dd-ee d.eee&eeef f  deeeef  d/eeeef  d0eeeeeeeef f f  d1ee d2ee d3ee d4ee d5ee ded6eeeef  d7eeeeeef f  dee d8ee' d9eeeef  de(f"d:d;Z)													dd-ee d.eee&eeef f  deeeef  d/eeeef  d0eeeeeeeef f f  d1ee d2ee ded6eeeef  d7eeeeeef f  dee d8ee' d9eeeef  de*fd<d=Z+													dd-ee d.eee&eeef f  deeeef  d/eeeef  d0eeeeeeeef f f  d1ee d2ee ded6eeeef  d7eeeeeef f  dee d8ee' d9eeeef  de*fd>d?Z,													dd-ee d.eee&eeef f  deeeef  d/eeeef  d0eeeeeeeef f f  d1ee d2ee ded6eeeef  d7eeeeeef f  dee d8ee' d9eeeef  de*fd@dAZ-e!						dd-ed.eee&eeef f  deeeef  dBedCed8ee' d9eeeef  de.fdDdEZ/e!						dd-ed.eee&eeef f  deeeef  dBedCed8ee' d9eeeef  de0j1fdFdGZ2											dd-ee d.eee&eeef f  deeeef  d/eeeef  d0eeeef  d1ee d2ee d5ee ded8ee' d9eeeef  fdHdIZ3											dd-ee d.eee&eeef f  deeeef  d/eeeef  d0eeeef  d1ee d2ee d5ee ded8ee' d9eeeef  de*fdJdKZ4																dd-ee d.eee&eeef f  deeeef  d/eeeef  d0eeeef  d1ee d2ee d5ee dLee d6ee d7eeeeeef f  dee ded8ee' dMee d9eeeef  f dNdOZ5																dd-ee d.eee&eeef f  deeeef  d/eeeef  d0eeeef  d1ee d2ee d5ee dLee d6ee d7eeeeeef f  dee ded8ee' dMee d9eeeef  de*f"dPdQZ6																				dd-eeee.f  d.eee&eeef f  deeeef  d/eeeef  d0eeeeeeeef f f  d1ee d2ee d3ee d4ee d5ee dee d6eeeef  d7eeeeeef f  dee dLee dRedSed8ee' dMee d9eeeef  def*dTdUZ7					dd-ed.eee&eeef f  deeeef  dVee d8ee' d9eeeef  fdWdXZ8					dd-ed.eee&eeef f  deeeef  dVee d8ee' d9eeeef  de*fdYdZZ9						[dd-ed.eee&eeef f  deeeef  dVee d8ee' d9eeeef  d\eded] fd^d_Z:						[dd-ed.eee&eeef f  deeeef  dVee d8ee' d9eeeef  d\ede*fd`daZ;deeeef  dVee deeef fdbdcZ<e!						ddded.eee&eeef f  deeee.f deeef dCed8ee' d9eeeef  deeee&e e=f fdfdgZ>e!defdhdiZ?			j							ddkee dee&e&e  dleee@e f dee dme&eA dne&e d3edeeeef  deBd9eeeef  de=fdodpZC									ddkedee dee dlee&e  dme&eA dne&e deBd9eeeef  de=fdqdrZD			ddkededee d9eeeef  de=f
dsdtZE			ddkedued] dee dee d9eeeef  de=fdvdwZF								ddkedleeee&e f  dee dme&eA dne&e d3edeeeef  deee&e&e   d9eeeef  deBfdxdyZGdzedefd{d|ZHd}eddfd~dZIe!deBde=fddZJe!						ddkedlee&e  deee.eKe.ddf eLf dee dBee dee d9eeeef  de=fddZMe!dd ZNe!dd ZOdePfddZQdd ZRdd ZSdS )Clientz(
    Base ClickHouse Connect client
    Ncompressionwrite_compressionr   FTdatabasequery_limituriquery_retriesserver_host_nameapply_server_timezoneutc_tz_awareshow_clickhouse_errorsc	           	      C   s\   t || _t || _|r|dks|| _|durt|| _|| _|| _t|| _	| 
| dS )a  
        Shared initialization of ClickHouse Connect client
        :param database: database name
        :param query_limit: default LIMIT for queries
        :param uri: uri for error messages
        :param utc_tz_aware: Default timezone behavior when the active timezone resolves to UTC.  If True,
          timezone-aware UTC datetimes are returned; otherwise legacy naive datetimes are used.
        __default__N)r   r7   r9   r6   r   r=   r:   r8   boolr<   _init_common_settings)	selfr6   r7   r8   r9   r:   r;   r<   r=    rB   Y/var/www/Datamplify/venv/lib/python3.10/site-packages/clickhouse_connect/driver/client.py__init__2   s   



zClient.__init__c                 C   s  t jd| _}t| jddd\| _}z t |}t|\}}|d u r&|}|dkp-t	|| _
|| _W n tyB   td| Y nw | j
sStjsStdtjd  d}| d	s_td}| d
| d}dd | D | _| trtdr| jdddtid}|dd dkrt| _| djr| dd | djr| djr| dd | dr| dsdt_ d S d S d S )NTzSELECT version(), timezone()F)use_databasealwayszJWarning, server is using an unrecognized timezone %s, will use UTC defaultzblocal timezone %s may return unexpected times due to Daylight Savings Time/Summer Time differencesreadonlyz19.17zSELECT name, value, z- as readonly FROM system.settings LIMIT 10000c                 S   s    i | ]}|d  t di |qS )namerB   )r*   .0rowrB   rB   rC   
<dictcomp>c        z0Client._init_common_settings.<locals>.<dictcomp>use_protocol_versionzSELECT 1 AS checkNativeclient_protocol_version)fmtsettings      s   checkdate_time_input_formatbest_effortallow_experimental_json_type$cast_string_to_dynamic_use_inference1z24.8z24.10r   )!pytzUTC	server_tztuplecommandserver_versiontimezoner   normalize_timezoner   r;   r   loggerwarninglocal_tz_dst_safelocal_tztznamemin_versionr   get_settingquerynamed_resultsserver_settingsr   	raw_queryr   protocol_version_setting_statusis_writableset_client_settingis_setdynamic_modulejson_serialization_format)rA   r;   dst_safer\   rG   rk   	test_datarB   rB   rC   r@   N   sJ   






zClient._init_common_settingsrR   returnc                 C   sB   i }t d}| D ]\}}| |||}|dur|||< q|S )z
        This strips any ClickHouse settings that are not recognized or are read only.
        :param settings:  Dictionary of setting name and values
        :return: A filtered dictionary of settings with values rendered as strings
        invalid_setting_actionN)r   rh   items_validate_setting)rA   rR   	validatedinvalid_actionkeyvalue	str_valuerB   rB   rC   _validate_settingsu   s   
zClient._validate_settingsr|   r}   r{   c                 C   s   t |}|du rd}n|du rd}|| jvrb| j|}| |}|r3|j|kr3|d u s1||jkr3d S |d u s:|jrb|| jv rAd S |dkrMt	d| |S |dkrYt	d| d S t
d	| d
d |S )NTrY   F0sendz6Attempting to send unrecognized or readonly setting %sdropz-Dropping unrecognized or readonly settings %szSetting z is unknown or readonly)strvalid_transport_settingsrk   getget_client_settingr}   rG   optional_transport_settingsrb   rc   r   )rA   r|   r}   r{   r~   setting_defcurrent_settingrB   rB   rC   ry      s*   

 
zClient._validate_settingc                 C   s0   | j |}|stddS t|jdk|jdkS )NFr      )rk   r   r+   r}   rG   )rA   r|   comp_settingrB   rB   rC   rn      s   
zClient._setting_statuscontextc                 C   sH   |j r!|js!| jr!d| j }t|jtr|j|  S |j| S |jS )Nz
 LIMIT )	is_select	has_limitr7   
isinstanceri   bytesfinal_queryencode)rA   r   limitrB   rB   rC   _prep_query   s   
zClient._prep_queryc                 C   sJ   |r#zt |}|| jkr|W S W d S  ty"   td| Y d S w d S )Nz1Unrecognized timezone %s received from ClickHouse)rZ   r`   r\   r   rb   rc   )rA   new_tz
new_tzinforB   rB   rC   _check_tz_change   s   

zClient._check_tz_changec                 C   s   d S NrB   rA   r   rB   rB   rC   _query_with_context      zClient._query_with_contextc                 C      dS )a  
        Set a clickhouse setting for the client after initialization.  If a setting is not recognized by ClickHouse,
        or the setting is identified as "read_only", this call will either throw a Programming exception or attempt
        to send the setting anyway based on the common setting 'invalid_setting_action'
        :param key: ClickHouse setting name
        :param value: ClickHouse setting value
        NrB   )rA   r|   r}   rB   rB   rC   rp          zClient.set_client_settingc                 C   r   )zu
        :param key: The setting key
        :return: The string value of the setting, if it exists, or None
        NrB   )rA   r|   rB   rB   rC   r      r   zClient.get_client_settingaccess_tokenc                 C   r   )zq
        Set the ClickHouse access token for the client
        :param access_token: Access token string
        NrB   )rA   r   rB   rB   rC   set_access_token   r   zClient.set_access_tokenri   
parametersquery_formatscolumn_formatsencodinguse_nonecolumn_oriented	use_numpymax_str_lenquery_tz
column_tzsexternal_datatransport_settingsc                 C   s   |r|   drtdt  dggddtdfS t  }|d= | jd	i |}|j	rU| j
||j|j|j|jd}t|trF| S tt|trP|gS |ggS | |S )
z
        Main query method for SELECT, DESCRIBE and other SQL statements that return a result matrix.  For
        parameters, see the create_query_context method
        :return: QueryResult -- data and metadata from response
        zselect __connect_version__zClickHouse Connect v.u     ⓒ ClickHouse Inc.N)connect_versionStringrA   )r   rR   r   r   rB   )lowerstrip
startswithr,   r   r   localscopycreate_query_context
is_commandr^   r   rR   r   r   r   r(   as_query_resultlistr   )rA   ri   r   rR   r   r   r   r   r   r   r   r   r   r   r<   r   r   kwargsquery_contextresponserB   rB   rC   ri      s$   



zClient.queryc                 C      | j t dddjS )z
        Variation of main query method that returns a stream of column oriented blocks. For
        parameters, see the create_query_context method.
        :return: StreamContext -- Iterable stream context that returns column oriented blocks
        FTr   	streaming)_context_queryr   column_block_streamrA   ri   r   rR   r   r   r   r   r   r   r   r<   r   r   rB   rB   rC   query_column_block_stream      z Client.query_column_block_streamc                 C   r   z
        Variation of main query method that returns a stream of row oriented blocks. For
        parameters, see the create_query_context method.
        :return: StreamContext -- Iterable stream context that returns blocks of rows
        FTr   )r   r   row_block_streamr   rB   rB   rC   query_row_block_stream  r   zClient.query_row_block_streamc                 C   r   r   )r   r   rows_streamr   rB   rB   rC   query_rows_stream   r   zClient.query_rows_streamrQ   rE   c                 C   r   )a  
        Query method that simply returns the raw ClickHouse format bytes
        :param query: Query statement/format string
        :param parameters: Optional dictionary used to format the query
        :param settings: Optional dictionary of ClickHouse settings (key/string values)
        :param fmt: ClickHouse output format
        :param use_database: Send the database parameter to ClickHouse so the command will be executed in the client
         database context.
        :param external_data: External data to send with the query
        :param transport_settings: Optional dictionary of transport level settings (HTTP headers, etc.)
        :return: bytes representing raw ClickHouse return value based on format
        NrB   rA   ri   r   rR   rQ   rE   r   r   rB   rB   rC   rl   5  r   zClient.raw_queryc                 C   r   )a  
       Query method that returns the result as an io.IOBase iterator
       :param query: Query statement/format string
       :param parameters: Optional dictionary used to format the query
       :param settings: Optional dictionary of ClickHouse settings (key/string values)
       :param fmt: ClickHouse output format
       :param use_database  Send the database parameter to ClickHouse so the command will be executed in the client
        database context.
       :param external_data: External data to send with the query.
       :param transport_settings: Optional dictionary of transport level settings (HTTP headers, etc.)
       :return: io.IOBase stream/iterator for the result
       NrB   r   rB   rB   rC   
raw_streamJ  r   zClient.raw_streamc                 C   s"   t   | d | jt ddjS )z
        Query method that returns the results as a numpy array.  For parameter values, see the
        create_query_context method
        :return: Numpy array representing the result set
        numpyT)r   )r"   _add_integration_tagr   r   	np_resultrA   ri   r   rR   r   r   r   r   r   r   r   r   rB   rB   rC   query_np`  s   
zClient.query_npc                 C   $   t   | d | jt dddjS )z
        Query method that returns the results as a stream of numpy arrays.  For parameter values, see the
        create_query_context method
        :return: Generator that yield a numpy array per block representing the result set
        r   Tr   )r"   r   r   r   	np_streamr   rB   rB   rC   query_np_streamv  s   
zClient.query_np_streamuse_na_valuesuse_extended_dtypesc                 C   r   )z
        Query method that results the results as a pandas dataframe.  For parameter values, see the
        create_query_context method
        :return: Pandas dataframe representing the result set
        pandasT)r   	as_pandas)r!   r   r   r   	df_resultrA   ri   r   rR   r   r   r   r   r   r   r   r   r<   r   r   r   r   rB   rB   rC   query_df  s   
zClient.query_dfc                 C   s&   t   | d | jt ddddjS )z
        Query method that returns the results as a StreamContext.  For parameter values, see the
        create_query_context method
        :return: Generator that yields a Pandas dataframe per block representing the result set
        r   T)r   r   r   )r!   r   r   r   	df_streamr   rB   rB   rC   query_df_stream  s   

zClient.query_df_streamr   r   c                 C   sH  |du r| j n|}|rK|jdi d|d|d|d|d|d|d| jd	|d
|d|	d|
d|d|d|d|d|d|d|d|S |	rS|
du rSd}
|du rY|}|ra|du rad}tdi d|d|d|d|d|d|d| jd	|d
|d|	d|
d|d|d|d|d|d|d| jd|d|S )a
  
        Creates or updates a reusable QueryContext object
        :param query: Query statement/format string
        :param parameters: Optional dictionary used to format the query
        :param settings: Optional dictionary of ClickHouse settings (key/string values)
        :param query_formats: See QueryContext __init__ docstring
        :param column_formats: See QueryContext __init__ docstring
        :param encoding: See QueryContext __init__ docstring
        :param use_none: Use None for ClickHouse NULL instead of default values.  Note that using None in Numpy
          arrays will force the numpy array dtype to 'object', which is often inefficient.  This effect also
          will impact the performance of Pandas dataframes.
        :param column_oriented: Deprecated. Controls orientation of the QueryResult result_set property
        :param use_numpy: Return QueryResult columns as one-dimensional numpy arrays
        :param max_str_len: Limit returned ClickHouse String values to this length, which allows a Numpy
          structured array even with ClickHouse variable length String columns.  If 0, Numpy arrays for
          String columns will always be object arrays
        :param context: An existing QueryContext to be updated with any provided parameter values
        :param query_tz: Either a string or a pytz tzinfo object.  (Strings will be converted to tzinfo objects).
          Values for any DateTime or DateTime64 column in the query will be converted to Python datetime.datetime
          objects with the selected timezone.
        :param column_tzs: A dictionary of column names to tzinfo objects (or strings that will be converted to
          tzinfo objects).  The timezone will be applied to datetime objects returned in the query
        :param utc_tz_aware: Override the client default for handling UTC results.  True forces timezone-aware
          UTC datetimes while False returns naive UTC datetimes.
        :param use_na_values: Deprecated alias for use_advanced_dtypes
        :param as_pandas Return the result columns as pandas.Series objects
        :param streaming Marker used to correctly configure streaming queries
        :param external_data ClickHouse "external data" to send with query
        :param use_extended_dtypes:  Only relevant to Pandas Dataframe queries.  Use Pandas "missing types", such as
          pandas.NA and pandas.NaT for ClickHouse NULL values, as well as extended Pandas dtypes such as IntegerArray
          and StringArray.  Defaulted to True for query_df methods
        :param transport_settings: Optional dictionary of transport level settings (HTTP headers, etc.)
        :return: Reusable QueryContext
        Nri   r   rR   r   r   r   r\   r   r   r   r   r   r   r<   r   r   r   r   r   r   Tapply_server_tzrB   )r<   updated_copyr\   r/   r;   )rA   ri   r   rR   r   r   r   r   r   r   r   r   r   r   r<   r   r   r   r   r   r   resolved_utc_tz_awarerB   rB   rC   r     s   7	
	
zClient.create_query_contextuse_stringsc              	   C   6   t   | d | ||}t| j|||d||dS )ae  
        Query method using the ClickHouse Arrow format to return a PyArrow table
        :param query: Query statement/format string
        :param parameters: Optional dictionary used to format the query
        :param settings: Optional dictionary of ClickHouse settings (key/string values)
        :param use_strings: Convert ClickHouse String type to Arrow string type (instead of binary)
        :param external_data: ClickHouse "external data" to send with query
        :param transport_settings: Optional dictionary of transport level settings (HTTP headers, etc.)
        :return: PyArrow.Table
        r%   ArrowrQ   r   r   )r    r   _update_arrow_settingsr-   rl   rA   ri   r   rR   r   r   r   rB   rB   rC   query_arrow*     
zClient.query_arrowc              	   C   r   )a  
        Query method that returns the results as a stream of Arrow tables
        :param query: Query statement/format string
        :param parameters: Optional dictionary used to format the query
        :param settings: Optional dictionary of ClickHouse settings (key/string values)
        :param use_strings: Convert ClickHouse String type to Arrow string type (instead of binary)
        :param external_data: ClickHouse "external data" to send with query
        :param transport_settings: Optional dictionary of transport level settings (HTTP headers, etc.)
        :return: Generator that yields a PyArrow.Table for per block representing the result set
        r%   ArrowStreamr   )r    r   r   r.   r   r   rB   rB   rC   query_arrow_streamE  r   zClient.query_arrow_streamr   dataframe_library)pd.DataFramezpl.DataFramec           
      C   s   t   |dkr!t  | d tstddtjdtjfdd}n |dkr9t	  | d dtjdt
jfdd}ntd	| d
| j||||||d}	||	S )a  
        Query method using the ClickHouse Arrow format to return a DataFrame
        with PyArrow dtype backend. This provides better performance and memory efficiency
        compared to the standard query_df method, though fewer output formatting options.

        :param query: Query statement/format string
        :param parameters: Optional dictionary used to format the query
        :param settings: Optional dictionary of ClickHouse settings (key/string values)
        :param use_strings: Convert ClickHouse String type to Arrow string type (instead of binary)
        :param external_data: ClickHouse "external data" to send with query
        :param transport_settings: Optional dictionary of transport level settings (HTTP headers, etc.)
        :param dataframe_library: Library to use for DataFrame creation ("pandas" or "polars")
        :return: DataFrame (pandas or polars based on dataframe_library parameter)
        r   ?PyArrow-backed dtypes are only supported when using pandas 2.x.tablerv   c                 S      | j tjddS NF)types_mappersafe	to_pandasr$   
ArrowDtyper   rB   rB   rC   	converter     z(Client.query_df_arrow.<locals>.converterpolarsc                 S   
   t | S r   r&   
from_arrowr   rB   rB   rC   r        
5dataframe_library must be 'pandas' or 'polars', got '')ri   r   rR   r   r   r   )r    r!   r   r'   r   r%   Tabler$   	DataFramer#   r&   
ValueErrorr   )
rA   ri   r   rR   r   r   r   r   r   arrow_tablerB   rB   rC   query_df_arrow`  s*   

	zClient.query_df_arrowc           
         s   t   |dkrt  | d tstdddd n |d	kr3t  | d	 dtjdtj	fd
d nt
d| d| ||}| j|||d||d}tj| fdd}	t||	 S )ao  
        Query method that returns the results as a stream of DataFrames with PyArrow dtype backend.
        Each DataFrame represents a block from the ClickHouse response.

        :param query: Query statement/format string
        :param parameters: Optional dictionary used to format the query
        :param settings: Optional dictionary of ClickHouse settings (key/string values)
        :param use_strings: Convert ClickHouse String type to Arrow string type (instead of binary)
        :param external_data: ClickHouse "external data" to send with query
        :param transport_settings: Optional dictionary of transport level settings (HTTP headers, etc.)
        :param dataframe_library: Library to use for DataFrame creation ("pandas" or "polars")
        :return: StreamContext that yields DataFrames (pandas or polars based on dataframe_library parameter)
        r   r   r   arrow.Tablerv   r   c                 S   r   r   r   r   rB   rB   rC   r     r   z/Client.query_df_arrow_stream.<locals>.converterr   c                 S   r   r   r   r   rB   rB   rC   r     r   r   r   r   r   c                  3   s    D ]}  | V  qd S r   rB   )batchr   readerrB   rC   df_generator  s   z2Client.query_df_arrow_stream.<locals>.df_generatorN)r   r  rv   r   )r    r!   r   r'   r   r#   r%   r   r&   r   r   r   r   ipcopen_streamr   )
rA   ri   r   rR   r   r   r   r   r   r  rB   r  rC   query_df_arrow_stream  s&   

zClient.query_df_arrow_streamc                 C   sz   t |}| jr| j|d< | t}|d u r!|jr|jsd|t< |S ||jkr;|js3tdt d| |r7dnd|t< |S )Nr6   rY   zCannot change readonly z to r   )r   r6   rn   arrow_str_settingro   rq   r   )rA   rR   r   
str_statusrB   rB   rC   r     s   


zClient._update_arrow_settingscmddatac                 C   r   )a  
        Client method that returns a single value instead of a result set
        :param cmd: ClickHouse query/command as a python format string
        :param parameters: Optional dictionary of key/values pairs to be formatted
        :param data: Optional 'data' for the command (for INSERT INTO in particular)
        :param settings: Optional dictionary of ClickHouse settings (key/string values)
        :param use_database: Send the database parameter to ClickHouse so the command will be executed in the client
         database context. Otherwise, no database will be specified with the command.  This is useful for determining
         the default user database
        :param external_data: ClickHouse "external data" to send with command/query
        :param transport_settings: Optional dictionary of transport level settings (HTTP headers, etc.)
        :return: Decoded response from ClickHouse as either a string, int, or sequence of strings, or QuerySummary
        if no data returned
        NrB   )rA   r  r   r  rR   rE   r   r   rB   rB   rC   r^     r   zClient.commandc                 C   r   )z
        Validate the connection, does not throw an Exception (see debug logs)
        :return: ClickHouse server is up and reachable
        NrB   rA   rB   rB   rC   ping  r   zClient.ping*r   column_namescolumn_typescolumn_type_namesc              
   C   sj   |	du s|	j r|du rtdd|	du r!| j||||||||
d}	|dur0|	j s-tdd||	_| |	S )a  
        Method to insert multiple rows/data matrix of native Python objects.  If context is specified arguments
        other than data are ignored
        :param table: Target table
        :param data: Sequence of sequences of Python data
        :param column_names: Ordered list of column names or '*' if column types should be retrieved from the
            ClickHouse table definition
        :param database: Target database -- will use client default database if not specified.
        :param column_types: ClickHouse column types.  If set then column data does not need to be retrieved from
            the server
        :param column_type_names: ClickHouse column type names.  If set then column data does not need to be
            retrieved from the server
        :param column_oriented: If true the data is already "pivoted" in column form
        :param settings: Optional dictionary of ClickHouse settings (key/string values)
        :param context: Optional reusable insert context to allow repeated inserts into the same table with
            different data batches
        :param transport_settings: Optional dictionary of transport level settings (HTTP headers, etc.)
        :return: QuerySummary with summary information, throws exception if insert fails
        NzNo data specified for insert)r   z;Attempting to insert new data with non-empty insert context)emptyr   create_insert_contextr  data_insert)rA   r   r  r  r6   r  r  r   rR   r   r   rB   rB   rC   insert  s"   


zClient.insertc
           
      C   s`   t   | d |du r"|du r|j}nt|t|jkr"tdd| j||||||||	|d	S )aX  
        Insert a pandas DataFrame into ClickHouse.  If context is specified arguments other than df are ignored
        :param table: ClickHouse table
        :param df: two-dimensional pandas dataframe
        :param database: Optional ClickHouse database
        :param settings: Optional dictionary of ClickHouse settings (key/string values)
        :param column_names: An optional list of ClickHouse column names.  If not set, the DataFrame column names
           will be used
        :param column_types: ClickHouse column types.  If set then column data does not need to be retrieved from
            the server
        :param column_type_names: ClickHouse column type names.  If set then column data does not need to be
            retrieved from the server
        :param context: Optional reusable insert context to allow repeated inserts into the same table with
            different data batches
        :param transport_settings: Optional dictionary of transport level settings (HTTP headers, etc.)
        :return: QuerySummary with summary information, throws exception if insert fails
        r   Nz4DataFrame column count does not match insert_columns)r  r  rR   r   r   )r!   r   columnslenr   r  )
rA   r   dfr6   rR   r  r  r  r   r   rB   rB   rC   	insert_df'  s"   

zClient.insert_dfc           
      C   sd   t   | d d|v s|s|n| d| }| jdv r| jnd}t||\}}	| |||	|d|S )a  
        Insert a PyArrow table DataFrame into ClickHouse using raw Arrow format
        :param table: ClickHouse table
        :param arrow_table: PyArrow Table object
        :param database: Optional ClickHouse database
        :param settings: Optional dictionary of ClickHouse settings (key/string values)
        :param transport_settings: Optional dictionary of transport level settings (HTTP headers, etc.)
        r%   .)zstdlz4Nr   )r    r   r5   r0   
raw_insert)
rA   r   r   r6   rR   r   
full_tabler4   r  insert_blockrB   rB   rC   insert_arrowR  s   
zClient.insert_arrowr  c           
   
   C   s:  t   tdurt|tjrd}n#tdurt|tjrd}ntdu r)tdu r)tdtdt|j |dkrst	s=t
ddd |j D }|rTt
d	d
| dz
tjj|dd}W n/ tyr }	 ztd|	 |	d}	~	ww z| }W n ty }	 ztd|	 |	d}	~	ww | | | j|||||dS )a  
        Insert a pandas DataFrame with PyArrow backend or a polars DataFrame into ClickHouse using Arrow format.
        This method is optimized for DataFrames that already use Arrow format, providing
        better performance than the standard insert_df method.
        
        Validation is performed and an exception will be raised if this requirement is not met.
        Polars DataFrames are natively Arrow-based and don't require additional validation.
        
        :param table: ClickHouse table name
        :param df: Pandas DataFrame with PyArrow dtype backend or Polars DataFrame
        :param database: Optional ClickHouse database name
        :param settings: Optional dictionary of ClickHouse settings (key/string values)
        :param transport_settings: Optional dictionary of transport level settings (HTTP headers, etc.)
        :return: QuerySummary with summary information, throws exception if insert fails
        Nr   r   zPA DataFrame library (pandas or polars) must be installed to use insert_df_arrow.z>df must be either a pandas DataFrame or polars DataFrame, got r   c                 S   s    g | ]\}}t |tjs|qS rB   )r   r$   r   )rJ   coldtyperB   rB   rC   
<listcomp>  rM   z*Client.insert_df_arrow.<locals>.<listcomp>zVinsert_df_arrow requires all columns to use PyArrow dtypes. Non-Arrow columns found: [z, z]. F)preserve_indexz3Failed to convert pandas DataFrame to Arrow table: z3Failed to convert polars DataFrame to Arrow table: )r   r   r6   rR   r   )r    r$   r   r   r&   ImportError	TypeErrortype__name__r'   r   dtypesrx   joinr%   r   from_pandas	Exceptionr   r-   r   r!  )
rA   r   r  r6   rR   r   df_libnon_arrow_colsr   erB   rB   rC   insert_df_arrowf  sH   
zClient.insert_df_arrowc
              
      sb  |}
d|vr|rt | dt | }
nt |}
g }|du r5|du r5| jd|
 |d}dd | D }|du sBt|trQ|dkrQdd |D }d	d |D }nt|trY|g}t|d
krctd|s|rodd |D }n*dd |D  z fdd|D }W n ty } ztd| d| dd}~ww t|t|krtddt	|
|||||	|dS )a  
        Builds a reusable insert context to hold state for a duration of an insert
        :param table: Target table
        :param database: Target database.  If not set, uses the client default database
        :param column_names: Optional ordered list of column names.  If not set, all columns ('*') will be assumed
          in the order specified by the table definition
        :param database: Target database -- will use client default database if not specified
        :param column_types: ClickHouse column types.  Optional  Sequence of ClickHouseType objects.  If neither column
           types nor column type names are set, actual column types will be retrieved from the server.
        :param column_type_names: ClickHouse column type names.  Specified column types by name string
        :param column_oriented: If true the data is already "pivoted" in column form
        :param settings: Optional dictionary of ClickHouse settings (key/string values)
        :param data: Initial dataset for insert
        :param transport_settings: Optional dictionary of transport level settings (HTTP headers, etc.)
        :return: Reusable insert context
        r  NzDESCRIBE TABLE )rR   c                 S   s&   g | ]}|d  dvrt di |qS )default_type)ALIASMATERIALIZEDrB   )r)   rI   rB   rB   rC   r$    s    z0Client.create_insert_context.<locals>.<listcomp>r  c                 S      g | ]}|j qS rB   rH   rJ   cdrB   rB   rC   r$        c                 S   r5  rB   ch_typer7  rB   rB   rC   r$    r9  r   z)Column names must be specified for insertc                 S      g | ]}t |qS rB   r   rJ   rH   rB   rB   rC   r$        c                 S   s   i | ]}|j |qS rB   r6  )rJ   drB   rB   rC   rL     r>  z0Client.create_insert_context.<locals>.<dictcomp>c                    s   g | ]} | j qS rB   r:  r=  
column_maprB   rC   r$    s    zUnrecognized column z
 in table z&Column names do not match column types)r   rR   r   r  )
r1   ri   rj   r   r   r  r   KeyErrorr   r   )rA   r   r  r6   r  r  r   rR   r  r   r  column_defsdescribe_resultexrB   r@  rC   r    sH   

zClient.create_insert_contextversion_strc                 C   s   z/dd | j dD }|dgdt|   dd |dD }|dgdt|   W n tyA   td| j | Y dS w t||D ]\}}||krR d	S ||k rY dS qGd	S )
a  
        Determine whether the connected server is at least the submitted version
        For Altinity Stable versions like 22.8.15.25.altinitystable
        the last condition in the first list comprehension expression is added
        :param version_str: A version string consisting of up to 4 integers delimited by dots
        :return: True if version_str is greater than the server_version, False if less than
        c                 S   s   g | ]
}|  rt|qS rB   )	isnumericintrJ   xrB   rB   rC   r$    s    z&Client.min_version.<locals>.<listcomp>r  r      c                 S   r<  rB   )rH  rI  rB   rB   rC   r$    r>  zTServer %s or requested version %s does not match format of numbers separated by dotsFT)r_   splitextendr  r   rb   rc   zip)rA   rF  server_partsversion_partsrJ  yrB   rB   rC   rg     s$   zClient.min_versionrH   c                 C   r   )zJTransport hook to surface 3rd party lib integration info (default: no-op).NrB   )rA   rH   rB   rB   rC   r     r   zClient._add_integration_tagc                 C   r   )z
        Subclass implementation of the data insert
        :context: InsertContext parameter object
        :return: No return, throws an exception if the insert fails
        NrB   r   rB   rB   rC   r    r   zClient.data_insertr   c                 C   r   )ar  
        Insert data already formatted in a bytes object
        :param table: Table name (whether qualified with the database name or not)
        :param column_names: Sequence of column names
        :param insert_block: Binary or string data already in a recognized ClickHouse format
        :param settings:  Optional dictionary of ClickHouse settings (key/string values)
        :param fmt: Valid clickhouse format
        :param compression:  Recognized ClickHouse `Accept-Encoding` header compression value
        :param transport_settings: Optional dictionary of transport level settings (HTTP headers, etc.)
        NrB   )rA   r   r  r   rR   rQ   r4   r   rB   rB   rC   r    r   zClient.raw_insertc                 C   r   )ze
        Subclass implementation to close the connection to the server/deallocate the client
        NrB   r  rB   rB   rC   close  r   zClient.closec                 C   r   )zX
        Subclass implementation to disconnect all "re-used" client connections
        NrB   r  rB   rB   rC   close_connections  r   zClient.close_connectionslclsc                 K   s2   |  }|d || | | jdi |S )NrA   rB   )r   popupdater   r   )rA   rT  	overridesr   rB   rB   rC   r   %  s   

zClient._context_queryc                 C   s   | S r   rB   r  rB   rB   rC   	__enter__+  s   zClient.__enter__c                 C   s   |    d S r   )rR  )rA   exc_type	exc_valueexc_tracebackrB   rB   rC   __exit__.  s   zClient.__exit__)NNNNNNNNNNNNNNNN)NNNNNNNNNNNNN)NNNTNN)NNNNNNNNNNN)NNNNNNNNFr   NNNNNFFNNN)NNNNN)NNNNNr   )
NNr  NNNFNNN)	NNNNNNNNN)NNN)NNNNFNNN)NNNNNN)Tr)  
__module____qualname____doc__r4   r   __annotations__r5   rm   setr   r   r6   max_error_messager;   r<   r=   rH  r   r   r?   rD   r@   r
   r   r   ry   r+   rn   r/   r   r   r   r   r   rp   r   r   r	   r   r,   ri   r   r   r   r   r   rl   ioIOBaser   r   r   r   r   r   r   r   r   r  r   r(   r^   r  r   r   r   r  r  r!  r1  r  rg   r   r  r   r   r  rR  rS  dictr   rX  r\  rB   rB   rB   rC   r3   #   s  
 
&'

		

(	

	

	

	

	

	

	

	

i


9
2




	


/

-

>		
?


r3   )Mrc  loggingdatetimer   rZ   abcr   r   typingr   r   r   r   r	   r
   r   r   pytz.exceptionsr   clickhouse_connectr   clickhouse_connect.commonr   %clickhouse_connect.datatypes.registryr   !clickhouse_connect.datatypes.baser   clickhouse_connect.datatypesr   rr   clickhouse_connect.driverr    clickhouse_connect.driver.commonr   r   r   r   #clickhouse_connect.driver.constantsr   r   $clickhouse_connect.driver.exceptionsr   r   r   "clickhouse_connect.driver.externalr    clickhouse_connect.driver.insertr   !clickhouse_connect.driver.optionsr    r!   r"   r#   r$   r%   r&   r'   !clickhouse_connect.driver.summaryr(    clickhouse_connect.driver.modelsr)   r*   r+   clickhouse_connect.driver.queryr,   r-   r.   r/   r0   !clickhouse_connect.driver.bindingr1   DEFAULT_BUFFER_SIZE	getLoggerr)  rb   r	  r3   rB   rB   rB   rC   <module>   s6    ((
