Skip to content

Commit 73d8718

Browse files
authored
GH-48504: [GLib][Ruby] Add SelectKOptions (#48525)
### Rationale for this change The `SelectKOptions` class is not available in GLib/Ruby, and it is used together with the `select_k_unstable` compute function. ### What changes are included in this PR? This adds the `SelectKOptions` class to GLib. ### Are these changes tested? Yes, with Ruby unit tests. ### Are there any user-facing changes? Yes, a new class. * GitHub Issue: #48504 Authored-by: Sten Larsson <[email protected]> Signed-off-by: Sutou Kouhei <[email protected]>
1 parent 3946f88 commit 73d8718

File tree

4 files changed

+260
-0
lines changed

4 files changed

+260
-0
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ G_BEGIN_DECLS
318318
* #GArrowRoundTemporalOptions is a class to customize the `round_temporal`,
319319
* `floor_temporal`, and `ceil_temporal` functions.
320320
*
321+
* #GArrowSelectKOptions is a class to customize the
322+
* `select_k_unstable` function.
323+
*
321324
* There are many functions to compute data on an array.
322325
*/
323326

@@ -9358,6 +9361,146 @@ garrow_round_temporal_options_new(void)
93589361
g_object_new(GARROW_TYPE_ROUND_TEMPORAL_OPTIONS, nullptr));
93599362
}
93609363

9364+
enum {
9365+
PROP_SELECT_K_OPTIONS_K = 1,
9366+
};
9367+
9368+
G_DEFINE_TYPE(GArrowSelectKOptions, garrow_select_k_options, GARROW_TYPE_FUNCTION_OPTIONS)
9369+
9370+
static void
9371+
garrow_select_k_options_set_property(GObject *object,
9372+
guint prop_id,
9373+
const GValue *value,
9374+
GParamSpec *pspec)
9375+
{
9376+
auto options = garrow_select_k_options_get_raw(GARROW_SELECT_K_OPTIONS(object));
9377+
9378+
switch (prop_id) {
9379+
case PROP_SELECT_K_OPTIONS_K:
9380+
options->k = g_value_get_int64(value);
9381+
break;
9382+
default:
9383+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
9384+
break;
9385+
}
9386+
}
9387+
9388+
static void
9389+
garrow_select_k_options_get_property(GObject *object,
9390+
guint prop_id,
9391+
GValue *value,
9392+
GParamSpec *pspec)
9393+
{
9394+
auto options = garrow_select_k_options_get_raw(GARROW_SELECT_K_OPTIONS(object));
9395+
9396+
switch (prop_id) {
9397+
case PROP_SELECT_K_OPTIONS_K:
9398+
g_value_set_int64(value, options->k);
9399+
break;
9400+
default:
9401+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
9402+
break;
9403+
}
9404+
}
9405+
9406+
static void
9407+
garrow_select_k_options_init(GArrowSelectKOptions *object)
9408+
{
9409+
auto arrow_priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
9410+
arrow_priv->options =
9411+
static_cast<arrow::compute::FunctionOptions *>(new arrow::compute::SelectKOptions());
9412+
}
9413+
9414+
static void
9415+
garrow_select_k_options_class_init(GArrowSelectKOptionsClass *klass)
9416+
{
9417+
auto gobject_class = G_OBJECT_CLASS(klass);
9418+
9419+
gobject_class->set_property = garrow_select_k_options_set_property;
9420+
gobject_class->get_property = garrow_select_k_options_get_property;
9421+
9422+
arrow::compute::SelectKOptions options;
9423+
9424+
GParamSpec *spec;
9425+
/**
9426+
* GArrowSelectKOptions:k:
9427+
*
9428+
* The number of k elements to keep.
9429+
*
9430+
* Since: 23.0.0
9431+
*/
9432+
spec = g_param_spec_int64("k",
9433+
"K",
9434+
"The number of k elements to keep",
9435+
G_MININT64,
9436+
G_MAXINT64,
9437+
options.k,
9438+
static_cast<GParamFlags>(G_PARAM_READWRITE));
9439+
g_object_class_install_property(gobject_class, PROP_SELECT_K_OPTIONS_K, spec);
9440+
}
9441+
9442+
/**
9443+
* garrow_select_k_options_new:
9444+
*
9445+
* Returns: A newly created #GArrowSelectKOptions.
9446+
*
9447+
* Since: 23.0.0
9448+
*/
9449+
GArrowSelectKOptions *
9450+
garrow_select_k_options_new(void)
9451+
{
9452+
return GARROW_SELECT_K_OPTIONS(g_object_new(GARROW_TYPE_SELECT_K_OPTIONS, nullptr));
9453+
}
9454+
9455+
/**
9456+
* garrow_select_k_options_get_sort_keys:
9457+
* @options: A #GArrowSelectKOptions.
9458+
*
9459+
* Returns: (transfer full) (element-type GArrowSortKey):
9460+
* The sort keys to be used.
9461+
*
9462+
* Since: 23.0.0
9463+
*/
9464+
GList *
9465+
garrow_select_k_options_get_sort_keys(GArrowSelectKOptions *options)
9466+
{
9467+
auto arrow_options = garrow_select_k_options_get_raw(options);
9468+
return garrow_sort_keys_new_raw(arrow_options->sort_keys);
9469+
}
9470+
9471+
/**
9472+
* garrow_select_k_options_set_sort_keys:
9473+
* @options: A #GArrowSelectKOptions.
9474+
* @sort_keys: (element-type GArrowSortKey): The sort keys to be used.
9475+
*
9476+
* Set sort keys to be used.
9477+
*
9478+
* Since: 23.0.0
9479+
*/
9480+
void
9481+
garrow_select_k_options_set_sort_keys(GArrowSelectKOptions *options, GList *sort_keys)
9482+
{
9483+
auto arrow_options = garrow_select_k_options_get_raw(options);
9484+
garrow_raw_sort_keys_set(arrow_options->sort_keys, sort_keys);
9485+
}
9486+
9487+
/**
9488+
* garrow_select_k_options_add_sort_key:
9489+
* @options: A #GArrowSelectKOptions.
9490+
* @sort_key: The sort key to be added.
9491+
*
9492+
* Add a sort key to be used.
9493+
*
9494+
* Since: 23.0.0
9495+
*/
9496+
void
9497+
garrow_select_k_options_add_sort_key(GArrowSelectKOptions *options,
9498+
GArrowSortKey *sort_key)
9499+
{
9500+
auto arrow_options = garrow_select_k_options_get_raw(options);
9501+
garrow_raw_sort_keys_add(arrow_options->sort_keys, sort_key);
9502+
}
9503+
93619504
G_END_DECLS
93629505

