Skip to content

Commit f5064ad

Browse files
committed
Added global flag which checks whether the interop behaviour is registered already
1 parent 7cbe6d1 commit f5064ad

File tree

1 file changed

+71
-64
lines changed

1 file changed

+71
-64
lines changed
Lines changed: 71 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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
@@ -48,99 +48,106 @@
4848

4949
if not __graalpython__.host_import_enabled:
5050
raise NotImplementedError("Host lookup is not allowed. You can allow it while building python context.")
51-
else:
52-
class TinyIntVector:
5351

54-
def __len__(self):
55-
return self.getValueCount()
5652

57-
def __arrow_c_array__(self, requested_schema=None):
58-
return __graalpython__.export_arrow_vector(self)
53+
class TinyIntVector:
5954

55+
def __len__(self):
56+
return self.getValueCount()
6057

61-
class SmallIntVector:
58+
def __arrow_c_array__(self, requested_schema=None):
59+
return __graalpython__.export_arrow_vector(self)
6260

63-
def __len__(self):
64-
return self.getValueCount()
6561

66-
def __arrow_c_array__(self, requested_schema=None):
67-
return __graalpython__.export_arrow_vector(self)
62+
class SmallIntVector:
6863

64+
def __len__(self):
65+
return self.getValueCount()
6966

70-
class IntVector:
67+
def __arrow_c_array__(self, requested_schema=None):
68+
return __graalpython__.export_arrow_vector(self)
7169

72-
def __len__(self):
73-
return self.getValueCount()
7470

75-
def __arrow_c_array__(self, requested_schema=None):
76-
return __graalpython__.export_arrow_vector(self)
71+
class IntVector:
7772

73+
def __len__(self):
74+
return self.getValueCount()
7875

79-
class BigIntVector:
76+
def __arrow_c_array__(self, requested_schema=None):
77+
return __graalpython__.export_arrow_vector(self)
8078

81-
def __len__(self):
82-
return self.getValueCount()
8379

84-
def __arrow_c_array__(self, requested_schema=None):
85-
return __graalpython__.export_arrow_vector(self)
80+
class BigIntVector:
8681

82+
def __len__(self):
83+
return self.getValueCount()
8784

88-
class BitVector:
85+
def __arrow_c_array__(self, requested_schema=None):
86+
return __graalpython__.export_arrow_vector(self)
8987

90-
def __len__(self):
91-
return self.getValueCount()
9288

93-
def __arrow_c_array__(self, requested_schema=None):
94-
return __graalpython__.export_arrow_vector(self)
89+
class BitVector:
9590

91+
def __len__(self):
92+
return self.getValueCount()
9693

97-
class Float2Vector:
94+
def __arrow_c_array__(self, requested_schema=None):
95+
return __graalpython__.export_arrow_vector(self)
9896

99-
def __len__(self):
100-
return self.getValueCount()
10197

102-
def __arrow_c_array__(self, requested_schema=None):
103-
return __graalpython__.export_arrow_vector(self)
98+
class Float2Vector:
10499

100+
def __len__(self):
101+
return self.getValueCount()
105102

106-
class Float4Vector:
103+
def __arrow_c_array__(self, requested_schema=None):
104+
return __graalpython__.export_arrow_vector(self)
107105

108-
def __len__(self):
109-
return self.getValueCount()
110106

111-
def __arrow_c_array__(self, requested_schema=None):
112-
return __graalpython__.export_arrow_vector(self)
107+
class Float4Vector:
113108

109+
def __len__(self):
110+
return self.getValueCount()
114111

115-
class Float8Vector:
112+
def __arrow_c_array__(self, requested_schema=None):
113+
return __graalpython__.export_arrow_vector(self)
116114

117-
def __len__(self):
118-
return self.getValueCount()
119115

120-
def __arrow_c_array__(self, requested_schema=None):
121-
return __graalpython__.export_arrow_vector(self)
116+
class Float8Vector:
117+
118+
def __len__(self):
119+
return self.getValueCount()
120+
121+
def __arrow_c_array__(self, requested_schema=None):
122+
return __graalpython__.export_arrow_vector(self)
123+
124+
125+
__interop_registered = False
122126

123127

124128
def register_interop_behavior():
125-
# Ints
126-
int8_vector_class_path = java.type("org.apache.arrow.vector.TinyIntVector")
127-
int16_vector_class_path = java.type("org.apache.arrow.vector.SmallIntVector")
128-
int32_vector_class_path = java.type("org.apache.arrow.vector.IntVector")
129-
int64_vector_class_path = java.type("org.apache.arrow.vector.BigIntVector")
130-
# Boolean
131-
boolean_vector_class_path = java.type("org.apache.arrow.vector.BitVector")
132-
# Floats
133-
float2_vector_class_path = java.type("org.apache.arrow.vector.Float2Vector")
134-
float4_vector_class_path = java.type("org.apache.arrow.vector.Float4Vector")
135-
float8_vector_class_path = java.type("org.apache.arrow.vector.Float8Vector")
136-
137-
polyglot.register_interop_type(int8_vector_class_path, TinyIntVector)
138-
polyglot.register_interop_type(int16_vector_class_path, SmallIntVector)
139-
polyglot.register_interop_type(int32_vector_class_path, IntVector)
140-
polyglot.register_interop_type(int64_vector_class_path, BigIntVector)
141-
142-
polyglot.register_interop_type(boolean_vector_class_path, BitVector)
143-
144-
polyglot.register_interop_type(float2_vector_class_path, Float2Vector)
145-
polyglot.register_interop_type(float4_vector_class_path, Float4Vector)
146-
polyglot.register_interop_type(float8_vector_class_path, Float8Vector)
129+
global __interop_registered
130+
if not __interop_registered:
131+
__interop_registered = True
132+
# Ints
133+
int8_vector_class_path = java.type("org.apache.arrow.vector.TinyIntVector")
134+
int16_vector_class_path = java.type("org.apache.arrow.vector.SmallIntVector")
135+
int32_vector_class_path = java.type("org.apache.arrow.vector.IntVector")
136+
int64_vector_class_path = java.type("org.apache.arrow.vector.BigIntVector")
137+
# Boolean
138+
boolean_vector_class_path = java.type("org.apache.arrow.vector.BitVector")
139+
# Floats
140+
float2_vector_class_path = java.type("org.apache.arrow.vector.Float2Vector")
141+
float4_vector_class_path = java.type("org.apache.arrow.vector.Float4Vector")
142+
float8_vector_class_path = java.type("org.apache.arrow.vector.Float8Vector")
143+
144+
polyglot.register_interop_type(int8_vector_class_path, TinyIntVector)
145+
polyglot.register_interop_type(int16_vector_class_path, SmallIntVector)
146+
polyglot.register_interop_type(int32_vector_class_path, IntVector)
147+
polyglot.register_interop_type(int64_vector_class_path, BigIntVector)
148+
149+
polyglot.register_interop_type(boolean_vector_class_path, BitVector)
150+
151+
polyglot.register_interop_type(float2_vector_class_path, Float2Vector)
152+
polyglot.register_interop_type(float4_vector_class_path, Float4Vector)
153+
polyglot.register_interop_type(float8_vector_class_path, Float8Vector)

0 commit comments

Comments
 (0)