3030
3131cdef class ArrowArrayImpl:
3232
33- def __cinit__ (self , ArrowType arrow_type , str name , int8_t precision ,
34- int8_t scale , ArrowTimeUnit time_unit ,
35- ArrowType child_arrow_type ):
36- cdef ArrowType storage_type = arrow_type
37- self .arrow_type = arrow_type
38- self .child_arrow_type = child_arrow_type
39- self .time_unit = time_unit
40- self .name = name
33+ def __cinit__ (self ):
4134 self .arrow_array = \
42- < ArrowArray* > cpython.PyMem_Malloc(sizeof(ArrowArray))
43- if arrow_type == NANOARROW_TYPE_TIMESTAMP:
44- storage_type = NANOARROW_TYPE_INT64
45- if time_unit == NANOARROW_TIME_UNIT_MILLI:
46- self .factor = 1e3
47- elif time_unit == NANOARROW_TIME_UNIT_MICRO:
48- self .factor = 1e6
49- elif time_unit == NANOARROW_TIME_UNIT_NANO:
50- self .factor = 1e9
51- else :
52- self .factor = 1
53-
35+ < ArrowArray* > cpython.PyMem_Calloc(1 , sizeof(ArrowArray))
5436 self .arrow_schema = \
55- < ArrowSchema* > cpython.PyMem_Malloc(sizeof(ArrowSchema))
56- if arrow_type == NANOARROW_TYPE_DECIMAL128:
57- self .precision = precision
58- self .scale = scale
59- ArrowSchemaInit(self .arrow_schema)
60- _check_nanoarrow(
61- ArrowSchemaSetTypeDecimal(
62- self .arrow_schema,
63- arrow_type,
64- precision,
65- scale
66- )
67- )
68- elif arrow_type == NANOARROW_TYPE_STRUCT:
69- # Currently struct is used for Sparse vector only
70- build_arrow_schema_for_sparse_vector(self .arrow_schema,
71- child_arrow_type)
72- else :
73- _check_nanoarrow(
74- ArrowSchemaInitFromType(
75- self .arrow_schema,
76- storage_type
77- )
78- )
79- if arrow_type == NANOARROW_TYPE_TIMESTAMP:
80- _check_nanoarrow(
81- ArrowSchemaSetTypeDateTime(
82- self .arrow_schema,
83- arrow_type,
84- time_unit,
85- NULL
86- )
87- )
88- if arrow_type == NANOARROW_TYPE_LIST:
89- # Set the schema for child using child_arrow_type
90- _check_nanoarrow(
91- ArrowSchemaSetType(
92- self .arrow_schema.children[0 ],
93- child_arrow_type
94- )
95- )
96- _check_nanoarrow(
97- ArrowArrayInitFromSchema(
98- self .arrow_array,
99- self .arrow_schema,
100- NULL
101- )
102- )
103- elif arrow_type == NANOARROW_TYPE_STRUCT:
104- _check_nanoarrow(
105- ArrowArrayInitFromSchema(
106- self .arrow_array,
107- self .arrow_schema,
108- NULL
109- )
110- )
111- else : # primitive type array init
112- _check_nanoarrow(
113- ArrowArrayInitFromType(
114- self .arrow_array,
115- storage_type
116- )
117- )
118- _check_nanoarrow(ArrowArrayStartAppending(self .arrow_array))
119- _check_nanoarrow(ArrowSchemaSetName(self .arrow_schema, name.encode()))
37+ < ArrowSchema* > cpython.PyMem_Calloc(1 , sizeof(ArrowSchema))
12038
12139 def __dealloc__ (self ):
12240 if self .arrow_array != NULL :
@@ -128,6 +46,20 @@ cdef class ArrowArrayImpl:
12846 ArrowSchemaRelease(self .arrow_schema)
12947 cpython.PyMem_Free(self .arrow_schema)
13048
49+ cdef int _set_time_unit(self , ArrowTimeUnit time_unit) except - 1 :
50+ """
51+ Sets the time unit and the corresponding factor.
52+ """
53+ self .time_unit = time_unit
54+ if time_unit == NANOARROW_TIME_UNIT_MILLI:
55+ self .time_factor = 1 _000
56+ elif time_unit == NANOARROW_TIME_UNIT_MICRO:
57+ self .time_factor = 1 _000_000
58+ elif time_unit == NANOARROW_TIME_UNIT_NANO:
59+ self .time_factor = 1 _000_000_000
60+ else :
61+ self .time_factor = 1
62+
13163 cdef int append_bytes(self , void * ptr, int64_t num_bytes) except - 1 :
13264 """
13365 Append a value of type bytes to the array.
@@ -318,6 +250,88 @@ cdef class ArrowArrayImpl:
318250 _check_nanoarrow(ArrowArrayFinishBuildingDefault(self .arrow_array,
319251 NULL ))
320252
253+ cdef int populate_from_metadata(self , ArrowType arrow_type, str name,
254+ int8_t precision, int8_t scale,
255+ ArrowTimeUnit time_unit,
256+ ArrowType child_arrow_type) except - 1 :
257+ """
258+ Populate the array from the supplied metadata.
259+ """
260+ cdef ArrowType storage_type = arrow_type
261+ self .arrow_type = arrow_type
262+ self ._set_time_unit(time_unit)
263+ self .name = name
264+ self .child_arrow_type = child_arrow_type
265+ if arrow_type == NANOARROW_TYPE_TIMESTAMP:
266+ storage_type = NANOARROW_TYPE_INT64
267+
268+ _check_nanoarrow(ArrowArrayInitFromType(self .arrow_array,
269+ storage_type))
270+ if arrow_type == NANOARROW_TYPE_DECIMAL128:
271+ self .precision = precision
272+ self .scale = scale
273+ ArrowSchemaInit(self .arrow_schema)
274+ _check_nanoarrow(
275+ ArrowSchemaSetTypeDecimal(
276+ self .arrow_schema,
277+ arrow_type,
278+ precision,
279+ scale
280+ )
281+ )
282+ elif arrow_type == NANOARROW_TYPE_STRUCT:
283+ # Currently struct is used for Sparse vector only
284+ build_arrow_schema_for_sparse_vector(self .arrow_schema,
285+ child_arrow_type)
286+ else :
287+ _check_nanoarrow(
288+ ArrowSchemaInitFromType(
289+ self .arrow_schema,
290+ storage_type
291+ )
292+ )
293+ if arrow_type == NANOARROW_TYPE_TIMESTAMP:
294+ _check_nanoarrow(
295+ ArrowSchemaSetTypeDateTime(
296+ self .arrow_schema,
297+ arrow_type,
298+ time_unit,
299+ NULL
300+ )
301+ )
302+ if arrow_type == NANOARROW_TYPE_LIST:
303+ # Set the schema for child using child_arrow_type
304+ _check_nanoarrow(
305+ ArrowSchemaSetType(
306+ self .arrow_schema.children[0 ],
307+ child_arrow_type
308+ )
309+ )
310+ _check_nanoarrow(
311+ ArrowArrayInitFromSchema(
312+ self .arrow_array,
313+ self .arrow_schema,
314+ NULL
315+ )
316+ )
317+ elif arrow_type == NANOARROW_TYPE_STRUCT:
318+ _check_nanoarrow(
319+ ArrowArrayInitFromSchema(
320+ self .arrow_array,
321+ self .arrow_schema,
322+ NULL
323+ )
324+ )
325+ else : # primitive type array init
326+ _check_nanoarrow(
327+ ArrowArrayInitFromType(
328+ self .arrow_array,
329+ storage_type
330+ )
331+ )
332+ _check_nanoarrow(ArrowArrayStartAppending(self .arrow_array))
333+ _check_nanoarrow(ArrowSchemaSetName(self .arrow_schema, name.encode()))
334+
321335 def get_array_capsule (self ):
322336 """
323337 Internal method for getting a PyCapsule pointer to the array.
0 commit comments