summaryrefslogtreecommitdiffstats
path: root/src/arrow/c_glib/parquet-glib/arrow-file-writer.cpp
blob: c53bb94cebd2bbbce291142224fadb4742c5afed (plain)
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
/*
 * 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/arrow-glib.hpp>

#include <parquet-glib/arrow-file-writer.hpp>

G_BEGIN_DECLS

/**
 * SECTION: arrow-file-writer
 * @short_description: Arrow file writer class
 * @include: parquet-glib/parquet-glib.h
 *
 * #GParquetWriterProperties is a class for the writer properties.
 *
 * #GParquetArrowFileWriter is a class for writer Apache Arrow data to
 * file as Apache Parquet format.
 */

typedef struct GParquetWriterPropertiesPrivate_ {
  std::shared_ptr<parquet::WriterProperties> properties;
  parquet::WriterProperties::Builder *builder;
  gboolean changed;
} GParquetWriterPropertiesPrivate;

G_DEFINE_TYPE_WITH_PRIVATE(GParquetWriterProperties,
                           gparquet_writer_properties,
                           G_TYPE_OBJECT)

#define GPARQUET_WRITER_PROPERTIES_GET_PRIVATE(object) \
  static_cast<GParquetWriterPropertiesPrivate *>(      \
    gparquet_writer_properties_get_instance_private(   \
      GPARQUET_WRITER_PROPERTIES(object)))

static void
gparquet_writer_properties_finalize(GObject *object)
{
  auto priv = GPARQUET_WRITER_PROPERTIES_GET_PRIVATE(object);

  priv->properties.~shared_ptr();
  delete priv->builder;

  G_OBJECT_CLASS(gparquet_writer_properties_parent_class)->finalize(object);
}

static void
gparquet_writer_properties_init(GParquetWriterProperties *object)
{
  auto priv = GPARQUET_WRITER_PROPERTIES_GET_PRIVATE(object);
  new(&priv->properties) std::shared_ptr<parquet::WriterProperties>;
  priv->builder = new parquet::WriterProperties::Builder();
  priv->changed = TRUE;
}

static void
gparquet_writer_properties_class_init(GParquetWriterPropertiesClass *klass)
{
  auto gobject_class = G_OBJECT_CLASS(klass);

  gobject_class->finalize     = gparquet_writer_properties_finalize;
}

/**
 * gparquet_writer_properties_new:
 *
 * Return: A newly created #GParquetWriterProperties.
 *
 * Since: 0.17.0
 */
GParquetWriterProperties *
gparquet_writer_properties_new(void)
{
  auto writer_properties = g_object_new(GPARQUET_TYPE_WRITER_PROPERTIES,
                                        NULL);
  return GPARQUET_WRITER_PROPERTIES(writer_properties);
}

/**
 * gparquet_writer_properties_set_compression:
 * @properties: A #GParquetWriterProperties.
 * @compression_type: A #GArrowCompressionType.
 * @path: (nullable): The column path as dot string.
 *
 * Since: 0.17.0
 */
void
gparquet_writer_properties_set_compression(GParquetWriterProperties *properties,
                                           GArrowCompressionType compression_type,
                                           const gchar *path)
{
  auto arrow_compression_type = garrow_compression_type_to_raw(compression_type);
  auto priv = GPARQUET_WRITER_PROPERTIES_GET_PRIVATE(properties);
  if (path) {
    priv->builder->compression(path, arrow_compression_type);
  } else {
    priv->builder->compression(arrow_compression_type);
  }
  priv->changed = TRUE;
}

/**
 * gparquet_writer_properties_get_compression_path:
 * @properties: A #GParquetWriterProperties.
 * @path: The path as dot string.
 *
 * Returns: The compression type of #GParquetWriterProperties.
 *
 * Since: 0.17.0
 */
GArrowCompressionType
gparquet_writer_properties_get_compression_path(GParquetWriterProperties *properties,
                                                const gchar *path)
{
  auto parquet_properties = gparquet_writer_properties_get_raw(properties);
  auto parquet_column_path = parquet::schema::ColumnPath::FromDotString(path);
  auto arrow_compression = parquet_properties->compression(parquet_column_path);
  return garrow_compression_type_from_raw(arrow_compression);
}

/**
 * gparquet_writer_properties_enable_dictionary:
 * @properties: A #GParquetWriterProperties.
 * @path: (nullable): The column path as dot string.
 *
 * Since: 0.17.0
 */
void
gparquet_writer_properties_enable_dictionary(GParquetWriterProperties *properties,
                                             const gchar *path)
{
  auto priv = GPARQUET_WRITER_PROPERTIES_GET_PRIVATE(properties);
  if (path) {
    priv->builder->enable_dictionary(path);
  } else {
    priv->builder->enable_dictionary();
  }
  priv->changed = TRUE;
}

