
    Lgy%                     `    S r SSKr " S S5      rS rS rS rS rS	 rS
 r	S r
S rS rS rg)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                   (    \ rS rSrSrSrS rS rSrg)_JvmBufferNanny   zT
An object that keeps a org.apache.arrow.memory.ArrowBuf's underlying
memory alive.
Nc                 P    UR                  5       nUR                  5         X l        g N)getReferenceManagerretainref_manager)selfjvm_bufr
   s      +lib/python3.12/site-packages/pyarrow/jvm.py__init___JvmBufferNanny.__init__&   s$    113 	&    c                 T    U R                   b  U R                   R                  5         g g r   )r
   release)r   s    r   __del___JvmBufferNanny.__del__.   s%    '$$& (r   )r
   )	__name__
__module____qualname____firstlineno____doc__r
   r   r   __static_attributes__ r   r   r   r      s     K''r   r   c                     [        U 5      nU R                  5       nU R                  5       n[        R                  " X#US9$ )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   memoryAddresscapacitypaforeign_buffer)r   nannyaddresssizes       r   
jvm_bufferr%   3   s=     G$E##%GDW77r   c                     U R                  5       nU R                  5       (       am  US:X  a  [        R                  " 5       $ US:X  a  [        R                  " 5       $ US:X  a  [        R
                  " 5       $ US:X  a  [        R                  " 5       $ gUS:X  a  [        R                  " 5       $ US:X  a  [        R                  " 5       $ US:X  a  [        R                  " 5       $ US:X  a  [        R                  " 5       $ g)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)getBitWidthgetIsSignedr    int8int16int32int64uint8uint16uint32uint64)jvm_type	bit_widths     r   _from_jvm_int_typer7   H   s     $$&I>779"_88:"_88:"_88:  >88:"_99;"_99;"_99; r   c                     U R                  5       R                  5       nUS:X  a  [        R                  " 5       $ US:X  a  [        R                  " 5       $ US:X  a  [        R
                  " 5       $ g)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
HALFSINGLEDOUBLEN)getPrecisiontoStringr    float16float32float64)r5   	precisions     r   _from_jvm_float_typerB   j   s\     %%'002IFzz|	h	zz|	h	zz| 
r   c                    U R                  5       R                  5       nUS:X  a,  U R                  5       S:X  d   e[        R                  " S5      $ US:X  a,  U R                  5       S:X  d   e[        R                  " S5      $ US:X  a,  U R                  5       S:X  d   e[        R
                  " S5      $ US	:X  a,  U R                  5       S:X  d   e[        R
                  " S
5      $ g)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+   r    time32time64)r5   	time_units     r   _from_jvm_time_typerP      s       "++-IH##%+++yy~	m	###%+++yy	m	###%+++yy	l	"##%+++yy 
#r   c                 T   U R                  5       R                  5       nU R                  5       nUb  [        U5      nUS:X  a  [        R
                  " SUS9$ US:X  a  [        R
                  " SUS9$ US:X  a  [        R
                  " SUS9$ US	:X  a  [        R
                  " S
US9$ g)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
NrD   rE   )tzrF   rG   rH   rI   rJ   rK   )rL   r=   getTimezonestrr    	timestamp)r5   rO   timezones      r   _from_jvm_timestamp_typerW      s       "++-I##%Hx=H||CH--	m	#||DX..	m	#||DX..	l	"||DX.. 
#r   c                     U R                  5       R                  5       nUS:X  a  [        R                  " 5       $ US:X  a  [        R                  " 5       $ g)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
DAYrF   N)rL   r=   r    date32date64)r5   day_units     r   _from_jvm_date_typer]      sH     !**,H5yy{	]	"yy{ 
#r   c                    [        U R                  5       5      nU R                  5       nSnUR                  5       (       Gdj  UR	                  5       R                  5       nUS:X  a  [        R                  " 5       nGO:US:X  a  [        U5      nGO'US:X  a  [        U5      nGOUS:X  a  [        R                  " 5       nOUS:X  a  [        R                  " 5       nOUS:X  a%  [        R                  " UR                  5       5      nOUS:X  a  [        R                  " 5       nOUS	:X  a  [        U5      nOUS
:X  a  [        U5      nOqUS:X  a  [!        U5      nO_US:X  a4  [        R"                  " UR%                  5       UR'                  5       5      nO%[)        SR+                  U5      5      e[)        S5      eU R-                  5       nU R/                  5       nUR1                  5       (       a  SnOOUR3                  5        Vs0 s H4  n[        UR5                  5       5      [        UR7                  5       5      _M6     nn[        R8                  " XXW5      $ s  snf )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
NNullIntFloatingPointUtf8BinaryFixedSizeBinaryBoolTime	TimestampDateDecimalzUnsupported JVM type: {}z:JVM field conversion only implemented for primitive types.)rT   getNamegetType	isComplex	getTypeIDr=   r    nullr7   rB   stringbinarygetByteWidthbool_rP   rW   r]   
decimal128r<   getScaleNotImplementedErrorformat
isNullablegetMetadataisEmptyentrySetgetKeygetValuefield)		jvm_fieldnamer5   typtype_strnullablejvm_metadatametadataentrys	            r   r}   r}      s    y  "#D  "H
C%%'002v'')C$X.C(&x0C))+C!))+C**))H1134C((*C%h/C$*84C%h/C"-- 5 5 79J9J9LMC%*11(;= =
 "HJ 	J ##%H((*L ".!6!6!8:!8 'U^^-=)>>!8 	 :88Dx22:s   ;Ic                    U R                  5       nU Vs/ s H  n[        U5      PM     nnU R                  5       nUR                  5       (       a  SnOOUR	                  5        Vs0 s H4  n[        UR                  5       5      [        UR                  5       5      _M6     nn[        R                  " X5      $ s  snf s  snf )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
N)
	getFieldsr}   getCustomMetadatary   rz   rT   r{   r|   r    schema)
jvm_schemafieldsfr   r   r   s         r   r   r     s     !!#F &'1eAhF'//1L ".!6!6!8:!8 'U^^-=)>>!8 	 :99V&& (
:s   B9&;B>c                 R   U R                  5       R                  5       R                  5       (       a8  U R                  5       R	                  5       n[        SR                  U5      5      e[        U R                  5       5      R                  n[        U R                  S5      5       Vs/ s H  n[        U5      PM     nn[        U5      S:X  a  [        R                  " / US9$ U R                  5       nU R!                  5       n[        R"                  R%                  X%XF5      $ s  snf )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.Fr   )type)getFieldrk   rl   getMinorTyper=   ru   rv   r}   r   list
getBuffersr%   lenr    arraygetValueCountgetNullCountArrayfrom_buffers)	jvm_arrayminor_type_strdtypebufbufferslength
null_counts          r   r   r     s     ##%//11"//1::<!228&2HJ 	J )$$&',,Ey33E:;=;3 #;  = 7|qxx''$$&F'')J88  DD=s   )D$c                    [        U R                  5       5      n/ nUR                   H,  nUR                  [	        U R                  U5      5      5        M.     [        R                  R                  UUR                  UR                  S9$ )z
Construct a (Python) RecordBatch from a JVM VectorSchemaRoot

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

Returns
-------
record_batch: pyarrow.RecordBatch
)r   )
r   	getSchemanamesappendr   	getVectorr    RecordBatchfrom_arraysr   )jvm_vector_schema_root	pa_schemaarraysr   s       r   record_batchr   9  sy     -779:IFe2<<TBCD   >>%%## &  r   )r   pyarrowr    r   r%   r7   rB   rP   rW   r]   r}   r   r   r   r   r   r   <module>r      sN   "	 ' '(8*D*6/4&93x'0E<r   