Skip to content

Commit

Permalink
GH-44619: [GLib] Add GArrowDecimal32Scalar (#44628)
Browse files Browse the repository at this point in the history
### Rationale for this change

The `arrow::Decimal32Scalar` has been released.
GLib needs to implement `GArrowDecimal32Scalar`.

### What changes are included in this PR?

Implement `GArrowDecimal32Scalar`.

### Are these changes tested?

YES

### Are there any user-facing changes?

NO

* GitHub Issue: #44619

Lead-authored-by: Hiroyuki Sato <[email protected]>
Co-authored-by: Sutou Kouhei <[email protected]>
Signed-off-by: Sutou Kouhei <[email protected]>
  • Loading branch information
hiroyuki-sato and kou authored Nov 4, 2024
1 parent 00e7c65 commit b6e8772
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 0 deletions.
126 changes: 126 additions & 0 deletions c_glib/arrow-glib/scalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<GArrowDecimal32ScalarPrivate *>( \
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<GParamFlags>(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<arrow::Scalar>(
std::make_shared<arrow::Decimal32Scalar>(*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<arrow::Decimal32Scalar>(
garrow_scalar_get_raw(GARROW_SCALAR(scalar)));
auto arrow_value = std::make_shared<arrow::Decimal32>(arrow_scalar->value);
priv->value = garrow_decimal32_new_raw(&arrow_value);
}
return priv->value;
}

typedef struct GArrowDecimal64ScalarPrivate_
{
GArrowDecimal64 *value;
Expand Down Expand Up @@ -2631,6 +2754,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::DECIMAL32:
type = GARROW_TYPE_DECIMAL32_SCALAR;
break;
case arrow::Type::type::DECIMAL64:
type = GARROW_TYPE_DECIMAL64_SCALAR;
break;
Expand Down
16 changes: 16 additions & 0 deletions c_glib/arrow-glib/scalar.h
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,22 @@ 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_19_0
G_DECLARE_DERIVABLE_TYPE(
GArrowDecimal32Scalar, garrow_decimal32_scalar, GARROW, DECIMAL32_SCALAR, GArrowScalar)
struct _GArrowDecimal32ScalarClass
{
GArrowScalarClass parent_class;
};

GARROW_AVAILABLE_IN_19_0
GArrowDecimal32Scalar *
garrow_decimal32_scalar_new(GArrowDecimal32DataType *data_type, GArrowDecimal32 *value);
GARROW_AVAILABLE_IN_19_0
GArrowDecimal32 *
garrow_decimal32_scalar_get_value(GArrowDecimal32Scalar *scalar);

#define GARROW_TYPE_DECIMAL64_SCALAR (garrow_decimal64_scalar_get_type())
GARROW_AVAILABLE_IN_19_0
G_DECLARE_DERIVABLE_TYPE(
Expand Down
48 changes: 48 additions & 0 deletions c_glib/test/test-decimal32-scalar.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b6e8772

Please sign in to comment.