-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
basic-array.cpp
3683 lines (3374 loc) · 109 KB
/
basic-array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <arrow-glib/array.hpp>
#include <arrow-glib/basic-data-type.hpp>
#include <arrow-glib/buffer.hpp>
#include <arrow-glib/decimal.hpp>
#include <arrow-glib/error.hpp>
#include <arrow-glib/interval.hpp>
#include <arrow-glib/type.hpp>
#include <arrow/c/bridge.h>
#include <sstream>
G_BEGIN_DECLS
/**
* SECTION: basic-array
* @section_id: basic-array-classes
* @title: Basic array classes
* @include: arrow-glib/arrow-glib.h
*
* #GArrowArray is a base class for all array classes such as
* #GArrowBooleanArray.
*
* All array classes are immutable. You need to use binary data or
* array builder to create a new array except #GArrowNullArray. If you
* have binary data that uses Arrow format data, you can create a new
* array with the binary data as #GArrowBuffer object. If you don't
* have binary data, you can use array builder class such as
* #GArrowBooleanArrayBuilder that creates Arrow format data
* internally and a new array from the data.
*
* #GArrowNullArray is a class for null array. It can store zero or
* more null values. You need to specify an array length to create a
* new array.
*
* #GArrowBooleanArray is a class for boolean array. It can store zero
* or more boolean data. If you don't have Arrow format data, you need
* to use #GArrowBooleanArrayBuilder to create a new array.
*
* #GArrowInt8Array is a class for 8-bit integer array. It can store
* zero or more 8-bit integer data. If you don't have Arrow format
* data, you need to use #GArrowInt8ArrayBuilder to create a new
* array.
*
* #GArrowUInt8Array is a class for 8-bit unsigned integer array. It
* can store zero or more 8-bit unsigned integer data. If you don't
* have Arrow format data, you need to use #GArrowUInt8ArrayBuilder to
* create a new array.
*
* #GArrowInt16Array is a class for 16-bit integer array. It can store
* zero or more 16-bit integer data. If you don't have Arrow format
* data, you need to use #GArrowInt16ArrayBuilder to create a new
* array.
*
* #GArrowUInt16Array is a class for 16-bit unsigned integer array. It
* can store zero or more 16-bit unsigned integer data. If you don't
* have Arrow format data, you need to use #GArrowUInt16ArrayBuilder
* to create a new array.
*
* #GArrowInt32Array is a class for 32-bit integer array. It can store
* zero or more 32-bit integer data. If you don't have Arrow format
* data, you need to use #GArrowInt32ArrayBuilder to create a new
* array.
*
* #GArrowUInt32Array is a class for 32-bit unsigned integer array. It
* can store zero or more 32-bit unsigned integer data. If you don't
* have Arrow format data, you need to use #GArrowUInt32ArrayBuilder
* to create a new array.
*
* #GArrowInt64Array is a class for 64-bit integer array. It can store
* zero or more 64-bit integer data. If you don't have Arrow format
* data, you need to use #GArrowInt64ArrayBuilder to create a new
* array.
*
* #GArrowUInt64Array is a class for 64-bit unsigned integer array. It
* can store zero or more 64-bit unsigned integer data. If you don't
* have Arrow format data, you need to use #GArrowUInt64ArrayBuilder
* to create a new array.
*
* #GArrowFloatArray is a class for 32-bit floating point array. It
* can store zero or more 32-bit floating data. If you don't have
* Arrow format data, you need to use #GArrowFloatArrayBuilder to
* create a new array.
*
* #GArrowDoubleArray is a class for 64-bit floating point array. It
* can store zero or more 64-bit floating data. If you don't have
* Arrow format data, you need to use #GArrowDoubleArrayBuilder to
* create a new array.
*
* #GArrowBinaryArray is a class for binary array. It can store zero
* or more binary data. If you don't have Arrow format data, you need
* to use #GArrowBinaryArrayBuilder to create a new array.
*
* #GArrowLargeBinaryArray is a class for 64-bit offsets binary array.
* It can store zero or more binary data. If you don't have Arrow
* format data, you need to use #GArrowLargeBinaryArrayBuilder to
* create a new array.
*
* #GArrowStringArray is a class for UTF-8 encoded string array. It
* can store zero or more UTF-8 encoded string data. If you don't have
* Arrow format data, you need to use #GArrowStringArrayBuilder to
* create a new array.
*
* #GArrowLargeStringArray is a class for 64-bit offsets UTF-8
* encoded string array. It can store zero or more UTF-8 encoded
* string data. If you don't have Arrow format data, you need to
* use #GArrowLargeStringArrayBuilder to create a new array.
*
* #GArrowFixedSizeBinaryArray is a class for fixed size binary array.
* It can store zero or more fixed size binary data. If you don't have
* Arrow format data, you need to use
* #GArrowFixedSizeBinaryArrayBuilder to create a new array.
*
* #GArrowDate32Array is a class for the number of days since UNIX
* epoch in 32-bit signed integer array. It can store zero or more
* date data. If you don't have Arrow format data, you need to use
* #GArrowDate32ArrayBuilder to create a new array.
*
* #GArrowDate64Array is a class for the number of milliseconds since
* UNIX epoch in 64-bit signed integer array. It can store zero or
* more date data. If you don't have Arrow format data, you need to
* use #GArrowDate64ArrayBuilder to create a new array.
*
* #GArrowTimestampArray is a class for the number of
* seconds/milliseconds/microseconds/nanoseconds since UNIX epoch in
* 64-bit signed integer array. It can store zero or more timestamp
* data. If you don't have Arrow format data, you need to use
* #GArrowTimestampArrayBuilder to create a new array.
*
* #GArrowTime32Array is a class for the number of seconds or
* milliseconds since midnight in 32-bit signed integer array. It can
* store zero or more time data. If you don't have Arrow format data,
* you need to use #GArrowTime32ArrayBuilder to create a new array.
*
* #GArrowTime64Array is a class for the number of microseconds or
* nanoseconds since midnight in 64-bit signed integer array. It can
* store zero or more time data. If you don't have Arrow format data,
* you need to use #GArrowTime64ArrayBuilder to create a new array.
*
* #GArrowMonthIntervalArray is a class for the month intarval array.
* It can store zero or more date data. If you don't have Arrow format
* data, you need to use #GArrowMonthIntervalArrayBuilder to create a
* new array.
*
* #GArrowDayTimeIntervalArray is a class for the day time intarval array.
* It can store zero or more date data. If you don't have Arrow format
* data, you need to use #GArrowDayTimeIntervalArrayBuilder to create a
* new array.
*
* #GArrowMonthDayNanoIntervalArray is a class for the month day nano
* intarval array. It can store zero or more date data. If you don't
* have Arrow format data, you need to use #GArrowMonthDayNanoIntervalArray
* to create a new array.
*
* #GArrowDecimal128Array is a class for 128-bit decimal array. It can
* store zero or more 128-bit decimal data. If you don't have Arrow
* format data, you need to use #GArrowDecimal128ArrayBuilder to
* create a new array.
*
* #GArrowDecimal256Array is a class for 256-bit decimal array. It can
* store zero or more 256-bit decimal data. If you don't have Arrow
* format data, you need to use #GArrowDecimal256ArrayBuilder to
* create a new array.
*
* #GArrowExtensionArray is a base class for array of user-defined
* extension types.
*/
typedef struct GArrowEqualOptionsPrivate_ {
gboolean approx;
arrow::EqualOptions options;
} GArrowEqualOptionsPrivate;
enum {
PROP_APPROX = 1,
PROP_NANS_EQUAL,
PROP_ABSOLUTE_TOLERANCE,
};
G_DEFINE_TYPE_WITH_PRIVATE(GArrowEqualOptions,
garrow_equal_options,
G_TYPE_OBJECT)
#define GARROW_EQUAL_OPTIONS_GET_PRIVATE(object) \
static_cast<GArrowEqualOptionsPrivate *>( \
garrow_equal_options_get_instance_private( \
GARROW_EQUAL_OPTIONS(object)))
static void
garrow_equal_options_finalize(GObject *object)
{
auto priv = GARROW_EQUAL_OPTIONS_GET_PRIVATE(object);
priv->options.~EqualOptions();
G_OBJECT_CLASS(garrow_equal_options_parent_class)->finalize(object);
}
static void
garrow_equal_options_set_property(GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
auto priv = GARROW_EQUAL_OPTIONS_GET_PRIVATE(object);
switch (prop_id) {
case PROP_APPROX:
priv->approx = g_value_get_boolean(value);
break;
case PROP_NANS_EQUAL:
priv->options = priv->options.nans_equal(g_value_get_boolean(value));
break;
case PROP_ABSOLUTE_TOLERANCE:
priv->options = priv->options.atol(g_value_get_double(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
garrow_equal_options_get_property(GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
auto priv = GARROW_EQUAL_OPTIONS_GET_PRIVATE(object);
switch (prop_id) {
case PROP_APPROX:
g_value_set_boolean(value, priv->approx);
break;
case PROP_NANS_EQUAL:
g_value_set_boolean(value, priv->options.nans_equal());
break;
case PROP_ABSOLUTE_TOLERANCE:
g_value_set_double(value, priv->options.atol());
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
garrow_equal_options_init(GArrowEqualOptions *object)
{
auto priv = GARROW_EQUAL_OPTIONS_GET_PRIVATE(object);
priv->approx = FALSE;
new(&priv->options) arrow::EqualOptions;
priv->options = arrow::EqualOptions::Defaults();
}
static void
garrow_equal_options_class_init(GArrowEqualOptionsClass *klass)
{
auto gobject_class = G_OBJECT_CLASS(klass);
gobject_class->finalize = garrow_equal_options_finalize;
gobject_class->set_property = garrow_equal_options_set_property;
gobject_class->get_property = garrow_equal_options_get_property;
auto options = arrow::EqualOptions::Defaults();
GParamSpec *spec;
/**
* GArrowEqualOptions:approx:
*
* Whether or not approximate comparison is used.
*
* Since: 5.0.0
*/
spec = g_param_spec_boolean("approx",
"Approx",
"Whether or not approximate comparison is used",
FALSE,
static_cast<GParamFlags>(G_PARAM_READWRITE));
g_object_class_install_property(gobject_class, PROP_APPROX, spec);
/**
* GArrowEqualOptions:nans-equal:
*
* Whether or not NaNs are considered equal.
*
* Since: 5.0.0
*/
spec = g_param_spec_boolean("nans-equal",
"NaNs equal",
"Whether or not NaNs are considered equal",
options.nans_equal(),
static_cast<GParamFlags>(G_PARAM_READWRITE));
g_object_class_install_property(gobject_class, PROP_NANS_EQUAL, spec);
/**
* GArrowEqualOptions:absolute-tolerance:
*
* The absolute tolerance for approximate comparison of
* floating-point values.
*
* Since: 5.0.0
*/
spec = g_param_spec_double("absolute-tolerance",
"Absolute tolerance",
"The absolute tolerance for approximate comparison "
"of floating-point values",
-G_MAXDOUBLE,
G_MAXDOUBLE,
options.atol(),
static_cast<GParamFlags>(G_PARAM_READWRITE));
g_object_class_install_property(gobject_class, PROP_ABSOLUTE_TOLERANCE, spec);
}
/**
* garrow_equal_options_new:
*
* Returns: A newly created #GArrowEqualOptions.
*
* Since: 5.0.0
*/
GArrowEqualOptions *
garrow_equal_options_new(void)
{
auto equal_options = g_object_new(GARROW_TYPE_EQUAL_OPTIONS, NULL);
return GARROW_EQUAL_OPTIONS(equal_options);
}
/**
* garrow_equal_options_is_approx:
* @options: A #GArrowEqualOptions.
*
* Returns: %TRUE if approximate comparison is used, %FALSE otherwise.
*
* Since: 5.0.0
*/
gboolean
garrow_equal_options_is_approx(GArrowEqualOptions *options)
{
auto priv = GARROW_EQUAL_OPTIONS_GET_PRIVATE(options);
return priv->approx;
}
typedef struct GArrowArrayPrivate_ {
std::shared_ptr<arrow::Array> array;
GArrowDataType *value_data_type;
GArrowBuffer *null_bitmap;
// Data for primitive array, value offsets for list array, type
// codes for union array and so on.
GArrowBuffer *buffer1;
// Data for binary array, value offsets for dense union array and so
// on.
GArrowBuffer *buffer2;
GArrowArray *parent;
} GArrowArrayPrivate;
enum {
PROP_ARRAY = 1,
PROP_VALUE_DATA_TYPE,
PROP_NULL_BITMAP,
PROP_BUFFER1,
PROP_BUFFER2,
PROP_PARENT,
};
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE(GArrowArray,
garrow_array,
G_TYPE_OBJECT)
#define GARROW_ARRAY_GET_PRIVATE(obj) \
static_cast<GArrowArrayPrivate *>( \
garrow_array_get_instance_private( \
GARROW_ARRAY(obj)))
G_END_DECLS
template <typename T>
const typename T::c_type *
garrow_array_get_values_raw(std::shared_ptr<arrow::Array> arrow_array,
gint64 *length)
{
auto arrow_specific_array =
std::static_pointer_cast<typename arrow::TypeTraits<T>::ArrayType>(arrow_array);
*length = arrow_specific_array->length();
return arrow_specific_array->raw_values();
};
G_BEGIN_DECLS
static void
garrow_array_dispose(GObject *object)
{
auto priv = GARROW_ARRAY_GET_PRIVATE(object);
if (priv->value_data_type) {
g_object_unref(priv->value_data_type);
priv->value_data_type = NULL;
}
if (priv->null_bitmap) {
g_object_unref(priv->null_bitmap);
priv->null_bitmap = NULL;
}
if (priv->buffer1) {
g_object_unref(priv->buffer1);
priv->buffer1 = NULL;
}
if (priv->buffer2) {
g_object_unref(priv->buffer2);
priv->buffer2 = NULL;
}
if (priv->parent) {
g_object_unref(priv->parent);
priv->parent = NULL;
}
G_OBJECT_CLASS(garrow_array_parent_class)->dispose(object);
}
static void
garrow_array_finalize(GObject *object)
{
auto priv = GARROW_ARRAY_GET_PRIVATE(object);
priv->array.~shared_ptr();
G_OBJECT_CLASS(garrow_array_parent_class)->finalize(object);
}
static void
garrow_array_set_property(GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
auto priv = GARROW_ARRAY_GET_PRIVATE(object);
switch (prop_id) {
case PROP_ARRAY:
priv->array =
*static_cast<std::shared_ptr<arrow::Array> *>(g_value_get_pointer(value));
break;
case PROP_VALUE_DATA_TYPE:
priv->value_data_type = GARROW_DATA_TYPE(g_value_dup_object(value));
break;
case PROP_NULL_BITMAP:
priv->null_bitmap = GARROW_BUFFER(g_value_dup_object(value));
break;
case PROP_BUFFER1:
priv->buffer1 = GARROW_BUFFER(g_value_dup_object(value));
break;
case PROP_BUFFER2:
priv->buffer2 = GARROW_BUFFER(g_value_dup_object(value));
break;
case PROP_PARENT:
priv->parent = GARROW_ARRAY(g_value_dup_object(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
garrow_array_get_property(GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
auto priv = GARROW_ARRAY_GET_PRIVATE(object);
switch (prop_id) {
case PROP_VALUE_DATA_TYPE:
g_value_set_object(value, priv->value_data_type);
break;
case PROP_NULL_BITMAP:
g_value_set_object(value, priv->null_bitmap);
break;
case PROP_BUFFER1:
g_value_set_object(value, priv->buffer1);
break;
case PROP_BUFFER2:
g_value_set_object(value, priv->buffer2);
break;
case PROP_PARENT:
g_value_set_object(value, priv->parent);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
garrow_array_init(GArrowArray *object)
{
auto priv = GARROW_ARRAY_GET_PRIVATE(object);
new(&priv->array) std::shared_ptr<arrow::Array>;
}
static void
garrow_array_class_init(GArrowArrayClass *klass)
{
auto gobject_class = G_OBJECT_CLASS(klass);
gobject_class->dispose = garrow_array_dispose;
gobject_class->finalize = garrow_array_finalize;
gobject_class->set_property = garrow_array_set_property;
gobject_class->get_property = garrow_array_get_property;
GParamSpec *spec;
spec = g_param_spec_pointer("array",
"Array",
"The raw std::shared<arrow::Array> *",
static_cast<GParamFlags>(G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property(gobject_class, PROP_ARRAY, spec);
spec = g_param_spec_object("value-data-type",
"Value data type",
"The data type of each value",
GARROW_TYPE_DATA_TYPE,
static_cast<GParamFlags>(G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property(gobject_class, PROP_VALUE_DATA_TYPE, spec);
spec = g_param_spec_object("null-bitmap",
"NULL bitmap",
"The NULL bitmap",
GARROW_TYPE_BUFFER,
static_cast<GParamFlags>(G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property(gobject_class, PROP_NULL_BITMAP, spec);
spec = g_param_spec_object("buffer1",
"Buffer1",
"The first buffer",
GARROW_TYPE_BUFFER,
static_cast<GParamFlags>(G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property(gobject_class, PROP_BUFFER1, spec);
spec = g_param_spec_object("buffer2",
"Buffer2",
"The second buffer",
GARROW_TYPE_BUFFER,
static_cast<GParamFlags>(G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property(gobject_class, PROP_BUFFER2, spec);
spec = g_param_spec_object("parent",
"Parent",
"The parent array",
GARROW_TYPE_ARRAY,
static_cast<GParamFlags>(G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property(gobject_class, PROP_PARENT, spec);
}
/**
* garrow_array_import:
* @c_abi_array: (not nullable): A `struct ArrowArray *`.
* @data_type: A #GArrowDataType of the C ABI array.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: (transfer full) (nullable): An imported #GArrowArray
* on success, %NULL on error.
*
* You don't need to release the passed `struct ArrowArray *`,
* even if this function reports an error.
*
* Since: 6.0.0
*/
GArrowArray *
garrow_array_import(gpointer c_abi_array,
GArrowDataType *data_type,
GError **error)
{
auto arrow_data_type = garrow_data_type_get_raw(data_type);
auto arrow_array_result =
arrow::ImportArray(static_cast<ArrowArray *>(c_abi_array),
arrow_data_type);
if (garrow::check(error, arrow_array_result, "[array][import]")) {
return garrow_array_new_raw(&(*arrow_array_result));
} else {
return NULL;
}
}
/**
* garrow_array_export:
* @array: A #GArrowArray.
* @c_abi_array: (out): Return location for a `struct ArrowArray *`.
* It should be freed with the `ArrowArray::release` callback then
* g_free() when no longer needed.
* @c_abi_schema: (out) (nullable): Return location for a
* `struct ArrowSchema *` or %NULL.
* It should be freed with the `ArrowSchema::release` callback then
* g_free() when no longer needed.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: %TRUE on success, %FALSE on error.
*
* Since: 6.0.0
*/
gboolean
garrow_array_export(GArrowArray *array,
gpointer *c_abi_array,
gpointer *c_abi_schema,
GError **error)
{
const auto arrow_array = garrow_array_get_raw(array);
*c_abi_array = g_new(ArrowArray, 1);
arrow::Status status;
if (c_abi_schema) {
*c_abi_schema = g_new(ArrowSchema, 1);
status = arrow::ExportArray(*arrow_array,
static_cast<ArrowArray *>(*c_abi_array),
static_cast<ArrowSchema *>(*c_abi_schema));
} else {
status = arrow::ExportArray(*arrow_array,
static_cast<ArrowArray *>(*c_abi_array));
}
if (garrow::check(error, status, "[array][export]")) {
return true;
} else {
g_free(*c_abi_array);
*c_abi_array = nullptr;
if (c_abi_schema) {
g_free(*c_abi_schema);
*c_abi_schema = nullptr;
}
return false;
}
}
/**
* garrow_array_equal:
* @array: A #GArrowArray.
* @other_array: A #GArrowArray to be compared.
*
* Returns: %TRUE if both of them have the same data, %FALSE
* otherwise.
*
* Since: 0.4.0
*/
gboolean
garrow_array_equal(GArrowArray *array, GArrowArray *other_array)
{
return garrow_array_equal_options(array, other_array, NULL);
}
/**
* garrow_array_equal_options:
* @array: A #GArrowArray.
* @other_array: A #GArrowArray to be compared.
* @options: (nullable): A #GArrowEqualOptions to custom how to compare.
*
* Returns: %TRUE if both of them have the same data, %FALSE
* otherwise.
*
* Since: 5.0.0
*/
gboolean
garrow_array_equal_options(GArrowArray *array,
GArrowArray *other_array,
GArrowEqualOptions *options)
{
const auto arrow_array = garrow_array_get_raw(array);
const auto arrow_other_array = garrow_array_get_raw(other_array);
if (options) {
auto is_approx = garrow_equal_options_is_approx(options);
const auto arrow_options = garrow_equal_options_get_raw(options);
if (is_approx) {
return arrow_array->ApproxEquals(arrow_other_array, *arrow_options);
} else {
return arrow_array->Equals(arrow_other_array, *arrow_options);
}
} else {
return arrow_array->Equals(arrow_other_array);
}
}
/**
* garrow_array_equal_approx:
* @array: A #GArrowArray.
* @other_array: A #GArrowArray to be compared.
*
* Returns: %TRUE if both of them have the approx same data, %FALSE
* otherwise.
*
* Since: 0.4.0
*/
gboolean
garrow_array_equal_approx(GArrowArray *array, GArrowArray *other_array)
{
const auto arrow_array = garrow_array_get_raw(array);
const auto arrow_other_array = garrow_array_get_raw(other_array);
return arrow_array->ApproxEquals(arrow_other_array);
}
/**
* garrow_array_equal_range:
* @array: A #GArrowArray.
* @start_index: The start index of @array to be used.
* @other_array: A #GArrowArray to be compared.
* @other_start_index: The start index of @other_array to be used.
* @end_index: The end index of @array to be used. The end index of
* @other_array is "@other_start_index + (@end_index -
* @start_index)".
* @options: (nullable): A #GArrowEqualOptions to custom how to compare.
*
* Returns: %TRUE if both of them have the same data in the range,
* %FALSE otherwise.
*
* Since: 0.4.0
*/
gboolean
garrow_array_equal_range(GArrowArray *array,
gint64 start_index,
GArrowArray *other_array,
gint64 other_start_index,
gint64 end_index,
GArrowEqualOptions *options)
{
const auto arrow_array = garrow_array_get_raw(array);
const auto arrow_other_array = garrow_array_get_raw(other_array);
if (options) {
const auto arrow_options = garrow_equal_options_get_raw(options);
return arrow_array->RangeEquals(arrow_other_array,
start_index,
end_index,
other_start_index,
*arrow_options);
} else {
return arrow_array->RangeEquals(arrow_other_array,
start_index,
end_index,
other_start_index);
}
}
/**
* garrow_array_is_null:
* @array: A #GArrowArray.
* @i: The index of the target value.
*
* Returns: Whether the @i-th value is null or not.
*
* Since: 0.3.0
*/
gboolean
garrow_array_is_null(GArrowArray *array, gint64 i)
{
auto arrow_array = garrow_array_get_raw(array);
return arrow_array->IsNull(i);
}
/**
* garrow_array_is_valid:
* @array: A #GArrowArray.
* @i: The index of the target value.
*
* Returns: Whether the @i-th value is valid (not null) or not.
*
* Since: 0.8.0
*/
gboolean
garrow_array_is_valid(GArrowArray *array, gint64 i)
{
auto arrow_array = garrow_array_get_raw(array);
return arrow_array->IsValid(i);
}
/**
* garrow_array_get_length:
* @array: A #GArrowArray.
*
* Returns: The number of rows in the array.
*/
gint64
garrow_array_get_length(GArrowArray *array)
{
auto arrow_array = garrow_array_get_raw(array);
return arrow_array->length();
}
/**
* garrow_array_get_offset:
* @array: A #GArrowArray.
*
* Returns: The number of values in the array.
*/
gint64
garrow_array_get_offset(GArrowArray *array)
{
auto arrow_array = garrow_array_get_raw(array);
return arrow_array->offset();
}
/**
* garrow_array_get_n_nulls:
* @array: A #GArrowArray.
*
* Returns: The number of NULLs in the array.
*/
gint64
garrow_array_get_n_nulls(GArrowArray *array)
{
auto arrow_array = garrow_array_get_raw(array);
return arrow_array->null_count();
}
/**
* garrow_array_get_null_bitmap:
* @array: A #GArrowArray.
*
* Returns: (transfer full) (nullable): The bitmap that indicates null
* value indices for the array as #GArrowBuffer or %NULL when
* garrow_array_get_n_nulls() returns 0.
*
* Since: 0.3.0
*/
GArrowBuffer *
garrow_array_get_null_bitmap(GArrowArray *array)
{
auto priv = GARROW_ARRAY_GET_PRIVATE(array);
if (priv->null_bitmap) {
g_object_ref(priv->null_bitmap);
return priv->null_bitmap;
}
auto arrow_array = garrow_array_get_raw(array);
auto arrow_null_bitmap = arrow_array->null_bitmap();
return garrow_buffer_new_raw(&arrow_null_bitmap);
}
/**
* garrow_array_get_value_data_type:
* @array: A #GArrowArray.
*
* Returns: (transfer full): The #GArrowDataType for each value of the
* array.
*
* Since: 0.3.0
*/
GArrowDataType *
garrow_array_get_value_data_type(GArrowArray *array)
{
auto priv = GARROW_ARRAY_GET_PRIVATE(array);
if (priv->value_data_type) {
g_object_ref(priv->value_data_type);
return priv->value_data_type;
}
auto arrow_array = garrow_array_get_raw(array);
auto arrow_data_type = arrow_array->type();
return garrow_data_type_new_raw(&arrow_data_type);
}
/**
* garrow_array_get_value_type:
* @array: A #GArrowArray.
*
* Returns: The #GArrowType for each value of the array.
*
* Since: 0.3.0
*/
GArrowType
garrow_array_get_value_type(GArrowArray *array)
{
auto arrow_array = garrow_array_get_raw(array);
return garrow_type_from_raw(arrow_array->type_id());
}
/**
* garrow_array_slice:
* @array: A #GArrowArray.
* @offset: The offset of sub #GArrowArray.
* @length: The length of sub #GArrowArray.
*
* Returns: (transfer full): The sub #GArrowArray. It covers only from
* `offset` to `offset + length` range. The sub #GArrowArray shares
* values with the base #GArrowArray.
*/
GArrowArray *
garrow_array_slice(GArrowArray *array,
gint64 offset,
gint64 length)
{
const auto arrow_array = garrow_array_get_raw(array);
auto arrow_sub_array = arrow_array->Slice(offset, length);
return garrow_array_new_raw(&arrow_sub_array,
"array", &arrow_sub_array,
"parent", array,
NULL);
}
/**
* garrow_array_to_string:
* @array: A #GArrowArray.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: (nullable):
* The formatted array content or %NULL on error.
*
* It should be freed with g_free() when no longer needed.
*
* Since: 0.4.0
*/
gchar *
garrow_array_to_string(GArrowArray *array, GError **error)
{
const auto arrow_array = garrow_array_get_raw(array);
const auto string = arrow_array->ToString();
return g_strdup(string.c_str());
}
/**
* garrow_array_view:
* @array: A #GArrowArray.
* @return_type: A #GArrowDataType of the returned view.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: (nullable) (transfer full): A zero-copy view of this array
* with the given type. This method checks if the `return_type` are
* layout-compatible.
*
* Since: 0.15.0
*/
GArrowArray *
garrow_array_view(GArrowArray *array,
GArrowDataType *return_type,
GError **error)
{
auto arrow_array_raw = garrow_array_get_raw(array);
auto arrow_return_type = garrow_data_type_get_raw(return_type);
auto arrow_array = arrow_array_raw->View(arrow_return_type);
if (garrow::check(error, arrow_array, "[array][view]")) {
return garrow_array_new_raw(&(*arrow_array));
} else {
return NULL;
}
}
/**
* garrow_array_diff_unified:
* @array: A #GArrowArray.
* @other_array: A #GArrowArray to be compared.
*
* Returns: (nullable) (transfer full): The string representation of
* the difference between two arrays as unified format. If there is
* no difference, the return value is %NULL.
*
* It should be freed with g_free() when no longer needed.
*
* Since: 0.15.0
*/
gchar *
garrow_array_diff_unified(GArrowArray *array, GArrowArray *other_array)
{
const auto arrow_array = garrow_array_get_raw(array);
const auto arrow_other_array = garrow_array_get_raw(other_array);
std::stringstream diff;
arrow_array->Equals(arrow_other_array,
arrow::EqualOptions().diff_sink(&diff));
auto string = diff.str();
if (string.empty()) {
return NULL;
} else {
return g_strndup(string.data(), string.size());
}
}
/**
* garrow_array_concatenate:
* @array: A #GArrowArray.
* @other_arrays: (element-type GArrowArray): A #GArrowArray to be
* concatenated.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: (nullable) (transfer full): The concatenated array.
*