Skip to content

Commit

Permalink
ARROW-11139: [GLib] Add support for extension type
Browse files Browse the repository at this point in the history
Closes apache#9109 from kou/glib-extension-type

Authored-by: Sutou Kouhei <[email protected]>
Signed-off-by: Sutou Kouhei <[email protected]>
  • Loading branch information
kou committed Jan 6, 2021
1 parent fdf5e88 commit a911e67
Show file tree
Hide file tree
Showing 48 changed files with 1,510 additions and 17 deletions.
1 change: 1 addition & 0 deletions c_glib/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Makefile.in
/parquet-glib/*.pc
/plasma-glib/*.pc
/example/build
/example/extension-type
/example/read-batch
/example/read-stream
/gtk-doc.make
Expand Down
5 changes: 2 additions & 3 deletions c_glib/arrow-glib/array-builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2985,7 +2985,7 @@ garrow_binary_array_builder_append_value_bytes(GArrowBinaryArrayBuilder *builder

/**
* garrow_binary_array_builder_append_values:
* @builder: A #GArrowLargeBinaryArrayBuilder.
* @builder: A #GArrowBinaryArrayBuilder.
* @values: (array length=values_length): The array of #GBytes.
* @values_length: The length of @values.
* @is_valids: (nullable) (array length=is_valids_length): The array of
Expand Down Expand Up @@ -3506,7 +3506,7 @@ garrow_fixed_size_binary_array_builder_class_init(

/**
* garrow_fixed_size_binary_array_builder_new:
* @data_type: A #GArrowFixedSizeDataType for created array.
* @data_type: A #GArrowFixedSizeBinaryDataType for created array.
*
* Returns: A newly created #GArrowFixedSizeBinaryArrayBuilder.
*/
Expand Down Expand Up @@ -3683,7 +3683,6 @@ garrow_fixed_size_binary_array_builder_append_values_packed(
}



G_DEFINE_TYPE(GArrowDate32ArrayBuilder,
garrow_date32_array_builder,
GARROW_TYPE_ARRAY_BUILDER)
Expand Down
4 changes: 2 additions & 2 deletions c_glib/arrow-glib/array-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ GArrowDataType *
garrow_array_builder_get_value_data_type(GArrowArrayBuilder *builder);
GArrowType garrow_array_builder_get_value_type(GArrowArrayBuilder *builder);

GArrowArray *garrow_array_builder_finish (GArrowArrayBuilder *builder,
GError **error);
GArrowArray *garrow_array_builder_finish(GArrowArrayBuilder *builder,
GError **error);

