1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 #include <arrow-glib/basic-array.hpp>
21 #include <arrow-glib/data-type.hpp>
22 #include <arrow-glib/enums.h>
23 #include <arrow-glib/error.hpp>
24 #include <arrow-glib/field.hpp>
25 #include <arrow-glib/type.hpp>
26
27 G_BEGIN_DECLS
28
29 /**
30 * SECTION: composite-data-type
31 * @section_id: composite-data-type-classes
32 * @title: Composite data type classes
33 * @include: arrow-glib/arrow-glib.h
34 *
35 * #GArrowListDataType is a class for list data type.
36 *
37 * #GArrowLargeListDataType is a class for 64-bit offsets list data type.
38 *
39 * #GArrowStructDataType is a class for struct data type.
40 *
41 * #GArrowMapDataType is a class for map data type.
42 *
43 * #GArrowUnionDataType is a base class for union data types.
44 *
45 * #GArrowSparseUnionDataType is a class for sparse union data type.
46 *
47 * #GArrowDenseUnionDataType is a class for dense union data type.
48 *
49 * #GArrowDictionaryDataType is a class for dictionary data type.
50 */
51
G_DEFINE_TYPE(GArrowListDataType,garrow_list_data_type,GARROW_TYPE_DATA_TYPE)52 G_DEFINE_TYPE(GArrowListDataType,
53 garrow_list_data_type,
54 GARROW_TYPE_DATA_TYPE)
55
56 static void
57 garrow_list_data_type_init(GArrowListDataType *object)
58 {
59 }
60
61 static void
garrow_list_data_type_class_init(GArrowListDataTypeClass * klass)62 garrow_list_data_type_class_init(GArrowListDataTypeClass *klass)
63 {
64 }
65
66 /**
67 * garrow_list_data_type_new:
68 * @field: The field of elements
69 *
70 * Returns: The newly created list data type.
71 */
72 GArrowListDataType *
garrow_list_data_type_new(GArrowField * field)73 garrow_list_data_type_new(GArrowField *field)
74 {
75 auto arrow_field = garrow_field_get_raw(field);
76 auto arrow_data_type =
77 std::make_shared<arrow::ListType>(arrow_field);
78
79 GArrowListDataType *data_type =
80 GARROW_LIST_DATA_TYPE(g_object_new(GARROW_TYPE_LIST_DATA_TYPE,
81 "data-type", &arrow_data_type,
82 NULL));
83 return data_type;
84 }
85
86 /**
87 * garrow_list_data_type_get_value_field:
88 * @list_data_type: A #GArrowListDataType.
89 *
90 * Returns: (transfer full): The field of value.
91 *
92 * Deprecated: 0.13.0:
93 * Use garrow_list_data_type_get_field() instead.
94 */
95 GArrowField *
garrow_list_data_type_get_value_field(GArrowListDataType * list_data_type)96 garrow_list_data_type_get_value_field(GArrowListDataType *list_data_type)
97 {
98 return garrow_list_data_type_get_field(list_data_type);
99 }
100
101 /**
102 * garrow_list_data_type_get_field:
103 * @list_data_type: A #GArrowListDataType.
104 *
105 * Returns: (transfer full): The field of value.
106 *
107 * Since: 0.13.0
108 */
109 GArrowField *
garrow_list_data_type_get_field(GArrowListDataType * list_data_type)110 garrow_list_data_type_get_field(GArrowListDataType *list_data_type)
111 {
112 auto data_type = GARROW_DATA_TYPE(list_data_type);
113 auto arrow_data_type = garrow_data_type_get_raw(data_type);
114 auto arrow_list_data_type =
115 static_cast<arrow::ListType *>(arrow_data_type.get());
116
117 auto arrow_field = arrow_list_data_type->value_field();
118 return garrow_field_new_raw(&arrow_field, nullptr);
119 }
120
121
G_DEFINE_TYPE(GArrowLargeListDataType,garrow_large_list_data_type,GARROW_TYPE_DATA_TYPE)122 G_DEFINE_TYPE(GArrowLargeListDataType,
123 garrow_large_list_data_type,
124 GARROW_TYPE_DATA_TYPE)
125
126 static void
127 garrow_large_list_data_type_init(GArrowLargeListDataType *object)
128 {
129 }
130
131 static void
garrow_large_list_data_type_class_init(GArrowLargeListDataTypeClass * klass)132 garrow_large_list_data_type_class_init(GArrowLargeListDataTypeClass *klass)
133 {
134 }
135
136 /**
137 * garrow_large_list_data_type_new:
138 * @field: The field of elements
139 *
140 * Returns: The newly created large list data type.
141 *
142 * Since: 0.16.0
143 */
144 GArrowLargeListDataType *
garrow_large_list_data_type_new(GArrowField * field)145 garrow_large_list_data_type_new(GArrowField *field)
146 {
147 auto arrow_field = garrow_field_get_raw(field);
148 auto arrow_data_type =
149 std::make_shared<arrow::LargeListType>(arrow_field);
150
151 GArrowLargeListDataType *data_type =
152 GARROW_LARGE_LIST_DATA_TYPE(g_object_new(GARROW_TYPE_LARGE_LIST_DATA_TYPE,
153 "data-type", &arrow_data_type,
154 NULL));
155 return data_type;
156 }
157
158 /**
159 * garrow_large_list_data_type_get_field:
160 * @large_list_data_type: A #GArrowLargeListDataType.
161 *
162 * Returns: (transfer full): The field of value.
163 *
164 * Since: 0.16.0
165 */
166 GArrowField *
garrow_large_list_data_type_get_field(GArrowLargeListDataType * large_list_data_type)167 garrow_large_list_data_type_get_field(GArrowLargeListDataType *large_list_data_type)
168 {
169 auto data_type = GARROW_DATA_TYPE(large_list_data_type);
170 auto arrow_data_type = garrow_data_type_get_raw(data_type);
171 auto arrow_large_list_data_type =
172 static_cast<arrow::LargeListType *>(arrow_data_type.get());
173
174 auto arrow_field = arrow_large_list_data_type->value_field();
175 return garrow_field_new_raw(&arrow_field, nullptr);
176 }
177
178
G_DEFINE_TYPE(GArrowStructDataType,garrow_struct_data_type,GARROW_TYPE_DATA_TYPE)179 G_DEFINE_TYPE(GArrowStructDataType,
180 garrow_struct_data_type,
181 GARROW_TYPE_DATA_TYPE)
182
183 static void
184 garrow_struct_data_type_init(GArrowStructDataType *object)
185 {
186 }
187
188 static void
garrow_struct_data_type_class_init(GArrowStructDataTypeClass * klass)189 garrow_struct_data_type_class_init(GArrowStructDataTypeClass *klass)
190 {
191 }
192
193 /**
194 * garrow_struct_data_type_new:
195 * @fields: (element-type GArrowField): The fields of the struct.
196 *
197 * Returns: The newly created struct data type.
198 */
199 GArrowStructDataType *
garrow_struct_data_type_new(GList * fields)200 garrow_struct_data_type_new(GList *fields)
201 {
202 std::vector<std::shared_ptr<arrow::Field>> arrow_fields;
203 for (auto *node = fields; node; node = g_list_next(node)) {
204 auto field = GARROW_FIELD(node->data);
205 auto arrow_field = garrow_field_get_raw(field);
206 arrow_fields.push_back(arrow_field);
207 }
208
209 auto arrow_data_type = std::make_shared<arrow::StructType>(arrow_fields);
210 auto data_type = g_object_new(GARROW_TYPE_STRUCT_DATA_TYPE,
211 "data-type", &arrow_data_type,
212 NULL);
213 return GARROW_STRUCT_DATA_TYPE(data_type);
214 }
215
216 /**
217 * garrow_struct_data_type_get_n_fields:
218 * @struct_data_type: A #GArrowStructDataType.
219 *
220 * Returns: The number of fields of the struct data type.
221 *
222 * Since: 0.12.0
223 */
224 gint
garrow_struct_data_type_get_n_fields(GArrowStructDataType * struct_data_type)225 garrow_struct_data_type_get_n_fields(GArrowStructDataType *struct_data_type)
226 {
227 auto arrow_data_type = garrow_data_type_get_raw(GARROW_DATA_TYPE(struct_data_type));
228 return arrow_data_type->num_fields();
229 }
230
231 /**
232 * garrow_struct_data_type_get_fields:
233 * @struct_data_type: A #GArrowStructDataType.
234 *
235 * Returns: (transfer full) (element-type GArrowField):
236 * The fields of the struct data type.
237 *
238 * Since: 0.12.0
239 */
240 GList *
garrow_struct_data_type_get_fields(GArrowStructDataType * struct_data_type)241 garrow_struct_data_type_get_fields(GArrowStructDataType *struct_data_type)
242 {
243 auto data_type = GARROW_DATA_TYPE(struct_data_type);
244 auto arrow_data_type = garrow_data_type_get_raw(data_type);
245 auto arrow_fields = arrow_data_type->fields();
246
247 GList *fields = NULL;
248 for (auto arrow_field : arrow_fields) {
249 fields = g_list_prepend(fields, garrow_field_new_raw(&arrow_field, nullptr));
250 }
251 return g_list_reverse(fields);
252 }
253
254 /**
255 * garrow_struct_data_type_get_field:
256 * @struct_data_type: A #GArrowStructDataType.
257 * @i: The index of the target field.
258 *
259 * Returns: (transfer full) (nullable):
260 * The field at the index in the struct data type or %NULL on not found.
261 *
262 * Since: 0.12.0
263 */
264 GArrowField *
garrow_struct_data_type_get_field(GArrowStructDataType * struct_data_type,gint i)265 garrow_struct_data_type_get_field(GArrowStructDataType *struct_data_type,
266 gint i)
267 {
268 auto data_type = GARROW_DATA_TYPE(struct_data_type);
269 auto arrow_data_type = garrow_data_type_get_raw(data_type);
270
271 if (i < 0) {
272 i += arrow_data_type->num_fields();
273 }
274 if (i < 0) {
275 return NULL;
276 }
277 if (i >= arrow_data_type->num_fields()) {
278 return NULL;
279 }
280
281 auto arrow_field = arrow_data_type->field(i);
282 if (arrow_field) {
283 return garrow_field_new_raw(&arrow_field, nullptr);
284 } else {
285 return NULL;
286 }
287 }
288
289 /**
290 * garrow_struct_data_type_get_field_by_name:
291 * @struct_data_type: A #GArrowStructDataType.
292 * @name: The name of the target field.
293 *
294 * Returns: (transfer full) (nullable):
295 * The field that has the name in the struct data type or %NULL on not found.
296 *
297 * Since: 0.12.0
298 */
299 GArrowField *
garrow_struct_data_type_get_field_by_name(GArrowStructDataType * struct_data_type,const gchar * name)300 garrow_struct_data_type_get_field_by_name(GArrowStructDataType *struct_data_type,
301 const gchar *name)
302 {
303 auto data_type = GARROW_DATA_TYPE(struct_data_type);
304 auto arrow_data_type = garrow_data_type_get_raw(data_type);
305 auto arrow_struct_data_type =
306 std::static_pointer_cast<arrow::StructType>(arrow_data_type);
307
308 auto arrow_field = arrow_struct_data_type->GetFieldByName(name);
309 if (arrow_field) {
310 return garrow_field_new_raw(&arrow_field, nullptr);
311 } else {
312 return NULL;
313 }
314 }
315
316 /**
317 * garrow_struct_data_type_get_field_index:
318 * @struct_data_type: A #GArrowStructDataType.
319 * @name: The name of the target field.
320 *
321 * Returns: The index of the target index in the struct data type
322 * or `-1` on not found.
323 *
324 * Since: 0.12.0
325 */
326 gint
garrow_struct_data_type_get_field_index(GArrowStructDataType * struct_data_type,const gchar * name)327 garrow_struct_data_type_get_field_index(GArrowStructDataType *struct_data_type,
328 const gchar *name)
329 {
330 auto arrow_data_type = garrow_data_type_get_raw(GARROW_DATA_TYPE(struct_data_type));
331 auto arrow_struct_data_type =
332 std::static_pointer_cast<arrow::StructType>(arrow_data_type);
333
334 return arrow_struct_data_type->GetFieldIndex(name);
335 }
336
337
G_DEFINE_TYPE(GArrowMapDataType,garrow_map_data_type,GARROW_TYPE_LIST_DATA_TYPE)338 G_DEFINE_TYPE(GArrowMapDataType,
339 garrow_map_data_type,
340 GARROW_TYPE_LIST_DATA_TYPE)
341
342 static void
343 garrow_map_data_type_init(GArrowMapDataType *object)
344 {
345 }
346
347 static void
garrow_map_data_type_class_init(GArrowMapDataTypeClass * klass)348 garrow_map_data_type_class_init(GArrowMapDataTypeClass *klass)
349 {
350 }
351
352 /**
353 * garrow_map_data_type_new:
354 * @key_type: The key type of the map.
355 * @item_type: The item type of the map.
356 *
357 * Returns: The newly created map data type.
358 *
359 * Since: 0.17.0
360 */
361 GArrowMapDataType *
garrow_map_data_type_new(GArrowDataType * key_type,GArrowDataType * item_type)362 garrow_map_data_type_new(GArrowDataType *key_type,
363 GArrowDataType *item_type)
364 {
365 auto arrow_key_type = garrow_data_type_get_raw(key_type);
366 auto arrow_item_type = garrow_data_type_get_raw(item_type);
367 auto arrow_data_type = std::make_shared<arrow::MapType>(arrow_key_type,
368 arrow_item_type);
369 auto data_type = g_object_new(GARROW_TYPE_MAP_DATA_TYPE,
370 "data-type", &arrow_data_type,
371 NULL);
372 return GARROW_MAP_DATA_TYPE(data_type);
373 }
374
375 /**
376 * garrow_map_data_type_get_key_type:
377 * @map_data_type: A #GArrowMapDataType.
378 *
379 * Returns: (transfer full): The key type of the map.
380 *
381 * Since: 0.17.0
382 */
383 GArrowDataType *
garrow_map_data_type_get_key_type(GArrowMapDataType * map_data_type)384 garrow_map_data_type_get_key_type(GArrowMapDataType *map_data_type)
385 {
386 auto data_type = GARROW_DATA_TYPE(map_data_type);
387 auto arrow_data_type = garrow_data_type_get_raw(data_type);
388 auto arrow_map_data_type =
389 std::static_pointer_cast<arrow::MapType>(arrow_data_type);
390 auto arrow_key_type = arrow_map_data_type->key_type();
391 return garrow_data_type_new_raw(&arrow_key_type);
392 }
393
394 /**
395 * garrow_map_data_type_get_item_type:
396 * @map_data_type: A #GArrowMapDataType.
397 *
398 * Returns: (transfer full): The item type of the map.
399 *
400 * Since: 0.17.0
401 */
402 GArrowDataType *
garrow_map_data_type_get_item_type(GArrowMapDataType * map_data_type)403 garrow_map_data_type_get_item_type(GArrowMapDataType *map_data_type)
404 {
405 auto data_type = GARROW_DATA_TYPE(map_data_type);
406 auto arrow_data_type = garrow_data_type_get_raw(data_type);
407 auto arrow_map_data_type =
408 std::static_pointer_cast<arrow::MapType>(arrow_data_type);
409 auto arrow_item_type = arrow_map_data_type->item_type();
410 return garrow_data_type_new_raw(&arrow_item_type);
411 }
412
413
G_DEFINE_ABSTRACT_TYPE(GArrowUnionDataType,garrow_union_data_type,GARROW_TYPE_DATA_TYPE)414 G_DEFINE_ABSTRACT_TYPE(GArrowUnionDataType,
415 garrow_union_data_type,
416 GARROW_TYPE_DATA_TYPE)
417
418 static void
419 garrow_union_data_type_init(GArrowUnionDataType *object)
420 {
421 }
422
423 static void
garrow_union_data_type_class_init(GArrowUnionDataTypeClass * klass)424 garrow_union_data_type_class_init(GArrowUnionDataTypeClass *klass)
425 {
426 }
427
428 /**
429 * garrow_union_data_type_get_n_fields:
430 * @union_data_type: A #GArrowUnionDataType.
431 *
432 * Returns: The number of fields of the union data type.
433 *
434 * Since: 0.12.0
435 */
436 gint
garrow_union_data_type_get_n_fields(GArrowUnionDataType * union_data_type)437 garrow_union_data_type_get_n_fields(GArrowUnionDataType *union_data_type)
438 {
439 auto arrow_data_type = garrow_data_type_get_raw(GARROW_DATA_TYPE(union_data_type));
440 return arrow_data_type->num_fields();
441 }
442
443 /**
444 * garrow_union_data_type_get_fields:
445 * @union_data_type: A #GArrowUnionDataType.
446 *
447 * Returns: (transfer full) (element-type GArrowField):
448 * The fields of the union data type.
449 *
450 * Since: 0.12.0
451 */
452 GList *
garrow_union_data_type_get_fields(GArrowUnionDataType * union_data_type)453 garrow_union_data_type_get_fields(GArrowUnionDataType *union_data_type)
454 {
455 auto data_type = GARROW_DATA_TYPE(union_data_type);
456 auto arrow_data_type = garrow_data_type_get_raw(data_type);
457 auto arrow_fields = arrow_data_type->fields();
458
459 GList *fields = NULL;
460 for (auto arrow_field : arrow_fields) {
461 fields = g_list_prepend(fields, garrow_field_new_raw(&arrow_field, nullptr));
462 }
463 return g_list_reverse(fields);
464 }
465
466 /**
467 * garrow_union_data_type_get_field:
468 * @union_data_type: A #GArrowUnionDataType.
469 * @i: The index of the target field.
470 *
471 * Returns: (transfer full) (nullable):
472 * The field at the index in the union data type or %NULL on not found.
473 *
474 * Since: 0.12.0
475 */
476 GArrowField *
garrow_union_data_type_get_field(GArrowUnionDataType * union_data_type,gint i)477 garrow_union_data_type_get_field(GArrowUnionDataType *union_data_type,
478 gint i)
479 {
480 auto data_type = GARROW_DATA_TYPE(union_data_type);
481 auto arrow_data_type = garrow_data_type_get_raw(data_type);
482
483 if (i < 0) {
484 i += arrow_data_type->num_fields();
485 }
486 if (i < 0) {
487 return NULL;
488 }
489 if (i >= arrow_data_type->num_fields()) {
490 return NULL;
491 }
492
493 auto arrow_field = arrow_data_type->field(i);
494 if (arrow_field) {
495 return garrow_field_new_raw(&arrow_field, nullptr);
496 } else {
497 return NULL;
498 }
499 }
500
501 /**
502 * garrow_union_data_type_get_type_codes:
503 * @union_data_type: A #GArrowUnionDataType.
504 * @n_type_codes: (out): The number of type codes.
505 *
506 * Returns: (transfer full) (array length=n_type_codes):
507 * The codes for each field.
508 *
509 * It should be freed with g_free() when no longer needed.
510 *
511 * Since: 0.12.0
512 */
513 gint8 *
garrow_union_data_type_get_type_codes(GArrowUnionDataType * union_data_type,gsize * n_type_codes)514 garrow_union_data_type_get_type_codes(GArrowUnionDataType *union_data_type,
515 gsize *n_type_codes)
516 {
517 auto arrow_data_type = garrow_data_type_get_raw(GARROW_DATA_TYPE(union_data_type));
518 auto arrow_union_data_type =
519 std::static_pointer_cast<arrow::UnionType>(arrow_data_type);
520
521 const auto arrow_type_codes = arrow_union_data_type->type_codes();
522 const auto n = arrow_type_codes.size();
523 auto type_codes = static_cast<gint8 *>(g_new(gint8, n));
524 for (size_t i = 0; i < n; ++i) {
525 type_codes[i] = arrow_type_codes[i];
526 }
527 *n_type_codes = n;
528 return type_codes;
529 }
530
531
G_DEFINE_TYPE(GArrowSparseUnionDataType,garrow_sparse_union_data_type,GARROW_TYPE_UNION_DATA_TYPE)532 G_DEFINE_TYPE(GArrowSparseUnionDataType,
533 garrow_sparse_union_data_type,
534 GARROW_TYPE_UNION_DATA_TYPE)
535
536 static void
537 garrow_sparse_union_data_type_init(GArrowSparseUnionDataType *object)
538 {
539 }
540
541 static void
garrow_sparse_union_data_type_class_init(GArrowSparseUnionDataTypeClass * klass)542 garrow_sparse_union_data_type_class_init(GArrowSparseUnionDataTypeClass *klass)
543 {
544 }
545
546 /**
547 * garrow_sparse_union_data_type_new:
548 * @fields: (element-type GArrowField): The fields of the union.
549 * @type_codes: (array length=n_type_codes): The codes to specify each field.
550 * @n_type_codes: The number of type codes.
551 *
552 * Returns: The newly created sparse union data type.
553 */
554 GArrowSparseUnionDataType *
garrow_sparse_union_data_type_new(GList * fields,gint8 * type_codes,gsize n_type_codes)555 garrow_sparse_union_data_type_new(GList *fields,
556 gint8 *type_codes,
557 gsize n_type_codes)
558 {
559 std::vector<std::shared_ptr<arrow::Field>> arrow_fields;
560 for (auto node = fields; node; node = g_list_next(node)) {
561 auto field = GARROW_FIELD(node->data);
562 auto arrow_field = garrow_field_get_raw(field);
563 arrow_fields.push_back(arrow_field);
564 }
565
566 std::vector<int8_t> arrow_type_codes;
567 for (gsize i = 0; i < n_type_codes; ++i) {
568 arrow_type_codes.push_back(type_codes[i]);
569 }
570
571 auto arrow_data_type =
572 std::make_shared<arrow::SparseUnionType>(arrow_fields,
573 arrow_type_codes);
574 auto data_type = g_object_new(GARROW_TYPE_SPARSE_UNION_DATA_TYPE,
575 "data-type", &arrow_data_type,
576 NULL);
577 return GARROW_SPARSE_UNION_DATA_TYPE(data_type);
578 }
579
580
G_DEFINE_TYPE(GArrowDenseUnionDataType,garrow_dense_union_data_type,GARROW_TYPE_UNION_DATA_TYPE)581 G_DEFINE_TYPE(GArrowDenseUnionDataType,
582 garrow_dense_union_data_type,
583 GARROW_TYPE_UNION_DATA_TYPE)
584
585 static void
586 garrow_dense_union_data_type_init(GArrowDenseUnionDataType *object)
587 {
588 }
589
590 static void
garrow_dense_union_data_type_class_init(GArrowDenseUnionDataTypeClass * klass)591 garrow_dense_union_data_type_class_init(GArrowDenseUnionDataTypeClass *klass)
592 {
593 }
594
595 /**
596 * garrow_dense_union_data_type_new:
597 * @fields: (element-type GArrowField): The fields of the union.
598 * @type_codes: (array length=n_type_codes): The codes to specify each field.
599 * @n_type_codes: The number of type codes.
600 *
601 * Returns: The newly created dense union data type.
602 */
603 GArrowDenseUnionDataType *
garrow_dense_union_data_type_new(GList * fields,gint8 * type_codes,gsize n_type_codes)604 garrow_dense_union_data_type_new(GList *fields,
605 gint8 *type_codes,
606 gsize n_type_codes)
607 {
608 std::vector<std::shared_ptr<arrow::Field>> arrow_fields;
609 for (auto node = fields; node; node = g_list_next(node)) {
610 auto field = GARROW_FIELD(node->data);
611 auto arrow_field = garrow_field_get_raw(field);
612 arrow_fields.push_back(arrow_field);
613 }
614
615 std::vector<int8_t> arrow_type_codes;
616 for (gsize i = 0; i < n_type_codes; ++i) {
617 arrow_type_codes.push_back(type_codes[i]);
618 }
619
620 auto arrow_data_type =
621 std::make_shared<arrow::DenseUnionType>(arrow_fields,
622 arrow_type_codes);
623 auto data_type = g_object_new(GARROW_TYPE_DENSE_UNION_DATA_TYPE,
624 "data-type", &arrow_data_type,
625 NULL);
626 return GARROW_DENSE_UNION_DATA_TYPE(data_type);
627 }
628
629
G_DEFINE_TYPE(GArrowDictionaryDataType,garrow_dictionary_data_type,GARROW_TYPE_FIXED_WIDTH_DATA_TYPE)630 G_DEFINE_TYPE(GArrowDictionaryDataType,
631 garrow_dictionary_data_type,
632 GARROW_TYPE_FIXED_WIDTH_DATA_TYPE)
633
634 static void
635 garrow_dictionary_data_type_init(GArrowDictionaryDataType *object)
636 {
637 }
638
639 static void
garrow_dictionary_data_type_class_init(GArrowDictionaryDataTypeClass * klass)640 garrow_dictionary_data_type_class_init(GArrowDictionaryDataTypeClass *klass)
641 {
642 }
643
644 /**
645 * garrow_dictionary_data_type_new:
646 * @index_data_type: The data type of index.
647 * @value_data_type: The data type of dictionary values.
648 * @ordered: Whether dictionary contents are ordered or not.
649 *
650 * Returns: The newly created dictionary data type.
651 *
652 * Since: 0.8.0
653 */
654 GArrowDictionaryDataType *
garrow_dictionary_data_type_new(GArrowDataType * index_data_type,GArrowDataType * value_data_type,gboolean ordered)655 garrow_dictionary_data_type_new(GArrowDataType *index_data_type,
656 GArrowDataType *value_data_type,
657 gboolean ordered)
658 {
659 auto arrow_index_data_type = garrow_data_type_get_raw(index_data_type);
660 auto arrow_value_data_type = garrow_data_type_get_raw(value_data_type);
661 auto arrow_data_type = arrow::dictionary(arrow_index_data_type,
662 arrow_value_data_type,
663 ordered);
664 return GARROW_DICTIONARY_DATA_TYPE(garrow_data_type_new_raw(&arrow_data_type));
665 }
666
667 /**
668 * garrow_dictionary_data_type_get_index_data_type:
669 * @dictionary_data_type: The #GArrowDictionaryDataType.
670 *
671 * Returns: (transfer full): The #GArrowDataType of index.
672 *
673 * Since: 0.8.0
674 */
675 GArrowDataType *
garrow_dictionary_data_type_get_index_data_type(GArrowDictionaryDataType * dictionary_data_type)676 garrow_dictionary_data_type_get_index_data_type(GArrowDictionaryDataType *dictionary_data_type)
677 {
678 auto arrow_data_type = garrow_data_type_get_raw(GARROW_DATA_TYPE(dictionary_data_type));
679 auto arrow_dictionary_data_type =
680 std::static_pointer_cast<arrow::DictionaryType>(arrow_data_type);
681 auto arrow_index_data_type = arrow_dictionary_data_type->index_type();
682 return garrow_data_type_new_raw(&arrow_index_data_type);
683 }
684
685 /**
686 * garrow_dictionary_data_type_get_value_data_type:
687 * @dictionary_data_type: The #GArrowDictionaryDataType.
688 *
689 * Returns: (transfer full): The #GArrowDataType of dictionary values.
690 *
691 * Since: 0.14.0
692 */
693 GArrowDataType *
garrow_dictionary_data_type_get_value_data_type(GArrowDictionaryDataType * dictionary_data_type)694 garrow_dictionary_data_type_get_value_data_type(GArrowDictionaryDataType *dictionary_data_type)
695 {
696 auto arrow_data_type = garrow_data_type_get_raw(GARROW_DATA_TYPE(dictionary_data_type));
697 auto arrow_dictionary_data_type =
698 std::static_pointer_cast<arrow::DictionaryType>(arrow_data_type);
699 auto arrow_value_data_type = arrow_dictionary_data_type->value_type();
700 return garrow_data_type_new_raw(&arrow_value_data_type);
701 }
702
703 /**
704 * garrow_dictionary_data_type_is_ordered:
705 * @dictionary_data_type: The #GArrowDictionaryDataType.
706 *
707 * Returns: Whether dictionary contents are ordered or not.
708 *
709 * Since: 0.8.0
710 */
711 gboolean
garrow_dictionary_data_type_is_ordered(GArrowDictionaryDataType * dictionary_data_type)712 garrow_dictionary_data_type_is_ordered(GArrowDictionaryDataType *dictionary_data_type)
713 {
714 auto arrow_data_type = garrow_data_type_get_raw(GARROW_DATA_TYPE(dictionary_data_type));
715 auto arrow_dictionary_data_type =
716 std::static_pointer_cast<arrow::DictionaryType>(arrow_data_type);
717 return arrow_dictionary_data_type->ordered();
718 }
719
720 G_END_DECLS
721