Skip to content

Commit 4620142

Browse files
committed
Add GArrowZeroFillOptions
1 parent dbec6aa commit 4620142

File tree

4 files changed

+242
-0
lines changed

4 files changed

+242
-0
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ G_BEGIN_DECLS
337337
*
338338
* #GArrowWinsorizeOptions is a class to customize the `winsorize` function.
339339
*
340+
* #GArrowZeroFillOptions is a class to customize the `utf8_zero_fill` function.
341+
*
340342
* There are many functions to compute data on an array.
341343
*/
342344

@@ -10571,6 +10573,156 @@ garrow_winsorize_options_new(void)
1057110573
return GARROW_WINSORIZE_OPTIONS(g_object_new(GARROW_TYPE_WINSORIZE_OPTIONS, NULL));
1057210574
}
1057310575

10576+
enum {
10577+
PROP_ZERO_FILL_OPTIONS_WIDTH = 1,
10578+
PROP_ZERO_FILL_OPTIONS_PADDING,
10579+
};
10580+
10581+
typedef struct _GArrowZeroFillOptionsPrivate GArrowZeroFillOptionsPrivate;
10582+
struct _GArrowZeroFillOptionsPrivate
10583+
{
10584+
gchar *padding;
10585+
};
10586+
10587+
G_DEFINE_TYPE_WITH_PRIVATE(GArrowZeroFillOptions,
10588+
garrow_zero_fill_options,
10589+
GARROW_TYPE_FUNCTION_OPTIONS)
10590+
10591+
#define GARROW_ZERO_FILL_OPTIONS_GET_PRIVATE(object) \
10592+
static_cast<GArrowZeroFillOptionsPrivate *>( \
10593+
garrow_zero_fill_options_get_instance_private(GARROW_ZERO_FILL_OPTIONS(object)))
10594+
10595+
static void
10596+
garrow_zero_fill_options_dispose(GObject *object)
10597+
{
10598+
auto priv = GARROW_ZERO_FILL_OPTIONS_GET_PRIVATE(object);
10599+
if (priv->padding) {
10600+
g_free(priv->padding);
10601+
priv->padding = nullptr;
10602+
}
10603+
G_OBJECT_CLASS(garrow_zero_fill_options_parent_class)->dispose(object);
10604+
}
10605+
10606+
static void
10607+
garrow_zero_fill_options_set_property(GObject *object,
10608+
guint prop_id,
10609+
const GValue *value,
10610+
GParamSpec *pspec)
10611+
{
10612+
auto options = garrow_zero_fill_options_get_raw(GARROW_ZERO_FILL_OPTIONS(object));
10613+
auto priv = GARROW_ZERO_FILL_OPTIONS_GET_PRIVATE(object);
10614+
10615+
switch (prop_id) {
10616+
case PROP_ZERO_FILL_OPTIONS_WIDTH:
10617+
options->width = g_value_get_int64(value);
10618+
break;
10619+
case PROP_ZERO_FILL_OPTIONS_PADDING:
10620+
{
10621+
const gchar *padding = g_value_get_string(value);
10622+
if (priv->padding) {
10623+
g_free(priv->padding);
10624+
}
10625+
priv->padding = g_strdup(padding);
10626+
options->padding = padding ? padding : "";
10627+
}
10628+
break;
10629+
default:
10630+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
10631+
break;
10632+
}
10633+
}
10634+
10635+
static void
10636+
garrow_zero_fill_options_get_property(GObject *object,
10637+
guint prop_id,
10638+
GValue *value,
10639+
GParamSpec *pspec)
10640+
{
10641+
auto options = garrow_zero_fill_options_get_raw(GARROW_ZERO_FILL_OPTIONS(object));
10642+
auto priv = GARROW_ZERO_FILL_OPTIONS_GET_PRIVATE(object);
10643+
10644+
switch (prop_id) {
10645+
case PROP_ZERO_FILL_OPTIONS_WIDTH:
10646+
g_value_set_int64(value, options->width);
10647+
break;
10648+
case PROP_ZERO_FILL_OPTIONS_PADDING:
10649+
g_value_set_string(value, priv->padding ? priv->padding : options->padding.c_str());
10650+
break;
10651+
default:
10652+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
10653+
break;
10654+
}
10655+
}
10656+
10657+
static void
10658+
garrow_zero_fill_options_init(GArrowZeroFillOptions *object)
10659+
{
10660+
auto priv = GARROW_ZERO_FILL_OPTIONS_GET_PRIVATE(object);
10661+
priv->padding = nullptr;
10662+
auto arrow_priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
10663+
arrow_priv->options =
10664+
static_cast<arrow::compute::FunctionOptions *>(new arrow::compute::ZeroFillOptions());
10665+
// Sync the private string with the C++ options
10666+
auto arrow_options = garrow_zero_fill_options_get_raw(GARROW_ZERO_FILL_OPTIONS(object));
10667+
priv->padding = g_strdup(arrow_options->padding.c_str());
10668+
}
10669+
10670+
static void
10671+
garrow_zero_fill_options_class_init(GArrowZeroFillOptionsClass *klass)
10672+
{
10673+
auto gobject_class = G_OBJECT_CLASS(klass);
10674+
10675+
gobject_class->dispose = garrow_zero_fill_options_dispose;
10676+
gobject_class->set_property = garrow_zero_fill_options_set_property;
10677+
gobject_class->get_property = garrow_zero_fill_options_get_property;
10678+
10679+
arrow::compute::ZeroFillOptions options;
10680+
10681+
GParamSpec *spec;
10682+
/**
10683+
* GArrowZeroFillOptions:width:
10684+
*
10685+
* The desired string length.
10686+
*
10687+
* Since: 23.0.0
10688+
*/
10689+
spec = g_param_spec_int64("width",
10690+
"Width",
10691+
"The desired string length",
10692+
G_MININT64,
10693+
G_MAXINT64,
10694+
options.width,
10695+
static_cast<GParamFlags>(G_PARAM_READWRITE));
10696+
g_object_class_install_property(gobject_class, PROP_ZERO_FILL_OPTIONS_WIDTH, spec);
10697+
10698+
/**
10699+
* GArrowZeroFillOptions:padding:
10700+
*
10701+
* What to pad the string with. Should be one codepoint (Unicode).
10702+
*
10703+
* Since: 23.0.0
10704+
*/
10705+
spec = g_param_spec_string("padding",
10706+
"Padding",
10707+
"What to pad the string with. Should be one codepoint (Unicode)",
10708+
options.padding.c_str(),
10709+
static_cast<GParamFlags>(G_PARAM_READWRITE));
10710+
g_object_class_install_property(gobject_class, PROP_ZERO_FILL_OPTIONS_PADDING, spec);
10711+
}
10712+
10713+
/**
10714+
* garrow_zero_fill_options_new:
10715+
*
10716+
* Returns: A newly created #GArrowZeroFillOptions.
10717+
*
10718+
* Since: 23.0.0
10719+
*/
10720+
GArrowZeroFillOptions *
10721+
garrow_zero_fill_options_new(void)
10722+
{
10723+
return GARROW_ZERO_FILL_OPTIONS(g_object_new(GARROW_TYPE_ZERO_FILL_OPTIONS, NULL));
10724+
}
10725+
1057410726
G_END_DECLS
1057510727

