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.
2
2
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3
3
#
4
4
# The Universal Permissive License (UPL), Version 1.0
43
43
try :
44
44
java .type ("org.apache.arrow.vector.BaseFixedWidthVector" )
45
45
except 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." )
47
48
48
49
if not __graalpython__ .host_import_enabled :
49
50
raise NotImplementedError ("Host lookup is not allowed. You can allow it while building python context." )
50
- else :
51
- class TinyIntVector :
52
51
53
- def __len__ (self ):
54
- return self .getValueCount ()
55
52
56
- def __arrow_c_array__ (self , requested_schema = None ):
57
- return __graalpython__ .export_arrow_vector (self )
53
+ class TinyIntVector :
58
54
55
+ def __len__ (self ):
56
+ return self .getValueCount ()
59
57
60
- class SmallIntVector :
58
+ def __arrow_c_array__ (self , requested_schema = None ):
59
+ return __graalpython__ .export_arrow_vector (self )
61
60
62
- def __len__ (self ):
63
- return self .getValueCount ()
64
61
65
- def __arrow_c_array__ (self , requested_schema = None ):
66
- return __graalpython__ .export_arrow_vector (self )
62
+ class SmallIntVector :
67
63
64
+ def __len__ (self ):
65
+ return self .getValueCount ()
68
66
69
- class IntVector :
67
+ def __arrow_c_array__ (self , requested_schema = None ):
68
+ return __graalpython__ .export_arrow_vector (self )
70
69
71
- def __len__ (self ):
72
- return self .getValueCount ()
73
70
74
- def __arrow_c_array__ (self , requested_schema = None ):
75
- return __graalpython__ .export_arrow_vector (self )
71
+ class IntVector :
76
72
73
+ def __len__ (self ):
74
+ return self .getValueCount ()
77
75
78
- class BigIntVector :
76
+ def __arrow_c_array__ (self , requested_schema = None ):
77
+ return __graalpython__ .export_arrow_vector (self )
79
78
80
- def __len__ (self ):
81
- return self .getValueCount ()
82
79
83
- def __arrow_c_array__ (self , requested_schema = None ):
84
- return __graalpython__ .export_arrow_vector (self )
80
+ class BigIntVector :
85
81
82
+ def __len__ (self ):
83
+ return self .getValueCount ()
86
84
87
- class BitVector :
85
+ def __arrow_c_array__ (self , requested_schema = None ):
86
+ return __graalpython__ .export_arrow_vector (self )
88
87
89
- def __len__ (self ):
90
- return self .getValueCount ()
91
88
92
- def __arrow_c_array__ (self , requested_schema = None ):
93
- return __graalpython__ .export_arrow_vector (self )
89
+ class BitVector :
94
90
91
+ def __len__ (self ):
92
+ return self .getValueCount ()
95
93
96
- class Float2Vector :
94
+ def __arrow_c_array__ (self , requested_schema = None ):
95
+ return __graalpython__ .export_arrow_vector (self )
97
96
98
- def __len__ (self ):
99
- return self .getValueCount ()
100
97
101
- def __arrow_c_array__ (self , requested_schema = None ):
102
- return __graalpython__ .export_arrow_vector (self )
98
+ class Float2Vector :
103
99
100
+ def __len__ (self ):
101
+ return self .getValueCount ()
104
102
105
- class Float4Vector :
103
+ def __arrow_c_array__ (self , requested_schema = None ):
104
+ return __graalpython__ .export_arrow_vector (self )
106
105
107
- def __len__ (self ):
108
- return self .getValueCount ()
109
106
110
- def __arrow_c_array__ (self , requested_schema = None ):
111
- return __graalpython__ .export_arrow_vector (self )
107
+ class Float4Vector :
112
108
109
+ def __len__ (self ):
110
+ return self .getValueCount ()
113
111
114
- class Float8Vector :
112
+ def __arrow_c_array__ (self , requested_schema = None ):
113
+ return __graalpython__ .export_arrow_vector (self )
115
114
116
- def __len__ (self ):
117
- return self .getValueCount ()
118
115
119
- def __arrow_c_array__ (self , requested_schema = None ):
120
- return __graalpython__ .export_arrow_vector (self )
116
+ class Float8Vector :
121
117
118
+ def __len__ (self ):
119
+ return self .getValueCount ()
122
120
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 )
134
123
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 )
139
124
140
- polyglot . register_interop_type ( boolean_vector_class_path , BitVector )
125
+ __enabled_java_integration = False
141
126
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