93639506
arrow::Result<arrow::FieldRef>
@@ -9598,6 +9741,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt
95989741
static_cast<const arrow::compute::RoundTemporalOptions *>(arrow_options);
95999742
auto options = garrow_round_temporal_options_new_raw(arrow_round_temporal_options);
96009743
return GARROW_FUNCTION_OPTIONS(options);
9744+
} else if (arrow_type_name == "SelectKOptions") {
9745+
const auto arrow_select_k_options =
9746+
static_cast<const arrow::compute::SelectKOptions *>(arrow_options);
9747+
auto options = garrow_select_k_options_new_raw(arrow_select_k_options);
9748+
return GARROW_FUNCTION_OPTIONS(options);
96019749
} else {
96029750
auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
96039751
return GARROW_FUNCTION_OPTIONS(options);
@@ -10574,3 +10722,20 @@ garrow_round_temporal_options_get_raw(GArrowRoundTemporalOptions *options)
1057410722
return static_cast<arrow::compute::RoundTemporalOptions *>(
1057510723
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
1057610724
}
10725+
10726+
GArrowSelectKOptions *
10727+
garrow_select_k_options_new_raw(const arrow::compute::SelectKOptions *arrow_options)
10728+
{
10729+
auto options = GARROW_SELECT_K_OPTIONS(
10730+
g_object_new(GARROW_TYPE_SELECT_K_OPTIONS, "k", arrow_options->k, nullptr));
10731+
auto arrow_new_options = garrow_select_k_options_get_raw(options);
10732+
arrow_new_options->sort_keys = arrow_options->sort_keys;
10733+
return options;
10734+
}
10735+
10736+
arrow::compute::SelectKOptions *
10737+
garrow_select_k_options_get_raw(GArrowSelectKOptions *options)
10738+
{
10739+
return static_cast<arrow::compute::SelectKOptions *>(
10740+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
10741+
}

c_glib/arrow-glib/compute.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,4 +1649,33 @@ GARROW_AVAILABLE_IN_23_0
16491649
GArrowRoundTemporalOptions *
16501650
garrow_round_temporal_options_new(void);
16511651

1652+
#define GARROW_TYPE_SELECT_K_OPTIONS (garrow_select_k_options_get_type())
1653+
GARROW_AVAILABLE_IN_23_0
1654+
G_DECLARE_DERIVABLE_TYPE(GArrowSelectKOptions,
1655+
garrow_select_k_options,
1656+
GARROW,
1657+
SELECT_K_OPTIONS,
1658+
GArrowFunctionOptions)
1659+
struct _GArrowSelectKOptionsClass
1660+
{
1661+
GArrowFunctionOptionsClass parent_class;
1662+
};
1663+
1664+
GARROW_AVAILABLE_IN_23_0
1665+
GArrowSelectKOptions *
1666+
garrow_select_k_options_new(void);
1667+
1668+
GARROW_AVAILABLE_IN_23_0
1669+
GList *
1670+
garrow_select_k_options_get_sort_keys(GArrowSelectKOptions *options);
1671+
1672+
GARROW_AVAILABLE_IN_23_0
1673+
void
1674+
garrow_select_k_options_set_sort_keys(GArrowSelectKOptions *options, GList *sort_keys);
1675+
1676+
GARROW_AVAILABLE_IN_23_0
1677+
void
1678+
garrow_select_k_options_add_sort_key(GArrowSelectKOptions *options,
1679+
GArrowSortKey *sort_key);
1680+
16521681
G_END_DECLS

c_glib/arrow-glib/compute.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,8 @@ garrow_round_temporal_options_new_raw(
298298
const arrow::compute::RoundTemporalOptions *arrow_options);
299299
arrow::compute::RoundTemporalOptions *
300300
garrow_round_temporal_options_get_raw(GArrowRoundTemporalOptions *options);
301+
302+
GArrowSelectKOptions *
303+
garrow_select_k_options_new_raw(const arrow::compute::SelectKOptions *arrow_options);
304+
arrow::compute::SelectKOptions *
305+
garrow_select_k_options_get_raw(GArrowSelectKOptions *options);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 TestSelectKOptions < Test::Unit::TestCase
19+
include Helper::Buildable
20+
21+
def setup
22+
@options = Arrow::SelectKOptions.new
23+
end
24+
25+
def test_k
26+
assert_equal(-1, @options.k)
27+
@options.k = 3
28+
assert_equal(3, @options.k)
29+
end
30+
31+
def test_sort_keys
32+
sort_keys = [
33+
Arrow::SortKey.new("column1", :ascending),
34+
Arrow::SortKey.new("column2", :descending),
35+
]
36+
@options.sort_keys = sort_keys
37+
assert_equal(sort_keys, @options.sort_keys)
38+
end
39+
40+
def test_add_sort_key
41+
@options.add_sort_key(Arrow::SortKey.new("column1", :ascending))
42+
@options.add_sort_key(Arrow::SortKey.new("column2", :descending))
43+
assert_equal([
44+
Arrow::SortKey.new("column1", :ascending),
45+
Arrow::SortKey.new("column2", :descending),
46+
],
47+
@options.sort_keys)
48+
end
49+
50+
def test_select_k_unstable_function
51+
input_array = build_int32_array([5, 2, 8, 1, 9, 3])
52+
args = [
53+
Arrow::ArrayDatum.new(input_array),
54+
]
55+
@options.k = 3
56+
@options.add_sort_key(Arrow::SortKey.new("dummy", :descending))
57+
select_k_unstable_function = Arrow::Function.find("select_k_unstable")
58+
result = select_k_unstable_function.execute(args, @options).value
59+
assert_equal(build_uint64_array([4, 2, 0]), result)
60+
end
61+
end

0 commit comments

Comments
 (0)