1057610728
arrow::Result<arrow::FieldRef>
@@ -10852,6 +11004,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt
1085211004
static_cast<const arrow::compute::WinsorizeOptions *>(arrow_options);
1085311005
auto options = garrow_winsorize_options_new_raw(arrow_winsorize_options);
1085411006
return GARROW_FUNCTION_OPTIONS(options);
11007+
} else if (arrow_type_name == "ZeroFillOptions") {
11008+
const auto arrow_zero_fill_options =
11009+
static_cast<const arrow::compute::ZeroFillOptions *>(arrow_options);
11010+
auto options = garrow_zero_fill_options_new_raw(arrow_zero_fill_options);
11011+
return GARROW_FUNCTION_OPTIONS(options);
1085511012
} else {
1085611013
auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
1085711014
return GARROW_FUNCTION_OPTIONS(options);
@@ -11997,3 +12154,21 @@ garrow_winsorize_options_get_raw(GArrowWinsorizeOptions *options)
1199712154
return static_cast<arrow::compute::WinsorizeOptions *>(
1199812155
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
1199912156
}
12157+
12158+
GArrowZeroFillOptions *
12159+
garrow_zero_fill_options_new_raw(const arrow::compute::ZeroFillOptions *arrow_options)
12160+
{
12161+
return GARROW_ZERO_FILL_OPTIONS(g_object_new(GARROW_TYPE_ZERO_FILL_OPTIONS,
12162+
"width",
12163+
arrow_options->width,
12164+
"padding",
12165+
arrow_options->padding.c_str(),
12166+
NULL));
12167+
}
12168+
12169+
arrow::compute::ZeroFillOptions *
12170+
garrow_zero_fill_options_get_raw(GArrowZeroFillOptions *options)
12171+
{
12172+
return static_cast<arrow::compute::ZeroFillOptions *>(
12173+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
12174+
}

c_glib/arrow-glib/compute.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,4 +1772,17 @@ GARROW_AVAILABLE_IN_23_0
17721772
GArrowWinsorizeOptions *
17731773
garrow_winsorize_options_new(void);
17741774

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

c_glib/arrow-glib/compute.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,8 @@ GArrowWinsorizeOptions *
337337
garrow_winsorize_options_new_raw(const arrow::compute::WinsorizeOptions *arrow_options);
338338
arrow::compute::WinsorizeOptions *
339339
garrow_winsorize_options_get_raw(GArrowWinsorizeOptions *options);
340+
341+
GArrowZeroFillOptions *
342+
garrow_zero_fill_options_new_raw(const arrow::compute::ZeroFillOptions *arrow_options);
343+
arrow::compute::ZeroFillOptions *
344+
garrow_zero_fill_options_get_raw(GArrowZeroFillOptions *options);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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
49+

0 commit comments

Comments
 (0)