o
    ED©ië  ã                   @   st   d dl mZ d dlZd dlZd dlmZ ddlm	Z	 ddl
mZ G dd„ deƒZG d	d
„ d
eƒZG dd„ deƒZdS )é    )ÚchainN)Úunion_categoricalsé   )ÚProgress)ÚQueryResultc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚNumpyQueryResultzC
    Stores query result from multiple blocks as numpy arrays.
    c                 C   sb   t |dd ƒ}|d u rd S |jr&| jr| j | ¡ ¡ d S | j | ¡ ¡ d S | js/|j| _d S d S )NÚblock)	ÚgetattrÚnum_rowsÚcolumnarÚdataÚappendÚget_columnsÚextendÚget_rowsÚcolumns_with_types)ÚselfÚpacketr   © r   úW/var/www/Datamplify/venv/lib/python3.10/site-packages/clickhouse_driver/numpy/result.pyÚstore   s   ÿzNumpyQueryResult.storec                 C   sš   | j D ]}|  |¡ q| jr@g }t| jŽ D ])}t|d tjƒr%t |¡}nt|d t	j
ƒr2t|ƒ}ntt |¡ƒ}| |¡ qn| j}| jrK|| jfS |S )z/
        :return: stored query result.
        r   )Úpacket_generatorr   r   Úzipr   Ú
isinstanceÚnpÚndarrayÚconcatenateÚpdÚCategoricalr   Útupler   Úfrom_iterabler   Úwith_column_typesr   )r   r   r   Úcolumn_chunksÚcolumnr   r   r   Ú
get_result   s    

ø

zNumpyQueryResult.get_resultN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r$   r   r   r   r   r      s    r   c                       s<   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡ fdd	„Z‡  ZS )
ÚNumpyProgressQueryResultzx
    Stores query result and progress information from multiple blocks.
    Provides iteration over query progress.
    c                    s"   t ƒ | _tt| ƒj|i |¤Ž d S ©N)r   Úprogress_totalsÚsuperr)   Ú__init__)r   ÚargsÚkwargs©Ú	__class__r   r   r-   B   s   z!NumpyProgressQueryResult.__init__c                 C   ó   | S r*   r   ©r   r   r   r   Ú__iter__G   ó   z!NumpyProgressQueryResult.__iter__c                 C   sD   	 t | jƒ}t|dd ƒ}|r| j |¡ | jj| jjfS |  |¡ q)NTÚprogress)Únextr   r	   r+   Ú	incrementÚrowsÚ
total_rowsr   )r   r   Úprogress_packetr   r   r   Ú__next__J   s   
ÿ
÷z!NumpyProgressQueryResult.__next__c                    s   | D ]}qt t| ƒ ¡ S r*   )r,   r)   r$   )r   Ú_r0   r   r   r$   V   s   z#NumpyProgressQueryResult.get_result)	r%   r&   r'   r(   r-   r4   r<   r$   Ú__classcell__r   r   r0   r   r)   <   s    r)   c                       s4   e Zd ZdZ	d	‡ fdd„	Zdd„ Zdd„ Z‡  ZS )
ÚNumpyIterQueryResultzP
    Provides iteration over returned data by chunks (streaming by chunks).
    Fc                    s$   || _ || _d| _tt| ƒ ¡  d S )NT)r   r!   Úfirst_blockr,   r?   r-   )r   r   r!   r0   r   r   r-   c   s   zNumpyIterQueryResult.__init__c                 C   r2   r*   r   r3   r   r   r   r4   l   r5   zNumpyIterQueryResult.__iter__c                 C   sV   t | jƒ}t|dd ƒ}|d u rg S | jr'| jr'd| _|jg}| | ¡ ¡ |S | ¡ S )Nr   F)r7   r   r	   r@   r!   r   r   r   )r   r   r   Úrvr   r   r   r<   o   s   
zNumpyIterQueryResult.__next__)F)r%   r&   r'   r(   r-   r4   r<   r>   r   r   r0   r   r?   ^   s    þ	r?   )Ú	itertoolsr   Únumpyr   Úpandasr   Úpandas.api.typesr   r6   r   Úresultr   r   r)   Úobjectr?   r   r   r   r   Ú<module>   s    1"