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 a08037f33f GH-44618: [GLib] Add GArrowDecimal64Scalar (#44620)
a08037f33f is described below

commit a08037f33f2fe00763032623e18ba049d19a024f
Author: Hiroyuki Sato <[email protected]>
AuthorDate: Sun Nov 3 11:01:49 2024 +0900

    GH-44618: [GLib] Add GArrowDecimal64Scalar (#44620)
    
    
    
    ### Rationale for this change
    
    The `arrow::Decimal64Scalar` has been released.
    GLib needs to implement `GArrowDecimal64Scalar`.
    
    ### What changes are included in this PR?
    
    Implement `GArrowDecimal64Scalar`.
    
    ### Are these changes tested?
    
    YES
    
    ### Are there any user-facing changes?
    
    NO
    
    * GitHub Issue: #44618
    
    Authored-by: Hiroyuki Sato <[email protected]>
    Signed-off-by: Sutou Kouhei <[email protected]>
---
 c_glib/arrow-glib/scalar.cpp         | 126 +++++++++++++++++++++++++++++++++++
 c_glib/arrow-glib/scalar.h           |  16 +++++
 c_glib/test/test-decimal64-scalar.rb |  48 +++++++++++++
 3 files changed, 190 insertions(+)

diff --git a/c_glib/arrow-glib/scalar.cpp b/c_glib/arrow-glib/scalar.cpp
index f965b49703..57085a00c4 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.
  *
+ * #GArrowDecimal64Scalar is a class for a 64-bit decimal scalar.
+ *
  * #GArrowDecimal128Scalar is a class for a 128-bit decimal scalar.
  *
  * #GArrowDecimal256Scalar is a class for a 256-bit decimal scalar.
@@ -1631,6 +1633,127 @@ 
garrow_month_day_nano_interval_scalar_get_value(GArrowMonthDayNanoIntervalScalar
   return priv->value;
 }
 
+typedef struct GArrowDecimal64ScalarPrivate_
+{
+  GArrowDecimal64 *value;
+} GArrowDecimal64ScalarPrivate;
+
+G_DEFINE_TYPE_WITH_PRIVATE(GArrowDecimal64Scalar,
+                           garrow_decimal64_scalar,
+                           GARROW_TYPE_SCALAR)
+
+#define GARROW_DECIMAL64_SCALAR_GET_PRIVATE(obj)                               
          \
+  static_cast<GArrowDecimal64ScalarPrivate *>(                                 
          \
+    garrow_decimal64_scalar_get_instance_private(GARROW_DECIMAL64_SCALAR(obj)))
+
+static void
+garrow_decimal64_scalar_dispose(GObject *object)
+{
+  auto priv = GARROW_DECIMAL64_SCALAR_GET_PRIVATE(object);
+
+  if (priv->value) {
+    g_object_unref(priv->value);
+    priv->value = NULL;
+  }
+
+  G_OBJECT_CLASS(garrow_decimal64_scalar_parent_class)->dispose(object);
+}
+
+static void
+garrow_decimal64_scalar_set_property(GObject *object,
+                                     guint prop_id,
+                                     const GValue *value,
+                                     GParamSpec *pspec)
+{
+  auto priv = GARROW_DECIMAL64_SCALAR_GET_PRIVATE(object);
+
+  switch (prop_id) {
+  case PROP_VALUE:
+    priv->value = GARROW_DECIMAL64(g_value_dup_object(value));
+    break;
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+    break;
+  }
+}
+
+static void
+garrow_decimal64_scalar_init(GArrowDecimal64Scalar *object)
+{
+}
+
+static void
+garrow_decimal64_scalar_class_init(GArrowDecimal64ScalarClass *klass)
+{
+  auto gobject_class = G_OBJECT_CLASS(klass);
+
+  gobject_class->dispose = garrow_decimal64_scalar_dispose;
+  gobject_class->set_property = garrow_decimal64_scalar_set_property;
+
+  GParamSpec *spec;
+  /**
+   * GArrowDecimal64Scalar:value:
+   *
+   * The value of the scalar.
+   *
+   * Since: 19.0.0
+   */
+  spec = g_param_spec_object(
+    "value",
+    "Value",
+    "The value of the scalar",
+    garrow_decimal64_get_type(),
+    static_cast<GParamFlags>(G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
+  g_object_class_install_property(gobject_class, PROP_VALUE, spec);
+}
+
+/**
+ * garrow_decimal64_scalar_new:
+ * @data_type: A #GArrowDecimal64DataType for this scalar.
+ * @value: The value of this scalar.
+ *
+ * Returns: A newly created #GArrowDecimal64Scalar.
+ *
+ * Since: 19.0.0
+ */
+GArrowDecimal64Scalar *
+garrow_decimal64_scalar_new(GArrowDecimal64DataType *data_type, 
GArrowDecimal64 *value)
+{
+  auto arrow_data_type = garrow_data_type_get_raw(GARROW_DATA_TYPE(data_type));
+  auto arrow_value = garrow_decimal64_get_raw(value);
+  auto arrow_scalar = std::static_pointer_cast<arrow::Scalar>(
+    std::make_shared<arrow::Decimal64Scalar>(*arrow_value, arrow_data_type));
+  return GARROW_DECIMAL64_SCALAR(garrow_scalar_new_raw(&arrow_scalar,
+                                                       "scalar",
+                                                       &arrow_scalar,
+                                                       "data-type",
+                                                       data_type,
+                                                       "value",
+                                                       value,
+                                                       NULL));
+}
+
+/**
+ * garrow_decimal64_scalar_get_value:
+ * @scalar: A #GArrowDecimal64Scalar.
+ *
+ * Returns: (transfer none): The value of this scalar.
+ *
+ * Since: 19.0.0
+ */
+GArrowDecimal64 *
+garrow_decimal64_scalar_get_value(GArrowDecimal64Scalar *scalar)
+{
+  auto priv = GARROW_DECIMAL64_SCALAR_GET_PRIVATE(scalar);
+  if (!priv->value) {
+    auto arrow_scalar = std::static_pointer_cast<arrow::Decimal64Scalar>(
+      garrow_scalar_get_raw(GARROW_SCALAR(scalar)));
+    auto arrow_value = std::make_shared<arrow::Decimal64>(arrow_scalar->value);
+    priv->value = garrow_decimal64_new_raw(&arrow_value);
+  }
+  return priv->value;
+}
+
 typedef struct GArrowDecimal128ScalarPrivate_
 {
   GArrowDecimal128 *value;
@@ -2508,6 +2631,9 @@ 
garrow_scalar_new_raw_valist(std::shared_ptr<arrow::Scalar> *arrow_scalar,
   case arrow::Type::type::INTERVAL_MONTH_DAY_NANO:
     type = GARROW_TYPE_MONTH_DAY_NANO_INTERVAL_SCALAR;
     break;
+  case arrow::Type::type::DECIMAL64:
+    type = GARROW_TYPE_DECIMAL64_SCALAR;
+    break;
   case arrow::Type::type::DECIMAL128:
     type = GARROW_TYPE_DECIMAL128_SCALAR;
     break;
diff --git a/c_glib/arrow-glib/scalar.h b/c_glib/arrow-glib/scalar.h
index 5f9015d29c..c9de9958ad 100644
--- a/c_glib/arrow-glib/scalar.h
+++ b/c_glib/arrow-glib/scalar.h
@@ -501,6 +501,22 @@ GARROW_AVAILABLE_IN_8_0
 GArrowMonthDayNano *
 
garrow_month_day_nano_interval_scalar_get_value(GArrowMonthDayNanoIntervalScalar
 *scalar);
 
+#define GARROW_TYPE_DECIMAL64_SCALAR (garrow_decimal64_scalar_get_type())
+GARROW_AVAILABLE_IN_19_0
+G_DECLARE_DERIVABLE_TYPE(
+  GArrowDecimal64Scalar, garrow_decimal64_scalar, GARROW, DECIMAL64_SCALAR, 
GArrowScalar)
+struct _GArrowDecimal64ScalarClass
+{
+  GArrowScalarClass parent_class;
+};
+
+GARROW_AVAILABLE_IN_19_0
+GArrowDecimal64Scalar *
+garrow_decimal64_scalar_new(GArrowDecimal64DataType *data_type, 
GArrowDecimal64 *value);
+GARROW_AVAILABLE_IN_19_0
+GArrowDecimal64 *
+garrow_decimal64_scalar_get_value(GArrowDecimal64Scalar *scalar);
+
 #define GARROW_TYPE_DECIMAL128_SCALAR (garrow_decimal128_scalar_get_type())
 GARROW_AVAILABLE_IN_5_0
 G_DECLARE_DERIVABLE_TYPE(GArrowDecimal128Scalar,
diff --git a/c_glib/test/test-decimal64-scalar.rb 
b/c_glib/test/test-decimal64-scalar.rb
new file mode 100644
index 0000000000..fb6a308b6d
--- /dev/null
+++ b/c_glib/test/test-decimal64-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 TestDecimal64Scalar < Test::Unit::TestCase
+  def setup
+    @data_type = Arrow::Decimal64DataType.new(8, 2)
+    @value = Arrow::Decimal64.new("23423445")
+    @scalar = Arrow::Decimal64Scalar.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::Decimal64Scalar.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

Reply via email to