o
    bi                     @   s   d dl Z d dlZd dlZd dlZd dlmZ d dl	Z
d dlmZ d dlmZ d dlmZ edZe Zdefdd	Zed
dG dd dejZed
dG dd dejZed
dG dd dejZz	ee  W dS  ejyx   Y dS w )    N)parse)pickle_dumps)get_pyarrow_version)	PublicAPIz9.0.0returnc                   C   s   t d uot tkS N)PYARROW_VERSION#MIN_PYARROW_VERSION_SCALAR_SUBCLASS r
   r
   X/home/ubuntu/.local/lib/python3.10/site-packages/ray/air/util/object_extensions/arrow.py_object_extension_type_allowed   s   r   alpha)	stabilityc                       sx   e Zd ZdZd fddZdefddZedej	d	edd fd
dZ
defddZdefddZdd Zdd Z  ZS )ArrowPythonObjectTypezDefines a new Arrow extension type for Python objects.
    We do not require a parametrized type, so the constructor does not
    take any arguments
    r   Nc                    s   t  t d d S )Nzray.data.arrow_pickled_object)super__init__palarge_binaryself	__class__r
   r   r   !   s   zArrowPythonObjectType.__init__c                 C   s   dS )N    r
   r   r
   r
   r   __arrow_ext_serialize__%   s   z-ArrowPythonObjectType.__arrow_ext_serialize__storage_type
serializedc                 C   s   t  S r   )r   )clsr   r   r
   r
   r   __arrow_ext_deserialize__)   s   z/ArrowPythonObjectType.__arrow_ext_deserialize__c                 C      t S )zReturns the scalar class of the extension type. Indexing out of the
        PyArrow extension array will return instances of this type.
        )ArrowPythonObjectScalarr   r
   r
   r   __arrow_ext_scalar_class__/   s   z0ArrowPythonObjectType.__arrow_ext_scalar_class__c                 C   r   )zReturns the array type of the extension type. Selecting one array
        out of the ChunkedArray that makes up a column in a Table with
        this custom type will return an instance of this type.
        )ArrowPythonObjectArrayr   r
   r
   r   __arrow_ext_class__5   s   z)ArrowPythonObjectType.__arrow_ext_class__c                 C   s   t jjjj S )zPandas interoperability type. This describes the Pandas counterpart
        to the Arrow type. See https://pandas.pydata.org/docs/development/extending.html
        for more information.
        )rayairutilobject_extensionspandasPythonObjectDtyper   r
   r
   r   to_pandas_dtype<   s   z%ArrowPythonObjectType.to_pandas_dtypec                 C   s   | j | j|  ffS r   )r   r   r   r   r
   r
   r   
__reduce__C   s   z ArrowPythonObjectType.__reduce__)r   N)__name__
__module____qualname____doc__r   bytesr   classmethodr   DataTyper   typer    r"   r)   r*   __classcell__r
   r
   r   r   r      s     r   c                   @   s    e Zd ZdZdejfddZdS )r   z&Scalar class for ArrowPythonObjectTyper   c                 K   s8   t | jtjstt| j dtt| j	 S )Nz& is not the expected LargeBinaryScalar)

isinstancevaluer   LargeBinaryScalarRuntimeErrorr2   pickleloadBufferReader	as_buffer)r   kwargsr
   r
   r   as_pyO   s
   zArrowPythonObjectScalar.as_pyN)r+   r,   r-   r.   typingAnyr=   r
   r
   r
   r   r   K   s    r   c                   @   sR   e Zd ZdZdejejejej	 f dd fddZ
	ddededejfd	d
ZdS )r!   z%Array class for ArrowPythonObjectTypeobjectsr   c                 C   sX   t | tjr
|  } t }g }| D ]}t|d}|| qtj||j	d}t
||S )Nz)Error pickling object to convert to Arrow)r2   )r4   npndarraytolistr   r   appendr   arrayr   r!   from_storage)r@   type_all_dumped_bytesobjdumped_bytesarrr
   r
   r   from_objects[   s   z#ArrowPythonObjectArray.from_objectsFzero_copy_onlywritablec                 C   s&   t jt| td}|  |d d < |S )N)dtype)rA   emptylenobject	to_pylist)r   rM   rN   rK   r
   r
   r   to_numpyj   s   zArrowPythonObjectArray.to_numpyN)FF)r+   r,   r-   r.   r>   UnionrA   rB   Iterabler?   rL   boolrT   r
   r
   r
   r   r!   W   s    
r!   )r8   r>   numpyrA   pyarrowr   packaging.versionr   parse_version%ray.air.util.object_extensions.pandasr#   ray._private.serializationr   ray._private.arrow_utilsr   ray.util.annotationsr   r	   r   rW   r   ExtensionTyper   ExtensionScalarr   ExtensionArrayr!   register_extension_typeArrowKeyErrorr
   r
   r
   r   <module>   s.    0