Skip to content

Commit a54cd53

Browse files
stenlarssonkou
authored andcommitted
Add GArrowPadOptions
1 parent 469976f commit a54cd53

File tree

4 files changed

+250
-0
lines changed

4 files changed

+250
-0
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,10 @@ G_BEGIN_DECLS
289289
*
290290
* #GArrowNullOptions is a class to customize the `is_null` function.
291291
*
292+
* #GArrowPadOptions is a class to customize the padding functions such as
293+
* `utf8_lpad`, `utf8_rpad`, `utf8_center`, `ascii_lpad`, `ascii_rpad`, and
294+
* `ascii_center`.
295+
*
292296
* There are many functions to compute data on an array.
293297
*/
294298

@@ -8107,6 +8111,147 @@ garrow_null_options_new(void)
81078111
return GARROW_NULL_OPTIONS(g_object_new(GARROW_TYPE_NULL_OPTIONS, NULL));
81088112
}
81098113

8114+
enum {
8115+
PROP_PAD_OPTIONS_WIDTH = 1,
8116+
PROP_PAD_OPTIONS_PADDING,
8117+
PROP_PAD_OPTIONS_LEAN_LEFT_ON_ODD_PADDING,
8118+
};
8119+
8120+
G_DEFINE_TYPE(GArrowPadOptions, garrow_pad_options, GARROW_TYPE_FUNCTION_OPTIONS)
8121+
8122+
static void
8123+
garrow_pad_options_set_property(GObject *object,
8124+
guint prop_id,
8125+
const GValue *value,
8126+
GParamSpec *pspec)
8127+
{
8128+
auto options = garrow_pad_options_get_raw(GARROW_PAD_OPTIONS(object));
8129+
8130+
switch (prop_id) {
8131+
case PROP_PAD_OPTIONS_WIDTH:
8132+
options->width = g_value_get_int64(value);
8133+
break;
8134+
case PROP_PAD_OPTIONS_PADDING:
8135+
options->padding = g_value_get_string(value);
8136+
break;
8137+
case PROP_PAD_OPTIONS_LEAN_LEFT_ON_ODD_PADDING:
8138+
options->lean_left_on_odd_padding = g_value_get_boolean(value);
8139+
break;
8140+
default:
8141+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
8142+
break;
8143+
}
8144+
}
8145+
8146+
static void
8147+
garrow_pad_options_get_property(GObject *object,
8148+
guint prop_id,
8149+
GValue *value,
8150+
GParamSpec *pspec)
8151+
{
8152+
auto options = garrow_pad_options_get_raw(GARROW_PAD_OPTIONS(object));
8153+
8154+
switch (prop_id) {
8155+
case PROP_PAD_OPTIONS_WIDTH:
8156+
g_value_set_int64(value, options->width);
8157+
break;
8158+
case PROP_PAD_OPTIONS_PADDING:
8159+
g_value_set_string(value, options->padding.c_str());
8160+
break;
8161+
case PROP_PAD_OPTIONS_LEAN_LEFT_ON_ODD_PADDING:
8162+
g_value_set_boolean(value, options->lean_left_on_odd_padding);
8163+
break;
8164+
default:
8165+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
8166+
break;
8167+
}
8168+
}
8169+
8170+
static void
8171+
garrow_pad_options_init(GArrowPadOptions *object)
8172+
{
8173+
auto priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
8174+
priv->options =
8175+
static_cast<arrow::compute::FunctionOptions *>(new arrow::compute::PadOptions());
8176+
}
8177+
8178+
static void
8179+
garrow_pad_options_class_init(GArrowPadOptionsClass *klass)
8180+
{
8181+
auto gobject_class = G_OBJECT_CLASS(klass);
8182+
8183+
gobject_class->set_property = garrow_pad_options_set_property;
8184+
gobject_class->get_property = garrow_pad_options_get_property;
8185+
8186+
arrow::compute::PadOptions options;
8187+
8188+
GParamSpec *spec;
8189+
/**
8190+
* GArrowPadOptions:width:
8191+
*
8192+
* The desired string length.
8193+
*
8194+
* Since: 23.0.0
8195+
*/
8196+
spec = g_param_spec_int64("width",
8197+
"Width",
8198+
"The desired string length",
8199+
0,
8200+
G_MAXINT64,
8201+
options.width,
8202+
static_cast<GParamFlags>(G_PARAM_READWRITE));
8203+
g_object_class_install_property(gobject_class, PROP_PAD_OPTIONS_WIDTH, spec);
8204+
8205+
/**
8206+
* GArrowPadOptions:padding:
8207+
*
8208+
* What to pad the string with. Should be one codepoint (Unicode)/byte (ASCII).
8209+
*
8210+
* Since: 23.0.0
8211+
*/
8212+
spec = g_param_spec_string(
8213+
"padding",
8214+
"Padding",
8215+
"What to pad the string with. Should be one codepoint (Unicode)/byte (ASCII)",
8216+
options.padding.c_str(),
8217+
static_cast<GParamFlags>(G_PARAM_READWRITE));
8218+
g_object_class_install_property(gobject_class, PROP_PAD_OPTIONS_PADDING, spec);
8219+
8220+
/**
8221+
* GArrowPadOptions:lean-left-on-odd-padding:
8222+
*
8223+
* What to do if there is an odd number of padding characters (in case of centered
8224+
* padding). Defaults to aligning on the left (i.e. adding the extra padding character
8225+
* on the right).
8226+
*
8227+
* Since: 23.0.0
8228+
*/
8229+
spec =
8230+
g_param_spec_boolean("lean-left-on-odd-padding",
8231+
"Lean left on odd padding",
8232+
"What to do if there is an odd number of padding characters (in "
8233+
"case of centered padding). Defaults to aligning on the left "
8234+
"(i.e. adding the extra padding character on the right)",
8235+
options.lean_left_on_odd_padding,
8236+
static_cast<GParamFlags>(G_PARAM_READWRITE));
8237+
g_object_class_install_property(gobject_class,
8238+
PROP_PAD_OPTIONS_LEAN_LEFT_ON_ODD_PADDING,
8239+
spec);
8240+
}
8241+
8242+
/**
8243+
* garrow_pad_options_new:
8244+
*
8245+
* Returns: A newly created #GArrowPadOptions.
8246+
*
8247+
* Since: 23.0.0
8248+
*/
8249+
GArrowPadOptions *
8250+
garrow_pad_options_new(void)
8251+
{
8252+
return GARROW_PAD_OPTIONS(g_object_new(GARROW_TYPE_PAD_OPTIONS, NULL));
8253+
}
8254+
81108255
G_END_DECLS
81118256

