o
    1 i*                     @   s   d dl mZmZmZmZmZ d dlZd dlm	Z	 d dl
mZmZmZ d dlmZ d dlmZmZmZ G dd dZeG d	d
 d
eZeG dd dZG dd deZeG dd deg ee f ZeG dd deZdS )    )CallableDictIterableListOptionalN)_check_pyarrow_version)BlockBlockMetadataSchema)_iter_sliced_blocks)
DeprecatedDeveloperAPI	PublicAPIc                   @   s^   e Zd ZdZdefddZdeee  fddZ	deee  dee
eef  dd	fd
dZdS )"_DatasourceProjectionPushdownMixinz:Mixin for reading operators supporting projection pushdownreturnc                 C      dS )zvReturns ``True`` in case ``Datasource`` supports projection operation
        being pushed down into the reading layerF selfr   r   j/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/ray/data/datasource/datasource.pysupports_projection_pushdown      z?_DatasourceProjectionPushdownMixin.supports_projection_pushdownc                 C   r   )zRetrurns current projectionNr   r   r   r   r   get_current_projection   s   z9_DatasourceProjectionPushdownMixin.get_current_projectioncolumnscolumn_rename_map
Datasourcec                 C   s   | S Nr   )r   r   r   r   r   r   apply_projection      z3_DatasourceProjectionPushdownMixin.apply_projectionN)__name__
__module____qualname____doc__boolr   r   r   strr   r   r   r   r   r   r   r      s    
r   c                   @   s   e Zd ZdZedddZededed fdd	Zde	fd
dZ
dee fddZ	ddedee ded fddZedefddZedefddZdS )r   zInterface for defining a custom :class:`~ray.data.Dataset` datasource.

    To read a datasource into a dataset, use :meth:`~ray.data.read_datasource`.
    r   Readerc                 K   s   t | fi |S z
        Deprecated: Implement :meth:`~ray.data.Datasource.get_read_tasks` and
        :meth:`~ray.data.Datasource.estimate_inmemory_data_size` instead.
        )_LegacyDatasourceReader)r   	read_argsr   r   r   create_reader&   s   zDatasource.create_readerparallelismReadTaskc                 K      t r&   NotImplementedError)r   r*   r(   r   r   r   prepare_read.   s   zDatasource.prepare_readc                 C   s.   t | j}d}||r|dt|  }|S )ztReturn a human-readable name for this datasource.
        This will be used as the names of the read tasks.
        r   N)typer   endswithlen)r   nameZdatasource_suffixr   r   r   get_name6   s
   

zDatasource.get_namec                 C   r,   zReturn an estimate of the in-memory data size, or None if unknown.

        Note that the in-memory data size may be larger than the on-disk data size.
        r-   r   r   r   r   estimate_inmemory_data_size@   r   z&Datasource.estimate_inmemory_data_sizeNper_task_row_limitc                 C   r,   )a  Execute the read and return read tasks.

        Args:
            parallelism: The requested read parallelism. The number of read
                tasks should equal to this value if possible.
            per_task_row_limit: The per-task row limit for the read tasks.
        Returns:
            A list of read tasks that can be executed to read blocks from the
            datasource in parallel.
        r-   r   r*   r7   r   r   r   get_read_tasksG   s   zDatasource.get_read_tasksc                 C   s,   t | jtju}t | jtju}| p| S r   )r0   r9   r   r6   )r   Zhas_implemented_get_read_tasksZ+has_implemented_estimate_inmemory_data_sizer   r   r   should_create_readerV   s   zDatasource.should_create_readerc                 C   r   )z:If ``False``, only launch read tasks on the driver's node.Tr   r   r   r   r   supports_distributed_readsd   r   z%Datasource.supports_distributed_reads)r   r%   r   )r   r    r!   r"   r   r)   intr   r/   r$   r4   r   r6   r9   propertyr#   r:   r;   r   r   r   r   r      s(    

r   c                   @   s8   e Zd ZdZdee fddZdeded fddZd	S )
r%   a  A bound read operation for a :class:`~ray.data.Datasource`.

    This is a stateful class so that reads can be prepared in multiple stages.
    For example, it is useful for :class:`Datasets <ray.data.Dataset>` to know the
    in-memory size of the read prior to executing it.
    r   c                 C   r,   r5   r-   r   r   r   r   r6   s   r   z"Reader.estimate_inmemory_data_sizer*   r+   c                 C   r,   )aM  Execute the read and return read tasks.

        Args:
            parallelism: The requested read parallelism. The number of read
                tasks should equal to this value if possible.

        Returns:
            A list of read tasks that can be executed to read blocks from the
            datasource in parallel.
        r-   )r   r*   r   r   r   r9   z   s   zReader.get_read_tasksN)	r   r    r!   r"   r   r<   r6   r   r9   r   r   r   r   r%   j   s    r%   c                   @   sN   e Zd ZdefddZdee fddZ	dded	ee ded
 fddZ	dS )r'   
datasourcec                 K   s   || _ || _d S r   )_datasource
_read_args)r   r>   r(   r   r   r   __init__   s   
z _LegacyDatasourceReader.__init__r   c                 C   s   d S r   r   r   r   r   r   r6      s   z3_LegacyDatasourceReader.estimate_inmemory_data_sizeNr*   r7   r+   c                 C   s   | j j|fi | jS )a  Execute the read and return read tasks.

        Args:
            parallelism: The requested read parallelism. The number of read
                tasks should equal to this value if possible.
            per_task_row_limit: The per-task row limit for the read tasks.

        Returns:
            A list of read tasks that can be executed to read blocks from the
            datasource in parallel.
        )r?   r/   r@   r8   r   r   r   r9      s   z&_LegacyDatasourceReader.get_read_tasksr   )