/**
 * gparquet_writer_properties_disable_dictionary:
 * @properties: A #GParquetWriterProperties.
 * @path: (nullable): The column path as dot string.
 *
 * Since: 0.17.0
 */
void
gparquet_writer_properties_disable_dictionary(GParquetWriterProperties *properties,
                                              const gchar *path)
{
  auto priv = GPARQUET_WRITER_PROPERTIES_GET_PRIVATE(properties);
  if (path) {
    priv->builder->disable_dictionary(path);
  } else {
    priv->builder->disable_dictionary();
  }
  priv->changed = TRUE;
}

/**
 * gparquet_writer_properties_is_dictionary_enabled:
 * @properties: A #GParquetWriterProperties.
 * @path: The path as dot string.
 *
 * Returns: %TRUE on dictionary enabled, %FALSE on dictionary disabled.
 *
 * Since: 0.17.0
 */
gboolean
gparquet_writer_properties_is_dictionary_enabled(GParquetWriterProperties *properties,
                                                 const gchar *path)
{
  auto parquet_properties = gparquet_writer_properties_get_raw(properties);
  auto parquet_column_path = parquet::schema::ColumnPath::FromDotString(path);
  return parquet_properties->dictionary_enabled(parquet_column_path);
}

/**
 * gparquet_writer_properties_set_dictionary_page_size_limit:
 * @properties: A #GParquetWriterProperties.
 * @limit: The dictionary page size limit.
 *
 * Since: 0.17.0
 */
void
gparquet_writer_properties_set_dictionary_page_size_limit(GParquetWriterProperties *properties,
                                                          gint64 limit)
{
  auto priv = GPARQUET_WRITER_PROPERTIES_GET_PRIVATE(properties);
  priv->builder->dictionary_pagesize_limit(limit);
  priv->changed = TRUE;
}

/**
 * gparquet_writer_properties_get_dictionary_page_size_limit:
 * @properties: A #GParquetWriterProperties.
 *
 * Returns: The dictionary page size limit.
 *
 * Since: 0.17.0
 */
gint64
gparquet_writer_properties_get_dictionary_page_size_limit(GParquetWriterProperties *properties)
{
  auto parquet_properties = gparquet_writer_properties_get_raw(properties);
  return parquet_properties->dictionary_pagesize_limit();
}

/**
 * gparquet_writer_properties_set_batch_size:
 * @properties: A #GParquetWriterProperties.
 * @batch_size: The batch size.
 *
 * Since: 0.17.0
 */
void
gparquet_writer_properties_set_batch_size(GParquetWriterProperties *properties,
                                          gint64 batch_size)
{
  auto priv = GPARQUET_WRITER_PROPERTIES_GET_PRIVATE(properties);
  priv->builder->write_batch_size(batch_size);
  priv->changed = TRUE;
}

/**
 * gparquet_writer_properties_get_batch_size:
 * @properties: A #GParquetWriterProperties.
 *
 * Returns: The batch size.
 *
 * Since: 0.17.0
 */
gint64
gparquet_writer_properties_get_batch_size(GParquetWriterProperties *properties)
{
  auto parquet_properties = gparquet_writer_properties_get_raw(properties);
  return parquet_properties->write_batch_size();
}

/**
 * gparquet_writer_properties_set_max_row_group_length:
 * @properties: A #GParquetWriterProperties.
 * @length: The max row group length.
 *
 * Since: 0.17.0
 */
void
gparquet_writer_properties_set_max_row_group_length(GParquetWriterProperties *properties,
                                                    gint64 length)
{
  auto priv = GPARQUET_WRITER_PROPERTIES_GET_PRIVATE(properties);
  priv->builder->max_row_group_length(length);
  priv->changed = TRUE;
}

/**
 * gparquet_writer_properties_get_max_row_group_length:
 * @properties: A #GParquetWriterProperties.
 *
 * Returns: The max row group length.
 *
 * Since: 0.17.0
 */
gint64
gparquet_writer_properties_get_max_row_group_length(GParquetWriterProperties *properties)
{
  auto parquet_properties = gparquet_writer_properties_get_raw(properties);
  return parquet_properties->max_row_group_length();
}

/**
 * gparquet_writer_properties_set_data_page_size:
 * @properties: A #GParquetWriterProperties.
 * @data_page_size: The data page size.
 *
 * Since: 0.17.0
 */
void
gparquet_writer_properties_set_data_page_size(GParquetWriterProperties *properties,
                                              gint64 data_page_size)
{
  auto priv = GPARQUET_WRITER_PROPERTIES_GET_PRIVATE(properties);
  priv->builder->data_pagesize(data_page_size);
  priv->changed = TRUE;
}

/**
 * gparquet_writer_properties_get_data_page_size:
 * @properties: A #GParquetWriterProperties.
 *
 * Returns: The data page size.
 *
 * Since: 0.17.0
 */
