Skip to content

Commit 6456944

Browse files
authored
GH-48368: [GLib][Ruby] Add ExtractRegexOptions (#48375)
### Rationale for this change The `ExtractRegexOptions` class is not available in GLib/Ruby, and it is used together with the `extract_regex` compute function. ### What changes are included in this PR? This adds the `ExtractRegexOptions` class to GLib. ### Are these changes tested? Yes, with Ruby unit tests. ### Are there any user-facing changes? Yes, a new class. * GitHub Issue: #48368 Authored-by: Sten Larsson <sten@burtcorp.com> Signed-off-by: Sutou Kouhei <kou@clear-code.com>
1 parent 8974ddc commit 6456944

File tree

4 files changed

+192
-0
lines changed

4 files changed

+192
-0
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ G_BEGIN_DECLS
266266
*
267267
* #GArrowDayOfWeekOptions is a class to customize the `day_of_week` function.
268268
*
269+
* #GArrowExtractRegexOptions is a class to customize the `extract_regex`
270+
* function.
271+
*
269272
* There are many functions to compute data on an array.
270273
*/
271274

@@ -6992,6 +6995,102 @@ garrow_day_of_week_options_new(void)
69926995
return GARROW_DAY_OF_WEEK_OPTIONS(options);
69936996
}
69946997

6998+
enum {
6999+
PROP_EXTRACT_REGEX_OPTIONS_PATTERN = 1,
7000+
};
7001+
7002+
G_DEFINE_TYPE(GArrowExtractRegexOptions,
7003+
garrow_extract_regex_options,
7004+
GARROW_TYPE_FUNCTION_OPTIONS)
7005+
7006+
static void
7007+
garrow_extract_regex_options_set_property(GObject *object,
7008+
guint prop_id,
7009+
const GValue *value,
7010+
GParamSpec *pspec)
7011+
{
7012+
auto options =
7013+
garrow_extract_regex_options_get_raw(GARROW_EXTRACT_REGEX_OPTIONS(object));
7014+
7015+
switch (prop_id) {
7016+
case PROP_EXTRACT_REGEX_OPTIONS_PATTERN:
7017+
options->pattern = g_value_get_string(value);
7018+
break;
7019+
default:
7020+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
7021+
break;
7022+
}
7023+
}
7024+
7025+
static void
7026+
garrow_extract_regex_options_get_property(GObject *object,
7027+
guint prop_id,
7028+
GValue *value,
7029+
GParamSpec *pspec)
7030+
{
7031+
auto options =
7032+
garrow_extract_regex_options_get_raw(GARROW_EXTRACT_REGEX_OPTIONS(object));
7033+
7034+
switch (prop_id) {
7035+
case PROP_EXTRACT_REGEX_OPTIONS_PATTERN:
7036+
g_value_set_string(value, options->pattern.c_str());
7037+
break;
7038+
default:
7039+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
7040+
break;
7041+
}
7042+
}
7043+
7044+
static void
7045+
garrow_extract_regex_options_init(GArrowExtractRegexOptions *object)
7046+
{
7047+
auto priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
7048+
priv->options = static_cast<arrow::compute::FunctionOptions *>(
7049+
new arrow::compute::ExtractRegexOptions());
7050+
}
7051+
7052+
static void
7053+
garrow_extract_regex_options_class_init(GArrowExtractRegexOptionsClass *klass)
7054+
{
7055+
auto gobject_class = G_OBJECT_CLASS(klass);
7056+
7057+
gobject_class->set_property = garrow_extract_regex_options_set_property;
7058+
gobject_class->get_property = garrow_extract_regex_options_get_property;
7059+
7060+
arrow::compute::ExtractRegexOptions options;
7061+
7062+
GParamSpec *spec;
7063+
/**
7064+
* GArrowExtractRegexOptions:pattern:
7065+
*
7066+
* Regular expression with named capture fields.
7067+
*
7068+
* Since: 23.0.0
7069+
*/
7070+
spec = g_param_spec_string("pattern",
7071+
"Pattern",
7072+
"Regular expression with named capture fields",
7073+
options.pattern.c_str(),
7074+
static_cast<GParamFlags>(G_PARAM_READWRITE));
7075+
g_object_class_install_property(gobject_class,
7076+
PROP_EXTRACT_REGEX_OPTIONS_PATTERN,
7077+
spec);
7078+
}
7079+
7080+
/**
7081+
* garrow_extract_regex_options_new:
7082+
*
7083+
* Returns: A newly created #GArrowExtractRegexOptions.
7084+
*
7085+
* Since: 23.0.0
7086+
*/
7087+
GArrowExtractRegexOptions *
7088+
garrow_extract_regex_options_new(void)
7089+
{
7090+
auto options = g_object_new(GARROW_TYPE_EXTRACT_REGEX_OPTIONS, NULL);
7091+
return GARROW_EXTRACT_REGEX_OPTIONS(options);
7092+
}
7093+
69957094
G_END_DECLS
69967095

