This is an automated email from the ASF dual-hosted git repository.
wesm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/master by this push:
new 1ab2abc ARROW-2729: [GLib] Add decimal128 array builder
1ab2abc is described below
commit 1ab2abc749f4b318a0b8c013ae680a434716f278
Author: yosuke shiro <[email protected]>
AuthorDate: Sat Jun 23 04:09:56 2018 -0400
ARROW-2729: [GLib] Add decimal128 array builder
Support Decimal128Array and DecimalType.
Author: yosuke shiro <[email protected]>
Closes #2152 from shiro615/add-garrow-decimal128-array-builder and squashes
the following commits:
1f3b62d4 <yosuke shiro> Add Decimal128ArrayBuilder
bde9537d <yosuke shiro> Add DecimalDataType
---
c_glib/arrow-glib/array-builder.cpp | 62 +++++++++++++++++++++++++++++++++++
c_glib/arrow-glib/array-builder.h | 50 ++++++++++++++++++++++++++++
c_glib/arrow-glib/basic-array.cpp | 56 +++++++++++++++++++++++++++++++
c_glib/arrow-glib/basic-array.h | 27 +++++++++++++++
c_glib/arrow-glib/basic-data-type.cpp | 40 ++++++++++++++++++++++
c_glib/arrow-glib/basic-data-type.h | 46 ++++++++++++++++++++++++++
c_glib/test/test-decimal-array.rb | 27 +++++++++++++++
c_glib/test/test-decimal-data-type.rb | 28 ++++++++++++++++
8 files changed, 336 insertions(+)
diff --git a/c_glib/arrow-glib/array-builder.cpp
b/c_glib/arrow-glib/array-builder.cpp
index 343615b..6ca7cbd 100644
--- a/c_glib/arrow-glib/array-builder.cpp
+++ b/c_glib/arrow-glib/array-builder.cpp
@@ -25,6 +25,7 @@
#include <arrow-glib/data-type.hpp>
#include <arrow-glib/error.hpp>
#include <arrow-glib/type.hpp>
+#include <arrow-glib/decimal.hpp>
template <typename BUILDER, typename VALUE>
gboolean
@@ -223,6 +224,9 @@ G_BEGIN_DECLS
*
* #GArrowStructArrayBuilder is the class to create a new
* #GArrowStructArray.
+ *
+ * #GArrowDecimal128ArrayBuilder is the class to create a new
+ * #GArrowDecimal128Array.
*/
typedef struct GArrowArrayBuilderPrivate_ {
@@ -3270,6 +3274,61 @@
garrow_struct_array_builder_get_field_builders(GArrowStructArrayBuilder *builder
}
+G_DEFINE_TYPE(GArrowDecimal128ArrayBuilder,
+ garrow_decimal128_array_builder,
+ GARROW_TYPE_ARRAY_BUILDER)
+
+static void
+garrow_decimal128_array_builder_init(GArrowDecimal128ArrayBuilder *builder)
+{
+}
+
+static void
+garrow_decimal128_array_builder_class_init(GArrowDecimal128ArrayBuilderClass
*klass)
+{
+}
+
+/**
+ * garrow_decimal128_array_builder_new:
+ * @data_type: #GArrowDecimalDataType for the decimal.
+ *
+ * Returns: A newly created #GArrowDecimal128ArrayBuilder.
+ *
+ * Since: 0.10.0
+ */
+GArrowDecimal128ArrayBuilder *
+garrow_decimal128_array_builder_new(GArrowDecimalDataType *data_type)
+{
+ auto arrow_data_type = garrow_data_type_get_raw(GARROW_DATA_TYPE(data_type));
+ auto builder = garrow_array_builder_new(arrow_data_type,
+ NULL,
+ "[decimal128-array-builder][new]");
+ return GARROW_DECIMAL128_ARRAY_BUILDER(builder);
+}
+
+/**
+ * garrow_decimal128_array_builder_append:
+ * @builder: A #GArrowDecimal128ArrayBuilder.
+ * @value: A decimal value.
+ * @error: (nullable): Return location for a #GError or %NULL.
+ *
+ * Returns: %TRUE on success, %FALSE if there was an error.
+ *
+ * Since: 0.10.0
+ */
+gboolean
+garrow_decimal128_array_builder_append(GArrowDecimal128ArrayBuilder *builder,
+ GArrowDecimal128 *value,
+ GError **error)
+{
+ auto arrow_decimal = garrow_decimal128_get_raw(value);
+ return garrow_array_builder_append<arrow::Decimal128Builder *>
+ (GARROW_ARRAY_BUILDER(builder),
+ *arrow_decimal,
+ error,
+ "[decimal128-array-builder][append]");
+}
+
G_END_DECLS
GArrowArrayBuilder *
@@ -3338,6 +3397,9 @@ garrow_array_builder_new_raw(arrow::ArrayBuilder
*arrow_builder,
case arrow::Type::type::STRUCT:
type = GARROW_TYPE_STRUCT_ARRAY_BUILDER;
break;
+ case arrow::Type::type::DECIMAL:
+ type = GARROW_TYPE_DECIMAL128_ARRAY_BUILDER;
+ break;
default:
type = GARROW_TYPE_ARRAY_BUILDER;
break;
diff --git a/c_glib/arrow-glib/array-builder.h
b/c_glib/arrow-glib/array-builder.h
index 284a78f..7a33058 100644
--- a/c_glib/arrow-glib/array-builder.h
+++ b/c_glib/arrow-glib/array-builder.h
@@ -21,6 +21,7 @@
#include <arrow-glib/array.h>
#include <arrow-glib/gobject-type.h>
+#include <arrow-glib/decimal.h>
G_BEGIN_DECLS
@@ -1309,4 +1310,53 @@ GArrowArrayBuilder
*garrow_struct_array_builder_get_field_builder(GArrowStructAr
gint i);
GList *garrow_struct_array_builder_get_field_builders(GArrowStructArrayBuilder
*builder);
+
+#define GARROW_TYPE_DECIMAL128_ARRAY_BUILDER \
+ (garrow_decimal128_array_builder_get_type())
+#define GARROW_DECIMAL128_ARRAY_BUILDER(obj) \
+ (G_TYPE_CHECK_INSTANCE_CAST((obj), \
+ GARROW_TYPE_DECIMAL128_ARRAY_BUILDER, \
+ GArrowDecimal128ArrayBuilder))
+#define GARROW_DECIMAL128_ARRAY_BUILDER_CLASS(klass) \
+ (G_TYPE_CHECK_CLASS_CAST((klass), \
+ GARROW_TYPE_DECIMAL128_ARRAY_BUILDER, \
+ GArrowDecimal128ArrayBuilderClass))
+#define GARROW_IS_DECIMAL128_ARRAY_BUILDER(obj) \
+ (G_TYPE_CHECK_INSTANCE_TYPE((obj), \
+ GARROW_TYPE_DECIMAL128_ARRAY_BUILDER))
+#define GARROW_IS_DECIMAL128_ARRAY_BUILDER_CLASS(klass) \
+ (G_TYPE_CHECK_CLASS_TYPE((klass), \
+ GARROW_TYPE_DECIMAL128_ARRAY_BUILDER))
+#define GARROW_DECIMAL128_ARRAY_BUILDER_GET_CLASS(obj) \
+ (G_TYPE_INSTANCE_GET_CLASS((obj), \
+ GARROW_TYPE_DECIMAL128_ARRAY_BUILDER, \
+ GArrowDecimal128ArrayBuilderClass))
+
+typedef struct _GArrowDecimal128ArrayBuilder
GArrowDecimal128ArrayBuilder;
+typedef struct _GArrowDecimal128ArrayBuilderClass
GArrowDecimal128ArrayBuilderClass;
+
+/**
+ * GArrowDecimal128ArrayBuilder:
+ *
+ * It wraps `arrow::Decimal128Builder`.
+ */
+struct _GArrowDecimal128ArrayBuilder
+{
+ /*< private >*/
+ GArrowArrayBuilder parent_instance;
+};
+
+struct _GArrowDecimal128ArrayBuilderClass
+{
+ GArrowArrayBuilderClass parent_class;
+};
+
+GType garrow_decimal128_array_builder_get_type(void) G_GNUC_CONST;
+
+GArrowDecimal128ArrayBuilder
*garrow_decimal128_array_builder_new(GArrowDecimalDataType *data_type);
+
+gboolean garrow_decimal128_array_builder_append(GArrowDecimal128ArrayBuilder
*builder,
+ GArrowDecimal128 *value,
+ GError **error);
+
G_END_DECLS
diff --git a/c_glib/arrow-glib/basic-array.cpp
b/c_glib/arrow-glib/basic-array.cpp
index 923d192..9223056 100644
--- a/c_glib/arrow-glib/basic-array.cpp
+++ b/c_glib/arrow-glib/basic-array.cpp
@@ -27,6 +27,7 @@
#include <arrow-glib/basic-data-type.hpp>
#include <arrow-glib/error.hpp>
#include <arrow-glib/type.hpp>
+#include <arrow-glib/decimal.hpp>
#include <iostream>
#include <sstream>
@@ -194,6 +195,10 @@ G_BEGIN_DECLS
* nanoseconds since midnight in 64-bit signed integer array. It can
* store zero or more time data. If you don't have Arrow format data,
* you need to use #GArrowTime64ArrayBuilder to create a new array.
+ *
+ * #GArrowDecimal128Array is a class for 128-bit decimal array. It can store
zero
+ * or more 128-bit decimal data. If you don't have Arrow format data, you need
+ * to use #GArrowDecimalArrayBuilder to create a new array.
*/
typedef struct GArrowArrayPrivate_ {
@@ -2105,6 +2110,54 @@ garrow_time64_array_get_values(GArrowTime64Array *array,
return reinterpret_cast<const gint64 *>(values);
}
+
+G_DEFINE_TYPE(GArrowFixedSizeBinaryArray,
+ garrow_fixed_size_binary_array,
+ GARROW_TYPE_PRIMITIVE_ARRAY)
+static void
+garrow_fixed_size_binary_array_init(GArrowFixedSizeBinaryArray *object)
+{
+}
+
+static void
+garrow_fixed_size_binary_array_class_init(GArrowFixedSizeBinaryArrayClass
*klass)
+{
+}
+
+
+G_DEFINE_TYPE(GArrowDecimal128Array,
+ garrow_decimal128_array,
+ GARROW_TYPE_FIXED_SIZE_BINARY_ARRAY)
+static void
+garrow_decimal128_array_init(GArrowDecimal128Array *object)
+{
+}
+
+static void
+garrow_decimal128_array_class_init(GArrowDecimal128ArrayClass *klass)
+{
+}
+
+/**
+ * garrow_decimal128_array_get_value:
+ * @array: A #GArrowDecimal128Array.
+ * @i: The index of the target value.
+ *
+ * Returns: The i-th value.
+ *
+ * Since: 0.10.0
+ */
+gchar *
+garrow_decimal128_array_get_value(GArrowDecimal128Array *array,
+ gint64 i)
+{
+ auto arrow_array = garrow_array_get_raw(GARROW_ARRAY(array));
+ auto arrow_decimal128_array =
+ static_cast<arrow::Decimal128Array *>(arrow_array.get());
+ auto value = arrow_decimal128_array->FormatValue(i);
+ return g_strndup(value.data(), value.size());
+}
+
G_END_DECLS
GArrowArray *
@@ -2180,6 +2233,9 @@ garrow_array_new_raw(std::shared_ptr<arrow::Array>
*arrow_array)
case arrow::Type::type::DICTIONARY:
type = GARROW_TYPE_DICTIONARY_ARRAY;
break;
+ case arrow::Type::type::DECIMAL:
+ type = GARROW_TYPE_DECIMAL128_ARRAY;
+ break;
default:
type = GARROW_TYPE_ARRAY;
break;
diff --git a/c_glib/arrow-glib/basic-array.h b/c_glib/arrow-glib/basic-array.h
index 4bf0af3..957e938 100644
--- a/c_glib/arrow-glib/basic-array.h
+++ b/c_glib/arrow-glib/basic-array.h
@@ -630,4 +630,31 @@ gint64 garrow_time64_array_get_value(GArrowTime64Array
*array,
const gint64 *garrow_time64_array_get_values(GArrowTime64Array *array,
gint64 *length);
+
+#define GARROW_TYPE_FIXED_SIZE_BINARY_ARRAY
(garrow_fixed_size_binary_array_get_type())
+G_DECLARE_DERIVABLE_TYPE(GArrowFixedSizeBinaryArray,
+ garrow_fixed_size_binary_array,
+ GARROW,
+ FIXED_SIZE_BINARY_ARRAY,
+ GArrowPrimitiveArray)
+struct _GArrowFixedSizeBinaryArrayClass
+{
+ GArrowPrimitiveArrayClass parent_class;
+};
+
+
+#define GARROW_TYPE_DECIMAL128_ARRAY (garrow_decimal128_array_get_type())
+G_DECLARE_DERIVABLE_TYPE(GArrowDecimal128Array,
+ garrow_decimal128_array,
+ GARROW,
+ DECIMAL128_ARRAY,
+ GArrowFixedSizeBinaryArray)
+struct _GArrowDecimal128ArrayClass
+{
+ GArrowFixedSizeBinaryArrayClass parent_class;
+};
+
+gchar *garrow_decimal128_array_get_value(GArrowDecimal128Array *array,
+ gint64 i);
+
G_END_DECLS
diff --git a/c_glib/arrow-glib/basic-data-type.cpp
b/c_glib/arrow-glib/basic-data-type.cpp
index 82abfa3..d5706f1 100644
--- a/c_glib/arrow-glib/basic-data-type.cpp
+++ b/c_glib/arrow-glib/basic-data-type.cpp
@@ -84,6 +84,8 @@ G_BEGIN_DECLS
*
* #GArrowTime64DataType is a class for the number of microseconds or
* nanoseconds since midnight in 64-bit signed integer data type.
+ *
+ * #GArrowDecimalDataType is a class for 128-bit decimal data type.
*/
typedef struct GArrowDataTypePrivate_ {
@@ -1037,6 +1039,41 @@ garrow_time64_data_type_new(GArrowTimeUnit unit, GError
**error)
return data_type;
}
+
+G_DEFINE_TYPE(GArrowDecimalDataType, \
+ garrow_decimal_data_type, \
+ GARROW_TYPE_DATA_TYPE)
+
+static void
+garrow_decimal_data_type_init(GArrowDecimalDataType *object)
+{
+}
+
+static void
+garrow_decimal_data_type_class_init(GArrowDecimalDataTypeClass *klass)
+{
+}
+
+/**
+ * garrow_decimal_data_type_new:
+ *
+ * Returns: The newly created decimal datatype.
+ *
+ * Since: 0.10.0
+ */
+GArrowDecimalDataType *
+garrow_decimal_data_type_new(gint32 precision,
+ gint32 scale)
+{
+ auto arrow_data_type = arrow::decimal(precision, scale);
+
+ GArrowDecimalDataType *data_type =
+ GARROW_DECIMAL_DATA_TYPE(g_object_new(GARROW_TYPE_DECIMAL_DATA_TYPE,
+ "data-type", &arrow_data_type,
+ NULL));
+ return data_type;
+}
+
G_END_DECLS
GArrowDataType *
@@ -1112,6 +1149,9 @@ garrow_data_type_new_raw(std::shared_ptr<arrow::DataType>
*arrow_data_type)
case arrow::Type::type::DICTIONARY:
type = GARROW_TYPE_DICTIONARY_DATA_TYPE;
break;
+ case arrow::Type::type::DECIMAL:
+ type = GARROW_TYPE_DECIMAL_DATA_TYPE;
+ break;
default:
type = GARROW_TYPE_DATA_TYPE;
break;
diff --git a/c_glib/arrow-glib/basic-data-type.h
b/c_glib/arrow-glib/basic-data-type.h
index 478c905..fe27042 100644
--- a/c_glib/arrow-glib/basic-data-type.h
+++ b/c_glib/arrow-glib/basic-data-type.h
@@ -21,6 +21,7 @@
#include <arrow-glib/gobject-type.h>
#include <arrow-glib/type.h>
+#include <arrow-glib/decimal.h>
G_BEGIN_DECLS
@@ -648,4 +649,49 @@ GType garrow_time64_data_type_get_type
(void) G_GNUC_CONST;
GArrowTime64DataType *garrow_time64_data_type_new (GArrowTimeUnit unit,
GError **error);
+
+#define GARROW_TYPE_DECIMAL_DATA_TYPE \
+ (garrow_decimal_data_type_get_type())
+#define GARROW_DECIMAL_DATA_TYPE(obj) \
+ (G_TYPE_CHECK_INSTANCE_CAST((obj), \
+ GARROW_TYPE_DECIMAL_DATA_TYPE, \
+ GArrowDecimalDataType))
+#define GARROW_DECIMAL_DATA_TYPE_CLASS(klass) \
+ (G_TYPE_CHECK_CLASS_CAST((klass), \
+ GARROW_TYPE_DECIMAL_DATA_TYPE, \
+ GArrowDecimalDataTypeClass))
+#define GARROW_IS_DECIMAL_DATA_TYPE(obj) \
+ (G_TYPE_CHECK_INSTANCE_TYPE((obj), \
+ GARROW_TYPE_DECIMAL_DATA_TYPE))
+#define GARROW_IS_DECIMAL_DATA_TYPE_CLASS(klass) \
+ (G_TYPE_CHECK_CLASS_TYPE((klass), \
+ GARROW_TYPE_DECIMAL_DATA_TYPE))
+#define GARROW_DECIMAL_DATA_TYPE_GET_CLASS(obj), \
+ (G_TYPE_INSTANCE_GET_CLASS((obj), \
+ GARROW_TYPE_DECIMAL_DATA_TYPE, \
+ GArrowDecimalDataTypeClass))
+
+typedef struct _GArrowDecimalDataType GArrowDecimalDataType;
+typedef struct _GArrowDecimalDataTypeClass GArrowDecimalDataTypeClass;
+
+/**
+ * GArrowDecimalType:
+ *
+ * It wraps `arrow::DecimalType`.
+ */
+struct _GArrowDecimalDataType
+{
+ /*< private >*/
+ GArrowDataType parent_instance;
+};
+
+struct _GArrowDecimalDataTypeClass
+{
+ GArrowDataTypeClass parent_class;
+};
+
+GType garrow_decimal_data_type_get_type (void) G_GNUC_CONST;
+GArrowDecimalDataType *garrow_decimal_data_type_new (gint32 precision,
+ gint32 scale);
+
G_END_DECLS
diff --git a/c_glib/test/test-decimal-array.rb
b/c_glib/test/test-decimal-array.rb
new file mode 100644
index 0000000..303cc07
--- /dev/null
+++ b/c_glib/test/test-decimal-array.rb
@@ -0,0 +1,27 @@
+# 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.
+
+class TestDecimalArray < Test::Unit::TestCase
+ def test_value
+ data_type = Arrow::DecimalDataType.new(8,2)
+ builder = Arrow::Decimal128ArrayBuilder.new(data_type)
+ decimal = Arrow::Decimal128.new("23423445")
+ builder.append(decimal)
+ array = builder.finish
+ assert_equal("234234.45", array.get_value(0))
+ end
+end
diff --git a/c_glib/test/test-decimal-data-type.rb
b/c_glib/test/test-decimal-data-type.rb
new file mode 100644
index 0000000..dc3db3c
--- /dev/null
+++ b/c_glib/test/test-decimal-data-type.rb
@@ -0,0 +1,28 @@
+# 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.
+
+class TestDecimalDataType < Test::Unit::TestCase
+ def test_type
+ data_type = Arrow::DecimalDataType.new(2, 0)
+ assert_equal(Arrow::Type::DECIMAL, data_type.id)
+ end
+
+ def test_to_s
+ data_type = Arrow::DecimalDataType.new(2, 0)
+ assert_equal("decimal(2, 0)", data_type.to_s)
+ end
+end