Skip to content

Commit 8b27bc0

Browse files
authored
GH-48512: [GLib][Ruby] Add ZeroFillOptions (#48533)
### Rationale for this change The `ZeroFillOptions` class is not available in GLib/Ruby, and it is used together with the `utf8_zero_fill` compute function. ### What changes are included in this PR? This adds the `ZeroFillOptions` class to GLib. ### Are these changes tested? Yes, with Ruby unit tests. ### Are there any user-facing changes? Yes, a new class. * GitHub Issue: #48512 Authored-by: Sten Larsson <[email protected]> Signed-off-by: Sutou Kouhei <[email protected]>
1 parent 553cae3 commit 8b27bc0

File tree

4 files changed

+210
-0
lines changed

4 files changed

+210
-0
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,9 @@ G_BEGIN_DECLS
336336
*
337337
* #GArrowWinsorizeOptions is a class to customize the `winsorize` function.
338338
*
339+
* #GArrowZeroFillOptions is a class to customize the `utf8_zero_fill`
340+
* function.
341+
*
339342
* There are many functions to compute data on an array.
340343
*/
341344

@@ -10364,6 +10367,121 @@ garrow_winsorize_options_new(void)
1036410367
return GARROW_WINSORIZE_OPTIONS(g_object_new(GARROW_TYPE_WINSORIZE_OPTIONS, nullptr));
1036510368
}
1036610369

10370+
enum {
10371+
PROP_ZERO_FILL_OPTIONS_WIDTH = 1,
10372+
PROP_ZERO_FILL_OPTIONS_PADDING,
10373+
};
10374+
10375+
G_DEFINE_TYPE(GArrowZeroFillOptions,
10376+
garrow_zero_fill_options,
10377+
GARROW_TYPE_FUNCTION_OPTIONS)
10378+
10379+
static void
10380+
garrow_zero_fill_options_set_property(GObject *object,
10381+
guint prop_id,
10382+
const GValue *value,
10383+
GParamSpec *pspec)
10384+
{
10385+
auto options = garrow_zero_fill_options_get_raw(GARROW_ZERO_FILL_OPTIONS(object));
10386+
10387+
switch (prop_id) {
10388+
case PROP_ZERO_FILL_OPTIONS_WIDTH:
10389+
options->width = g_value_get_int64(value);
10390+
break;
10391+
case PROP_ZERO_FILL_OPTIONS_PADDING:
10392+
options->padding = g_value_get_string(value);
10393+
break;
10394+
default:
10395+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
10396+
break;
10397+
}
10398+
}
10399+
10400+
static void
10401+
garrow_zero_fill_options_get_property(GObject *object,
10402+
guint prop_id,
10403+
GValue *value,
10404+
GParamSpec *pspec)
10405+
{
10406+
auto options = garrow_zero_fill_options_get_raw(GARROW_ZERO_FILL_OPTIONS(object));
10407+
10408+
switch (prop_id) {
10409+
case PROP_ZERO_FILL_OPTIONS_WIDTH:
10410+
g_value_set_int64(value, options->width);
10411+
break;
10412+
case PROP_ZERO_FILL_OPTIONS_PADDING:
10413+
g_value_set_string(value, options->padding.c_str());
10414+
break;
10415+
default:
10416+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
10417+
break;
10418+
}
10419+
}
10420+
10421+
static void
10422+
garrow_zero_fill_options_init(GArrowZeroFillOptions *object)
10423+
{
10424+
auto arrow_priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
10425+
arrow_priv->options =
10426+
static_cast<arrow::compute::FunctionOptions *>(new arrow::compute::ZeroFillOptions());
10427+
}
10428+
10429+
static void
10430+
garrow_zero_fill_options_class_init(GArrowZeroFillOptionsClass *klass)
10431+
{
10432+
auto gobject_class = G_OBJECT_CLASS(klass);
10433+
10434+
gobject_class->set_property = garrow_zero_fill_options_set_property;
10435+
gobject_class->get_property = garrow_zero_fill_options_get_property;
10436+
10437+
arrow::compute::ZeroFillOptions options;
10438+
10439+
GParamSpec *spec;
10440+
/**
10441+
* GArrowZeroFillOptions:width:
10442+
*
10443+
* The desired string length.
10444+
*
10445+
* Since: 23.0.0
10446+
*/
10447+
spec = g_param_spec_int64("width",
10448+
"Width",
10449+
"The desired string length",
10450+
G_MININT64,
10451+
G_MAXINT64,
10452+
options.width,
10453+
static_cast<GParamFlags>(G_PARAM_READWRITE));
10454+
g_object_class_install_property(gobject_class, PROP_ZERO_FILL_OPTIONS_WIDTH, spec);
10455+
10456+
/**
10457+
* GArrowZeroFillOptions:padding:
10458+
*
10459+
* What to pad the string with. Should be one codepoint (Unicode).
10460+
*
10461+
* Since: 23.0.0
10462+
*/
10463+
spec =
10464+
g_param_spec_string("padding",
10465+
"Padding",
10466+
"What to pad the string with. Should be one codepoint (Unicode)",
10467+
options.padding.c_str(),
10468+
static_cast<GParamFlags>(G_PARAM_READWRITE));
10469+
g_object_class_install_property(gobject_class, PROP_ZERO_FILL_OPTIONS_PADDING, spec);
10470+
}
10471+
10472+
/**
10473+
* garrow_zero_fill_options_new:
10474+
*
10475+
* Returns: A newly created #GArrowZeroFillOptions.
10476+
*
10477+
* Since: 23.0.0
10478+
*/
10479+
GArrowZeroFillOptions *
10480+
garrow_zero_fill_options_new(void)
10481+
{
10482+
return GARROW_ZERO_FILL_OPTIONS(g_object_new(GARROW_TYPE_ZERO_FILL_OPTIONS, nullptr));
10483+
}
10484+
1036710485
G_END_DECLS
1036810486

