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
|
/*
* 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/error.hpp>
#include <arrow-glib/file-system.hpp>
#include <arrow-glib/output-stream.hpp>
#include <arrow-glib/record-batch.hpp>
#include <arrow-glib/reader.hpp>
#include <arrow-glib/schema.hpp>
#include <arrow-dataset-glib/file-format.hpp>
G_BEGIN_DECLS
/**
* SECTION: file-format
* @section_id: file-format
* @title: File format classes
* @include: arrow-dataset-glib/arrow-dataset-glib.h
*
* #GADatasetFileWriteOptions is a class for options to write a file
* of this format.
*
* #GADatasetFileWriter is a class for writing a file of this format.
*
* #GADatasetFileFormat is a base class for file format classes.
*
* #GADatasetCSVFileFormat is a class for CSV file format.
*
* #GADatasetIPCFileFormat is a class for IPC file format.
*
* #GADatasetParquetFileFormat is a class for Parquet file format.
*
* Since: 3.0.0
*/
typedef struct GADatasetFileWriteOptionsPrivate_ {
std::shared_ptr<arrow::dataset::FileWriteOptions> options;
} GADatasetFileWriteOptionsPrivate;
enum {
PROP_OPTIONS = 1,
};
G_DEFINE_TYPE_WITH_PRIVATE(GADatasetFileWriteOptions,
gadataset_file_write_options,
G_TYPE_OBJECT)
#define GADATASET_FILE_WRITE_OPTIONS_GET_PRIVATE(obj) \
static_cast<GADatasetFileWriteOptionsPrivate *>( \
gadataset_file_write_options_get_instance_private( \
GADATASET_FILE_WRITE_OPTIONS(obj)))
static void
gadataset_file_write_options_finalize(GObject *object)
{
auto priv = GADATASET_FILE_WRITE_OPTIONS_GET_PRIVATE(object);
priv->options.~shared_ptr();
G_OBJECT_CLASS(gadataset_file_write_options_parent_class)->finalize(object);
}
static void
gadataset_file_write_options_set_property(GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
auto priv = GADATASET_FILE_WRITE_OPTIONS_GET_PRIVATE(object);
switch (prop_id) {
case PROP_OPTIONS:
priv->options =
*static_cast<std::shared_ptr<arrow::dataset::FileWriteOptions> *>(
g_value_get_pointer(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
gadataset_file_write_options_init(GADatasetFileWriteOptions *object)
{
auto priv = GADATASET_FILE_WRITE_OPTIONS_GET_PRIVATE(object);
new(&priv->options) std::shared_ptr<arrow::dataset::FileWriteOptions>;
}
static void
gadataset_file_write_options_class_init(GADatasetFileWriteOptionsClass *klass)
{
auto gobject_class = G_OBJECT_CLASS(klass);
gobject_class->finalize = gadataset_file_write_options_finalize;
gobject_class->set_property = gadataset_file_write_options_set_property;
GParamSpec *spec;
spec = g_param_spec_pointer("options",
"Options",
"The raw "
"std::shared<arrow::dataset::FileWriteOptions> *",
static_cast<GParamFlags>(G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property(gobject_class, PROP_OPTIONS, spec);
}
typedef struct GADatasetFileWriterPrivate_ {
std::shared_ptr<arrow::dataset::FileWriter> writer;
} GADatasetFileWriterPrivate;
enum {
PROP_WRITER = 1,
};
G_DEFINE_TYPE_WITH_PRIVATE(GADatasetFileWriter,
gadataset_file_writer,
G_TYPE_OBJECT)
#define GADATASET_FILE_WRITER_GET_PRIVATE(obj) \
static_cast<GADatasetFileWriterPrivate *>( \
gadataset_file_writer_get_instance_private( \
GADATASET_FILE_WRITER(obj)))
static void
gadataset_file_writer_finalize(GObject *object)
{
auto priv = GADATASET_FILE_WRITER_GET_PRIVATE(object);
priv->writer.~shared_ptr();
G_OBJECT_CLASS(gadataset_file_writer_parent_class)->finalize(object);
}
static void
gadataset_file_writer_set_property(GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
auto priv = GADATASET_FILE_WRITER_GET_PRIVATE(object);
switch (prop_id) {
case PROP_WRITER:
priv->writer =
*static_cast<std::shared_ptr<arrow::dataset::FileWriter> *>(
g_value_get_pointer(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
gadataset_file_writer_init(GADatasetFileWriter *object)
{
auto priv = GADATASET_FILE_WRITER_GET_PRIVATE(object);
new(&(priv->writer)) std::shared_ptr<arrow::dataset::FileWriter>;
}
static void
gadataset_file_writer_class_init(GADatasetFileWriterClass *klass)
{
auto gobject_class = G_OBJECT_CLASS(klass);
gobject_class->finalize = gadataset_file_writer_finalize;
gobject_class->set_property = gadataset_file_writer_set_property;
GParamSpec *spec;
spec = g_param_spec_pointer("writer",
"Writer",
"The raw "
"std::shared<arrow::dataset::FileWriter> *",
static_cast<GParamFlags>(G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property(gobject_class, PROP_WRITER, spec);
}
/**
* gadataset_file_writer_write_record_batch:
* @writer: A #GADatasetFileWriter.
* @record_batch: A #GArrowRecordBatch to be written.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: %TRUE on success, %FALSE on error.
*
* Since: 6.0.0
*/
gboolean
gadataset_file_writer_write_record_batch(GADatasetFileWriter *writer,
GArrowRecordBatch *record_batch,
GError **error)
{
const auto arrow_writer = gadataset_file_writer_get_raw(writer);
const auto arrow_record_batch = garrow_record_batch_get_raw(record_batch);
auto status = arrow_writer->Write(arrow_record_batch);
return garrow::check(error, status, "[file-writer][write-record-batch]");
}
/**
* gadataset_file_writer_write_record_batch_reader:
* @writer: A #GADatasetFileWriter.
* @reader: A #GArrowRecordBatchReader to be written.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: %TRUE on success, %FALSE on error.
*
* Since: 6.0.0
*/
gboolean
gadataset_file_writer_write_record_batch_reader(GADatasetFileWriter *writer,
GArrowRecordBatchReader *reader,
GError **error)
{
const auto arrow_writer = gadataset_file_writer_get_raw(writer);
auto arrow_reader = garrow_record_batch_reader_get_raw(reader);
auto status = arrow_writer->Write(arrow_reader.get());
return garrow::check(error,
status,
"[file-writer][write-record-batch-reader]");
}
/**
* gadataset_file_writer_finish:
* @writer: A #GADatasetFileWriter.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: %TRUE on success, %FALSE on error.
*
* Since: 6.0.0
*/
gboolean
gadataset_file_writer_finish(GADatasetFileWriter *writer,
GError **error)
{
const auto arrow_writer = gadataset_file_writer_get_raw(writer);
auto status = arrow_writer->Finish();
return garrow::check(error,
status,
"[file-writer][finish]");
}
typedef struct GADatasetFileFormatPrivate_ {
std::shared_ptr<arrow::dataset::FileFormat> format;
} GADatasetFileFormatPrivate;
enum {
PROP_FORMAT = 1,
};
G_DEFINE_TYPE_WITH_PRIVATE(GADatasetFileFormat,
gadataset_file_format,
G_TYPE_OBJECT)
#define GADATASET_FILE_FORMAT_GET_PRIVATE(obj) \
static_cast<GADatasetFileFormatPrivate *>( \
gadataset_file_format_get_instance_private( \
GADATASET_FILE_FORMAT(obj)))
static void
gadataset_file_format_finalize(GObject *object)
{
auto priv = GADATASET_FILE_FORMAT_GET_PRIVATE(object);
priv->format.~shared_ptr();
G_OBJECT_CLASS(gadataset_file_format_parent_class)->finalize(object);
}
static void
gadataset_file_format_set_property(GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
auto priv = GADATASET_FILE_FORMAT_GET_PRIVATE(object);
switch (prop_id) {
case PROP_FORMAT:
priv->format =
*static_cast<std::shared_ptr<arrow::dataset::FileFormat> *>(
g_value_get_pointer(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
gadataset_file_format_init(GADatasetFileFormat *object)
{
auto priv = GADATASET_FILE_FORMAT_GET_PRIVATE(object);
new(&priv->format) std::shared_ptr<arrow::dataset::FileFormat>;
}
static void
gadataset_file_format_class_init(GADatasetFileFormatClass *klass)
{
auto gobject_class = G_OBJECT_CLASS(klass);
gobject_class->finalize = gadataset_file_format_finalize;
gobject_class->set_property = gadataset_file_format_set_property;
GParamSpec *spec;
spec = g_param_spec_pointer("format",
"Format",
"The raw std::shared<arrow::dataset::FileFormat> *",
static_cast<GParamFlags>(G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property(gobject_class, PROP_FORMAT, spec);
}
/**
* gadataset_file_format_get_type_name:
* @format: A #GADatasetFileFormat.
*
* Returns: The type name of @format.
*
* It should be freed with g_free() when no longer needed.
*
* Since: 3.0.0
*/
gchar *
gadataset_file_format_get_type_name(GADatasetFileFormat *format)
{
const auto arrow_format = gadataset_file_format_get_raw(format);
const auto &type_name = arrow_format->type_name();
return g_strndup(type_name.data(), type_name.size());
}
/**
* gadataset_file_format_get_default_write_options:
* @format: A #GADatasetFileFormat.
*
* Returns: (transfer full): The default #GADatasetFileWriteOptions of @format.
*
* Since: 6.0.0
*/
GADatasetFileWriteOptions *
gadataset_file_format_get_default_write_options(GADatasetFileFormat *format)
{
const auto arrow_format = gadataset_file_format_get_raw(format);
auto arrow_options = arrow_format->DefaultWriteOptions();
return gadataset_file_write_options_new_raw(&arrow_options);
}
/**
* gadataset_file_format_open_writer:
* @format: A #GADatasetFileFormat.
* @destination: A #GArrowOutputStream.
* @file_system: The #GArrowFileSystem of @destination.
* @path: The path of @destination.
* @schema: A #GArrowSchema that is used by written record batches.
* @options: A #GADatasetFileWriteOptions.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: (transfer full): The newly created #GADatasetFileWriter of @format
* on success, %NULL on error.
*
* Since: 6.0.0
*/
GADatasetFileWriter *
gadataset_file_format_open_writer(GADatasetFileFormat *format,
GArrowOutputStream *destination,
GArrowFileSystem *file_system,
const gchar *path,
GArrowSchema *schema,
GADatasetFileWriteOptions *options,
GError **error)
{
const auto arrow_format = gadataset_file_format_get_raw(format);
auto arrow_destination = garrow_output_stream_get_raw(destination);
auto arrow_file_system = garrow_file_system_get_raw(file_system);
auto arrow_schema = garrow_schema_get_raw(schema);
auto arrow_options = gadataset_file_write_options_get_raw(options);
auto arrow_writer_result =
arrow_format->MakeWriter(arrow_destination,
arrow_schema,
arrow_options,
{arrow_file_system, path});
if (garrow::check(error, arrow_writer_result, "[file-format][open-writer]")) {
auto arrow_writer = *arrow_writer_result;
return gadataset_file_writer_new_raw(&arrow_writer);
} else {
return NULL;
}
}
/**
* gadataset_file_format_equal:
* @format: A #GADatasetFileFormat.
* @other_format: A #GADatasetFileFormat to be compared.
*
* Returns: %TRUE if they are the same content file format, %FALSE otherwise.
*
* Since: 3.0.0
*/
gboolean
gadataset_file_format_equal(GADatasetFileFormat *format,
GADatasetFileFormat *other_format)
{
const auto arrow_format = gadataset_file_format_get_raw(format);
const auto arrow_other_format = gadataset_file_format_get_raw(other_format);
return arrow_format->Equals(*arrow_other_format);
}
G_DEFINE_TYPE(GADatasetCSVFileFormat,
gadataset_csv_file_format,
GADATASET_TYPE_FILE_FORMAT)
static void
gadataset_csv_file_format_init(GADatasetCSVFileFormat *object)
{
}
static void
gadataset_csv_file_format_class_init(GADatasetCSVFileFormatClass *klass)
{
}
/**
* gadataset_csv_file_format_new:
*
* Returns: The newly created CSV file format.
*
* Since: 3.0.0
*/
GADatasetCSVFileFormat *
gadataset_csv_file_format_new(void)
{
std::shared_ptr<arrow::dataset::FileFormat> arrow_format =
std::make_shared<arrow::dataset::CsvFileFormat>();
return GADATASET_CSV_FILE_FORMAT(gadataset_file_format_new_raw(&arrow_format));
}
G_DEFINE_TYPE(GADatasetIPCFileFormat,
gadataset_ipc_file_format,
GADATASET_TYPE_FILE_FORMAT)
static void
gadataset_ipc_file_format_init(GADatasetIPCFileFormat *object)
{
}
static void
gadataset_ipc_file_format_class_init(GADatasetIPCFileFormatClass *klass)
{
}
/**
* gadataset_ipc_file_format_new:
*
* Returns: The newly created IPC file format.
*
* Since: 3.0.0
*/
GADatasetIPCFileFormat *
gadataset_ipc_file_format_new(void)
{
std::shared_ptr<arrow::dataset::FileFormat> arrow_format =
std::make_shared<arrow::dataset::IpcFileFormat>();
return GADATASET_IPC_FILE_FORMAT(gadataset_file_format_new_raw(&arrow_format));
}
G_DEFINE_TYPE(GADatasetParquetFileFormat,
gadataset_parquet_file_format,
GADATASET_TYPE_FILE_FORMAT)
static void
gadataset_parquet_file_format_init(GADatasetParquetFileFormat *object)
{
}
static void
gadataset_parquet_file_format_class_init(GADatasetParquetFileFormatClass *klass)
{
}
/**
* gadataset_parquet_file_format_new:
*
* Returns: The newly created Parquet file format.
*
* Since: 3.0.0
*/
GADatasetParquetFileFormat *
gadataset_parquet_file_format_new(void)
{
std::shared_ptr<arrow::dataset::FileFormat> arrow_format =
std::make_shared<arrow::dataset::ParquetFileFormat>();
return GADATASET_PARQUET_FILE_FORMAT(
gadataset_file_format_new_raw(&arrow_format));
}
G_END_DECLS
GADatasetFileWriteOptions *
gadataset_file_write_options_new_raw(
std::shared_ptr<arrow::dataset::FileWriteOptions> *arrow_options)
{
return GADATASET_FILE_WRITE_OPTIONS(
g_object_new(GADATASET_TYPE_FILE_WRITE_OPTIONS,
"options", arrow_options,
NULL));
}
std::shared_ptr<arrow::dataset::FileWriteOptions>
gadataset_file_write_options_get_raw(GADatasetFileWriteOptions *options)
{
auto priv = GADATASET_FILE_WRITE_OPTIONS_GET_PRIVATE(options);
return priv->options;
}
GADatasetFileWriter *
gadataset_file_writer_new_raw(
std::shared_ptr<arrow::dataset::FileWriter> *arrow_writer)
{
return GADATASET_FILE_WRITER(g_object_new(GADATASET_TYPE_FILE_WRITER,
"writer", arrow_writer,
NULL));
}
std::shared_ptr<arrow::dataset::FileWriter>
gadataset_file_writer_get_raw(GADatasetFileWriter *writer)
{
auto priv = GADATASET_FILE_WRITER_GET_PRIVATE(writer);
return priv->writer;
}
GADatasetFileFormat *
gadataset_file_format_new_raw(
std::shared_ptr<arrow::dataset::FileFormat> *arrow_format)
{
GType type = GADATASET_TYPE_FILE_FORMAT;
const auto &type_name = (*arrow_format)->type_name();
if (type_name == "csv") {
type = GADATASET_TYPE_CSV_FILE_FORMAT;
} else if (type_name == "ipc") {
type = GADATASET_TYPE_IPC_FILE_FORMAT;
} else if (type_name == "parquet") {
type = GADATASET_TYPE_PARQUET_FILE_FORMAT;
}
return GADATASET_FILE_FORMAT(g_object_new(type,
"format", arrow_format,
NULL));
}
std::shared_ptr<arrow::dataset::FileFormat>
gadataset_file_format_get_raw(GADatasetFileFormat *format)
{
auto priv = GADATASET_FILE_FORMAT_GET_PRIVATE(format);
return priv->format;
}
|