Skip to content

Commit 1fe2a32

Browse files
committed
Add GArrowTrimOptions
1 parent 64b6ade commit 1fe2a32

File tree

4 files changed

+211
-0
lines changed

4 files changed

+211
-0
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@ G_BEGIN_DECLS
330330
* #GArrowTDigestOptions is a class to customize the `tdigest` and
331331
* `hash_tdigest` functions.
332332
*
333+
* #GArrowTrimOptions is a class to customize the `utf8_trim`, `utf8_ltrim`,
334+
* `utf8_rtrim`, `ascii_trim`, `ascii_ltrim`, and `ascii_rtrim` functions.
335+
*
333336
* There are many functions to compute data on an array.
334337
*/
335338

@@ -10185,6 +10188,133 @@ garrow_tdigest_options_set_qs(GArrowTDigestOptions *options, const gdouble *qs,
1018510188
}
1018610189
}
1018710190

10191+
enum {
10192+
PROP_TRIM_OPTIONS_CHARACTERS = 1,
10193+
};
10194+
10195+
typedef struct _GArrowTrimOptionsPrivate GArrowTrimOptionsPrivate;
10196+
struct _GArrowTrimOptionsPrivate
10197+
{
10198+
gchar *characters;
10199+
};
10200+
10201+
G_DEFINE_TYPE_WITH_PRIVATE(GArrowTrimOptions,
10202+
garrow_trim_options,
10203+
GARROW_TYPE_FUNCTION_OPTIONS)
10204+
10205+
#define GARROW_TRIM_OPTIONS_GET_PRIVATE(object) \
10206+
static_cast<GArrowTrimOptionsPrivate *>( \
10207+
garrow_trim_options_get_instance_private(GARROW_TRIM_OPTIONS(object)))
10208+
10209+
static void
10210+
garrow_trim_options_dispose(GObject *object)
10211+
{
10212+
auto priv = GARROW_TRIM_OPTIONS_GET_PRIVATE(object);
10213+
if (priv->characters) {
10214+
g_free(priv->characters);
10215+
priv->characters = nullptr;
10216+
}
10217+
G_OBJECT_CLASS(garrow_trim_options_parent_class)->dispose(object);
10218+
}
10219+
10220+
static void
10221+
garrow_trim_options_set_property(GObject *object,
10222+
guint prop_id,
10223+
const GValue *value,
10224+
GParamSpec *pspec)
10225+
{
10226+
auto options = garrow_trim_options_get_raw(GARROW_TRIM_OPTIONS(object));
10227+
auto priv = GARROW_TRIM_OPTIONS_GET_PRIVATE(object);
10228+
10229+
switch (prop_id) {
10230+
case PROP_TRIM_OPTIONS_CHARACTERS:
10231+
{
10232+
const gchar *characters = g_value_get_string(value);
10233+
if (priv->characters) {
10234+
g_free(priv->characters);
10235+
}
10236+
priv->characters = g_strdup(characters);
10237+
options->characters = characters ? characters : "";
10238+
}
10239+
break;
10240+
default:
10241+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
10242+
break;
10243+
}
10244+
}
10245+
10246+
static void
10247+
garrow_trim_options_get_property(GObject *object,
10248+
guint prop_id,
10249+
GValue *value,
10250+
GParamSpec *pspec)
10251+
{
10252+
auto options = garrow_trim_options_get_raw(GARROW_TRIM_OPTIONS(object));
10253+
auto priv = GARROW_TRIM_OPTIONS_GET_PRIVATE(object);
10254+
10255+
switch (prop_id) {
10256+
case PROP_TRIM_OPTIONS_CHARACTERS:
10257+
g_value_set_string(value, priv->characters ? priv->characters : options->characters.c_str());
10258+
break;
10259+
default:
10260+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
10261+
break;
10262+
}
10263+
}
10264+
10265+
static void
10266+
garrow_trim_options_init(GArrowTrimOptions *object)
10267+
{
10268+
auto priv = GARROW_TRIM_OPTIONS_GET_PRIVATE(object);
10269+
priv->characters = nullptr;
10270+
auto arrow_priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
10271+
arrow_priv->options =
10272+
static_cast<arrow::compute::FunctionOptions *>(new arrow::compute::TrimOptions());
10273+
// Sync the private string with the C++ options
10274+
auto arrow_options = garrow_trim_options_get_raw(GARROW_TRIM_OPTIONS(object));
10275+
priv->characters = g_strdup(arrow_options->characters.c_str());
10276+
}
10277+
10278+
static void
10279+
garrow_trim_options_class_init(GArrowTrimOptionsClass *klass)
10280+
{
10281+
auto gobject_class = G_OBJECT_CLASS(klass);
10282+
10283+
gobject_class->dispose = garrow_trim_options_dispose;
10284+
gobject_class->set_property = garrow_trim_options_set_property;
10285+
gobject_class->get_property = garrow_trim_options_get_property;
10286+
10287+
arrow::compute::TrimOptions options;
10288+
10289+
GParamSpec *spec;
10290+
/**
10291+
* GArrowTrimOptions:characters:
10292+
*
10293+
* The individual characters to be trimmed from the string.
10294+
*
10295+
* Since: 23.0.0
10296+
*/
10297+
spec = g_param_spec_string("characters",
10298+
"Characters",
10299+
"The individual characters to be trimmed from the string",
10300+
options.characters.c_str(),
10301+
static_cast<GParamFlags>(G_PARAM_READWRITE));
10302+
g_object_class_install_property(gobject_class, PROP_TRIM_OPTIONS_CHARACTERS, spec);
10303+
}
10304+
10305+
/**
10306+
* garrow_trim_options_new:
10307+
*
10308+
* Returns: A newly created #GArrowTrimOptions.
10309+
*
10310+
* Since: 23.0.0
10311+
*/
10312+
GArrowTrimOptions *
10313+
garrow_trim_options_new(void)
10314+
{
10315+
return GARROW_TRIM_OPTIONS(g_object_new(GARROW_TYPE_TRIM_OPTIONS, NULL));
10316+
}
10317+
1018810318
G_END_DECLS
1018910319

