Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-44569: [GLib] Add GArrowDecimal64DataType #44571

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion c_glib/arrow-glib/basic-data-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions c_glib/arrow-glib/basic-data-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions c_glib/arrow-glib/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions c_glib/arrow-glib/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ G_BEGIN_DECLS
* @GARROW_TYPE_TIME64: Exact time encoded with int64, supporting micro- or nanoseconds
* @GARROW_TYPE_MONTH_INTERVAL: YEAR_MONTH interval in SQL style.
* @GARROW_TYPE_DAY_TIME_INTERVAL: DAY_TIME interval in SQL style.
* @GARROW_TYPE_DECIMAL64: Precision- and scale-based decimal
* type with 64-bit. Storage type depends on the parameters.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use the same order as the definition? (Could you move this to after the @GARROW_TYPE_RUN_END_ENCODED?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the order in 8217b31

* @GARROW_TYPE_DECIMAL128: Precision- and scale-based decimal
* type with 128-bit. Storage type depends on the parameters.
* @GARROW_TYPE_DECIMAL256: Precision- and scale-based decimal
Expand Down Expand Up @@ -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;

/**
Expand Down
4 changes: 2 additions & 2 deletions c_glib/test/test-decimal128-data-type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def test_scale
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
Expand Down
54 changes: 54 additions & 0 deletions c_glib/test/test-decimal64-data-type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 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 TestDecimal64DataType < Test::Unit::TestCase
def test_type
data_type = Arrow::Decimal64DataType.new(2, 0)
assert_equal(Arrow::Type::DECIMAL64, data_type.id)
end

def test_name
data_type = Arrow::Decimal64DataType.new(2, 0)
assert_equal("decimal64", data_type.name)
end

def test_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::Decimal64DataType.new(8, 2)
assert_equal(8, data_type.precision)
end

def test_scale
data_type = Arrow::Decimal64DataType.new(8, 2)
assert_equal(2, data_type.scale)
end

def test_decimal_data_type_new
assert_equal(Arrow::Decimal64DataType.new(18, 2),
Arrow::DecimalDataType.new(18, 2))
end

def test_invalid_precision
assert_raise(Arrow::Error::Invalid) do
Arrow::Decimal64DataType.new(19, 1)
end
end
end
Loading