Skip to content

Commit 7d217af

Browse files
committed
Add GArrowJoinOptions
1 parent 4127ca2 commit 7d217af

File tree

4 files changed

+247
-0
lines changed

4 files changed

+247
-0
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 146 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+
* #GArrowJoinOptions is a class to customize the `binary_join_element_wise`
273+
* function.
274+
*
272275
* There are many functions to compute data on an array.
273276
*/
274277

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

7097+
enum {
7098+
PROP_JOIN_OPTIONS_NULL_HANDLING = 1,
7099+
PROP_JOIN_OPTIONS_NULL_REPLACEMENT,
7100+
};
7101+
7102+
G_DEFINE_TYPE(GArrowJoinOptions, garrow_join_options, GARROW_TYPE_FUNCTION_OPTIONS)
7103+
7104+
static void
7105+
garrow_join_options_set_property(GObject *object,
7106+
guint prop_id,
7107+
const GValue *value,
7108+
GParamSpec *pspec)
7109+
{
7110+
auto options = garrow_join_options_get_raw(GARROW_JOIN_OPTIONS(object));
7111+
7112+
switch (prop_id) {
7113+
case PROP_JOIN_OPTIONS_NULL_HANDLING:
7114+
options->null_handling =
7115+
static_cast<arrow::compute::JoinOptions::NullHandlingBehavior>(
7116+
g_value_get_enum(value));
7117+
break;
7118+
case PROP_JOIN_OPTIONS_NULL_REPLACEMENT:
7119+
options->null_replacement = g_value_get_string(value);
7120+
break;
7121+
default:
7122+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
7123+
break;
7124+
}
7125+
}
7126+
7127+
static void
7128+
garrow_join_options_get_property(GObject *object,
7129+
guint prop_id,
7130+
GValue *value,
7131+
GParamSpec *pspec)
7132+
{
7133+
auto options = garrow_join_options_get_raw(GARROW_JOIN_OPTIONS(object));
7134+
7135+
switch (prop_id) {
7136+
case PROP_JOIN_OPTIONS_NULL_HANDLING:
7137+
g_value_set_enum(value,
7138+
static_cast<GArrowJoinNullHandlingBehavior>(options->null_handling));
7139+
break;
7140+
case PROP_JOIN_OPTIONS_NULL_REPLACEMENT:
7141+
g_value_set_string(value, options->null_replacement.c_str());
7142+
break;
7143+
default:
7144+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
7145+
break;
7146+
}
7147+
}
7148+
7149+
static void
7150+
garrow_join_options_init(GArrowJoinOptions *object)
7151+
{
7152+
auto priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
7153+
priv->options =
7154+
static_cast<arrow::compute::FunctionOptions *>(new arrow::compute::JoinOptions());
7155+
}
7156+
7157+
static void
7158+
garrow_join_options_class_init(GArrowJoinOptionsClass *klass)
7159+
{
7160+
auto gobject_class = G_OBJECT_CLASS(klass);
7161+
7162+
gobject_class->set_property = garrow_join_options_set_property;
7163+
gobject_class->get_property = garrow_join_options_get_property;
7164+
7165+
arrow::compute::JoinOptions options;
7166+
7167+
GParamSpec *spec;
7168+
/**
7169+
* GArrowJoinOptions:null-handling:
7170+
*
7171+
* How to handle null values. (A null separator always results in a null output.)
7172+
*
7173+
* Since: 23.0.0
7174+
*/
7175+
spec =
7176+
g_param_spec_enum("null-handling",
7177+
"Null handling",
7178+
"How to handle null values",
7179+
GARROW_TYPE_JOIN_NULL_HANDLING_BEHAVIOR,
7180+
static_cast<GArrowJoinNullHandlingBehavior>(options.null_handling),
7181+
static_cast<GParamFlags>(G_PARAM_READWRITE));
7182+
g_object_class_install_property(gobject_class, PROP_JOIN_OPTIONS_NULL_HANDLING, spec);
7183+
7184+
/**
7185+
* GArrowJoinOptions:null-replacement:
7186+
*
7187+
* Replacement string for null values when null-handling is REPLACE.
7188+
*
7189+
* Since: 23.0.0
7190+
*/
7191+
spec = g_param_spec_string(
7192+
"null-replacement",
7193+
"Null replacement",
7194+
"Replacement string for null values when null-handling is REPLACE",
7195+
options.null_replacement.c_str(),
7196+
static_cast<GParamFlags>(G_PARAM_READWRITE));
7197+
g_object_class_install_property(gobject_class,
7198+
PROP_JOIN_OPTIONS_NULL_REPLACEMENT,
7199+
spec);
7200+
}
7201+
7202+
/**
7203+
* garrow_join_options_new:
7204+
*
7205+
* Returns: A newly created #GArrowJoinOptions.
7206+
*
7207+
* Since: 23.0.0
7208+
*/
7209+
GArrowJoinOptions *
7210+
garrow_join_options_new(void)
7211+
{
7212+
auto options = g_object_new(GARROW_TYPE_JOIN_OPTIONS, NULL);
7213+
return GARROW_JOIN_OPTIONS(options);
7214+
}
7215+
70947216
G_END_DECLS
70957217

