Skip to content

Commit d11eb30

Browse files
stenlarssonkou
authored andcommitted
Add constand for unspecified stop
This also overrides the stop getter and setters in Ruby so that nil can be used instead.
1 parent 3b0dbf1 commit d11eb30

File tree

6 files changed

+106
-19
lines changed

6 files changed

+106
-19
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7527,7 +7527,7 @@ garrow_list_slice_options_set_property(GObject *object,
75277527
case PROP_LIST_SLICE_OPTIONS_STOP:
75287528
{
75297529
gint64 stop_value = g_value_get_int64(value);
7530-
if (stop_value == -1) {
7530+
if (stop_value == GARROW_LIST_SLICE_OPTIONS_STOP_UNSPECIFIED) {
75317531
options->stop = std::nullopt;
75327532
} else {
75337533
options->stop = stop_value;
@@ -7579,7 +7579,7 @@ garrow_list_slice_options_get_property(GObject *object,
75797579
if (options->stop.has_value()) {
75807580
g_value_set_int64(value, options->stop.value());
75817581
} else {
7582-
g_value_set_int64(value, -1); // Sentinel value for "not set"
7582+
g_value_set_int64(value, GARROW_LIST_SLICE_OPTIONS_STOP_UNSPECIFIED);
75837583
}
75847584
break;
75857585
case PROP_LIST_SLICE_OPTIONS_STEP:
@@ -7641,18 +7641,20 @@ garrow_list_slice_options_class_init(GArrowListSliceOptionsClass *klass)
76417641
/**
76427642
* GArrowListSliceOptions:stop:
76437643
*
7644-
* Optional stop of list slicing. If not set (value is -1), then slice to end.
7644+
* Optional stop of list slicing. If not set (value is
7645+
* %GARROW_LIST_SLICE_OPTIONS_STOP_UNSPECIFIED), then slice to end.
76457646
*
76467647
* Since: 23.0.0
76477648
*/
7648-
spec = g_param_spec_int64(
7649-
"stop",
7650-
"Stop",
7651-
"Optional stop of list slicing. If not set (value is -1), then slice to end",
7652-
-1, // Use -1 as sentinel for "not set"
7653-
G_MAXINT64,
7654-
-1, // Default to -1 (not set)
7655-
static_cast<GParamFlags>(G_PARAM_READWRITE));
7649+
spec =
7650+
g_param_spec_int64("stop",
7651+
"Stop",
7652+
"Optional stop of list slicing. If not set (value is "
7653+
"GARROW_LIST_SLICE_OPTIONS_STOP_UNSPECIFIED), then slice to end",
7654+
GARROW_LIST_SLICE_OPTIONS_STOP_UNSPECIFIED,
7655+
G_MAXINT64,
7656+
GARROW_LIST_SLICE_OPTIONS_STOP_UNSPECIFIED,
7657+
static_cast<GParamFlags>(G_PARAM_READWRITE));
76567658
g_object_class_install_property(gobject_class, PROP_LIST_SLICE_OPTIONS_STOP, spec);
76577659

76587660
/**
@@ -8586,7 +8588,7 @@ garrow_list_flatten_options_get_raw(GArrowListFlattenOptions *options)
85868588
GArrowListSliceOptions *
85878589
garrow_list_slice_options_new_raw(const arrow::compute::ListSliceOptions *arrow_options)
85888590
{
8589-
gint64 stop_value = -1;
8591+
gint64 stop_value = GARROW_LIST_SLICE_OPTIONS_STOP_UNSPECIFIED;
85908592
if (arrow_options->stop.has_value()) {
85918593
stop_value = arrow_options->stop.value();
85928594
}

c_glib/arrow-glib/compute.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,18 @@ typedef enum {
13591359
GARROW_LIST_SLICE_RETURN_FIXED_SIZE_LIST_TRUE,
13601360
} GArrowListSliceReturnFixedSizeList;
13611361

1362-
#define GARROW_TYPE_LIST_SLICE_OPTIONS (garrow_list_slice_options_get_type())
1362+
/**
1363+
* GARROW_LIST_SLICE_OPTIONS_STOP_UNSPECIFIED:
1364+
*
1365+
* Sentinel value for the stop property in #GArrowListSliceOptions indicating
1366+
* that the stop value is not set. When this value is used, the slice will
1367+
* continue to the end of the list.
1368+
*
1369+
* Since: 23.0.0
1370+
*/
1371+
#define GARROW_LIST_SLICE_OPTIONS_STOP_UNSPECIFIED -1
1372+
1373+
#define GARROW_TYPE_LIST_SLICE_OPTIONS (garrow_list_slice_options_get_type())
13631374
GARROW_AVAILABLE_IN_23_0
13641375
G_DECLARE_DERIVABLE_TYPE(GArrowListSliceOptions,
13651376
garrow_list_slice_options,

c_glib/test/test-list-slice-options.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,11 @@ def test_start_property
2929
end
3030

3131
def test_stop_property
32-
# Default is -1 (not set)
33-
assert_equal(-1, @options.stop)
32+
assert_equal(Arrow::LIST_SLICE_OPTIONS_STOP_UNSPECIFIED, @options.stop)
3433
@options.stop = 5
3534
assert_equal(5, @options.stop)
36-
# Setting to -1 means "not set"
37-
@options.stop = -1
38-
assert_equal(-1, @options.stop)
35+
@options.stop = Arrow::LIST_SLICE_OPTIONS_STOP_UNSPECIFIED
36+
assert_equal(LIST_SLICE_OPTIONS_STOP_UNSPECIFIED, @options.stop)
3937
end
4038

4139
def test_step_property
@@ -76,7 +74,7 @@ def test_list_slice_function_without_stop
7674
[[1, 2, 3], [4, 5]])),
7775
]
7876
@options.start = 1
79-
@options.stop = -1
77+
@options.stop = Arrow::LIST_SLICE_OPTIONS_STOP_UNSPECIFIED
8078
list_slice_function = Arrow::Function.find("list_slice")
8179
result = list_slice_function.execute(args, @options).value
8280
assert_equal(build_list_array(Arrow::Int8DataType.new, [[2, 3], [5]]),

ruby/red-arrow/lib/arrow/libraries.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
require_relative "large-list-data-type"
8080
require_relative "list-array-builder"
8181
require_relative "list-data-type"
82+
require_relative "list-slice-options"
8283
require_relative "map-array"
8384
require_relative "map-array-builder"
8485
require_relative "map-data-type"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
module Arrow
19+
class ListSliceOptions
20+
alias_method :stop_raw, :stop
21+
private :stop_raw
22+
23+
# Optional stop of list slicing. If set to nil, then slice to end.
24+
def stop
25+
stop_raw == LIST_SLICE_OPTIONS_STOP_UNSPECIFIED ? nil : stop_raw
26+
end
27+
28+
# Optional stop of list slicing. If set to nil, then slice to end.
29+
def stop=(stop)
30+
set_property(:stop, stop.nil? ? LIST_SLICE_OPTIONS_STOP_UNSPECIFIED : stop)
31+
end
32+
end
33+
end
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 TestListSliceOptions < Test::Unit::TestCase
19+
def setup
20+
@options = Arrow::ListSliceOptions.new
21+
end
22+
23+
def test_stop_property
24+
assert_equal(nil, @options.stop)
25+
@options.stop = 5
26+
assert_equal(5, @options.stop)
27+
@options.stop = nil
28+
assert_equal(nil, @options.stop)
29+
end
30+
31+
def test_list_slice_function_without_stop
32+
args = [
33+
Arrow::ArrayDatum.new(Arrow::ListArray.new([:list, :int8], [[1, 2, 3], [4, 5]])),
34+
]
35+
@options.start = 1
36+
@options.stop = nil
37+
list_slice_function = Arrow::Function.find("list_slice")
38+
result = list_slice_function.execute(args, @options).value
39+
assert_equal(Arrow::ListArray.new([:list, :int8], [[2, 3], [5]]),
40+
result)
41+
end
42+
end

0 commit comments

Comments
 (0)