Skip to content

Commit abdf3bd

Browse files
committed
Add GArrowReplaceSliceOptions
1 parent 4127ca2 commit abdf3bd

File tree

4 files changed

+288
-0
lines changed

4 files changed

+288
-0
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ G_BEGIN_DECLS
269269
* #GArrowExtractRegexOptions is a class to customize the `extract_regex`
270270
* function.
271271
*
272+
* #GArrowReplaceSliceOptions is a class to customize the `utf8_replace_slice` and
273+
* `binary_replace_slice` functions.
274+
*
272275
* There are many functions to compute data on an array.
273276
*/
274277

@@ -7091,6 +7094,188 @@ garrow_extract_regex_options_new(void)
70917094
return GARROW_EXTRACT_REGEX_OPTIONS(options);
70927095
}
70937096

7097+
enum {
7098+
PROP_REPLACE_SLICE_OPTIONS_START = 1,
7099+
PROP_REPLACE_SLICE_OPTIONS_STOP,
7100+
PROP_REPLACE_SLICE_OPTIONS_REPLACEMENT,
7101+
};
7102+
7103+
typedef struct _GArrowReplaceSliceOptionsPrivate GArrowReplaceSliceOptionsPrivate;
7104+
struct _GArrowReplaceSliceOptionsPrivate
7105+
{
7106+
gchar *replacement;
7107+
};
7108+
7109+
G_DEFINE_TYPE_WITH_PRIVATE(GArrowReplaceSliceOptions,
7110+
garrow_replace_slice_options,
7111+
GARROW_TYPE_FUNCTION_OPTIONS)
7112+
7113+
#define GARROW_REPLACE_SLICE_OPTIONS_GET_PRIVATE(object) \
7114+
static_cast<GArrowReplaceSliceOptionsPrivate *>( \
7115+
garrow_replace_slice_options_get_instance_private( \
7116+
GARROW_REPLACE_SLICE_OPTIONS(object)))
7117+
7118+
static void
7119+
garrow_replace_slice_options_dispose(GObject *object)
7120+
{
7121+
auto priv = GARROW_REPLACE_SLICE_OPTIONS_GET_PRIVATE(object);
7122+
if (priv->replacement) {
7123+
g_free(priv->replacement);
7124+
priv->replacement = nullptr;
7125+
}
7126+
G_OBJECT_CLASS(garrow_replace_slice_options_parent_class)->dispose(object);
7127+
}
7128+
7129+
static void
7130+
garrow_replace_slice_options_set_property(GObject *object,
7131+
guint prop_id,
7132+
const GValue *value,
7133+
GParamSpec *pspec)
7134+
{
7135+
auto options =
7136+
garrow_replace_slice_options_get_raw(GARROW_REPLACE_SLICE_OPTIONS(object));
7137+
auto priv = GARROW_REPLACE_SLICE_OPTIONS_GET_PRIVATE(object);
7138+
7139+
switch (prop_id) {
7140+
case PROP_REPLACE_SLICE_OPTIONS_START:
7141+
options->start = g_value_get_int64(value);
7142+
break;
7143+
case PROP_REPLACE_SLICE_OPTIONS_STOP:
7144+
options->stop = g_value_get_int64(value);
7145+
break;
7146+
case PROP_REPLACE_SLICE_OPTIONS_REPLACEMENT:
7147+
{
7148+
const gchar *replacement = g_value_get_string(value);
7149+
if (priv->replacement) {
7150+
g_free(priv->replacement);
7151+
}
7152+
priv->replacement = g_strdup(replacement);
7153+
options->replacement = replacement ? replacement : "";
7154+
}
7155+
break;
7156+
default:
7157+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
7158+
break;
7159+
}
7160+
}
7161+
7162+
static void
7163+
garrow_replace_slice_options_get_property(GObject *object,
7164+
guint prop_id,
7165+
GValue *value,
7166+
GParamSpec *pspec)
7167+
{
7168+
auto options =
7169+
garrow_replace_slice_options_get_raw(GARROW_REPLACE_SLICE_OPTIONS(object));
7170+
auto priv = GARROW_REPLACE_SLICE_OPTIONS_GET_PRIVATE(object);
7171+
7172+
switch (prop_id) {
7173+
case PROP_REPLACE_SLICE_OPTIONS_START:
7174+
g_value_set_int64(value, options->start);
7175+
break;
7176+
case PROP_REPLACE_SLICE_OPTIONS_STOP:
7177+
g_value_set_int64(value, options->stop);
7178+
break;
7179+
case PROP_REPLACE_SLICE_OPTIONS_REPLACEMENT:
7180+
g_value_set_string(value,
7181+
priv->replacement ? priv->replacement
7182+
: options->replacement.c_str());
7183+
break;
7184+
default:
7185+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
7186+
break;
7187+
}
7188+
}
7189+
7190+
static void
7191+
garrow_replace_slice_options_init(GArrowReplaceSliceOptions *object)
7192+
{
7193+
auto priv = GARROW_REPLACE_SLICE_OPTIONS_GET_PRIVATE(object);
7194+
priv->replacement = nullptr;
7195+
auto arrow_priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
7196+
arrow_priv->options = static_cast<arrow::compute::FunctionOptions *>(
7197+
new arrow::compute::ReplaceSliceOptions());
7198+
// Sync the private replacement string with the C++ options
7199+
auto arrow_options =
7200+
garrow_replace_slice_options_get_raw(GARROW_REPLACE_SLICE_OPTIONS(object));
7201+
priv->replacement = g_strdup(arrow_options->replacement.c_str());
7202+
}
7203+
7204+
static void
7205+
garrow_replace_slice_options_class_init(GArrowReplaceSliceOptionsClass *klass)
7206+
{
7207+
auto gobject_class = G_OBJECT_CLASS(klass);
7208+
7209+
gobject_class->dispose = garrow_replace_slice_options_dispose;
7210+
gobject_class->set_property = garrow_replace_slice_options_set_property;
7211+
gobject_class->get_property = garrow_replace_slice_options_get_property;
7212+
7213+
arrow::compute::ReplaceSliceOptions options;
7214+
7215+
GParamSpec *spec;
7216+
/**
7217+
* GArrowReplaceSliceOptions:start:
7218+
*
7219+
* Index to start slicing at.
7220+
*
7221+
* Since: 23.0.0
7222+
*/
7223+
spec = g_param_spec_int64("start",
7224+
"Start",
7225+
"Index to start slicing at",
7226+
G_MININT64,
7227+
G_MAXINT64,
7228+
options.start,
7229+
static_cast<GParamFlags>(G_PARAM_READWRITE));
7230+
g_object_class_install_property(gobject_class, PROP_REPLACE_SLICE_OPTIONS_START, spec);
7231+
7232+
/**
7233+
* GArrowReplaceSliceOptions:stop:
7234+
*
7235+
* Index to stop slicing at.
7236+
*
7237+
* Since: 23.0.0
7238+
*/
7239+
spec = g_param_spec_int64("stop",
7240+
"Stop",
7241+
"Index to stop slicing at",
7242+
G_MININT64,
7243+
G_MAXINT64,
7244+
options.stop,
7245+
static_cast<GParamFlags>(G_PARAM_READWRITE));
7246+
g_object_class_install_property(gobject_class, PROP_REPLACE_SLICE_OPTIONS_STOP, spec);
7247+
7248+
/**
7249+
* GArrowReplaceSliceOptions:replacement:
7250+
*
7251+
* String to replace the slice with.
7252+
*
7253+
* Since: 23.0.0
7254+
*/
7255+
spec = g_param_spec_string("replacement",
7256+
"Replacement",
7257+
"String to replace the slice with",
7258+
options.replacement.c_str(),
7259+
static_cast<GParamFlags>(G_PARAM_READWRITE));
7260+
g_object_class_install_property(gobject_class,
7261+
PROP_REPLACE_SLICE_OPTIONS_REPLACEMENT,
7262+
spec);
7263+
}
7264+
7265+
/**
7266+
* garrow_replace_slice_options_new:
7267+
*
7268+
* Returns: A newly created #GArrowReplaceSliceOptions.
7269+
*
7270+
* Since: 23.0.0
7271+
*/
7272+
GArrowReplaceSliceOptions *
7273+
garrow_replace_slice_options_new(void)
7274+
{
7275+
return GARROW_REPLACE_SLICE_OPTIONS(
7276+
g_object_new(GARROW_TYPE_REPLACE_SLICE_OPTIONS, NULL));
7277+
}
7278+
70947279
G_END_DECLS
70957280

