o
    $i                     @   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mZ d dlmZ e  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y}   Y dS w )    N)parse)pickle_dumps)_check_pyarrow_version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   a/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/air/util/object_extensions/arrow.py_object_extension_type_allowed   s   r   alpha)	stabilityc                       s   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def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__4   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__:   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_dtypeA   s   z%ArrowPythonObjectType.to_pandas_dtypec                 C   s   | j | j|  ffS r   )r   r   r   r   r   r   r   
__reduce__H   s   z ArrowPythonObjectType.__reduce__c                 C   s   t t| | jj| jfS r   )hashtyper   idextension_namer   r   r   r   __hash__O   s   zArrowPythonObjectType.__hash__)r   N)__name__
__module____qualname____doc__r   bytesr   classmethodr   DataTyper   r-   r!   r#   r*   r+   intr0   __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   sF   | j d u rd S t| j tjstt| j  dtt| j 	 S )Nz& is not the expected LargeBinaryScalar)
value
isinstancer   LargeBinaryScalarRuntimeErrorr-   pickleloadBufferReader	as_buffer)r   kwargsr   r   r   as_pyW   s   
zArrowPythonObjectScalar.as_pyN)r1   r2   r3   r4   typingAnyrC   r   r   r   r   r    S   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   sV   t | tjr
|  } t }g }| D ]}t|d}|| qtj||j	d}|
|S )Nz)Error pickling object to convert to Arrow)r-   )r;   npndarraytolistr   r   appendr   arrayr   
wrap_array)rF   type_all_dumped_bytesobjdumped_bytesarrr   r   r   from_objectsg   s   
z#ArrowPythonObjectArray.from_objectsFzero_copy_onlywritablec                 C   s&   t jt| td}|  |d d < |S )N)dtype)rG   emptylenobject	to_pylist)r   rS   rT   rQ   r   r   r   to_numpyv   s   zArrowPythonObjectArray.to_numpyN)FF)r1   r2   r3   r4   rD   UnionrG   rH   IterablerE   rR   boolrZ   r   r   r   r   r"   c   s    
r"   )r>   rD   numpyrG   pyarrowr   packaging.versionr   parse_version%ray.air.util.object_extensions.pandasr$   ray._common.serializationr   ray._private.arrow_utilsr   r   ray.util.annotationsr   r
   r	   r]   r   ExtensionTyper   ExtensionScalarr    ExtensionArrayr"   register_extension_typeArrowKeyErrorr   r   r   r   <module>   s0    3