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
|
/*
* 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/chunked-array.hpp>
#include <arrow-glib/enums.h>
#include <arrow-glib/error.hpp>
#include <arrow-glib/field.hpp>
#include <arrow-glib/internal-index.hpp>
#include <arrow-glib/output-stream.hpp>
#include <arrow-glib/record-batch.hpp>
#include <arrow-glib/schema.hpp>
#include <arrow-glib/table.hpp>
#include <sstream>
G_BEGIN_DECLS
/**
* SECTION: table
* @section_id: GArrowTable
* @title: GArrowTable
* @short_description: Table class
* @include: arrow-glib/arrow-glib.h
*
* #GArrowTableConcatenateOptions is a class for customizing
* garrow_table_concatenate() behavior.
*
* #GArrowTable is a class for table. Table has zero or more
* #GArrowChunkedArrays and zero or more records.
*
* #GArrowFeatherWriteProperties is a class to customize how to write
* Feather data.
*/
typedef struct GArrowTableConcatenateOptionsPrivate_ {
arrow::ConcatenateTablesOptions options;
} GArrowTableConcatenateOptionsPrivate;
enum {
PROP_UNIFY_SCHEMAS = 1,
PROP_PROMOTE_NULLABILITY,
};
G_DEFINE_TYPE_WITH_PRIVATE(GArrowTableConcatenateOptions,
garrow_table_concatenate_options,
G_TYPE_OBJECT)
#define GARROW_TABLE_CONCATENATE_OPTIONS_GET_PRIVATE(obj) \
static_cast<GArrowTableConcatenateOptionsPrivate *>( \
garrow_table_concatenate_options_get_instance_private( \
GARROW_TABLE_CONCATENATE_OPTIONS(obj)))
static void
garrow_table_concatenate_options_finalize(GObject *object)
{
auto priv = GARROW_TABLE_CONCATENATE_OPTIONS_GET_PRIVATE(object);
priv->options.~ConcatenateTablesOptions();
G_OBJECT_CLASS(garrow_table_concatenate_options_parent_class)->finalize(object);
}
static void
garrow_table_concatenate_options_set_property(GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
auto priv = GARROW_TABLE_CONCATENATE_OPTIONS_GET_PRIVATE(object);
switch (prop_id) {
case PROP_UNIFY_SCHEMAS:
priv->options.unify_schemas = g_value_get_boolean(value);
break;
case PROP_PROMOTE_NULLABILITY:
priv->options.field_merge_options.promote_nullability =
g_value_get_boolean(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
garrow_table_concatenate_options_get_property(GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
auto priv = GARROW_TABLE_CONCATENATE_OPTIONS_GET_PRIVATE(object);
switch (prop_id) {
case PROP_UNIFY_SCHEMAS:
g_value_set_boolean(value, priv->options.unify_schemas);
break;
case PROP_PROMOTE_NULLABILITY:
g_value_set_boolean(value,
priv->options.field_merge_options.promote_nullability);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
garrow_table_concatenate_options_init(GArrowTableConcatenateOptions *object)
{
auto priv = GARROW_TABLE_CONCATENATE_OPTIONS_GET_PRIVATE(object);
new(&(priv->options)) arrow::ConcatenateTablesOptions;
}
static void
garrow_table_concatenate_options_class_init(
GArrowTableConcatenateOptionsClass *klass)
{
auto gobject_class = G_OBJECT_CLASS(klass);
gobject_class->finalize = garrow_table_concatenate_options_finalize;
gobject_class->set_property = garrow_table_concatenate_options_set_property;
gobject_class->get_property = garrow_table_concatenate_options_get_property;
GParamSpec *spec;
auto default_options = arrow::ConcatenateTablesOptions::Defaults();
/**
* GArrowTableConcatenateOptions:unify-schemas:
*
* If true, the schemas of the tables will be first unified with
* fields of the same name being merged, according to
* #GArrowTableConcatenateOptions:promote-nullability, then each
* table will be promoted to the unified schema before being
* concatenated.
*
* Otherwise, all tables should have the same schema. Each column in
* the output table is the result of concatenating the corresponding
* columns in all input tables.
*
* Since: 6.0.0
*/
spec = g_param_spec_boolean("unify-schemas",
"Unify schemas",
"Whether unifying schemas or not",
default_options.unify_schemas,
static_cast<GParamFlags>(G_PARAM_READWRITE));
g_object_class_install_property(gobject_class, PROP_UNIFY_SCHEMAS, spec);
/**
* GArrowTableConcatenateOptions:promote-nullability:
*
* If true, a #GArrowField of #GArrowNullDataType can be unified
* with a #GArrowField of another type. The unified field will be of
* the other type and become nullable. Nullability will be promoted
* to the looser option (nullable if one is not nullable).
*
* Since: 6.0.0
*/
spec = g_param_spec_boolean("promote-nullability",
"Promote nullability",
"Whether promoting nullability or not",
default_options.field_merge_options.promote_nullability,
static_cast<GParamFlags>(G_PARAM_READWRITE));
g_object_class_install_property(gobject_class, PROP_PROMOTE_NULLABILITY, spec);
}
/**
* garrow_table_concatenate_options_new:
*
* Returns: A newly created #GArrowTableConcatenateOptions.
*
* Since: 6.0.0
*/
GArrowTableConcatenateOptions *
garrow_table_concatenate_options_new(void)
{
return GARROW_TABLE_CONCATENATE_OPTIONS(
g_object_new(GARROW_TYPE_TABLE_CONCATENATE_OPTIONS,
NULL));
}
typedef struct GArrowTablePrivate_ {
std::shared_ptr<arrow::Table> table;
} GArrowTablePrivate;
enum {
PROP_TABLE = 1,
};
G_DEFINE_TYPE_WITH_PRIVATE(GArrowTable,
garrow_table,
G_TYPE_OBJECT)
#define GARROW_TABLE_GET_PRIVATE(obj) \
static_cast<GArrowTablePrivate *>( \
garrow_table_get_instance_private( \
GARROW_TABLE(obj)))
static void
garrow_table_finalize(GObject *object)
{
auto priv = GARROW_TABLE_GET_PRIVATE(object);
priv->table.~shared_ptr();
G_OBJECT_CLASS(garrow_table_parent_class)->finalize(object);
}
static void
garrow_table_set_property(GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
auto priv = GARROW_TABLE_GET_PRIVATE(object);
switch (prop_id) {
case PROP_TABLE:
priv->table =
*static_cast<std::shared_ptr<arrow::Table> *>(g_value_get_pointer(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
garrow_table_get_property(GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
switch (prop_id) {
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
garrow_table_init(GArrowTable *object)
{
auto priv = GARROW_TABLE_GET_PRIVATE(object);
new(&priv->table) std::shared_ptr<arrow::Table>;
}
static void
garrow_table_class_init(GArrowTableClass *klass)
{
GObjectClass *gobject_class;
GParamSpec *spec;
gobject_class = G_OBJECT_CLASS(klass);
gobject_class->finalize = garrow_table_finalize;
gobject_class->set_property = garrow_table_set_property;
gobject_class->get_property = garrow_table_get_property;
spec = g_param_spec_pointer("table",
"Table",
"The raw std::shared_ptr<arrow::Table> *",
static_cast<GParamFlags>(G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property(gobject_class, PROP_TABLE, spec);
}
/**
* garrow_table_new_values: (skip)
* @schema: The schema of the table.
* @values: The values of the table. All values must be instance of
* the same class. Available classes are #GArrowChunkedArray,
* #GArrowArray and #GArrowRecordBatch.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: (nullable): A newly created #GArrowTable or %NULL on error.
*
* Since: 0.12.0
*/
GArrowTable *
garrow_table_new_values(GArrowSchema *schema,
GList *values,
GError **error)
{
const auto context = "[table][new][values]";
auto arrow_schema = garrow_schema_get_raw(schema);
std::vector<std::shared_ptr<arrow::ChunkedArray>> arrow_chunked_arrays;
std::vector<std::shared_ptr<arrow::Array>> arrow_arrays;
std::vector<std::shared_ptr<arrow::RecordBatch>> arrow_record_batches;
for (GList *node = values; node; node = node->next) {
if (GARROW_IS_CHUNKED_ARRAY(node->data)) {
auto chunked_array = GARROW_CHUNKED_ARRAY(node->data);
arrow_chunked_arrays.push_back(garrow_chunked_array_get_raw(chunked_array));
} else if (GARROW_IS_ARRAY(node->data)) {
auto array = GARROW_ARRAY(node->data);
arrow_arrays.push_back(garrow_array_get_raw(array));
} else if (GARROW_IS_RECORD_BATCH(node->data)) {
auto record_batch = GARROW_RECORD_BATCH(node->data);
arrow_record_batches.push_back(garrow_record_batch_get_raw(record_batch));
} else {
g_set_error(error,
GARROW_ERROR,
GARROW_ERROR_INVALID,
"%s: %s",
context,
"value must be one of "
"GArrowChunkedArray, GArrowArray and GArrowRecordBatch");
return NULL;
}
}
size_t n_types = 0;
if (!arrow_chunked_arrays.empty()) {
++n_types;
}
if (!arrow_arrays.empty()) {
++n_types;
}
if (!arrow_record_batches.empty()) {
++n_types;
}
if (n_types > 1) {
g_set_error(error,
GARROW_ERROR,
GARROW_ERROR_INVALID,
"%s: %s",
context,
"all values must be the same objects of "
"GArrowChunkedArray, GArrowArray or GArrowRecordBatch");
return NULL;
}
if (!arrow_chunked_arrays.empty()) {
auto arrow_table = arrow::Table::Make(arrow_schema,
std::move(arrow_chunked_arrays));
auto status = arrow_table->Validate();
if (garrow_error_check(error, status, context)) {
return garrow_table_new_raw(&arrow_table);
} else {
return NULL;
}
} else if (!arrow_arrays.empty()) {
auto arrow_table = arrow::Table::Make(arrow_schema, std::move(arrow_arrays));
auto status = arrow_table->Validate();
if (garrow_error_check(error, status, context)) {
return garrow_table_new_raw(&arrow_table);
} else {
return NULL;
}
} else {
auto maybe_table = arrow::Table::FromRecordBatches(
arrow_schema, std::move(arrow_record_batches));
if (garrow::check(error, maybe_table, context)) {
return garrow_table_new_raw(&(*maybe_table));
} else {
return NULL;
}
}
}
/**
* garrow_table_new_chunked_arrays:
* @schema: The schema of the table.
* @chunked_arrays: (array length=n_chunked_arrays): The chunked arrays of
* the table.
* @n_chunked_arrays: The number of chunked arrays.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: (nullable): A newly created #GArrowTable or %NULL on error.
*
* Since: 0.15.0
*/
GArrowTable *
garrow_table_new_chunked_arrays(GArrowSchema *schema,
GArrowChunkedArray **chunked_arrays,
gsize n_chunked_arrays,
GError **error)
{
auto arrow_schema = garrow_schema_get_raw(schema);
std::vector<std::shared_ptr<arrow::ChunkedArray>> arrow_chunked_arrays;
for (gsize i = 0; i < n_chunked_arrays; ++i) {
auto arrow_chunked_array = garrow_chunked_array_get_raw(chunked_arrays[i]);
arrow_chunked_arrays.push_back(arrow_chunked_array);
}
auto arrow_table = arrow::Table::Make(arrow_schema, arrow_chunked_arrays);
auto status = arrow_table->Validate();
if (garrow_error_check(error, status, "[table][new][chunked-arrays]")) {
return garrow_table_new_raw(&arrow_table);
} else {
return NULL;
}
}
/**
* garrow_table_new_arrays:
* @schema: The schema of the table.
* @arrays: (array length=n_arrays): The arrays of the table.
* @n_arrays: The number of arrays.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: (nullable): A newly created #GArrowTable or %NULL on error.
*
* Since: 0.12.0
*/
GArrowTable *
garrow_table_new_arrays(GArrowSchema *schema,
GArrowArray **arrays,
gsize n_arrays,
GError **error)
{
auto arrow_schema = garrow_schema_get_raw(schema);
std::vector<std::shared_ptr<arrow::Array>> arrow_arrays;
for (gsize i = 0; i < n_arrays; ++i) {
arrow_arrays.push_back(garrow_array_get_raw(arrays[i]));
}
auto arrow_table = arrow::Table::Make(arrow_schema, arrow_arrays);
auto status = arrow_table->Validate();
if (garrow_error_check(error, status, "[table][new][arrays]")) {
return garrow_table_new_raw(&arrow_table);
} else {
return NULL;
}
}
/**
* garrow_table_new_record_batches:
* @schema: The schema of the table.
* @record_batches: (array length=n_record_batches): The record batches
* that have data for the table.
* @n_record_batches: The number of record batches.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: (nullable): A newly created #GArrowTable or %NULL on error.
*
* Since: 0.12.0
*/
GArrowTable *
garrow_table_new_record_batches(GArrowSchema *schema,
GArrowRecordBatch **record_batches,
gsize n_record_batches,
GError **error)
{
auto arrow_schema = garrow_schema_get_raw(schema);
std::vector<std::shared_ptr<arrow::RecordBatch>> arrow_record_batches;
for (gsize i = 0; i < n_record_batches; ++i) {
auto arrow_record_batch = garrow_record_batch_get_raw(record_batches[i]);
arrow_record_batches.push_back(arrow_record_batch);
}
auto maybe_table = arrow::Table::FromRecordBatches(arrow_schema,
arrow_record_batches);
if (garrow::check(error, maybe_table, "[table][new][record-batches]")) {
return garrow_table_new_raw(&(*maybe_table));
} else {
return NULL;
}
}
/**
* garrow_table_equal:
* @table: A #GArrowTable.
* @other_table: A #GArrowTable to be compared.
*
* Returns: %TRUE if both of them have the same data, %FALSE
* otherwise.
*
* Since: 0.4.0
*/
gboolean
garrow_table_equal(GArrowTable *table, GArrowTable *other_table)
{
const auto arrow_table = garrow_table_get_raw(table);
const auto arrow_other_table = garrow_table_get_raw(other_table);
return arrow_table->Equals(*arrow_other_table);
}
/**
* garrow_table_equal_metadata:
* @table: A #GArrowTable.
* @other_table: A #GArrowTable to be compared.
* @check_metadata: Whether to compare metadata.
*
* Returns: %TRUE if both of them have the same data, %FALSE
* otherwise.
*
* Since: 0.17.0
*/
gboolean
garrow_table_equal_metadata(GArrowTable *table,
GArrowTable *other_table,
gboolean check_metadata)
{
const auto arrow_table = garrow_table_get_raw(table);
const auto arrow_other_table = garrow_table_get_raw(other_table);
return arrow_table->Equals(*arrow_other_table, check_metadata);
}
/**
* garrow_table_get_schema:
* @table: A #GArrowTable.
*
* Returns: (transfer full): The schema of the table.
*/
GArrowSchema *
garrow_table_get_schema(GArrowTable *table)
{
const auto arrow_table = garrow_table_get_raw(table);
auto arrow_schema = arrow_table->schema();
return garrow_schema_new_raw(&arrow_schema);
}
/**
* garrow_table_get_column_data:
* @table: A #GArrowTable.
* @i: The index of the target column. If it's negative, index is
* counted backward from the end of the columns. `-1` means the last
* column.
*
* Returns: (nullable) (transfer full): The i-th column's data in the table.
*
* Since: 0.15.0
*/
GArrowChunkedArray *
garrow_table_get_column_data(GArrowTable *table,
gint i)
{
const auto &arrow_table = garrow_table_get_raw(table);
if (!garrow_internal_index_adjust(i, arrow_table->num_columns())) {
return NULL;
}
auto arrow_column = arrow_table->column(i);
return garrow_chunked_array_new_raw(&arrow_column);
}
/**
* garrow_table_get_n_columns:
* @table: A #GArrowTable.
*
* Returns: The number of columns in the table.
*/
guint
garrow_table_get_n_columns(GArrowTable *table)
{
const auto arrow_table = garrow_table_get_raw(table);
return arrow_table->num_columns();
}
/**
* garrow_table_get_n_rows:
* @table: A #GArrowTable.
*
* Returns: The number of rows in the table.
*/
guint64
garrow_table_get_n_rows(GArrowTable *table)
{
const auto arrow_table = garrow_table_get_raw(table);
return arrow_table->num_rows();
}
/**
* garrow_table_add_column:
* @table: A #GArrowTable.
* @i: The index of the new column.
* @field: The field for the column to be added.
* @chunked_array: The column data to be added.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: (nullable) (transfer full): The newly allocated
* #GArrowTable that has a new column or %NULL on error.
*
* Since: 0.15.0
*/
GArrowTable *
garrow_table_add_column(GArrowTable *table,
guint i,
GArrowField *field,
GArrowChunkedArray *chunked_array,
GError **error)
{
const auto arrow_table = garrow_table_get_raw(table);
const auto arrow_field = garrow_field_get_raw(field);
const auto arrow_chunked_array = garrow_chunked_array_get_raw(chunked_array);
auto maybe_new_table = arrow_table->AddColumn(i,
arrow_field,
arrow_chunked_array);
if (garrow::check(error, maybe_new_table, "[table][add-column]")) {
return garrow_table_new_raw(&(*maybe_new_table));
} else {
return NULL;
}
}
/**
* garrow_table_remove_column:
* @table: A #GArrowTable.
* @i: The index of the column to be removed.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: (nullable) (transfer full): The newly allocated
* #GArrowTable that doesn't have the column or %NULL on error.
*
* Since: 0.3.0
*/
GArrowTable *
garrow_table_remove_column(GArrowTable *table,
guint i,
GError **error)
{
const auto arrow_table = garrow_table_get_raw(table);
auto maybe_new_table = arrow_table->RemoveColumn(i);
if (garrow::check(error, maybe_new_table, "[table][remove-column]")) {
return garrow_table_new_raw(&(*maybe_new_table));
} else {
return NULL;
}
}
/**
* garrow_table_replace_column:
* @table: A #GArrowTable.
* @i: The index of the column to be replaced.
* @field: The field for the new column.
* @chunked_array: The newly added column data.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: (nullable) (transfer full): The newly allocated
* #GArrowTable that has @column as the @i-th column or %NULL on
* error.
*
* Since: 0.15.0
*/
GArrowTable *
garrow_table_replace_column(GArrowTable *table,
guint i,
GArrowField *field,
GArrowChunkedArray *chunked_array,
GError **error)
{
const auto arrow_table = garrow_table_get_raw(table);
const auto arrow_field = garrow_field_get_raw(field);
const auto arrow_chunked_array = garrow_chunked_array_get_raw(chunked_array);
auto maybe_new_table = arrow_table->SetColumn(i,
arrow_field,
arrow_chunked_array);
if (garrow::check(error, maybe_new_table, "[table][replace-column]")) {
return garrow_table_new_raw(&(*maybe_new_table));
} else {
return NULL;
}
}
/**
* garrow_table_to_string:
* @table: A #GArrowTable.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: (nullable):
* The formatted table content or %NULL on error.
*
* It should be freed with g_free() when no longer needed.
*
* Since: 0.12.0
*/
gchar *
garrow_table_to_string(GArrowTable *table, GError **error)
{
const auto arrow_table = garrow_table_get_raw(table);
return g_strdup(arrow_table->ToString().c_str());
}
/**
* garrow_table_concatenate:
* @table: A #GArrowTable.
* @other_tables: (element-type GArrowTable): The tables to be concatenated.
* @options: (nullable): The options to customize concatenation.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: (nullable) (transfer full): The table concatenated vertically.
*
* Since: 0.14.0
*/
GArrowTable *
garrow_table_concatenate(GArrowTable *table,
GList *other_tables,
GArrowTableConcatenateOptions *options,
GError **error)
{
auto arrow_table = garrow_table_get_raw(table);
std::vector<std::shared_ptr<arrow::Table>> arrow_tables = { arrow_table };
for (auto node = other_tables; node; node = g_list_next(node)) {
auto arrow_other_table = garrow_table_get_raw(GARROW_TABLE(node->data));
arrow_tables.push_back(arrow_other_table);
}
auto arrow_options = arrow::ConcatenateTablesOptions::Defaults();
if (options) {
auto options_priv = GARROW_TABLE_CONCATENATE_OPTIONS_GET_PRIVATE(options);
arrow_options = options_priv->options;
}
auto arrow_concatenated_table_result =
arrow::ConcatenateTables(arrow_tables, arrow_options);
if (garrow::check(error,
arrow_concatenated_table_result,
"[table][concatenate]")) {
auto arrow_concatenated_table = std::move(*arrow_concatenated_table_result);
return garrow_table_new_raw(&arrow_concatenated_table);
} else {
return NULL;
}
}
/**
* garrow_table_slice:
* @table: A #GArrowTable.
* @offset: The offset of sub #GArrowTable. If the offset is negative,
* the offset is counted from the last.
* @length: The length of sub #GArrowTable.
*
* Returns: (transfer full): The sub #GArrowTable. It covers
* only from `offset` to `offset + length` range. The sub
* #GArrowTable shares values with the base
* #GArrowTable.
*
* Since: 0.14.0
*/
GArrowTable *
garrow_table_slice(GArrowTable *table,
gint64 offset,
gint64 length)
{
const auto arrow_table = garrow_table_get_raw(table);
if (offset < 0) {
offset += arrow_table->num_rows();
}
auto arrow_sub_table = arrow_table->Slice(offset, length);
return garrow_table_new_raw(&arrow_sub_table);
}
/**
* garrow_table_combine_chunks:
* @table: A #GArrowTable.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: (nullable) (transfer full): The #GArrowTable with
* chunks combined, or %NULL on error.
*
* Since: 0.16.0
*/
GArrowTable *
garrow_table_combine_chunks(GArrowTable *table,
GError **error)
{
const auto arrow_table = garrow_table_get_raw(table);
auto maybe_new_table = arrow_table->CombineChunks();
if (garrow::check(error, maybe_new_table, "[table][combine-chunks]")) {
return garrow_table_new_raw(&(*maybe_new_table));
} else {
return NULL;
}
}
typedef struct GArrowFeatherWritePropertiesPrivate_ {
arrow::ipc::feather::WriteProperties properties;
} GArrowFeatherWritePropertiesPrivate;
enum {
PROP_VERSION = 1,
PROP_CHUNK_SIZE,
PROP_COMPRESSION,
PROP_COMPRESSION_LEVEL,
};
G_DEFINE_TYPE_WITH_PRIVATE(GArrowFeatherWriteProperties,
garrow_feather_write_properties,
G_TYPE_OBJECT)
#define GARROW_FEATHER_WRITE_PROPERTIES_GET_PRIVATE(obj) \
static_cast<GArrowFeatherWritePropertiesPrivate *>( \
garrow_feather_write_properties_get_instance_private( \
GARROW_FEATHER_WRITE_PROPERTIES(obj)))
static void
garrow_feather_write_properties_finalize(GObject *object)
{
auto priv = GARROW_FEATHER_WRITE_PROPERTIES_GET_PRIVATE(object);
priv->properties.~WriteProperties();
G_OBJECT_CLASS(garrow_feather_write_properties_parent_class)->finalize(object);
}
static void
garrow_feather_write_properties_set_property(GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
auto priv = GARROW_FEATHER_WRITE_PROPERTIES_GET_PRIVATE(object);
switch (prop_id) {
case PROP_COMPRESSION:
priv->properties.compression =
static_cast<arrow::Compression::type>(g_value_get_enum(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
garrow_feather_write_properties_get_property(GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
auto priv = GARROW_FEATHER_WRITE_PROPERTIES_GET_PRIVATE(object);
switch (prop_id) {
case PROP_COMPRESSION:
g_value_set_enum(value, priv->properties.compression);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
garrow_feather_write_properties_init(GArrowFeatherWriteProperties *object)
{
auto priv = GARROW_FEATHER_WRITE_PROPERTIES_GET_PRIVATE(object);
new(&priv->properties) arrow::ipc::feather::WriteProperties;
priv->properties = arrow::ipc::feather::WriteProperties::Defaults();
}
static void
garrow_feather_write_properties_class_init(GArrowFeatherWritePropertiesClass *klass)
{
auto gobject_class = G_OBJECT_CLASS(klass);
gobject_class->finalize = garrow_feather_write_properties_finalize;
gobject_class->set_property = garrow_feather_write_properties_set_property;
gobject_class->get_property = garrow_feather_write_properties_get_property;
auto properties = arrow::ipc::feather::WriteProperties::Defaults();
GParamSpec *spec;
// TODO: version
// TODO: chunk_size
/**
* GArrowFeatherWriteProperties:compression:
*
* Compression type to use. Only
* %GARROW_COMPRESSION_TYPE_UNCOMPRESSED,
* %GARROW_COMPRESSION_TYPE_LZ4 and %GARROW_COMPRESSION_TYPE_ZSTD
* are supported. The default compression is
* %GARROW_COMPRESSION_TYPE_LZ4 if Apache Arrow C++ is built with
* support for it, otherwise %GARROW_COMPRESSION_TYPE_UNCOMPRESSED.
* %GARROW_COMPRESSION_TYPE_UNCOMPRESSED is set as the object
* default here.
*
* Since: 0.17.0
*/
spec = g_param_spec_enum("compression",
"Compression",
"The compression type to use",
GARROW_TYPE_COMPRESSION_TYPE,
properties.compression,
static_cast<GParamFlags>(G_PARAM_READWRITE));
g_object_class_install_property(gobject_class, PROP_COMPRESSION, spec);
// TODO: compression_level
}
/**
* garrow_feather_write_properties_new:
*
* Returns: A newly created #GArrowFeatherWriteProperties.
*
* Since: 0.17.0
*/
GArrowFeatherWriteProperties *
garrow_feather_write_properties_new(void)
{
auto properties = g_object_new(GARROW_TYPE_FEATHER_WRITE_PROPERTIES, NULL);
return GARROW_FEATHER_WRITE_PROPERTIES(properties);
}
/**
* garrow_table_write_as_feather:
* @table: A #GArrowTable.
* @sink: The output.
* @properties: (nullable): The properties for this write.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Writes the @table as Feather format data to the @sink.
*
* Returns: %TRUE on success, %FALSE if there was an error.
*
* Since: 0.17.0
*/
gboolean
garrow_table_write_as_feather(GArrowTable *table,
GArrowOutputStream *sink,
GArrowFeatherWriteProperties *properties,
GError **error)
{
auto arrow_table = garrow_table_get_raw(table);
auto arrow_sink = garrow_output_stream_get_raw(sink);
arrow::Status status;
if (properties) {
auto arrow_properties = garrow_feather_write_properties_get_raw(properties);
status = arrow::ipc::feather::WriteTable(*arrow_table,
arrow_sink.get(),
*arrow_properties);
} else {
status = arrow::ipc::feather::WriteTable(*arrow_table, arrow_sink.get());
}
return garrow::check(error, status, "[feather-write-file]");
}
G_END_DECLS
GArrowTable *
garrow_table_new_raw(std::shared_ptr<arrow::Table> *arrow_table)
{
auto table = GARROW_TABLE(g_object_new(GARROW_TYPE_TABLE,
"table", arrow_table,
NULL));
return table;
}
std::shared_ptr<arrow::Table>
garrow_table_get_raw(GArrowTable *table)
{
auto priv = GARROW_TABLE_GET_PRIVATE(table);
return priv->table;
}
arrow::ipc::feather::WriteProperties *
garrow_feather_write_properties_get_raw(GArrowFeatherWriteProperties *properties)
{
auto priv = GARROW_FEATHER_WRITE_PROPERTIES_GET_PRIVATE(properties);
return &(priv->properties);
}
|