This is an automated email from the ASF dual-hosted git repository. kou pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/main by this push: new 4c36f12524 GH-44569: [GLib] Add GArrowDecimal64DataType (#44571) 4c36f12524 is described below commit 4c36f12524752779e682959c7a000a6554fcb784 Author: Hiroyuki Sato <hiroys...@gmail.com> AuthorDate: Wed Oct 30 13:44:58 2024 +0900 GH-44569: [GLib] Add GArrowDecimal64DataType (#44571) ### Rationale for this change `arrow::Decimal64Type` data type has been introduced. It is also necessary to support the same data type in GLib. ### What changes are included in this PR? This PR implements `GArrowDecimal64DataType`. ### Are these changes tested? YES ### Are there any user-facing changes? Before this change: `Arrow::DecimalDataType.new(8, 2)` returned `Decimal128DataType`. After this change: `Arrow::DecimalDataType.new(8, 2)` returns `Decimal64DataType` * GitHub Issue: #44569 Authored-by: Hiroyuki Sato <hiroys...@gmail.com> Signed-off-by: Sutou Kouhei <k...@clear-code.com> --- c_glib/arrow-glib/basic-data-type.cpp | 61 +++++++++++++++++++++- c_glib/arrow-glib/basic-data-type.h | 20 +++++++ c_glib/arrow-glib/type.cpp | 2 + c_glib/arrow-glib/type.h | 4 ++ c_glib/test/test-decimal128-data-type.rb | 4 +- ...28-data-type.rb => test-decimal64-data-type.rb} | 24 ++++----- 6 files changed, 100 insertions(+), 15 deletions(-) diff --git a/c_glib/arrow-glib/basic-data-type.cpp b/c_glib/arrow-glib/basic-data-type.cpp index dff9725150..b2bd99e0f2 100644 --- a/c_glib/arrow-glib/basic-data-type.cpp +++ b/c_glib/arrow-glib/basic-data-type.cpp @@ -114,6 +114,8 @@ G_BEGIN_DECLS * * #GArrowDecimalDataType is a base class for the decimal data types. * + * #GArrowDecimal64DataType is a class for the 64-bit decimal data type. + * * #GArrowDecimal128DataType is a class for the 128-bit decimal data type. * * #GArrowDecimal256DataType is a class for the 256-bit decimal data type. @@ -1500,7 +1502,10 @@ garrow_decimal_data_type_class_init(GArrowDecimalDataTypeClass *klass) GArrowDecimalDataType * garrow_decimal_data_type_new(gint32 precision, gint32 scale, GError **error) { - if (precision <= garrow_decimal128_data_type_max_precision()) { + if (precision <= garrow_decimal64_data_type_max_precision()) { + return GARROW_DECIMAL_DATA_TYPE( + garrow_decimal64_data_type_new(precision, scale, error)); + } else if (precision <= garrow_decimal128_data_type_max_precision()) { return GARROW_DECIMAL_DATA_TYPE( garrow_decimal128_data_type_new(precision, scale, error)); } else { @@ -1545,6 +1550,57 @@ garrow_decimal_data_type_get_scale(GArrowDecimalDataType *decimal_data_type) return arrow_decimal_type->scale(); } +G_DEFINE_TYPE(GArrowDecimal64DataType, + garrow_decimal64_data_type, + GARROW_TYPE_DECIMAL_DATA_TYPE) + +static void +garrow_decimal64_data_type_init(GArrowDecimal64DataType *object) +{ +} + +static void +garrow_decimal64_data_type_class_init(GArrowDecimal64DataTypeClass *klass) +{ +} + +/** + * garrow_decimal64_data_type_max_precision: + * + * Returns: The max precision of 64-bit decimal data type. + * + * Since: 19.0.0 + */ +gint32 +garrow_decimal64_data_type_max_precision() +{ + return arrow::Decimal64Type::kMaxPrecision; +} + +/** + * garrow_decimal64_data_type_new: + * @precision: The precision of decimal data. + * @scale: The scale of decimal data. + * @error: (nullable): Return location for a #GError or %NULL. + * + * Returns: (nullable): + * The newly created 64-bit decimal data type on success, %NULL on error. + * + * Since: 19.0.0 + */ +GArrowDecimal64DataType * +garrow_decimal64_data_type_new(gint32 precision, gint32 scale, GError **error) +{ + auto arrow_data_type_result = arrow::Decimal64Type::Make(precision, scale); + if (garrow::check(error, arrow_data_type_result, "[decimal64-data-type][new]")) { + auto arrow_data_type = *arrow_data_type_result; + return GARROW_DECIMAL64_DATA_TYPE( + g_object_new(GARROW_TYPE_DECIMAL64_DATA_TYPE, "data-type", &arrow_data_type, NULL)); + } else { + return NULL; + } +} + G_DEFINE_TYPE(GArrowDecimal128DataType, garrow_decimal128_data_type, GARROW_TYPE_DECIMAL_DATA_TYPE) @@ -2193,6 +2249,9 @@ garrow_data_type_new_raw(std::shared_ptr<arrow::DataType> *arrow_data_type) case arrow::Type::type::MAP: type = GARROW_TYPE_MAP_DATA_TYPE; break; + case arrow::Type::type::DECIMAL64: + type = GARROW_TYPE_DECIMAL64_DATA_TYPE; + break; case arrow::Type::type::DECIMAL128: type = GARROW_TYPE_DECIMAL128_DATA_TYPE; break; diff --git a/c_glib/arrow-glib/basic-data-type.h b/c_glib/arrow-glib/basic-data-type.h index 77180018c9..1a677a6e45 100644 --- a/c_glib/arrow-glib/basic-data-type.h +++ b/c_glib/arrow-glib/basic-data-type.h @@ -608,6 +608,26 @@ GARROW_AVAILABLE_IN_ALL gint32 garrow_decimal_data_type_get_scale(GArrowDecimalDataType *decimal_data_type); +#define GARROW_TYPE_DECIMAL64_DATA_TYPE (garrow_decimal64_data_type_get_type()) +GARROW_AVAILABLE_IN_19_0 +G_DECLARE_DERIVABLE_TYPE(GArrowDecimal64DataType, + garrow_decimal64_data_type, + GARROW, + DECIMAL64_DATA_TYPE, + GArrowDecimalDataType) +struct _GArrowDecimal64DataTypeClass +{ + GArrowDecimalDataTypeClass parent_class; +}; + +GARROW_AVAILABLE_IN_19_0 +gint32 +garrow_decimal64_data_type_max_precision(); + +GARROW_AVAILABLE_IN_19_0 +GArrowDecimal64DataType * +garrow_decimal64_data_type_new(gint32 precision, gint32 scale, GError **error); + #define GARROW_TYPE_DECIMAL128_DATA_TYPE (garrow_decimal128_data_type_get_type()) GARROW_AVAILABLE_IN_ALL G_DECLARE_DERIVABLE_TYPE(GArrowDecimal128DataType, diff --git a/c_glib/arrow-glib/type.cpp b/c_glib/arrow-glib/type.cpp index d9fc36b886..c4c0748357 100644 --- a/c_glib/arrow-glib/type.cpp +++ b/c_glib/arrow-glib/type.cpp @@ -78,6 +78,8 @@ garrow_type_from_raw(arrow::Type::type type) return GARROW_TYPE_MONTH_INTERVAL; case arrow::Type::type::INTERVAL_DAY_TIME: return GARROW_TYPE_DAY_TIME_INTERVAL; + case arrow::Type::type::DECIMAL64: + return GARROW_TYPE_DECIMAL64; case arrow::Type::type::DECIMAL128: return GARROW_TYPE_DECIMAL128; case arrow::Type::type::DECIMAL256: diff --git a/c_glib/arrow-glib/type.h b/c_glib/arrow-glib/type.h index 6f33ad64ef..3c7c2d5aa4 100644 --- a/c_glib/arrow-glib/type.h +++ b/c_glib/arrow-glib/type.h @@ -70,6 +70,8 @@ G_BEGIN_DECLS * @GARROW_TYPE_LARGE_LIST: A list of some logical data type with 64-bit offsets. * @GARROW_TYPE_MONTH_DAY_NANO_INTERVAL: MONTH_DAY_NANO interval in SQL style. * @GARROW_TYPE_RUN_END_ENCODED: Run-end encoded data. + * @GARROW_TYPE_DECIMAL64: Precision- and scale-based decimal + * type with 64-bit. Storage type depends on the parameters. * * They are corresponding to `arrow::Type::type` values. */ @@ -113,6 +115,8 @@ typedef enum { GARROW_TYPE_LARGE_LIST, GARROW_TYPE_MONTH_DAY_NANO_INTERVAL, GARROW_TYPE_RUN_END_ENCODED, + /* TODO: Remove = 44 when we add STRING_VIEW..DECIMAL32. */ + GARROW_TYPE_DECIMAL64 = 44, } GArrowType; /** diff --git a/c_glib/test/test-decimal128-data-type.rb b/c_glib/test/test-decimal128-data-type.rb index 8cf97e38d4..74f39f6ef3 100644 --- a/c_glib/test/test-decimal128-data-type.rb +++ b/c_glib/test/test-decimal128-data-type.rb @@ -42,8 +42,8 @@ class TestDecimal128DataType < Test::Unit::TestCase end def test_decimal_data_type_new - assert_equal(Arrow::Decimal128DataType.new(8, 2), - Arrow::DecimalDataType.new(8, 2)) + assert_equal(Arrow::Decimal128DataType.new(19, 2), + Arrow::DecimalDataType.new(19, 2)) end def test_invalid_precision diff --git a/c_glib/test/test-decimal128-data-type.rb b/c_glib/test/test-decimal64-data-type.rb similarity index 64% copy from c_glib/test/test-decimal128-data-type.rb copy to c_glib/test/test-decimal64-data-type.rb index 8cf97e38d4..5a0c5d0840 100644 --- a/c_glib/test/test-decimal128-data-type.rb +++ b/c_glib/test/test-decimal64-data-type.rb @@ -15,40 +15,40 @@ # specific language governing permissions and limitations # under the License. -class TestDecimal128DataType < Test::Unit::TestCase +class TestDecimal64DataType < Test::Unit::TestCase def test_type - data_type = Arrow::Decimal128DataType.new(2, 0) - assert_equal(Arrow::Type::DECIMAL128, data_type.id) + data_type = Arrow::Decimal64DataType.new(2, 0) + assert_equal(Arrow::Type::DECIMAL64, data_type.id) end def test_name - data_type = Arrow::Decimal128DataType.new(2, 0) - assert_equal("decimal128", data_type.name) + data_type = Arrow::Decimal64DataType.new(2, 0) + assert_equal("decimal64", data_type.name) end def test_to_s - data_type = Arrow::Decimal128DataType.new(2, 0) - assert_equal("decimal128(2, 0)", data_type.to_s) + data_type = Arrow::Decimal64DataType.new(2, 0) + assert_equal("decimal64(2, 0)", data_type.to_s) end def test_precision - data_type = Arrow::Decimal128DataType.new(8, 2) + data_type = Arrow::Decimal64DataType.new(8, 2) assert_equal(8, data_type.precision) end def test_scale - data_type = Arrow::Decimal128DataType.new(8, 2) + data_type = Arrow::Decimal64DataType.new(8, 2) assert_equal(2, data_type.scale) end def test_decimal_data_type_new - assert_equal(Arrow::Decimal128DataType.new(8, 2), - Arrow::DecimalDataType.new(8, 2)) + assert_equal(Arrow::Decimal64DataType.new(18, 2), + Arrow::DecimalDataType.new(18, 2)) end def test_invalid_precision assert_raise(Arrow::Error::Invalid) do - Arrow::Decimal128DataType.new(39, 1) + Arrow::Decimal64DataType.new(19, 1) end end end