From 5b597750750388acd5aad1961ff2583e255b87b8 Mon Sep 17 00:00:00 2001 From: Kouhei Sutou Date: Thu, 21 Sep 2017 23:08:04 +0900 Subject: [PATCH 1/3] [GLib] Add UIntArrayBuilder --- c_glib/arrow-glib/array-builder.cpp | 140 +++++++++++++++++++++++++ c_glib/arrow-glib/array-builder.h | 73 ++++++------- c_glib/test/helper/buildable.rb | 4 + c_glib/test/test-array-builder.rb | 22 ++++ c_glib/test/test-uint-array-builder.rb | 59 +++++++++++ 5 files changed, 257 insertions(+), 41 deletions(-) create mode 100644 c_glib/test/test-uint-array-builder.rb diff --git a/c_glib/arrow-glib/array-builder.cpp b/c_glib/arrow-glib/array-builder.cpp index a6fad87ab8f..c7e0cef0f6d 100644 --- a/c_glib/arrow-glib/array-builder.cpp +++ b/c_glib/arrow-glib/array-builder.cpp @@ -137,6 +137,11 @@ G_BEGIN_DECLS * you use this builder instead of specific integer size builder such * as #GArrowInt8ArrayBuilder. * + * #GArrowUIntArrayBuilder is the class to create a new unsigned + * integer array. Unsigned integer size is automatically chosen. It's + * recommend that you use this builder instead of specific unsigned + * integer size builder such as #GArrowUInt8ArrayBuilder. + * * #GArrowInt8ArrayBuilder is the class to create a new * #GArrowInt8Array. * @@ -587,6 +592,141 @@ garrow_int_array_builder_append_nulls(GArrowIntArrayBuilder *builder, } +G_DEFINE_TYPE(GArrowUIntArrayBuilder, + garrow_uint_array_builder, + GARROW_TYPE_ARRAY_BUILDER) + +static void +garrow_uint_array_builder_init(GArrowUIntArrayBuilder *builder) +{ +} + +static void +garrow_uint_array_builder_class_init(GArrowUIntArrayBuilderClass *klass) +{ +} + +/** + * garrow_uint_array_builder_new: + * + * Returns: A newly created #GArrowUIntArrayBuilder. + * + * Since: 0.8.0 + */ +GArrowUIntArrayBuilder * +garrow_uint_array_builder_new(void) +{ + auto memory_pool = arrow::default_memory_pool(); + auto arrow_builder = new arrow::AdaptiveUIntBuilder(memory_pool); + auto builder = garrow_array_builder_new_raw(arrow_builder, + GARROW_TYPE_UINT_ARRAY_BUILDER); + return GARROW_UINT_ARRAY_BUILDER(builder); +} + +/** + * garrow_uint_array_builder_append: + * @builder: A #GArrowUIntArrayBuilder. + * @value: A int value. + * @error: (nullable): Return location for a #GError or %NULL. + * + * Returns: %TRUE on success, %FALSE if there was an error. + * + * Since: 0.8.0 + */ +gboolean +garrow_uint_array_builder_append(GArrowUIntArrayBuilder *builder, + guint64 value, + GError **error) +{ + return garrow_array_builder_append + (GARROW_ARRAY_BUILDER(builder), + value, + error, + "[uint-array-builder][append]"); +} + +/** + * garrow_uint_array_builder_append_values: + * @builder: A #GArrowUIntArrayBuilder. + * @values: (array length=values_length): The array of int. + * @values_length: The length of `values`. + * @is_valids: (nullable) (array length=is_valids_length): The array of + * boolean that shows whether the Nth value is valid or not. If the + * Nth `is_valids` is %TRUE, the Nth `values` is valid value. Otherwise + * the Nth value is null value. + * @is_valids_length: The length of `is_valids`. + * @error: (nullable): Return location for a #GError or %NULL. + * + * Append multiple values at once. It's efficient than multiple + * `append()` and `append_null()` calls. + * + * Returns: %TRUE on success, %FALSE if there was an error. + * + * Since: 0.8.0 + */ +gboolean +garrow_uint_array_builder_append_values(GArrowUIntArrayBuilder *builder, + const guint64 *values, + gint64 values_length, + const gboolean *is_valids, + gint64 is_valids_length, + GError **error) +{ + return garrow_array_builder_append_values + (GARROW_ARRAY_BUILDER(builder), + values, + values_length, + is_valids, + is_valids_length, + error, + "[uint-array-builder][append-values]"); +} + +/** + * garrow_uint_array_builder_append_null: + * @builder: A #GArrowUIntArrayBuilder. + * @error: (nullable): Return location for a #GError or %NULL. + * + * Returns: %TRUE on success, %FALSE if there was an error. + * + * Since: 0.8.0 + */ +gboolean +garrow_uint_array_builder_append_null(GArrowUIntArrayBuilder *builder, + GError **error) +{ + return garrow_array_builder_append_null + (GARROW_ARRAY_BUILDER(builder), + error, + "[uint-array-builder][append-null]"); +} + +/** + * garrow_uint_array_builder_append_nulls: + * @builder: A #GArrowUIntArrayBuilder. + * @n: The number of null values to be appended. + * @error: (nullable): Return location for a #GError or %NULL. + * + * Append multiple nulls at once. It's efficient than multiple + * `append_null()` calls. + * + * Returns: %TRUE on success, %FALSE if there was an error. + * + * Since: 0.8.0 + */ +gboolean +garrow_uint_array_builder_append_nulls(GArrowUIntArrayBuilder *builder, + gint64 n, + GError **error) +{ + return garrow_array_builder_append_nulls + (GARROW_ARRAY_BUILDER(builder), + n, + error, + "[uint-array-builder][append-nulls]"); +} + + G_DEFINE_TYPE(GArrowInt8ArrayBuilder, garrow_int8_array_builder, GARROW_TYPE_ARRAY_BUILDER) diff --git a/c_glib/arrow-glib/array-builder.h b/c_glib/arrow-glib/array-builder.h index 87808549afa..7a53bff11de 100644 --- a/c_glib/arrow-glib/array-builder.h +++ b/c_glib/arrow-glib/array-builder.h @@ -20,50 +20,16 @@ #pragma once #include +#include G_BEGIN_DECLS -#define GARROW_TYPE_ARRAY_BUILDER \ - (garrow_array_builder_get_type()) -#define GARROW_ARRAY_BUILDER(obj) \ - (G_TYPE_CHECK_INSTANCE_CAST((obj), \ - GARROW_TYPE_ARRAY_BUILDER, \ - GArrowArrayBuilder)) -#define GARROW_ARRAY_BUILDER_CLASS(klass) \ - (G_TYPE_CHECK_CLASS_CAST((klass), \ - GARROW_TYPE_ARRAY_BUILDER, \ - GArrowArrayBuilderClass)) -#define GARROW_IS_ARRAY_BUILDER(obj) \ - (G_TYPE_CHECK_INSTANCE_TYPE((obj), \ - GARROW_TYPE_ARRAY_BUILDER)) -#define GARROW_IS_ARRAY_BUILDER_CLASS(klass) \ - (G_TYPE_CHECK_CLASS_TYPE((klass), \ - GARROW_TYPE_ARRAY_BUILDER)) -#define GARROW_ARRAY_BUILDER_GET_CLASS(obj) \ - (G_TYPE_INSTANCE_GET_CLASS((obj), \ - GARROW_TYPE_ARRAY_BUILDER, \ - GArrowArrayBuilderClass)) - -typedef struct _GArrowArrayBuilder GArrowArrayBuilder; -typedef struct _GArrowArrayBuilderClass GArrowArrayBuilderClass; - -/** - * GArrowArrayBuilder: - * - * It wraps `arrow::ArrayBuilder`. - */ -struct _GArrowArrayBuilder -{ - /*< private >*/ - GObject parent_instance; -}; - -struct _GArrowArrayBuilderClass -{ - GObjectClass parent_class; -}; - -GType garrow_array_builder_get_type (void) G_GNUC_CONST; +#define GARROW_TYPE_ARRAY_BUILDER (garrow_array_builder_get_type()) +GARROW_DECLARE_TYPE(GArrowArrayBuilder, + garrow_array_builder, + GARROW, + ARRAY_BUILDER, + GObject) GArrowArray *garrow_array_builder_finish (GArrowArrayBuilder *builder, GError **error); @@ -189,6 +155,31 @@ gboolean garrow_int_array_builder_append_nulls(GArrowIntArrayBuilder *builder, GError **error); +#define GARROW_TYPE_UINT_ARRAY_BUILDER (garrow_uint_array_builder_get_type()) +GARROW_DECLARE_TYPE(GArrowUIntArrayBuilder, + garrow_uint_array_builder, + GARROW, + UINT_ARRAY_BUILDER, + GArrowArrayBuilder) + +GArrowUIntArrayBuilder *garrow_uint_array_builder_new(void); + +gboolean garrow_uint_array_builder_append(GArrowUIntArrayBuilder *builder, + guint64 value, + GError **error); +gboolean garrow_uint_array_builder_append_values(GArrowUIntArrayBuilder *builder, + const guint64 *values, + gint64 values_length, + const gboolean *is_valids, + gint64 is_valids_length, + GError **error); +gboolean garrow_uint_array_builder_append_null(GArrowUIntArrayBuilder *builder, + GError **error); +gboolean garrow_uint_array_builder_append_nulls(GArrowUIntArrayBuilder *builder, + gint64 n, + GError **error); + + #define GARROW_TYPE_INT8_ARRAY_BUILDER \ (garrow_int8_array_builder_get_type()) #define GARROW_INT8_ARRAY_BUILDER(obj) \ diff --git a/c_glib/test/helper/buildable.rb b/c_glib/test/helper/buildable.rb index 0ed57594126..bdb7e11caa3 100644 --- a/c_glib/test/helper/buildable.rb +++ b/c_glib/test/helper/buildable.rb @@ -25,6 +25,10 @@ def build_int_array(values) build_array(Arrow::IntArrayBuilder.new, values) end + def build_uint_array(values) + build_array(Arrow::UIntArrayBuilder.new, values) + end + def build_int8_array(values) build_array(Arrow::Int8ArrayBuilder.new, values) end diff --git a/c_glib/test/test-array-builder.rb b/c_glib/test/test-array-builder.rb index 6c0f984854d..0c47b0a75f4 100644 --- a/c_glib/test/test-array-builder.rb +++ b/c_glib/test/test-array-builder.rb @@ -130,6 +130,28 @@ def sample_values end end + sub_test_case("UIntArrayBuilder") do + def create_builder + Arrow::UIntArrayBuilder.new + end + + def builder_class_name + "uint-array-builder" + end + + def sample_values + [1, 2, 3] + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + end + sub_test_case("Int8ArrayBuilder") do def create_builder Arrow::Int8ArrayBuilder.new diff --git a/c_glib/test/test-uint-array-builder.rb b/c_glib/test/test-uint-array-builder.rb new file mode 100644 index 00000000000..89621189b45 --- /dev/null +++ b/c_glib/test/test-uint-array-builder.rb @@ -0,0 +1,59 @@ +# 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 TestUIntArrayBuilder < Test::Unit::TestCase + include Helper::Buildable + + def test_uint8 + values = [0, 2] + assert_equal(build_uint_array([*values, nil]), + Arrow::UInt8Array.new(3, + Arrow::Buffer.new(values.pack("C*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_uint16 + border_value = 2 ** 8 + values = [0, border_value] + assert_equal(build_uint_array([*values, nil]), + Arrow::UInt16Array.new(3, + Arrow::Buffer.new(values.pack("S*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_uint32 + border_value = 2 ** 16 + values = [0, border_value] + assert_equal(build_uint_array([*values, nil]), + Arrow::UInt32Array.new(3, + Arrow::Buffer.new(values.pack("L*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_uint64 + border_value = 2 ** 32 + values = [0, border_value] + assert_equal(build_uint_array([*values, nil]), + Arrow::UInt64Array.new(3, + Arrow::Buffer.new(values.pack("Q*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end +end From fd23f24855b30ea48793941a13ca5204755de5dc Mon Sep 17 00:00:00 2001 From: Kouhei Sutou Date: Fri, 22 Sep 2017 06:22:39 +0900 Subject: [PATCH 2/3] [GLib] Fix build error on macOS --- c_glib/arrow-glib/array-builder.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/c_glib/arrow-glib/array-builder.cpp b/c_glib/arrow-glib/array-builder.cpp index c7e0cef0f6d..432b394c279 100644 --- a/c_glib/arrow-glib/array-builder.cpp +++ b/c_glib/arrow-glib/array-builder.cpp @@ -674,7 +674,7 @@ garrow_uint_array_builder_append_values(GArrowUIntArrayBuilder *builder, { return garrow_array_builder_append_values (GARROW_ARRAY_BUILDER(builder), - values, + reinterpret_cast(values), values_length, is_valids, is_valids_length, From 24bb9a7dd37a5670a5b83016e5dd6a214ffb8f11 Mon Sep 17 00:00:00 2001 From: Kouhei Sutou Date: Fri, 22 Sep 2017 09:47:43 +0900 Subject: [PATCH 3/3] [GLib] Add missing "unsigned" --- c_glib/arrow-glib/array-builder.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/c_glib/arrow-glib/array-builder.cpp b/c_glib/arrow-glib/array-builder.cpp index 432b394c279..bea7e8342c7 100644 --- a/c_glib/arrow-glib/array-builder.cpp +++ b/c_glib/arrow-glib/array-builder.cpp @@ -626,7 +626,7 @@ garrow_uint_array_builder_new(void) /** * garrow_uint_array_builder_append: * @builder: A #GArrowUIntArrayBuilder. - * @value: A int value. + * @value: A unsigned int value. * @error: (nullable): Return location for a #GError or %NULL. * * Returns: %TRUE on success, %FALSE if there was an error. @@ -648,7 +648,7 @@ garrow_uint_array_builder_append(GArrowUIntArrayBuilder *builder, /** * garrow_uint_array_builder_append_values: * @builder: A #GArrowUIntArrayBuilder. - * @values: (array length=values_length): The array of int. + * @values: (array length=values_length): The array of unsigned int. * @values_length: The length of `values`. * @is_valids: (nullable) (array length=is_valids_length): The array of * boolean that shows whether the Nth value is valid or not. If the