From d43b2d08c2255539eb8bf8ae5a14021c29b90928 Mon Sep 17 00:00:00 2001 From: Hiroyuki Sato Date: Sun, 3 Nov 2024 16:50:11 +0900 Subject: [PATCH] GH-44619: [GLib] Add GArrowDecimal32Scalar --- c_glib/arrow-glib/scalar.cpp | 126 +++++++++++++++++++++++++++ c_glib/arrow-glib/scalar.h | 22 ++++- c_glib/test/test-decimal32-scalar.rb | 48 ++++++++++ 3 files changed, 193 insertions(+), 3 deletions(-) create mode 100644 c_glib/test/test-decimal32-scalar.rb diff --git a/c_glib/arrow-glib/scalar.cpp b/c_glib/arrow-glib/scalar.cpp index 57085a00c4b10..f2093e3e41ae2 100644 --- a/c_glib/arrow-glib/scalar.cpp +++ b/c_glib/arrow-glib/scalar.cpp @@ -104,6 +104,8 @@ G_BEGIN_DECLS * #GArrowMonthDayNanoIntervalScalar is a class for the month day nano * intarval scalar. * + * #GArrowDecimal32Scalar is a class for a 32-bit decimal scalar. + * * #GArrowDecimal64Scalar is a class for a 64-bit decimal scalar. * * #GArrowDecimal128Scalar is a class for a 128-bit decimal scalar. @@ -1633,6 +1635,127 @@ garrow_month_day_nano_interval_scalar_get_value(GArrowMonthDayNanoIntervalScalar return priv->value; } +typedef struct GArrowDecimal32ScalarPrivate_ +{ + GArrowDecimal32 *value; +} GArrowDecimal32ScalarPrivate; + +G_DEFINE_TYPE_WITH_PRIVATE(GArrowDecimal32Scalar, + garrow_decimal32_scalar, + GARROW_TYPE_SCALAR) + +#define GARROW_DECIMAL32_SCALAR_GET_PRIVATE(obj) \ + static_cast( \ + garrow_decimal32_scalar_get_instance_private(GARROW_DECIMAL32_SCALAR(obj))) + +static void +garrow_decimal32_scalar_dispose(GObject *object) +{ + auto priv = GARROW_DECIMAL32_SCALAR_GET_PRIVATE(object); + + if (priv->value) { + g_object_unref(priv->value); + priv->value = NULL; + } + + G_OBJECT_CLASS(garrow_decimal32_scalar_parent_class)->dispose(object); +} + +static void +garrow_decimal32_scalar_set_property(GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + auto priv = GARROW_DECIMAL32_SCALAR_GET_PRIVATE(object); + + switch (prop_id) { + case PROP_VALUE: + priv->value = GARROW_DECIMAL32(g_value_dup_object(value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + +static void +garrow_decimal32_scalar_init(GArrowDecimal32Scalar *object) +{ +} + +static void +garrow_decimal32_scalar_class_init(GArrowDecimal32ScalarClass *klass) +{ + auto gobject_class = G_OBJECT_CLASS(klass); + + gobject_class->dispose = garrow_decimal32_scalar_dispose; + gobject_class->set_property = garrow_decimal32_scalar_set_property; + + GParamSpec *spec; + /** + * GArrowDecimal32Scalar:value: + * + * The value of the scalar. + * + * Since: 19.0.0 + */ + spec = g_param_spec_object( + "value", + "Value", + "The value of the scalar", + garrow_decimal32_get_type(), + static_cast(G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); + g_object_class_install_property(gobject_class, PROP_VALUE, spec); +} + +/** + * garrow_decimal32_scalar_new: + * @data_type: A #GArrowDecimal32DataType for this scalar. + * @value: The value of this scalar. + * + * Returns: A newly created #GArrowDecimal32Scalar. + * + * Since: 19.0.0 + */ +GArrowDecimal32Scalar * +garrow_decimal32_scalar_new(GArrowDecimal32DataType *data_type, GArrowDecimal32 *value) +{ + auto arrow_data_type = garrow_data_type_get_raw(GARROW_DATA_TYPE(data_type)); + auto arrow_value = garrow_decimal32_get_raw(value); + auto arrow_scalar = std::static_pointer_cast( + std::make_shared(*arrow_value, arrow_data_type)); + return GARROW_DECIMAL32_SCALAR(garrow_scalar_new_raw(&arrow_scalar, + "scalar", + &arrow_scalar, + "data-type", + data_type, + "value", + value, + NULL)); +} + +/** + * garrow_decimal32_scalar_get_value: + * @scalar: A #GArrowDecimal32Scalar. + * + * Returns: (transfer none): The value of this scalar. + * + * Since: 19.0.0 + */ +GArrowDecimal32 * +garrow_decimal32_scalar_get_value(GArrowDecimal32Scalar *scalar) +{ + auto priv = GARROW_DECIMAL32_SCALAR_GET_PRIVATE(scalar); + if (!priv->value) { + auto arrow_scalar = std::static_pointer_cast( + garrow_scalar_get_raw(GARROW_SCALAR(scalar))); + auto arrow_value = std::make_shared(arrow_scalar->value); + priv->value = garrow_decimal32_new_raw(&arrow_value); + } + return priv->value; +} + typedef struct GArrowDecimal64ScalarPrivate_ { GArrowDecimal64 *value; @@ -2631,6 +2754,9 @@ garrow_scalar_new_raw_valist(std::shared_ptr *arrow_scalar, case arrow::Type::type::INTERVAL_MONTH_DAY_NANO: type = GARROW_TYPE_MONTH_DAY_NANO_INTERVAL_SCALAR; break; + case arrow::Type::type::DECIMAL32: + type = GARROW_TYPE_DECIMAL32_SCALAR; + break; case arrow::Type::type::DECIMAL64: type = GARROW_TYPE_DECIMAL64_SCALAR; break; diff --git a/c_glib/arrow-glib/scalar.h b/c_glib/arrow-glib/scalar.h index c9de9958ad402..495a695ae8c8c 100644 --- a/c_glib/arrow-glib/scalar.h +++ b/c_glib/arrow-glib/scalar.h @@ -501,8 +501,24 @@ GARROW_AVAILABLE_IN_8_0 GArrowMonthDayNano * garrow_month_day_nano_interval_scalar_get_value(GArrowMonthDayNanoIntervalScalar *scalar); +#define GARROW_TYPE_DECIMAL32_SCALAR (garrow_decimal32_scalar_get_type()) +GARROW_AVAILABLE_IN_5_0 +G_DECLARE_DERIVABLE_TYPE( + GArrowDecimal32Scalar, garrow_decimal32_scalar, GARROW, DECIMAL32_SCALAR, GArrowScalar) +struct _GArrowDecimal32ScalarClass +{ + GArrowScalarClass parent_class; +}; + +GARROW_AVAILABLE_IN_5_0 +GArrowDecimal32Scalar * +garrow_decimal32_scalar_new(GArrowDecimal32DataType *data_type, GArrowDecimal32 *value); +GARROW_AVAILABLE_IN_5_0 +GArrowDecimal32 * +garrow_decimal32_scalar_get_value(GArrowDecimal32Scalar *scalar); + #define GARROW_TYPE_DECIMAL64_SCALAR (garrow_decimal64_scalar_get_type()) -GARROW_AVAILABLE_IN_19_0 +GARROW_AVAILABLE_IN_5_0 G_DECLARE_DERIVABLE_TYPE( GArrowDecimal64Scalar, garrow_decimal64_scalar, GARROW, DECIMAL64_SCALAR, GArrowScalar) struct _GArrowDecimal64ScalarClass @@ -510,10 +526,10 @@ struct _GArrowDecimal64ScalarClass GArrowScalarClass parent_class; }; -GARROW_AVAILABLE_IN_19_0 +GARROW_AVAILABLE_IN_5_0 GArrowDecimal64Scalar * garrow_decimal64_scalar_new(GArrowDecimal64DataType *data_type, GArrowDecimal64 *value); -GARROW_AVAILABLE_IN_19_0 +GARROW_AVAILABLE_IN_5_0 GArrowDecimal64 * garrow_decimal64_scalar_get_value(GArrowDecimal64Scalar *scalar); diff --git a/c_glib/test/test-decimal32-scalar.rb b/c_glib/test/test-decimal32-scalar.rb new file mode 100644 index 0000000000000..cb54a6bb6a1e5 --- /dev/null +++ b/c_glib/test/test-decimal32-scalar.rb @@ -0,0 +1,48 @@ +# 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 TestDecimal32Scalar < Test::Unit::TestCase + def setup + @data_type = Arrow::Decimal32DataType.new(8, 2) + @value = Arrow::Decimal32.new("23423445") + @scalar = Arrow::Decimal32Scalar.new(@data_type, @value) + end + + def test_data_type + assert_equal(@data_type, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::Decimal32Scalar.new(@data_type, @value), + @scalar) + end + + def test_to_s + assert_equal("234234.45", @scalar.to_s) + end + + def test_value + assert_equal(@value, @scalar.value) + end +end