1019010320
arrow::Result<arrow::FieldRef>
@@ -10451,6 +10581,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt
1045110581
static_cast<const arrow::compute::TDigestOptions *>(arrow_options);
1045210582
auto options = garrow_tdigest_options_new_raw(arrow_tdigest_options);
1045310583
return GARROW_FUNCTION_OPTIONS(options);
10584+
} else if (arrow_type_name == "TrimOptions") {
10585+
const auto arrow_trim_options =
10586+
static_cast<const arrow::compute::TrimOptions *>(arrow_options);
10587+
auto options = garrow_trim_options_new_raw(arrow_trim_options);
10588+
return GARROW_FUNCTION_OPTIONS(options);
1045410589
} else {
1045510590
auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
1045610591
return GARROW_FUNCTION_OPTIONS(options);
@@ -11540,3 +11675,19 @@ garrow_tdigest_options_get_raw(GArrowTDigestOptions *options)
1154011675
return static_cast<arrow::compute::TDigestOptions *>(
1154111676
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
1154211677
}
11678+
11679+
GArrowTrimOptions *
11680+
garrow_trim_options_new_raw(const arrow::compute::TrimOptions *arrow_options)
11681+
{
11682+
return GARROW_TRIM_OPTIONS(g_object_new(GARROW_TYPE_TRIM_OPTIONS,
11683+
"characters",
11684+
arrow_options->characters.c_str(),
11685+
NULL));
11686+
}
11687+
11688+
arrow::compute::TrimOptions *
11689+
garrow_trim_options_get_raw(GArrowTrimOptions *options)
11690+
{
11691+
return static_cast<arrow::compute::TrimOptions *>(
11692+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
11693+
}

c_glib/arrow-glib/compute.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,4 +1733,17 @@ garrow_tdigest_options_set_qs(GArrowTDigestOptions *options,
17331733
const gdouble *qs,
17341734
gsize n);
17351735

1736+
#define GARROW_TYPE_TRIM_OPTIONS (garrow_trim_options_get_type())
1737+
GARROW_AVAILABLE_IN_23_0
1738+
G_DECLARE_DERIVABLE_TYPE(
1739+
GArrowTrimOptions, garrow_trim_options, GARROW, TRIM_OPTIONS, GArrowFunctionOptions)
1740+
struct _GArrowTrimOptionsClass
1741+
{
1742+
GArrowFunctionOptionsClass parent_class;
1743+
};
1744+
1745+
GARROW_AVAILABLE_IN_23_0
1746+
GArrowTrimOptions *
1747+
garrow_trim_options_new(void);
1748+
17361749
G_END_DECLS

c_glib/arrow-glib/compute.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,3 +322,8 @@ GArrowTDigestOptions *
322322
garrow_tdigest_options_new_raw(const arrow::compute::TDigestOptions *arrow_options);
323323
arrow::compute::TDigestOptions *
324324
garrow_tdigest_options_get_raw(GArrowTDigestOptions *options);
325+
326+
GArrowTrimOptions *
327+
garrow_trim_options_new_raw(const arrow::compute::TrimOptions *arrow_options);
328+
arrow::compute::TrimOptions *
329+
garrow_trim_options_get_raw(GArrowTrimOptions *options);

c_glib/test/test-trim-options.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 TestTrimOptions < Test::Unit::TestCase
19+
include Helper::Buildable
20+
21+
def setup
22+
@options = Arrow::TrimOptions.new
23+
end
24+
25+
def test_characters_property
26+
assert_equal("", @options.characters)
27+
@options.characters = " \t"
28+
assert_equal(" \t", @options.characters)
29+
end
30+
31+
def test_utf8_trim_function
32+
args = [
33+
Arrow::ArrayDatum.new(build_string_array([" hello ", " world "])),
34+
]
35+
@options.characters = " "
36+
utf8_trim_function = Arrow::Function.find("utf8_trim")
37+
result = utf8_trim_function.execute(args, @options).value
38+
expected = build_string_array(["hello", "world"])
39+
assert_equal(expected, result)
40+
end
41+
end
42+

0 commit comments

Comments
 (0)