
    IZj                        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
ZddlmZ  G d d          Zd	ej        d
eej        ej        f         fdZed
ej        fd            Ze                    ej                  dej        d
ej        fd            Ze                    ej                  dej        d
ej        fd            Ze                    e          ded
ej        fd            ZdS )    )singledispatch)ListOptionalTupleUnion)
LanceModelmodel_to_dictN   )RecordBatchStreamc                       e Zd ZdZ	 ddeeej        f         dee	         fdZ
deej                 fdZd Zdej        fd	Ze	 dd
ej        dee	         fd            ZdS )AsyncRecordBatchReaderzo
    An async iterator over a stream of RecordBatches.

    Also allows access to the schema of the stream
    Ninnermax_batch_lengthc                     t          |t          j                  r)|                     ||          | _        |j        | _        dS t          |t                    r|| _        |j        | _        dS t          d          )z

        Attributes
        ----------
        schema : pa.Schema
            The schema of the batches produced by the stream.
            Accessing the schema does not consume any data from the stream
        z,inner must be a RecordBatchStream or a TableN)
isinstancepaTable_async_iter_from_table_innerschemar   	TypeError)selfr   r   s      Y/Users/jameslopez/projects/MentorCore/.venv/lib/python3.11/site-packages/lancedb/arrow.py__init__zAsyncRecordBatchReader.__init__   su     eRX&& 	L55e=MNNDK%*\DKKK011 	LDK%*\DKKKJKKK    returnc                 *   K   d | 2              d{V S )z
        Read all the record batches from the stream

        This consumes the entire stream and returns a list of record batches

        If there are a lot of results this may consume a lot of memory
        c                 "   K   g | 3 d {V }|
6 S N ).0batchs     r   
<listcomp>z3AsyncRecordBatchReader.read_all.<locals>.<listcomp>2   s.      ............s   Nr    r   s    r   read_allzAsyncRecordBatchReader.read_all*   s.       /...........r   c                     | S r   r    r$   s    r   	__aiter__z AsyncRecordBatchReader.__aiter__4   s    r   c                 D   K   | j                                          d {V S r   )r   	__anext__r$   s    r   r)   z AsyncRecordBatchReader.__anext__7   s,      [**,,,,,,,,,r   tablec                J   K   |                      |          }|D ]}|W V  dS )z
        Create an AsyncRecordBatchReader from a Table

        This is useful when you have a Table that you want to iterate
        over asynchronously
        )max_chunksizeN)
to_batches)r*   r   batchesr"   s       r   r   z-AsyncRecordBatchReader._async_iter_from_table:   sE       ""1A"BB 	 	EKKKKK	 	r   r   )__name__
__module____qualname____doc__r   r   r   r   r   intr   r   RecordBatchr%   r'   r)   staticmethodr   r    r   r   r   r      s          +/L L&01L #3-L L L L,/R^ 4 / / / /  - - - - - ;? x+3C=   \  r   r   readerr   c                      t           t          j                  st          d                                            fd}t          j                            j         |                      fS )Nz"reader must be a RecordBatchReaderc               3   $   K    V  E d {V  d S r   r    )r"   r6   s   r   all_batchesz peek_reader.<locals>.all_batchesP   s.      r   )r   r   RecordBatchReaderr   read_next_batchfrom_batchesr   )r6   r9   r"   s   ` @r   peek_readerr=   I   s     fb233 ><===""$$E      "&33EL++--PPPPr   c                 @    t          dt          |                      )z+Convert a single data object to a pa.Table.z"to_arrow not implemented for type )NotImplementedErrortypedatas    r   to_arrowrC   W   s!     O4::OO
P
PPr   rB   c                 B    t           j                            | g          S r   )r   r   r<   rA   s    r   _arrow_from_batchrE   ]   s    8  $(((r   c                     | S r   r    rA   s    r   _arrow_from_tablerG   b   s    Kr   c                 0   | st          d          t          | d         t                    rL| d         j                                        }d | D             }t
          j                            ||          S t
          j                            |           S )Nz4Cannot create table from empty list without a schemar   c                 ,    g | ]}t          |          S r    )r	   )r!   ds     r   r#   z$_arrow_from_list.<locals>.<listcomp>n   s     000aq!!000r   )r   )
ValueErrorr   r   	__class__to_arrow_schemar   r   from_pylist)rB   r   dictss      r   _arrow_from_listrP   g   s     QOPPP$q':&& :a"2244004000x##E&#9998%%%r   )	functoolsr   typingr   r   r   r   lancedb.pydanticr   r	   pyarrowr   _lancedbr   r   r:   r4   r=   r   rC   registerrE   rG   listrP   r    r   r   <module>rX      s   % $ $ $ $ $ / / / / / / / / / / / / 6 6 6 6 6 6 6 6     ' ' ' ' ' '9 9 9 9 9 9 9 9xQ Q
2>2//0Q Q Q Q Qbh Q Q Q Q
 
2>"")BN )rx ) ) ) #") 
28BH      
4	&4 	&BH 	& 	& 	& 	& 	& 	&r   