From f24913096a87e2dfed3e26618c5c0006425ccd50 Mon Sep 17 00:00:00 2001 From: Yosuke Shiro Date: Wed, 9 Jan 2019 22:07:03 +0900 Subject: [PATCH 1/3] [GLib] Add GArrowFixedSizeBinaryDataType --- c_glib/arrow-glib/basic-data-type.cpp | 42 ++++++++++++++++++- c_glib/arrow-glib/basic-data-type.h | 21 ++++++++-- c_glib/arrow-glib/type.cpp | 2 + c_glib/arrow-glib/type.h | 3 ++ .../test/test-fixed-size-binary-data-type.rb | 30 +++++++++++++ 5 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 c_glib/test/test-fixed-size-binary-data-type.rb diff --git a/c_glib/arrow-glib/basic-data-type.cpp b/c_glib/arrow-glib/basic-data-type.cpp index 2a599963ee3a..70bac0f203a3 100644 --- a/c_glib/arrow-glib/basic-data-type.cpp +++ b/c_glib/arrow-glib/basic-data-type.cpp @@ -66,6 +66,8 @@ G_BEGIN_DECLS * * #GArrowBinaryDataType is a class for binary data type. * + * #GArrowFixedSizeBinaryDataType is a class for fixed-size binary data type. + * * #GArrowStringDataType is a class for UTF-8 encoded string data * type. * @@ -716,6 +718,41 @@ garrow_binary_data_type_new(void) } +G_DEFINE_TYPE(GArrowFixedSizeBinaryDataType, + garrow_fixed_size_binary_data_type, + GARROW_TYPE_DATA_TYPE) + +static void +garrow_fixed_size_binary_data_type_init(GArrowFixedSizeBinaryDataType *object) +{ +} + +static void +garrow_fixed_size_binary_data_type_class_init(GArrowFixedSizeBinaryDataTypeClass *klass) +{ +} + +/** + * garrow_fixed_size_binary_data_type: + * @byte_width: The byte width. + * + * Returns: The newly created fixed-size binary data type. + * + * Since: 0.12.0 + */ +GArrowFixedSizeBinaryDataType * +garrow_fixed_size_binary_data_type_new(gint32 byte_width) +{ + auto arrow_fixed_size_binary_data_type = arrow::fixed_size_binary(byte_width); + + auto fixed_size_binary_data_type = + GARROW_FIXED_SIZE_BINARY_DATA_TYPE(g_object_new(GARROW_TYPE_FIXED_SIZE_BINARY_DATA_TYPE, + "data-type", &arrow_fixed_size_binary_data_type, + NULL)); + return fixed_size_binary_data_type; +} + + G_DEFINE_TYPE(GArrowStringDataType, garrow_string_data_type, GARROW_TYPE_DATA_TYPE) @@ -1044,7 +1081,7 @@ garrow_time64_data_type_new(GArrowTimeUnit unit, GError **error) G_DEFINE_ABSTRACT_TYPE(GArrowDecimalDataType, garrow_decimal_data_type, - GARROW_TYPE_DATA_TYPE) + GARROW_TYPE_FIXED_SIZE_BINARY_DATA_TYPE) static void garrow_decimal_data_type_init(GArrowDecimalDataType *object) @@ -1197,6 +1234,9 @@ garrow_data_type_new_raw(std::shared_ptr *arrow_data_type) case arrow::Type::type::BINARY: type = GARROW_TYPE_BINARY_DATA_TYPE; break; + case arrow::Type::type::FIXED_SIZE_BINARY: + type = GARROW_TYPE_FIXED_SIZE_BINARY_DATA_TYPE; + break; case arrow::Type::type::STRING: type = GARROW_TYPE_STRING_DATA_TYPE; break; diff --git a/c_glib/arrow-glib/basic-data-type.h b/c_glib/arrow-glib/basic-data-type.h index ef41f1dbcfa0..b8c0ebe795f1 100644 --- a/c_glib/arrow-glib/basic-data-type.h +++ b/c_glib/arrow-glib/basic-data-type.h @@ -338,6 +338,22 @@ GType garrow_binary_data_type_get_type (void) G_GNUC_CONST; GArrowBinaryDataType *garrow_binary_data_type_new (void); +#define GARROW_TYPE_FIXED_SIZE_BINARY_DATA_TYPE (garrow_fixed_size_binary_data_type_get_type()) +G_DECLARE_DERIVABLE_TYPE(GArrowFixedSizeBinaryDataType, + garrow_fixed_size_binary_data_type, + GARROW, + FIXED_SIZE_BINARY_DATA_TYPE, + GArrowDataType) +struct _GArrowFixedSizeBinaryDataTypeClass +{ + GArrowDataTypeClass parent_class; +}; + +GARROW_AVAILABLE_IN_0_12 +GArrowFixedSizeBinaryDataType * +garrow_fixed_size_binary_data_type_new(gint32 byte_width); + + #define GARROW_TYPE_STRING_DATA_TYPE \ (garrow_string_data_type_get_type()) #define GARROW_STRING_DATA_TYPE(obj) \ @@ -651,15 +667,14 @@ GArrowTime64DataType *garrow_time64_data_type_new (GArrowTimeUnit unit, #define GARROW_TYPE_DECIMAL_DATA_TYPE (garrow_decimal_data_type_get_type()) -/* TODO: Delivered from GArrowFixedSizeBinaryDataType. */ G_DECLARE_DERIVABLE_TYPE(GArrowDecimalDataType, garrow_decimal_data_type, GARROW, DECIMAL_DATA_TYPE, - GArrowDataType) + GArrowFixedSizeBinaryDataType) struct _GArrowDecimalDataTypeClass { - GArrowDataTypeClass parent_class; + GArrowFixedSizeBinaryDataTypeClass parent_class; }; #ifndef GARROW_DISABLE_DEPRECATED diff --git a/c_glib/arrow-glib/type.cpp b/c_glib/arrow-glib/type.cpp index 0642004e2f07..e227ed2c31fc 100644 --- a/c_glib/arrow-glib/type.cpp +++ b/c_glib/arrow-glib/type.cpp @@ -66,6 +66,8 @@ garrow_type_from_raw(arrow::Type::type type) return GARROW_TYPE_STRING; case arrow::Type::type::BINARY: return GARROW_TYPE_BINARY; + case arrow::Type::type::FIXED_SIZE_BINARY: + return GARROW_TYPE_FIXED_SIZE_BINARY; case arrow::Type::type::DATE32: return GARROW_TYPE_DATE32; case arrow::Type::type::DATE64: diff --git a/c_glib/arrow-glib/type.h b/c_glib/arrow-glib/type.h index 2137c785515f..85f55c452be5 100644 --- a/c_glib/arrow-glib/type.h +++ b/c_glib/arrow-glib/type.h @@ -40,6 +40,8 @@ G_BEGIN_DECLS * @GARROW_TYPE_DOUBLE: 8-byte floating point value. * @GARROW_TYPE_STRING: UTF-8 variable-length string. * @GARROW_TYPE_BINARY: Variable-length bytes (no guarantee of UTF-8-ness). + * @GARROW_TYPE_FIXED_SIZE_BINARY: Fixed-size binary. Each value occupies + * the same number of bytes. * @GARROW_TYPE_DATE32: int32 days since the UNIX epoch. * @GARROW_TYPE_DATE64: int64 milliseconds since the UNIX epoch. * @GARROW_TYPE_TIMESTAMP: Exact timestamp encoded with int64 since UNIX epoch. @@ -72,6 +74,7 @@ typedef enum { GARROW_TYPE_DOUBLE, GARROW_TYPE_STRING, GARROW_TYPE_BINARY, + GARROW_TYPE_FIXED_SIZE_BINARY, GARROW_TYPE_DATE32, GARROW_TYPE_DATE64, GARROW_TYPE_TIMESTAMP, diff --git a/c_glib/test/test-fixed-size-binary-data-type.rb b/c_glib/test/test-fixed-size-binary-data-type.rb new file mode 100644 index 000000000000..85946ab8a5b5 --- /dev/null +++ b/c_glib/test/test-fixed-size-binary-data-type.rb @@ -0,0 +1,30 @@ +# 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 TestFixedSizeBinaryDataType < Test::Unit::TestCase + def setup + @data_type = Arrow::FixedSizeBinaryDataType.new(10) + end + + def test_type + assert_equal(Arrow::Type::FIXED_SIZE_BINARY, @data_type.id) + end + + def test_to_s + assert_equal("fixed_size_binary[10]", @data_type.to_s) + end +end From 8dd6781157a4eae7357fba0f3d8a1710e056d7b7 Mon Sep 17 00:00:00 2001 From: Kouhei Sutou Date: Thu, 10 Jan 2019 16:32:53 +0900 Subject: [PATCH 2/3] Fix GArrowFixedSizeBinaryDataType's parent --- c_glib/arrow-glib/basic-data-type.cpp | 4 ++-- c_glib/arrow-glib/basic-data-type.h | 2 +- c_glib/test/test-fixed-size-binary-data-type.rb | 7 ++++++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/c_glib/arrow-glib/basic-data-type.cpp b/c_glib/arrow-glib/basic-data-type.cpp index 70bac0f203a3..c6a7b488b068 100644 --- a/c_glib/arrow-glib/basic-data-type.cpp +++ b/c_glib/arrow-glib/basic-data-type.cpp @@ -241,7 +241,7 @@ garrow_fixed_width_data_type_class_init(GArrowFixedWidthDataTypeClass *klass) } /** - * garrow_fixed_width_data_type_get_id: + * garrow_fixed_width_data_type_get_bit_width: * @data_type: A #GArrowFixedWidthDataType. * * Returns: The number of bits for one data. @@ -720,7 +720,7 @@ garrow_binary_data_type_new(void) G_DEFINE_TYPE(GArrowFixedSizeBinaryDataType, garrow_fixed_size_binary_data_type, - GARROW_TYPE_DATA_TYPE) + GARROW_TYPE_FIXED_WIDTH_DATA_TYPE) static void garrow_fixed_size_binary_data_type_init(GArrowFixedSizeBinaryDataType *object) diff --git a/c_glib/arrow-glib/basic-data-type.h b/c_glib/arrow-glib/basic-data-type.h index b8c0ebe795f1..3aac4a6e372c 100644 --- a/c_glib/arrow-glib/basic-data-type.h +++ b/c_glib/arrow-glib/basic-data-type.h @@ -346,7 +346,7 @@ G_DECLARE_DERIVABLE_TYPE(GArrowFixedSizeBinaryDataType, GArrowDataType) struct _GArrowFixedSizeBinaryDataTypeClass { - GArrowDataTypeClass parent_class; + GArrowFixedWidthDataTypeClass parent_class; }; GARROW_AVAILABLE_IN_0_12 diff --git a/c_glib/test/test-fixed-size-binary-data-type.rb b/c_glib/test/test-fixed-size-binary-data-type.rb index 85946ab8a5b5..6be6718ae5a6 100644 --- a/c_glib/test/test-fixed-size-binary-data-type.rb +++ b/c_glib/test/test-fixed-size-binary-data-type.rb @@ -17,7 +17,8 @@ class TestFixedSizeBinaryDataType < Test::Unit::TestCase def setup - @data_type = Arrow::FixedSizeBinaryDataType.new(10) + @byte_width = 10 + @data_type = Arrow::FixedSizeBinaryDataType.new(@byte_width) end def test_type @@ -27,4 +28,8 @@ def test_type def test_to_s assert_equal("fixed_size_binary[10]", @data_type.to_s) end + + def test_bit_width + assert_equal(@byte_width * 8, @data_type.bit_width) + end end From a19354e9f84fc663c0a8c044ca9e7df91f9377dc Mon Sep 17 00:00:00 2001 From: Kouhei Sutou Date: Thu, 10 Jan 2019 16:33:21 +0900 Subject: [PATCH 3/3] Add garrow_fixed_size_binary_data_type_get_byte_width() --- c_glib/arrow-glib/basic-data-type.cpp | 18 ++++++++++++++++++ c_glib/arrow-glib/basic-data-type.h | 3 +++ .../test/test-fixed-size-binary-data-type.rb | 4 ++++ 3 files changed, 25 insertions(+) diff --git a/c_glib/arrow-glib/basic-data-type.cpp b/c_glib/arrow-glib/basic-data-type.cpp index c6a7b488b068..9f8472ce3190 100644 --- a/c_glib/arrow-glib/basic-data-type.cpp +++ b/c_glib/arrow-glib/basic-data-type.cpp @@ -752,6 +752,24 @@ garrow_fixed_size_binary_data_type_new(gint32 byte_width) return fixed_size_binary_data_type; } +/** + * garrow_fixed_size_binary_data_type_get_byte_width: + * @data_type: A #GArrowFixedSizeBinaryDataType. + * + * Returns: The number of bytes for one data. + * + * Since: 0.12.0 + */ +gint32 +garrow_fixed_size_binary_data_type_get_byte_width(GArrowFixedSizeBinaryDataType *data_type) +{ + const auto arrow_data_type = + garrow_data_type_get_raw(GARROW_DATA_TYPE(data_type)); + const auto arrow_fixed_size_binary_type = + std::static_pointer_cast(arrow_data_type); + return arrow_fixed_size_binary_type->byte_width(); +} + G_DEFINE_TYPE(GArrowStringDataType, garrow_string_data_type, diff --git a/c_glib/arrow-glib/basic-data-type.h b/c_glib/arrow-glib/basic-data-type.h index 3aac4a6e372c..d18958265748 100644 --- a/c_glib/arrow-glib/basic-data-type.h +++ b/c_glib/arrow-glib/basic-data-type.h @@ -352,6 +352,9 @@ struct _GArrowFixedSizeBinaryDataTypeClass GARROW_AVAILABLE_IN_0_12 GArrowFixedSizeBinaryDataType * garrow_fixed_size_binary_data_type_new(gint32 byte_width); +GARROW_AVAILABLE_IN_0_12 +gint32 +garrow_fixed_size_binary_data_type_get_byte_width(GArrowFixedSizeBinaryDataType *data_type); #define GARROW_TYPE_STRING_DATA_TYPE \ diff --git a/c_glib/test/test-fixed-size-binary-data-type.rb b/c_glib/test/test-fixed-size-binary-data-type.rb index 6be6718ae5a6..584fb3deec93 100644 --- a/c_glib/test/test-fixed-size-binary-data-type.rb +++ b/c_glib/test/test-fixed-size-binary-data-type.rb @@ -29,6 +29,10 @@ def test_to_s assert_equal("fixed_size_binary[10]", @data_type.to_s) end + def test_byte_width + assert_equal(@byte_width, @data_type.byte_width) + end + def test_bit_width assert_equal(@byte_width * 8, @data_type.bit_width) end