gint64
gparquet_writer_properties_get_data_page_size(GParquetWriterProperties *properties)
{
  auto parquet_properties = gparquet_writer_properties_get_raw(properties);
  return parquet_properties->data_pagesize();
}


typedef struct GParquetArrowFileWriterPrivate_ {
  parquet::arrow::FileWriter *arrow_file_writer;
} GParquetArrowFileWriterPrivate;

enum {
  PROP_0,
  PROP_ARROW_FILE_WRITER
};

G_DEFINE_TYPE_WITH_PRIVATE(GParquetArrowFileWriter,
                           gparquet_arrow_file_writer,
                           G_TYPE_OBJECT)

#define GPARQUET_ARROW_FILE_WRITER_GET_PRIVATE(obj)     \
  static_cast<GParquetArrowFileWriterPrivate *>(        \
     gparquet_arrow_file_writer_get_instance_private(   \
       GPARQUET_ARROW_FILE_WRITER(obj)))

static void
gparquet_arrow_file_writer_finalize(GObject *object)
{
  auto priv = GPARQUET_ARROW_FILE_WRITER_GET_PRIVATE(object);

  delete priv->arrow_file_writer;

  G_OBJECT_CLASS(gparquet_arrow_file_writer_parent_class)->finalize(object);
}

static void
gparquet_arrow_file_writer_set_property(GObject *object,
                                        guint prop_id,
                                        const GValue *value,
                                        GParamSpec *pspec)
{
  auto priv = GPARQUET_ARROW_FILE_WRITER_GET_PRIVATE(object);

  switch (prop_id) {
  case PROP_ARROW_FILE_WRITER:
    priv->arrow_file_writer =
      static_cast<parquet::arrow::FileWriter *>(g_value_get_pointer(value));
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
    break;
  }
}

static void
gparquet_arrow_file_writer_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
gparquet_arrow_file_writer_init(GParquetArrowFileWriter *object)
{
}

static void
gparquet_arrow_file_writer_class_init(GParquetArrowFileWriterClass *klass)
{
  GParamSpec *spec;

  auto gobject_class = G_OBJECT_CLASS(klass);

  gobject_class->finalize     = gparquet_arrow_file_writer_finalize;
  gobject_class->set_property = gparquet_arrow_file_writer_set_property;
  gobject_class->get_property = gparquet_arrow_file_writer_get_property;

  spec = g_param_spec_pointer("arrow-file-writer",
                              "ArrowFileWriter",
                              "The raw std::shared<parquet::arrow::FileWriter> *",
                              static_cast<GParamFlags>(G_PARAM_WRITABLE |
                                                       G_PARAM_CONSTRUCT_ONLY));
  g_object_class_install_property(gobject_class, PROP_ARROW_FILE_WRITER, spec);
}

/**
 * gparquet_arrow_file_writer_new_arrow:
 * @schema: Arrow schema for written data.
 * @sink: Arrow output stream to be written.
 * @writer_properties: (nullable): A #GParquetWriterProperties.
 * @error: (nullable): Return locatipcn for a #GError or %NULL.
 *
 * Returns: (nullable): A newly created #GParquetArrowFileWriter.
 *
 * Since: 0.11.0
 */
GParquetArrowFileWriter *
gparquet_arrow_file_writer_new_arrow(GArrowSchema *schema,
                                     GArrowOutputStream *sink,
                                     GParquetWriterProperties *writer_properties,
                                     GError **error)
{
  auto arrow_schema = garrow_schema_get_raw(schema).get();
  auto arrow_output_stream = garrow_output_stream_get_raw(sink);
  auto arrow_memory_pool = arrow::default_memory_pool();
  std::unique_ptr<parquet::arrow::FileWriter> parquet_arrow_file_writer;
  arrow::Status status;
  if (writer_properties) {
    auto parquet_writer_properties = gparquet_writer_properties_get_raw(writer_properties);
    status = parquet::arrow::FileWriter::Open(*arrow_schema,
                                              arrow_memory_pool,
                                              arrow_output_stream,
                                              parquet_writer_properties,
                                              &parquet_arrow_file_writer);
  } else {
    auto parquet_writer_properties = parquet::default_writer_properties();
    status = parquet::arrow::FileWriter::Open(*arrow_schema,
                                              arrow_memory_pool,
                                              arrow_output_stream,
                                              parquet_writer_properties,
                                              &parquet_arrow_file_writer);
  }
  if (garrow_error_check(error,
                         status,
                         "[parquet][arrow][file-writer][new-arrow]")) {
    return gparquet_arrow_file_writer_new_raw(parquet_arrow_file_writer.release());
  } else {
    return NULL;
  }
}