69977096
arrow::Result<arrow::FieldRef>
@@ -7150,6 +7249,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt
71507249
static_cast<const arrow::compute::DayOfWeekOptions *>(arrow_options);
71517250
auto options = garrow_day_of_week_options_new_raw(arrow_day_of_week_options);
71527251
return GARROW_FUNCTION_OPTIONS(options);
7252+
} else if (arrow_type_name == "ExtractRegexOptions") {
7253+
const auto arrow_extract_regex_options =
7254+
static_cast<const arrow::compute::ExtractRegexOptions *>(arrow_options);
7255+
auto options = garrow_extract_regex_options_new_raw(arrow_extract_regex_options);
7256+
return GARROW_FUNCTION_OPTIONS(options);
71537257
} else {
71547258
auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
71557259
return GARROW_FUNCTION_OPTIONS(options);
@@ -7772,3 +7876,20 @@ garrow_day_of_week_options_get_raw(GArrowDayOfWeekOptions *options)
77727876
return static_cast<arrow::compute::DayOfWeekOptions *>(
77737877
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
77747878
}
7879+
7880+
GArrowExtractRegexOptions *
7881+
garrow_extract_regex_options_new_raw(
7882+
const arrow::compute::ExtractRegexOptions *arrow_options)
7883+
{
7884+
return GARROW_EXTRACT_REGEX_OPTIONS(g_object_new(GARROW_TYPE_EXTRACT_REGEX_OPTIONS,
7885+
"pattern",
7886+
arrow_options->pattern.c_str(),
7887+
NULL));
7888+
}
7889+
7890+
arrow::compute::ExtractRegexOptions *
7891+
garrow_extract_regex_options_get_raw(GArrowExtractRegexOptions *options)
7892+
{
7893+
return static_cast<arrow::compute::ExtractRegexOptions *>(
7894+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
7895+
}

c_glib/arrow-glib/compute.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,4 +1256,20 @@ GARROW_AVAILABLE_IN_23_0
12561256
GArrowDayOfWeekOptions *
12571257
garrow_day_of_week_options_new(void);
12581258

1259+
#define GARROW_TYPE_EXTRACT_REGEX_OPTIONS (garrow_extract_regex_options_get_type())
1260+
GARROW_AVAILABLE_IN_23_0
1261+
G_DECLARE_DERIVABLE_TYPE(GArrowExtractRegexOptions,
1262+
garrow_extract_regex_options,
1263+
GARROW,
1264+
EXTRACT_REGEX_OPTIONS,
1265+
GArrowFunctionOptions)
1266+
struct _GArrowExtractRegexOptionsClass
1267+
{
1268+
GArrowFunctionOptionsClass parent_class;
1269+
};
1270+
1271+
GARROW_AVAILABLE_IN_23_0
1272+
GArrowExtractRegexOptions *
1273+
garrow_extract_regex_options_new(void);
1274+
12591275
G_END_DECLS

c_glib/arrow-glib/compute.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,9 @@ GArrowDayOfWeekOptions *
203203
garrow_day_of_week_options_new_raw(const arrow::compute::DayOfWeekOptions *arrow_options);
204204
arrow::compute::DayOfWeekOptions *
205205
garrow_day_of_week_options_get_raw(GArrowDayOfWeekOptions *options);
206+
207+
GArrowExtractRegexOptions *
208+
garrow_extract_regex_options_new_raw(
209+
const arrow::compute::ExtractRegexOptions *arrow_options);
210+
arrow::compute::ExtractRegexOptions *
211+
garrow_extract_regex_options_get_raw(GArrowExtractRegexOptions *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 TestExtractRegexOptions < Test::Unit::TestCase
19+
include Helper::Buildable
20+
21+
def setup
22+
@options = Arrow::ExtractRegexOptions.new
23+
end
24+
25+
def test_pattern_property
26+
assert_equal("", @options.pattern)
27+
@options.pattern = "(?P<year>\\d{4})-(?P<month>\\d{2})"
28+
assert_equal("(?P<year>\\d{4})-(?P<month>\\d{2})", @options.pattern)
29+
end
30+
31+
def test_extract_regex_function
32+
args = [
33+
Arrow::ArrayDatum.new(build_string_array(["2023-01-15", "2024-12-31"])),
34+
]
35+
@options.pattern = "(?P<year>\\d{4})-(?P<month>\\d{2})-(?P<day>\\d{2})"
36+
extract_regex_function = Arrow::Function.find("extract_regex")
37+
result = extract_regex_function.execute(args, @options).value
38+
fields = [
39+
Arrow::Field.new("year", Arrow::StringDataType.new),
40+
Arrow::Field.new("month", Arrow::StringDataType.new),
41+
Arrow::Field.new("day", Arrow::StringDataType.new),
42+
]
43+
assert_equal(build_struct_array(fields, [
44+
{"year" => "2023", "month" => "01", "day" => "15"},
45+
{"year" => "2024", "month" => "12", "day" => "31"},
46+
]),
47+
result)
48+
end
49+
end

0 commit comments

Comments
 (0)