U
    饡cy%                     @   sn   d Z ddlZG dd dZdd Zdd Zd	d
 Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS )a  
Functions to interact with Arrow memory allocated by Arrow Java.

These functions convert the objects holding the metadata, the actual
data is not copied at all.

This will only work with a JVM running in the same process such as provided
through jpype. Modules that talk to a remote JVM like py4j will not work as the
memory addresses reported by them are not reachable in the python process.
    Nc                   @   s$   e Zd ZdZdZdd Zdd ZdS )_JvmBufferNannyz`
    An object that keeps a org.apache.arrow.memory.ArrowBuf's underlying
    memory alive.
    Nc                 C   s   |  }|  || _d S N)ZgetReferenceManagerZretainref_manager)selfjvm_bufr    r   //tmp/pip-unpacked-wheel-qoi2rb4q/pyarrow/jvm.py__init__&   s    z_JvmBufferNanny.__init__c                 C   s   | j d k	r| j   d S r   )r   release)r   r   r   r   __del__.   s    
z_JvmBufferNanny.__del__)__name__
__module____qualname____doc__r   r	   r   r   r   r   r   r      s   r   c                 C   s(   t | }|  }|  }tj|||dS )a)  
    Construct an Arrow buffer from org.apache.arrow.memory.ArrowBuf

    Parameters
    ----------

    jvm_buf: org.apache.arrow.memory.ArrowBuf
        Arrow Buffer representation on the JVM.

    Returns
    -------
    pyarrow.Buffer
        Python Buffer that references the JVM memory.
    )base)r   ZmemoryAddresscapacitypaZforeign_buffer)r   Znannyaddresssizer   r   r   
jvm_buffer3   s    r   c                 C   s   |   }|  rR|dkr t S |dkr0t S |dkr@t S |dkrt S n@|dkrbt S |dkrrt S |dkrt	 S |dkrt
 S dS )z
    Convert a JVM int type to its Python equivalent.

    Parameters
    ----------
    jvm_type : org.apache.arrow.vector.types.pojo.ArrowType$Int

    Returns
    -------
    typ : pyarrow.DataType
              @   N)getBitWidthZgetIsSignedr   Zint8Zint16Zint32Zint64Zuint8Zuint16Zuint32Zuint64)jvm_typeZ	bit_widthr   r   r   _from_jvm_int_typeH   s$    
r   c                 C   s@   |    }|dkrt S |dkr,t S |dkr<t S dS )z
    Convert a JVM float type to its Python equivalent.

    Parameters
    ----------
    jvm_type: org.apache.arrow.vector.types.pojo.ArrowType$FloatingPoint

    Returns
    -------
    typ: pyarrow.DataType
    ZHALFZSINGLEZDOUBLEN)getPrecisiontoStringr   Zfloat16Zfloat32Zfloat64)r   Z	precisionr   r   r   _from_jvm_float_typej   s    r   c                 C   s   |    }|dkr.|  dks$ttdS |dkrP|  dksFttdS |dkrr|  dkshttdS |d	kr|  dksttd
S dS )z
    Convert a JVM time type to its Python equivalent.

    Parameters
    ----------
    jvm_type: org.apache.arrow.vector.types.pojo.ArrowType$Time

    Returns
    -------
    typ: pyarrow.DataType
    SECONDr   sMILLISECONDmsMICROSECONDr   us
NANOSECONDnsN)getUnitr   r   AssertionErrorr   Ztime32Ztime64)r   	time_unitr   r   r   _from_jvm_time_type   s    


r+   c                 C   s   |    }|  }|dk	r$t|}|dkr:tjd|dS |dkrPtjd|dS |dkrftjd|dS |d	kr|tjd
|dS dS )z
    Convert a JVM timestamp type to its Python equivalent.

    Parameters
    ----------
    jvm_type: org.apache.arrow.vector.types.pojo.ArrowType$Timestamp

    Returns
    -------
    typ: pyarrow.DataType
    Nr    r!   )tzr"   r#   r$   r%   r&   r'   )r(   r   ZgetTimezonestrr   	timestamp)r   r*   timezoner   r   r   _from_jvm_timestamp_type   s    r0   c                 C   s0   |    }|dkrt S |dkr,t S dS )z
    Convert a JVM date type to its Python equivalent

    Parameters
    ----------
    jvm_type: org.apache.arrow.vector.types.pojo.ArrowType$Date

    Returns
    -------
    typ: pyarrow.DataType
    ZDAYr"   N)r(   r   r   Zdate32Zdate64)r   Zday_unitr   r   r   _from_jvm_date_type   s
    r1   c                 C   sb  t |  }|  }d}| s|  }|dkr@t }n|dkrRt|}n|dkrdt	|}n|dkrvt
 }n|dkrt }n|dkrt| }nv|dkrt }nd|d	krt|}nR|d
krt|}n@|dkrt|}n.|dkrt| | }ntd|ntd|  }|  }| r@d}ndd | D }t||||S )z
    Construct a Field from a org.apache.arrow.vector.types.pojo.Field
    instance.

    Parameters
    ----------
    jvm_field: org.apache.arrow.vector.types.pojo.Field

    Returns
    -------
    pyarrow.Field
    NZNullZIntZFloatingPointZUtf8BinaryZFixedSizeBinaryZBoolZTimeZ	TimestampDateDecimalzUnsupported JVM type: {}z:JVM field conversion only implemented for primitive types.c                 S   s"   i | ]}t | t | qS r   r-   ZgetKeygetValue.0entryr   r   r   
<dictcomp>   s   
 zfield.<locals>.<dictcomp>)r-   getNamegetType	isComplexZ	getTypeIDr   r   nullr   r   stringbinaryZgetByteWidthZbool_r+   r0   r1   Z
decimal128r   ZgetScaleNotImplementedErrorformatZ
isNullableZgetMetadataisEmptyentrySetfield)Z	jvm_fieldnamer   typZtype_strZnullablejvm_metadatametadatar   r   r   rE      sR    











rE   c                 C   sJ   |   }dd |D }|  }| r,d}ndd | D }t||S )z
    Construct a Schema from a org.apache.arrow.vector.types.pojo.Schema
    instance.

    Parameters
    ----------
    jvm_schema: org.apache.arrow.vector.types.pojo.Schema

    Returns
    -------
    pyarrow.Schema
    c                 S   s   g | ]}t |qS r   )rE   )r8   fr   r   r   
<listcomp>  s     zschema.<locals>.<listcomp>Nc                 S   s"   i | ]}t | t | qS r   r5   r7   r   r   r   r:     s   
 zschema.<locals>.<dictcomp>)Z	getFieldsZgetCustomMetadatarC   rD   r   schema)Z
jvm_schemafieldsrH   rI   r   r   r   rL     s    rL   c                 C   s   |     r*|   }td|t|   j}dd t	| 
dD }t|dkrjtjg |dS |  }|  }tj||||S )z
    Construct an (Python) Array from its JVM equivalent.

    Parameters
    ----------
    jvm_array : org.apache.arrow.vector.ValueVector

    Returns
    -------
    array : Array
    zMCannot convert JVM Arrow array of type {}, complex types not yet implemented.c                 S   s   g | ]}t |qS r   )r   )r8   bufr   r   r   rK   -  s   zarray.<locals>.<listcomp>Fr   )type)ZgetFieldr<   r=   ZgetMinorTyper   rA   rB   rE   rO   listZ
getBufferslenr   arrayZgetValueCountZgetNullCountArrayZfrom_buffers)Z	jvm_arrayZminor_type_strZdtypebufferslengthZ
null_countr   r   r   rR     s     rR   c                 C   sF   t |  }g }|jD ]}|t| | qtjj||j|j	dS )z
    Construct a (Python) RecordBatch from a JVM VectorSchemaRoot

    Parameters
    ----------
    jvm_vector_schema_root : org.apache.arrow.vector.VectorSchemaRoot

    Returns
    -------
    record_batch: pyarrow.RecordBatch
    )rI   )
rL   Z	getSchemanamesappendrR   Z	getVectorr   ZRecordBatchZfrom_arraysrI   )Zjvm_vector_schema_rootZ	pa_schemaZarraysrF   r   r   r   record_batch9  s    
rX   )r   Zpyarrowr   r   r   r   r   r+   r0   r1   rE   rL   rR   rX   r   r   r   r   <module>   s   "<