1036910487
arrow::Result<arrow::FieldRef>
@@ -10639,6 +10757,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt
1063910757
static_cast<const arrow::compute::WinsorizeOptions *>(arrow_options);
1064010758
auto options = garrow_winsorize_options_new_raw(arrow_winsorize_options);
1064110759
return GARROW_FUNCTION_OPTIONS(options);
10760+
} else if (arrow_type_name == "ZeroFillOptions") {
10761+
const auto arrow_zero_fill_options =
10762+
static_cast<const arrow::compute::ZeroFillOptions *>(arrow_options);
10763+
auto options = garrow_zero_fill_options_new_raw(arrow_zero_fill_options);
10764+
return GARROW_FUNCTION_OPTIONS(options);
1064210765
} else {
1064310766
auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
1064410767
return GARROW_FUNCTION_OPTIONS(options);
@@ -11756,3 +11879,21 @@ garrow_winsorize_options_get_raw(GArrowWinsorizeOptions *options)
1175611879
return static_cast<arrow::compute::WinsorizeOptions *>(
1175711880
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
1175811881
}
11882+
11883+
GArrowZeroFillOptions *
11884+
garrow_zero_fill_options_new_raw(const arrow::compute::ZeroFillOptions *arrow_options)
11885+
{
11886+
return GARROW_ZERO_FILL_OPTIONS(g_object_new(GARROW_TYPE_ZERO_FILL_OPTIONS,
11887+
"width",
11888+
arrow_options->width,
11889+
"padding",
11890+
arrow_options->padding.c_str(),
11891+
nullptr));
11892+
}
11893+
11894+
arrow::compute::ZeroFillOptions *
11895+
garrow_zero_fill_options_get_raw(GArrowZeroFillOptions *options)
11896+
{
11897+
return static_cast<arrow::compute::ZeroFillOptions *>(
11898+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
11899+
}

c_glib/arrow-glib/compute.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,4 +1771,20 @@ GARROW_AVAILABLE_IN_23_0
17711771
GArrowWinsorizeOptions *
17721772
garrow_winsorize_options_new(void);
17731773

1774+
#define GARROW_TYPE_ZERO_FILL_OPTIONS (garrow_zero_fill_options_get_type())
1775+
GARROW_AVAILABLE_IN_23_0
1776+
G_DECLARE_DERIVABLE_TYPE(GArrowZeroFillOptions,
1777+
garrow_zero_fill_options,
1778+
GARROW,
1779+
ZERO_FILL_OPTIONS,
1780+
GArrowFunctionOptions)
1781+
struct _GArrowZeroFillOptionsClass
1782+
{
1783+
GArrowFunctionOptionsClass parent_class;
1784+
};
1785+
1786+
GARROW_AVAILABLE_IN_23_0
1787+
GArrowZeroFillOptions *
1788+
garrow_zero_fill_options_new(void);
1789+
17741790
G_END_DECLS

c_glib/arrow-glib/compute.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,8 @@ GArrowWinsorizeOptions *
333333
garrow_winsorize_options_new_raw(const arrow::compute::WinsorizeOptions *arrow_options);
334334
arrow::compute::WinsorizeOptions *
335335
garrow_winsorize_options_get_raw(GArrowWinsorizeOptions *options);
336+
337+
GArrowZeroFillOptions *
338+
garrow_zero_fill_options_new_raw(const arrow::compute::ZeroFillOptions *arrow_options);
339+
arrow::compute::ZeroFillOptions *
340+
garrow_zero_fill_options_get_raw(GArrowZeroFillOptions *options);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
class TestZeroFillOptions < Test::Unit::TestCase
19+
include Helper::Buildable
20+
21+
def setup
22+
@options = Arrow::ZeroFillOptions.new
23+
end
24+
25+
def test_width_property
26+
assert_equal(0, @options.width)
27+
@options.width = 4
28+
assert_equal(4, @options.width)
29+
end
30+
31+
def test_padding_property
32+
assert_equal("0", @options.padding)
33+
@options.padding = "x"
34+
assert_equal("x", @options.padding)
35+
end
36+
37+
def test_utf8_zero_fill_function
38+
args = [
39+
Arrow::ArrayDatum.new(build_string_array(["1", "-2", "+3"])),
40+
]
41+
@options.width = 4
42+
@options.padding = "0"
43+
utf8_zero_fill_function = Arrow::Function.find("utf8_zero_fill")
44+
result = utf8_zero_fill_function.execute(args, @options).value
45+
expected = build_string_array(["0001", "-002", "+003"])
46+
assert_equal(expected, result)
47+
end
48+
end

0 commit comments

Comments
 (0)