81128257
arrow::Result<arrow::FieldRef>
@@ -8301,6 +8446,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt
83018446
static_cast<const arrow::compute::NullOptions *>(arrow_options);
83028447
auto options = garrow_null_options_new_raw(arrow_null_options);
83038448
return GARROW_FUNCTION_OPTIONS(options);
8449+
} else if (arrow_type_name == "PadOptions") {
8450+
const auto arrow_pad_options =
8451+
static_cast<const arrow::compute::PadOptions *>(arrow_options);
8452+
auto options = garrow_pad_options_new_raw(arrow_pad_options);
8453+
return GARROW_FUNCTION_OPTIONS(options);
83048454
} else {
83058455
auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
83068456
return GARROW_FUNCTION_OPTIONS(options);
@@ -9091,3 +9241,23 @@ garrow_null_options_get_raw(GArrowNullOptions *options)
90919241
return static_cast<arrow::compute::NullOptions *>(
90929242
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
90939243
}
9244+
9245+
GArrowPadOptions *
9246+
garrow_pad_options_new_raw(const arrow::compute::PadOptions *arrow_options)
9247+
{
9248+
return GARROW_PAD_OPTIONS(g_object_new(GARROW_TYPE_PAD_OPTIONS,
9249+
"width",
9250+
arrow_options->width,
9251+
"padding",
9252+
arrow_options->padding.c_str(),
9253+
"lean-left-on-odd-padding",
9254+
arrow_options->lean_left_on_odd_padding,
9255+
NULL));
9256+
}
9257+
9258+
arrow::compute::PadOptions *
9259+
garrow_pad_options_get_raw(GArrowPadOptions *options)
9260+
{
9261+
return static_cast<arrow::compute::PadOptions *>(
9262+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
9263+
}

c_glib/arrow-glib/compute.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,4 +1446,17 @@ GARROW_AVAILABLE_IN_23_0
14461446
GArrowNullOptions *
14471447
garrow_null_options_new(void);
14481448

1449+
#define GARROW_TYPE_PAD_OPTIONS (garrow_pad_options_get_type())
1450+
GARROW_AVAILABLE_IN_23_0
1451+
G_DECLARE_DERIVABLE_TYPE(
1452+
GArrowPadOptions, garrow_pad_options, GARROW, PAD_OPTIONS, GArrowFunctionOptions)
1453+
struct _GArrowPadOptionsClass
1454+
{
1455+
GArrowFunctionOptionsClass parent_class;
1456+
};
1457+
1458+
GARROW_AVAILABLE_IN_23_0
1459+
GArrowPadOptions *
1460+
garrow_pad_options_new(void);
1461+
14491462
G_END_DECLS

c_glib/arrow-glib/compute.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,8 @@ GArrowNullOptions *
246246
garrow_null_options_new_raw(const arrow::compute::NullOptions *arrow_options);
247247
arrow::compute::NullOptions *
248248
garrow_null_options_get_raw(GArrowNullOptions *options);
249+
250+
GArrowPadOptions *
251+
garrow_pad_options_new_raw(const arrow::compute::PadOptions *arrow_options);
252+
arrow::compute::PadOptions *
253+
garrow_pad_options_get_raw(GArrowPadOptions *options);

c_glib/test/test-pad-options.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 TestPadOptions < Test::Unit::TestCase
19+
include Helper::Buildable
20+
21+
def setup
22+
@options = Arrow::PadOptions.new
23+
end
24+
25+
def test_width_property
26+
assert_equal(0, @options.width)
27+
@options.width = 5
28+
assert_equal(5, @options.width)
29+
end
30+
31+
def test_padding_property
32+
assert_equal(" ", @options.padding)
33+
@options.padding = "0"
34+
assert_equal("0", @options.padding)
35+
end
36+
37+
def test_lean_left_on_odd_padding_property
38+
assert do
39+
@options.lean_left_on_odd_padding?
40+
end
41+
@options.lean_left_on_odd_padding = false
42+
assert do
43+
not @options.lean_left_on_odd_padding?
44+
end
45+
end
46+
47+
def test_utf8_center_function
48+
args = [
49+
Arrow::ArrayDatum.new(build_string_array(["a", "ab", "abc"])),
50+
]
51+
utf8_center_function = Arrow::Function.find("utf8_center")
52+
53+
@options.width = 5
54+
@options.padding = " "
55+
result = utf8_center_function.execute(args, @options).value
56+
assert_equal(build_string_array([" a ", " ab ", " abc "]), result)
57+
58+
@options.lean_left_on_odd_padding = false
59+
result = utf8_center_function.execute(args, @options).value
60+
assert_equal(build_string_array([" a ", " ab ", " abc "]), result)
61+
end
62+
end

0 commit comments

Comments
 (0)