r   r    r!   r   rA   r   r<   r6   r   r9   r   r   r   r   r'      s    r'   c                
   @   s   e Zd ZdZ		ddeg ee f deded dee	 fdd	Z
ed
efddZed
ed fddZed
eg ee f fddZed
ee	 fddZd
ee fddZdS )r+   a  A function used to read blocks from the :class:`~ray.data.Dataset`.

    Read tasks are generated by :meth:`~ray.data.Datasource.get_read_tasks`,
    and return a list of ``ray.data.Block`` when called. Initial metadata about the read
    operation can be retrieved via the ``metadata`` attribute prior to executing the
    read. Final metadata is returned after the read along with the blocks.

    Ray will execute read tasks in remote functions to parallelize execution.
    Note that the number of blocks returned can vary at runtime. For example,
    if a task is reading a single large file it can return multiple blocks to
    avoid running out of memory during the read.

    The initial metadata should reflect all the blocks returned by the read,
    e.g., if the metadata says ``num_rows=1000``, the read can return a single
    block of 1000 rows, or multiple blocks with 1000 rows altogether.

    The final metadata (returned with the actual block) reflects the exact
    contents of the block itself.
    Nread_fnmetadataschemar
   r7   c                 C   s   || _ || _|| _|| _d S r   )	_metadata_read_fn_schema_per_task_row_limit)r   rB   rC   rD   r7   r   r   r   rA      s   
zReadTask.__init__r   c                 C      | j S r   )rE   r   r   r   r   rC         zReadTask.metadatac                 C   rI   r   )rG   r   r   r   r   rD      rJ   zReadTask.schemac                 C   rI   r   )rF   r   r   r   r   rB      rJ   zReadTask.read_fnc                 C   rI   )z.Get the per-task row limit for this read task.)rH   r   r   r   r   r7      s   zReadTask.per_task_row_limitc                 c   sP    |   }t|dstd| | jd u r|E d H  d S t|| jE d H  d S )N__iter__zlRead function must return Iterable[Block], got {}. Probably you need to return `[block]` instead of `block`.)rF   hasattrDeprecationWarningformatrH   r   )r   resultr   r   r   __call__   s   


zReadTask.__call__)NN)r   r    r!   r"   r   r   r   r	   r   r<   rA   r=   rC   rD   rB   r7   rP   r   r   r   r   r+      s,    
r+   c                   @   sd   e Zd ZdZdedefddZdee fddZ		dd
edee dee	 fddZ
defddZd	S )RandomIntRowDatasourcea  An example datasource that generates rows with random int64 columns.

    Examples:
        >>> import ray
        >>> from ray.data.datasource import RandomIntRowDatasource
        >>> source = RandomIntRowDatasource() # doctest: +SKIP
        >>> ray.data.read_datasource( # doctest: +SKIP
        ...     source, n=10, num_columns=2).take()
        {'c_0': 1717767200176864416, 'c_1': 999657309586757214}
        {'c_0': 4983608804013926748, 'c_1': 1160140066899844087}
    nnum_columnsc                 C   s   || _ || _dS )zInitialize the datasource that generates random-integer rows.

        Args:
            n: The number of rows to generate.
            num_columns: The number of columns to generate.
        N_n_num_columns)r   rR   rS   r   r   r   rA      s   
zRandomIntRowDatasource.__init__r   c                 C   s   | j | j d S )N   rT   r   r   r   r   r6      s   z2RandomIntRowDatasource.estimate_inmemory_data_sizeNr*   r7   c                    s   t   dd lg }| j}| j}td|| }dtdtdtffdd jdd	 t	|D j
}d}||k rdt||| }	t|	d
|	 | d d d}
|t|	|f fdd	|
||d ||7 }||k s7|S )Nr      countrS   r   c                    s<    j jtjjttjj|| ftjddd t|D dS )N)sizeZdtypec                 S   s   g | ]}d | qS )c_r   .0ir   r   r   
<listcomp>  s    zMRandomIntRowDatasource.get_read_tasks.<locals>.make_block.<locals>.<listcomp>)names)	TableZfrom_arraysnprandomrandintZiinfoZint64maxrangerY   rS   )pyarrowr   r   
make_block  s   z9RandomIntRowDatasource.get_read_tasks.<locals>.make_blockc                 S   s   i | ]	}d | dgqS )r[   r   r   r\   r   r   r   
<dictcomp>  s    z9RandomIntRowDatasource.get_read_tasks.<locals>.<dictcomp>rW   )Znum_rowsZ
size_bytesZinput_filesZ
exec_statsc                    s    | |gS r   r   rg   )ri   r   r   <lambda>#  s   z7RandomIntRowDatasource.get_read_tasks.<locals>.<lambda>)rD   r7   )r   rh   rU   rV   re   r<   r   ra   Zfrom_pydictrf   rD   minr	   appendr+   )r   r*   r7   Z
read_tasksrR   rS   
block_sizerD   r^   rY   metar   )ri   rh   r   r9      s@   

z%RandomIntRowDatasource.get_read_tasksc                 C   r   )zReturn a human-readable name for this datasource.
        This will be used as the names of the read tasks.
        Note: overrides the base `Datasource` method.
        Z	RandomIntr   r   r   r   r   r4   /  r   zRandomIntRowDatasource.get_namer   )r   r    r!   r"   r<   rA   r   r6   r   r+   r9   r$   r4   r   r   r   r   rQ      s    

0rQ   )typingr   r   r   r   r   numpyrb   Zray.data._internal.utilr   Zray.data.blockr   r	   r
   Zray.data.datasource.utilr   Zray.util.annotationsr   r   r   r   r   r%   r'   r+   rQ   r   r   r   r   <module>   s     JB