70967218
arrow::Result<arrow::FieldRef>
@@ -7254,6 +7376,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt
72547376
static_cast<const arrow::compute::ExtractRegexOptions *>(arrow_options);
72557377
auto options = garrow_extract_regex_options_new_raw(arrow_extract_regex_options);
72567378
return GARROW_FUNCTION_OPTIONS(options);
7379+
} else if (arrow_type_name == "JoinOptions") {
7380+
const auto arrow_join_options =
7381+
static_cast<const arrow::compute::JoinOptions *>(arrow_options);
7382+
auto options = garrow_join_options_new_raw(arrow_join_options);
7383+
return GARROW_FUNCTION_OPTIONS(options);
72577384
} else {
72587385
auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
72597386
return GARROW_FUNCTION_OPTIONS(options);
@@ -7893,3 +8020,22 @@ garrow_extract_regex_options_get_raw(GArrowExtractRegexOptions *options)
78938020
return static_cast<arrow::compute::ExtractRegexOptions *>(
78948021
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
78958022
}
8023+
8024+
GArrowJoinOptions *
8025+
garrow_join_options_new_raw(const arrow::compute::JoinOptions *arrow_options)
8026+
{
8027+
return GARROW_JOIN_OPTIONS(g_object_new(
8028+
GARROW_TYPE_JOIN_OPTIONS,
8029+
"null-handling",
8030+
static_cast<GArrowJoinNullHandlingBehavior>(arrow_options->null_handling),
8031+
"null-replacement",
8032+
arrow_options->null_replacement.c_str(),
8033+
NULL));
8034+
}
8035+
8036+
arrow::compute::JoinOptions *
8037+
garrow_join_options_get_raw(GArrowJoinOptions *options)
8038+
{
8039+
return static_cast<arrow::compute::JoinOptions *>(
8040+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
8041+
}

c_glib/arrow-glib/compute.h

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

1275+
/**
1276+
* GArrowJoinNullHandlingBehavior:
1277+
* @GARROW_JOIN_NULL_HANDLING_EMIT_NULL: A null in any input results in a null in the
1278+
* output.
1279+
* @GARROW_JOIN_NULL_HANDLING_SKIP: Nulls in inputs are skipped.
1280+
* @GARROW_JOIN_NULL_HANDLING_REPLACE: Nulls in inputs are replaced with the replacement
1281+
* string.
1282+
*
1283+
* They correspond to the values of
1284+
* `arrow::compute::JoinOptions::NullHandlingBehavior`.
1285+
*
1286+
* Since: 23.0.0
1287+
*/
1288+
typedef enum {
1289+
GARROW_JOIN_NULL_HANDLING_EMIT_NULL,
1290+
GARROW_JOIN_NULL_HANDLING_SKIP,
1291+
GARROW_JOIN_NULL_HANDLING_REPLACE,
1292+
} GArrowJoinNullHandlingBehavior;
1293+
1294+
#define GARROW_TYPE_JOIN_OPTIONS (garrow_join_options_get_type())
1295+
GARROW_AVAILABLE_IN_23_0
1296+
G_DECLARE_DERIVABLE_TYPE(
1297+
GArrowJoinOptions, garrow_join_options, GARROW, JOIN_OPTIONS, GArrowFunctionOptions)
1298+
struct _GArrowJoinOptionsClass
1299+
{
1300+
GArrowFunctionOptionsClass parent_class;
1301+
};
1302+
1303+
GARROW_AVAILABLE_IN_23_0
1304+
GArrowJoinOptions *
1305+
garrow_join_options_new(void);
1306+
12751307
G_END_DECLS

c_glib/arrow-glib/compute.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,8 @@ 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+
GArrowJoinOptions *
214+
garrow_join_options_new_raw(const arrow::compute::JoinOptions *arrow_options);
215+
arrow::compute::JoinOptions *
216+
garrow_join_options_get_raw(GArrowJoinOptions *options);

c_glib/test/test-join-options.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 TestJoinOptions < Test::Unit::TestCase
19+
include Helper::Buildable
20+
21+
def setup
22+
@options = Arrow::JoinOptions.new
23+
end
24+
25+
def test_null_handling_property
26+
assert_equal(Arrow::JoinNullHandlingBehavior::EMIT_NULL, @options.null_handling)
27+
@options.null_handling = Arrow::JoinNullHandlingBehavior::SKIP
28+
assert_equal(Arrow::JoinNullHandlingBehavior::SKIP, @options.null_handling)
29+
@options.null_handling = Arrow::JoinNullHandlingBehavior::REPLACE
30+
assert_equal(Arrow::JoinNullHandlingBehavior::REPLACE, @options.null_handling)
31+
end
32+
33+
def test_null_replacement_property
34+
assert_equal("", @options.null_replacement)
35+
@options.null_replacement = "NULL"
36+
assert_equal("NULL", @options.null_replacement)
37+
end
38+
39+
def test_binary_join_element_wise_function
40+
args = [
41+
Arrow::ArrayDatum.new(build_string_array(["a", "b", nil])),
42+
Arrow::ArrayDatum.new(build_string_array(["x", "y", "z"])),
43+
Arrow::ScalarDatum.new(Arrow::StringScalar.new(Arrow::Buffer.new("-"))),
44+
]
45+
binary_join_element_wise_function = Arrow::Function.find("binary_join_element_wise")
46+
47+
@options.null_handling = Arrow::JoinNullHandlingBehavior::EMIT_NULL
48+
result = binary_join_element_wise_function.execute(args, @options).value
49+
assert_equal(build_string_array(["a-x", "b-y", nil]),
50+
result)
51+
52+
@options.null_handling = Arrow::JoinNullHandlingBehavior::SKIP
53+
result = binary_join_element_wise_function.execute(args, @options).value
54+
assert_equal(build_string_array(["a-x", "b-y", "z"]),
55+
result)
56+
57+
@options.null_handling = Arrow::JoinNullHandlingBehavior::REPLACE
58+
@options.null_replacement = "NULL"
59+
result = binary_join_element_wise_function.execute(args, @options).value
60+
assert_equal(build_string_array(["a-x", "b-y", "NULL-z"]),
61+
result)
62+
end
63+
end
64+

0 commit comments

Comments
 (0)