o
    :i*                  
   @   s   d dl mZmZ d dlmZ d dlmZ G dd deZdedee ded	e	fd
dZ
dddefddZG dd dZdddee fddZdeddded	e	def
ddZdS )    )
NamedTupleSequence)ClickHouseType)check_arrowc                   @   sF   e Zd ZU dZeed< eed< dZeed< dZeed< e	dd Z
dS )	TableColumnDefz?
    Simplified ClickHouse Table Column definition for DDL
    namech_typeN	expr_typeexprc                 C   s6   | j  d| jj  }| jr|d| j d| j 7 }|S )N )r   r   r	   r
   )selfr
    r   j/home/aiteam/pcaa-dev/dashboard-backend/venv/lib/python3.10/site-packages/clickhouse_connect/driver/ddl.pycol_expr   s   zTableColumnDef.col_expr)__name__
__module____qualname____doc__str__annotations__r   r	   r
   propertyr   r   r   r   r   r      s   
 r   
table_namecolumnsengineengine_paramsc                 C   sV   d|  dd dd |D  d| d}|r)| D ]\}}|d| d| 7 }q|S )NzCREATE TABLE z (z, c                 s   s    | ]}|j V  qd S N)r   ).0colr   r   r   	<genexpr>   s    zcreate_table.<locals>.<genexpr>z	) ENGINE r   )joinitems)r   r   r   r   stmtkeyvaluer   r   r   create_table   s
   (r$   
arrow_typezpa.DataTypereturnc                 C   sn  t  }|j}|| rdS || rdS || rdS || r"dS || r)dS || r0dS || r7dS |	| r>dS |
| sH|| rJd	S || rQd
S || rXdS || r_dS || rfdS || rt| dd}t| dd}|dkrd}|rd| dS |S dddd}||d}|rd| d| dS d| dS || s|| rdS td| )a  
    Best-effort mapping from common PyArrow types to ClickHouse type names.

    Covers core scalar and common date/time/timestamp types. For anything unknown, we raise, so the
    caller is aware that the automatic mapping is not implemented for that Arrow type.
    Int8Int16Int32Int64UInt8UInt16UInt32UInt64Float32Float64BoolDate32zDateTime64(3)unitstzNDateTimez
DateTime('z')      	   )msusnszDateTime64(z, ')Stringz.Unsupported Arrow type for automatic mapping: )r   typesis_int8is_int16is_int32is_int64is_uint8	is_uint16	is_uint32	is_uint64
is_float16
is_float32
is_float64
is_boolean	is_date32	is_date64is_timestampgetattrget	is_stringis_large_string	TypeError)r%   papatr3   r5   base	scale_mapscaler   r   r   _arrow_type_to_ch    sX   












rY   c                   @   s   e Zd ZdZdefddZdS )_DDLTypea	  
    Minimal helper used to satisfy TableColumnDef.ch_type.

    create_table() only needs ch_type.name when building the DDL string,
    so we'll wrap the ClickHouse type name in this tiny object instead of
    constructing full ClickHouseType instances here.
    r   c                 C   s
   || _ d S r   )r   )r   r   r   r   r   __init__q   s   
z_DDLType.__init__N)r   r   r   r   r   r[   r   r   r   r   rZ   i   s    rZ   schemaz	pa.Schemac                 C   sX   t  }t| |jstdt| g }| D ]}t|j}|t|jt	|d q|S )a  
    Convert a PyArrow Schema into a list of TableColumnDef objects.
    
    This helper uses an *optimistic non-null* strategy: it always produces
    non-nullable ClickHouse types, even though Arrow fields are nullable by
    default.

    Note that if the user inserts a table with nulls into a non-Nullable column,
    ClickHouse will silently convert those nulls to default values due to the default
    server setting input_format_null_as_default=1 and current lack of client-side
    validation on arrow inserts.
    zExpected pyarrow.Schema, got )r   r   )
r   
isinstanceSchemarS   typerY   appendr   r   rZ   )r\   rT   col_defsfieldch_type_namer   r   r   arrow_schema_to_column_defsu   s   
rd   c                 C   s   t |}t| |||S )z
    Helper function to build a CREATE TABLE statement from a PyArrow Schema.

    Internally:
      schema -> arrow_schema_to_column_defs -> create_table(...)
    )rd   r$   )r   r\   r   r   ra   r   r   r   create_table_from_arrow_schema   s   re   N)typingr   r   !clickhouse_connect.datatypes.baser   !clickhouse_connect.driver.optionsr   r   r   dictr$   rY   rZ   listrd   re   r   r   r   r   <module>   s&    I