70967281
arrow::Result<arrow::FieldRef>
@@ -7254,6 +7439,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt
72547439
static_cast<const arrow::compute::ExtractRegexOptions *>(arrow_options);
72557440
auto options = garrow_extract_regex_options_new_raw(arrow_extract_regex_options);
72567441
return GARROW_FUNCTION_OPTIONS(options);
7442+
} else if (arrow_type_name == "ReplaceSliceOptions") {
7443+
const auto arrow_replace_slice_options =
7444+
static_cast<const arrow::compute::ReplaceSliceOptions *>(arrow_options);
7445+
auto options = garrow_replace_slice_options_new_raw(arrow_replace_slice_options);
7446+
return GARROW_FUNCTION_OPTIONS(options);
72577447
} else {
72587448
auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
72597449
return GARROW_FUNCTION_OPTIONS(options);
@@ -7893,3 +8083,24 @@ garrow_extract_regex_options_get_raw(GArrowExtractRegexOptions *options)
78938083
return static_cast<arrow::compute::ExtractRegexOptions *>(
78948084
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
78958085
}
8086+
8087+
GArrowReplaceSliceOptions *
8088+
garrow_replace_slice_options_new_raw(
8089+
const arrow::compute::ReplaceSliceOptions *arrow_options)
8090+
{
8091+
return GARROW_REPLACE_SLICE_OPTIONS(g_object_new(GARROW_TYPE_REPLACE_SLICE_OPTIONS,
8092+
"start",
8093+
arrow_options->start,
8094+
"stop",
8095+
arrow_options->stop,
8096+
"replacement",
8097+
arrow_options->replacement.c_str(),
8098+
NULL));
8099+
}
8100+
8101+
arrow::compute::ReplaceSliceOptions *
8102+
garrow_replace_slice_options_get_raw(GArrowReplaceSliceOptions *options)
8103+
{
8104+
return static_cast<arrow::compute::ReplaceSliceOptions *>(
8105+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
8106+
}

c_glib/arrow-glib/compute.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,4 +1272,20 @@ GARROW_AVAILABLE_IN_23_0
12721272
GArrowExtractRegexOptions *
12731273
garrow_extract_regex_options_new(void);
12741274

1275+
#define GARROW_TYPE_REPLACE_SLICE_OPTIONS (garrow_replace_slice_options_get_type())
1276+
GARROW_AVAILABLE_IN_23_0
1277+
G_DECLARE_DERIVABLE_TYPE(GArrowReplaceSliceOptions,
1278+
garrow_replace_slice_options,
1279+
GARROW,
1280+
REPLACE_SLICE_OPTIONS,
1281+
GArrowFunctionOptions)
1282+
struct _GArrowReplaceSliceOptionsClass
1283+
{
1284+
GArrowFunctionOptionsClass parent_class;
1285+
};
1286+
1287+
GARROW_AVAILABLE_IN_23_0
1288+
GArrowReplaceSliceOptions *
1289+
garrow_replace_slice_options_new(void);
1290+
12751291
G_END_DECLS

c_glib/arrow-glib/compute.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,9 @@ garrow_extract_regex_options_new_raw(
209209
const arrow::compute::ExtractRegexOptions *arrow_options);
210210
arrow::compute::ExtractRegexOptions *
211211
garrow_extract_regex_options_get_raw(GArrowExtractRegexOptions *options);
212+
213+
GArrowReplaceSliceOptions *
214+
garrow_replace_slice_options_new_raw(
215+
const arrow::compute::ReplaceSliceOptions *arrow_options);
216+
arrow::compute::ReplaceSliceOptions *
217+
garrow_replace_slice_options_get_raw(GArrowReplaceSliceOptions *options);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 TestReplaceSliceOptions < Test::Unit::TestCase
19+
include Helper::Buildable
20+
21+
def setup
22+
@options = Arrow::ReplaceSliceOptions.new
23+
end
24+
25+
def test_start_property
26+
assert_equal(0, @options.start)
27+
@options.start = 1
28+
assert_equal(1, @options.start)
29+
end
30+
31+
def test_stop_property
32+
assert_equal(0, @options.stop)
33+
@options.stop = 2
34+
assert_equal(2, @options.stop)
35+
end
36+
37+
def test_replacement_property
38+
assert_equal("", @options.replacement)
39+
@options.replacement = "XX"
40+
assert_equal("XX", @options.replacement)
41+
end
42+
43+
def test_utf8_replace_slice_function
44+
args = [
45+
Arrow::ArrayDatum.new(build_string_array(["hello", "world"])),
46+
]
47+
@options.start = 1
48+
@options.stop = 3
49+
@options.replacement = "XX"
50+
utf8_replace_slice_function = Arrow::Function.find("utf8_replace_slice")
51+
result = utf8_replace_slice_function.execute(args, @options).value
52+
expected = build_string_array(["hXXlo", "wXXld"])
53+
assert_equal(expected, result)
54+
end
55+
end

0 commit comments

Comments
 (0)