Skip to content

Commit

Permalink
[GLib] Add more GArrowTable constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Dec 29, 2018
1 parent 8ed97cc commit 8bab804
Show file tree
Hide file tree
Showing 6 changed files with 310 additions and 17 deletions.
2 changes: 2 additions & 0 deletions c_glib/arrow-glib/composite-array.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,10 @@ GArrowStructArray *garrow_struct_array_new(GArrowDataType *data_type,
GArrowArray *garrow_struct_array_get_field(GArrowStructArray *array,
gint i);

#ifndef GARROW_DISABLE_DEPRECATED
GARROW_DEPRECATED_IN_0_10_FOR(garrow_struct_array_flatten)
GList *garrow_struct_array_get_fields(GArrowStructArray *array);
#endif

GARROW_AVAILABLE_IN_0_10
GList *garrow_struct_array_flatten(GArrowStructArray *array, GError **error);
Expand Down
4 changes: 2 additions & 2 deletions c_glib/arrow-glib/orc-file-reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ garrow_orc_file_reader_new(GArrowSeekableInputStream *file,
GError **error);

#ifndef GARROW_DISABLE_DEPRECATED
G_GNUC_DEPRECATED_FOR(garrow_orc_file_reader_set_field_indices)
GARROW_DEPRECATED_IN_0_12_FOR(garrow_orc_file_reader_set_field_indices)
void
garrow_orc_file_reader_set_field_indexes(GArrowORCFileReader *reader,
const gint *field_indexes,
Expand All @@ -50,7 +50,7 @@ garrow_orc_file_reader_set_field_indices(GArrowORCFileReader *reader,
const gint *field_indices,
guint n_field_indices);
#ifndef GARROW_DISABLE_DEPRECATED
G_GNUC_DEPRECATED_FOR(garrow_orc_file_reader_get_field_indices)
GARROW_DEPRECATED_IN_0_12_FOR(garrow_orc_file_reader_get_field_indices)
const gint *
garrow_orc_file_reader_get_field_indexes(GArrowORCFileReader *reader,
guint *n_field_indexes);
Expand Down
204 changes: 201 additions & 3 deletions c_glib/arrow-glib/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
# include <config.h>
#endif

#include <arrow-glib/array.hpp>
#include <arrow-glib/column.hpp>
#include <arrow-glib/error.hpp>
#include <arrow-glib/record-batch.hpp>
#include <arrow-glib/schema.hpp>
#include <arrow-glib/table.hpp>

Expand Down Expand Up @@ -133,22 +135,218 @@ garrow_table_class_init(GArrowTableClass *klass)
* @columns: (element-type GArrowColumn): The columns of the table.
*
* Returns: A newly created #GArrowTable.
*
* Deprecated: 0.12.0: Use garrow_table_new_values() instead.
*/
GArrowTable *
garrow_table_new(GArrowSchema *schema,
GList *columns)
{
auto arrow_schema = garrow_schema_get_raw(schema);
std::vector<std::shared_ptr<arrow::Column>> arrow_columns;
for (GList *node = columns; node; node = node->next) {
GArrowColumn *column = GARROW_COLUMN(node->data);
auto column = GARROW_COLUMN(node->data);
arrow_columns.push_back(garrow_column_get_raw(column));
}

auto arrow_table =
arrow::Table::Make(garrow_schema_get_raw(schema), arrow_columns);
auto arrow_table = arrow::Table::Make(arrow_schema, arrow_columns);
return garrow_table_new_raw(&arrow_table);
}

/**
* 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 #GArrowColumn, #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::Column>> arrow_columns;
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_COLUMN(node->data)) {
auto column = GARROW_COLUMN(node->data);
arrow_columns.push_back(garrow_column_get_raw(column));
} 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 "
"GArrowColumn, GArrowArray and GArrowRecordBatch");
return NULL;
}
}

size_t n_types = 0;
if (!arrow_columns.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 "
"GArrowColumn, GArrowArray or GArrowRecordBatch");
return NULL;
}

if (!arrow_columns.empty()) {
auto arrow_table = arrow::Table::Make(arrow_schema, arrow_columns);
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, 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 {
std::shared_ptr<arrow::Table> arrow_table;
auto status = arrow::Table::FromRecordBatches(arrow_schema,
arrow_record_batches,
&arrow_table);
if (garrow_error_check(error, status, context)) {
return garrow_table_new_raw(&arrow_table);
} else {
return NULL;
}
}
}

/**
* garrow_table_new_columns:
* @schema: The schema of the table.
* @columns: (array length=n_columns): The columns of the table.
* @n_columns: The number of columns.
* @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_columns(GArrowSchema *schema,
GArrowColumn **columns,
gsize n_columns,
GError **error)
{
auto arrow_schema = garrow_schema_get_raw(schema);
std::vector<std::shared_ptr<arrow::Column>> arrow_columns;
for (gsize i = 0; i < n_columns; ++i) {
arrow_columns.push_back(garrow_column_get_raw(columns[i]));
}

auto arrow_table = arrow::Table::Make(arrow_schema, arrow_columns);
auto status = arrow_table->Validate();
if (garrow_error_check(error, status, "[table][new][columns]")) {
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);
}

std::shared_ptr<arrow::Table> arrow_table;
auto status = arrow::Table::FromRecordBatches(arrow_schema,
arrow_record_batches,
&arrow_table);
if (garrow_error_check(error, status, "[table][new][record-batches]")) {
return garrow_table_new_raw(&arrow_table);
} else {
return NULL;
}
}

/**
* garrow_table_equal:
* @table: A #GArrowTable.
Expand Down
33 changes: 31 additions & 2 deletions c_glib/arrow-glib/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
#pragma once

#include <arrow-glib/column.h>
#include <arrow-glib/record-batch.h>
#include <arrow-glib/schema.h>
#include <arrow-glib/version.h>

G_BEGIN_DECLS

Expand All @@ -35,8 +37,35 @@ struct _GArrowTableClass
GObjectClass parent_class;
};

GArrowTable *garrow_table_new (GArrowSchema *schema,
GList *columns);
#ifndef GARROW_DISABLE_DEPRECATED
GARROW_DEPRECATED_IN_0_12_FOR(garrow_table_new_values)
GArrowTable *
garrow_table_new(GArrowSchema *schema,
GList *columns);
#endif
GARROW_AVAILABLE_IN_0_12
GArrowTable *
garrow_table_new_values(GArrowSchema *schema,
GList *values,
GError **error);
GARROW_AVAILABLE_IN_0_12
GArrowTable *
garrow_table_new_columns(GArrowSchema *schema,
GArrowColumn **columns,
gsize n_columns,
GError **error);
GARROW_AVAILABLE_IN_0_12
GArrowTable *
garrow_table_new_arrays(GArrowSchema *schema,
GArrowArray **arrays,
gsize n_arrays,
GError **error);
GARROW_AVAILABLE_IN_0_12
GArrowTable *
garrow_table_new_record_batches(GArrowSchema *schema,
GArrowRecordBatch **record_batches,
gsize n_record_batches,
GError **error);

gboolean garrow_table_equal (GArrowTable *table,
GArrowTable *other_table);
Expand Down
23 changes: 23 additions & 0 deletions c_glib/arrow-glib/version.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@
# define GARROW_UNAVAILABLE(major, minor) G_UNAVAILABLE(major, minor)
#endif

/**
* GARROW_VERSION_0_12:
*
* You can use this macro value for compile time API version check.
*
* Since: 0.12.0
*/
#define GARROW_VERSION_0_12 G_ENCODE_VERSION(0, 12)

/**
* GARROW_VERSION_0_10:
*
Expand Down Expand Up @@ -166,6 +175,20 @@

#define GARROW_AVAILABLE_IN_ALL

#if GARROW_VERSION_MIN_REQUIRED >= GARROW_VERSION_0_12
# define GARROW_DEPRECATED_IN_0_12 GARROW_DEPRECATED
# define GARROW_DEPRECATED_IN_0_12_FOR(function) GARROW_DEPRECATED_FOR(function)
#else
# define GARROW_DEPRECATED_IN_0_12
# define GARROW_DEPRECATED_IN_0_12_FOR(function)
#endif

#if GARROW_VERSION_MAX_ALLOWED < GARROW_VERSION_0_12
# define GARROW_AVAILABLE_IN_0_12 GARROW_UNAVAILABLE(0, 12)
#else
# define GARROW_AVAILABLE_IN_0_12
#endif

#if GARROW_VERSION_MIN_REQUIRED >= GARROW_VERSION_0_10
# define GARROW_DEPRECATED_IN_0_10 GARROW_DEPRECATED
# define GARROW_DEPRECATED_IN_0_10_FOR(function) GARROW_DEPRECATED_FOR(function)
Expand Down
Loading

0 comments on commit 8bab804

Please sign in to comment.