/**
 * gparquet_arrow_file_writer_new_path:
 * @schema: Arrow schema for written data.
 * @path: Path to be read.
 * @writer_properties: (nullable): A #GParquetWriterProperties.
 * @error: (nullable): Return locatipcn for a #GError or %NULL.
 *
 * Returns: (nullable): A newly created #GParquetArrowFileWriter.
 *
 * Since: 0.11.0
 */
GParquetArrowFileWriter *
gparquet_arrow_file_writer_new_path(GArrowSchema *schema,
                                    const gchar *path,
                                    GParquetWriterProperties *writer_properties,
                                    GError **error)
{
  auto arrow_file_output_stream =
    arrow::io::FileOutputStream::Open(path, false);
  if (!garrow::check(error,
                     arrow_file_output_stream,
                     "[parquet][arrow][file-writer][new-path]")) {
    return NULL;
  }

  auto arrow_schema = garrow_schema_get_raw(schema).get();
  std::shared_ptr<arrow::io::OutputStream> arrow_output_stream =
    arrow_file_output_stream.ValueOrDie();
  auto arrow_memory_pool = arrow::default_memory_pool();
  std::unique_ptr<parquet::arrow::FileWriter> parquet_arrow_file_writer;
  arrow::Status status;
  if (writer_properties) {
    auto parquet_writer_properties = gparquet_writer_properties_get_raw(writer_properties);
    status = parquet::arrow::FileWriter::Open(*arrow_schema,
                                              arrow_memory_pool,
                                              arrow_output_stream,
                                              parquet_writer_properties,
                                              &parquet_arrow_file_writer);
  } else {
    auto parquet_writer_properties = parquet::default_writer_properties();
    status = parquet::arrow::FileWriter::Open(*arrow_schema,
                                              arrow_memory_pool,
                                              arrow_output_stream,
                                              parquet_writer_properties,
                                              &parquet_arrow_file_writer);
  }
  if (garrow::check(error,
                    status,
                    "[parquet][arrow][file-writer][new-path]")) {
    return gparquet_arrow_file_writer_new_raw(parquet_arrow_file_writer.release());
  } else {
    return NULL;
  }
}

/**
 * gparquet_arrow_file_writer_write_table:
 * @writer: A #GParquetArrowFileWriter.
 * @table: A table to be written.
 * @chunk_size: The max number of rows in a row group.
 * @error: (nullable): Return locatipcn for a #GError or %NULL.
 *
 * Returns: %TRUE on success, %FALSE if there was an error.
 *
 * Since: 0.11.0
 */
gboolean
gparquet_arrow_file_writer_write_table(GParquetArrowFileWriter *writer,
                                       GArrowTable *table,
                                       guint64 chunk_size,
                                       GError **error)
{
  auto parquet_arrow_file_writer = gparquet_arrow_file_writer_get_raw(writer);
  auto arrow_table = garrow_table_get_raw(table).get();
  auto status = parquet_arrow_file_writer->WriteTable(*arrow_table, chunk_size);
  return garrow_error_check(error,
                            status,
                            "[parquet][arrow][file-writer][write-table]");
}

/**
 * gparquet_arrow_file_writer_close:
 * @writer: A #GParquetArrowFileWriter.
 * @error: (nullable): Return locatipcn for a #GError or %NULL.
 *
 * Returns: %TRUE on success, %FALSE if there was an error.
 *
 * Since: 0.11.0
 */
gboolean
gparquet_arrow_file_writer_close(GParquetArrowFileWriter *writer,
                                 GError **error)
{
  auto parquet_arrow_file_writer = gparquet_arrow_file_writer_get_raw(writer);
  auto status = parquet_arrow_file_writer->Close();
  return garrow_error_check(error,
                            status,
                            "[parquet][arrow][file-writer][close]");
}


G_END_DECLS

GParquetArrowFileWriter *
gparquet_arrow_file_writer_new_raw(parquet::arrow::FileWriter *parquet_arrow_file_writer)
{
  auto arrow_file_writer =
    GPARQUET_ARROW_FILE_WRITER(g_object_new(GPARQUET_TYPE_ARROW_FILE_WRITER,
                                            "arrow-file-writer", parquet_arrow_file_writer,
                                            NULL));
  return arrow_file_writer;
}

parquet::arrow::FileWriter *
gparquet_arrow_file_writer_get_raw(GParquetArrowFileWriter *arrow_file_writer)
{
  auto priv = GPARQUET_ARROW_FILE_WRITER_GET_PRIVATE(arrow_file_writer);
  return priv->arrow_file_writer;
}

std::shared_ptr<parquet::WriterProperties>
gparquet_writer_properties_get_raw(GParquetWriterProperties *properties)
{
  auto priv = GPARQUET_WRITER_PROPERTIES_GET_PRIVATE(properties);
  if (priv->changed) {
    priv->properties = priv->builder->build();
    priv->changed = FALSE;
  }
  return priv->properties;
}