GARROW_AVAILABLE_IN_2_0
void garrow_array_builder_reset(GArrowArrayBuilder *builder);
Expand Down
139 changes: 138 additions & 1 deletion c_glib/arrow-glib/basic-array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ G_BEGIN_DECLS
* 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 GArrowArrayPrivate_ {
Expand Down Expand Up @@ -383,7 +386,6 @@ garrow_array_class_init(GArrowArrayClass *klass)
static_cast<GParamFlags>(G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property(gobject_class, PROP_PARENT, spec);

}

/**
Expand Down Expand Up @@ -2700,6 +2702,118 @@ garrow_decimal256_array_get_value(GArrowDecimal256Array *array,
}


typedef struct GArrowExtensionArrayPrivate_ {
GArrowArray *storage;
} GArrowExtensionArrayPrivate;

enum {
PROP_STORAGE = 1
};

G_DEFINE_TYPE_WITH_PRIVATE(GArrowExtensionArray,
garrow_extension_array,
GARROW_TYPE_ARRAY)

#define GARROW_EXTENSION_ARRAY_GET_PRIVATE(obj) \
static_cast<GArrowExtensionArrayPrivate *>( \
garrow_extension_array_get_instance_private( \
GARROW_EXTENSION_ARRAY(obj)))

static void
garrow_extension_array_dispose(GObject *object)
{
auto priv = GARROW_EXTENSION_ARRAY_GET_PRIVATE(object);

if (priv->storage) {
g_object_unref(priv->storage);
priv->storage = NULL;
}

G_OBJECT_CLASS(garrow_extension_array_parent_class)->dispose(object);
}

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

switch (prop_id) {
case PROP_STORAGE:
priv->storage = GARROW_ARRAY(g_value_dup_object(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}

static void
garrow_extension_array_get_property(GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
auto priv = GARROW_EXTENSION_ARRAY_GET_PRIVATE(object);

switch (prop_id) {
case PROP_STORAGE:
g_value_set_object(value, priv->storage);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}

static void
garrow_extension_array_init(GArrowExtensionArray *object)
{
}

static void
garrow_extension_array_class_init(GArrowExtensionArrayClass *klass)
{
auto gobject_class = G_OBJECT_CLASS(klass);

gobject_class->dispose = garrow_extension_array_dispose;
gobject_class->set_property = garrow_extension_array_set_property;
gobject_class->get_property = garrow_extension_array_get_property;

GParamSpec *spec;
spec = g_param_spec_object("storage",
"storage",
"The storage array",
GARROW_TYPE_ARRAY,
static_cast<GParamFlags>(G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property(gobject_class, PROP_STORAGE, spec);
}

/**
* garrow_extension_array_get_storage:
* @array: A #GArrowExtensionArray.
*
* Returns: (transfer full): The underlying storage of the array.
*
* Since: 3.0.0
*/
GArrowArray *
garrow_extension_array_get_storage(GArrowExtensionArray *array)
{
auto priv = GARROW_EXTENSION_ARRAY_GET_PRIVATE(array);
if (priv->storage) {
g_object_ref(priv->storage);
return priv->storage;
}

auto array_priv = GARROW_ARRAY_GET_PRIVATE(array);
return garrow_array_new_raw(&(array_priv->array));
}


G_END_DECLS

GArrowArray *
Expand Down Expand Up @@ -2825,6 +2939,18 @@ garrow_array_new_raw_valist(std::shared_ptr<arrow::Array> *arrow_array,
case arrow::Type::type::DECIMAL256:
type = GARROW_TYPE_DECIMAL256_ARRAY;
break;
case arrow::Type::type::EXTENSION:
{
auto arrow_data_type = (*arrow_array)->type();
auto arrow_gextension_data_type =
std::static_pointer_cast<garrow::GExtensionType>(arrow_data_type);
if (arrow_gextension_data_type) {
type = arrow_gextension_data_type->array_gtype();
} else {
type = GARROW_TYPE_EXTENSION_ARRAY;
}
}
break;
default:
type = GARROW_TYPE_ARRAY;
break;
Expand All @@ -2834,6 +2960,17 @@ garrow_array_new_raw_valist(std::shared_ptr<arrow::Array> *arrow_array,
args));
}

GArrowExtensionArray *
garrow_extension_array_new_raw(std::shared_ptr<arrow::Array> *arrow_array,
GArrowArray *storage)
{
auto array = garrow_array_new_raw(arrow_array,
"array", arrow_array,
"storage", storage,
NULL);
return GARROW_EXTENSION_ARRAY(array);
}

std::shared_ptr<arrow::Array>
garrow_array_get_raw(GArrowArray *array)
{
Expand Down
16 changes: 16 additions & 0 deletions c_glib/arrow-glib/basic-array.h
Original file line number Diff line number Diff line change
Expand Up @@ -638,4 +638,20 @@ gchar *garrow_decimal256_array_format_value(GArrowDecimal256Array *array,
GArrowDecimal256 *garrow_decimal256_array_get_value(GArrowDecimal256Array *array,
gint64 i);

#define GARROW_TYPE_EXTENSION_ARRAY (garrow_extension_array_get_type())
G_DECLARE_DERIVABLE_TYPE(GArrowExtensionArray,
garrow_extension_array,
GARROW,
EXTENSION_ARRAY,
GArrowArray)
struct _GArrowExtensionArrayClass
{
GArrowArrayClass parent_class;
};

GARROW_AVAILABLE_IN_3_0
GArrowArray *
garrow_extension_array_get_storage(GArrowExtensionArray *array);


G_END_DECLS
3 changes: 3 additions & 0 deletions c_glib/arrow-glib/basic-array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ GArrowArray *
garrow_array_new_raw_valist(std::shared_ptr<arrow::Array> *arrow_array,
const gchar *first_property_name,
va_list args);
GArrowExtensionArray *
garrow_extension_array_new_raw(std::shared_ptr<arrow::Array> *arrow_array,
GArrowArray *storage);
std::shared_ptr<arrow::Array>
garrow_array_get_raw(GArrowArray *array);

Expand Down
Loading

0 comments on commit a911e67

Please sign in to comment.