1- # Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
1+ # Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
22# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33#
44# The Universal Permissive License (UPL), Version 1.0
4343try :
4444 java .type ("org.apache.arrow.vector.BaseFixedWidthVector" )
4545except KeyError :
46- raise ImportError ("It is not possible to import Apache Arrow Vector classes because arrow-vector package is not on the class path. Please add this library to your project." )
46+ raise ImportError (
47+ "It is not possible to import Apache Arrow Vector classes because arrow-vector package is not on the class path. Please add this library to your project." )
4748
4849if not __graalpython__ .host_import_enabled :
4950 raise NotImplementedError ("Host lookup is not allowed. You can allow it while building python context." )
50- else :
51- class TinyIntVector :
5251
53- def __len__ (self ):
54- return self .getValueCount ()
5552
56- def __arrow_c_array__ (self , requested_schema = None ):
57- return __graalpython__ .export_arrow_vector (self )
53+ class TinyIntVector :
5854
55+ def __len__ (self ):
56+ return self .getValueCount ()
5957
60- class SmallIntVector :
58+ def __arrow_c_array__ (self , requested_schema = None ):
59+ return __graalpython__ .export_arrow_vector (self )
6160
62- def __len__ (self ):
63- return self .getValueCount ()
6461
65- def __arrow_c_array__ (self , requested_schema = None ):
66- return __graalpython__ .export_arrow_vector (self )
62+ class SmallIntVector :
6763
64+ def __len__ (self ):
65+ return self .getValueCount ()
6866
69- class IntVector :
67+ def __arrow_c_array__ (self , requested_schema = None ):
68+ return __graalpython__ .export_arrow_vector (self )
7069
71- def __len__ (self ):
72- return self .getValueCount ()
7370
74- def __arrow_c_array__ (self , requested_schema = None ):
75- return __graalpython__ .export_arrow_vector (self )
71+ class IntVector :
7672
73+ def __len__ (self ):
74+ return self .getValueCount ()
7775
78- class BigIntVector :
76+ def __arrow_c_array__ (self , requested_schema = None ):
77+ return __graalpython__ .export_arrow_vector (self )
7978
80- def __len__ (self ):
81- return self .getValueCount ()
8279
83- def __arrow_c_array__ (self , requested_schema = None ):
84- return __graalpython__ .export_arrow_vector (self )
80+ class BigIntVector :
8581
82+ def __len__ (self ):
83+ return self .getValueCount ()
8684
87- class BitVector :
85+ def __arrow_c_array__ (self , requested_schema = None ):
86+ return __graalpython__ .export_arrow_vector (self )
8887
89- def __len__ (self ):
90- return self .getValueCount ()
9188
92- def __arrow_c_array__ (self , requested_schema = None ):
93- return __graalpython__ .export_arrow_vector (self )
89+ class BitVector :
9490
91+ def __len__ (self ):
92+ return self .getValueCount ()
9593
96- class Float2Vector :
94+ def __arrow_c_array__ (self , requested_schema = None ):
95+ return __graalpython__ .export_arrow_vector (self )
9796
98- def __len__ (self ):
99- return self .getValueCount ()
10097
101- def __arrow_c_array__ (self , requested_schema = None ):
102- return __graalpython__ .export_arrow_vector (self )
98+ class Float2Vector :
10399
100+ def __len__ (self ):
101+ return self .getValueCount ()
104102
105- class Float4Vector :
103+ def __arrow_c_array__ (self , requested_schema = None ):
104+ return __graalpython__ .export_arrow_vector (self )
106105
107- def __len__ (self ):
108- return self .getValueCount ()
109106
110- def __arrow_c_array__ (self , requested_schema = None ):
111- return __graalpython__ .export_arrow_vector (self )
107+ class Float4Vector :
112108
109+ def __len__ (self ):
110+ return self .getValueCount ()
113111
114- class Float8Vector :
112+ def __arrow_c_array__ (self , requested_schema = None ):
113+ return __graalpython__ .export_arrow_vector (self )
115114
116- def __len__ (self ):
117- return self .getValueCount ()
118115
119- def __arrow_c_array__ (self , requested_schema = None ):
120- return __graalpython__ .export_arrow_vector (self )
116+ class Float8Vector :
121117
118+ def __len__ (self ):
119+ return self .getValueCount ()
122120
123- # Ints
124- int8_vector_class_path = java .type ("org.apache.arrow.vector.TinyIntVector" )
125- int16_vector_class_path = java .type ("org.apache.arrow.vector.SmallIntVector" )
126- int32_vector_class_path = java .type ("org.apache.arrow.vector.IntVector" )
127- int64_vector_class_path = java .type ("org.apache.arrow.vector.BigIntVector" )
128- # Boolean
129- boolean_vector_class_path = java .type ("org.apache.arrow.vector.BitVector" )
130- # Floats
131- float2_vector_class_path = java .type ("org.apache.arrow.vector.Float2Vector" )
132- float4_vector_class_path = java .type ("org.apache.arrow.vector.Float4Vector" )
133- float8_vector_class_path = java .type ("org.apache.arrow.vector.Float8Vector" )
121+ def __arrow_c_array__ (self , requested_schema = None ):
122+ return __graalpython__ .export_arrow_vector (self )
134123
135- polyglot .register_interop_type (int8_vector_class_path , TinyIntVector )
136- polyglot .register_interop_type (int16_vector_class_path , SmallIntVector )
137- polyglot .register_interop_type (int32_vector_class_path , IntVector )
138- polyglot .register_interop_type (int64_vector_class_path , BigIntVector )
139124
140- polyglot . register_interop_type ( boolean_vector_class_path , BitVector )
125+ __enabled_java_integration = False
141126
142- polyglot .register_interop_type (float2_vector_class_path , Float2Vector )
143- polyglot .register_interop_type (float4_vector_class_path , Float4Vector )
144- polyglot .register_interop_type (float8_vector_class_path , Float8Vector )
127+
128+ def enable_java_integration (allow_method_overwrites : bool = False ):
129+ """
130+ This method enables passing Java Apache Arrow vector classes directly to Python code without copying any memory.
131+ It basically calls `polyglot.register_interop_type` on every vector class defined in the library.
132+ Calling the method more than once has no effect.
133+
134+ If allow_method_overwrites=True, defining the same method is explicitly allowed.
135+ """
136+ global __enabled_java_integration
137+ if not __enabled_java_integration :
138+ __enabled_java_integration = True
139+ # Ints
140+ int8_vector_class_path = java .type ("org.apache.arrow.vector.TinyIntVector" )
141+ int16_vector_class_path = java .type ("org.apache.arrow.vector.SmallIntVector" )
142+ int32_vector_class_path = java .type ("org.apache.arrow.vector.IntVector" )
143+ int64_vector_class_path = java .type ("org.apache.arrow.vector.BigIntVector" )
144+ # Boolean
145+ boolean_vector_class_path = java .type ("org.apache.arrow.vector.BitVector" )
146+ # Floats
147+ float2_vector_class_path = java .type ("org.apache.arrow.vector.Float2Vector" )
148+ float4_vector_class_path = java .type ("org.apache.arrow.vector.Float4Vector" )
149+ float8_vector_class_path = java .type ("org.apache.arrow.vector.Float8Vector" )
150+
151+ polyglot .register_interop_type (int8_vector_class_path , TinyIntVector ,
152+ allow_method_overwrites = allow_method_overwrites )
153+ polyglot .register_interop_type (int16_vector_class_path , SmallIntVector ,
154+ allow_method_overwrites = allow_method_overwrites )
155+ polyglot .register_interop_type (int32_vector_class_path , IntVector ,
156+ allow_method_overwrites = allow_method_overwrites )
157+ polyglot .register_interop_type (int64_vector_class_path , BigIntVector ,
158+ allow_method_overwrites = allow_method_overwrites )
159+ polyglot .register_interop_type (boolean_vector_class_path , BitVector ,
160+ allow_method_overwrites = allow_method_overwrites )
161+ polyglot .register_interop_type (float2_vector_class_path , Float2Vector ,
162+ allow_method_overwrites = allow_method_overwrites )
163+ polyglot .register_interop_type (float4_vector_class_path , Float4Vector ,
164+ allow_method_overwrites = allow_method_overwrites )
165+ polyglot .register_interop_type (float8_vector_class_path , Float8Vector ,
166+ allow_method_overwrites = allow_method_overwrites )
0 commit comments