Skip to content

Commit cb184c6

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

File tree

4 files changed

+270
-0
lines changed

4 files changed

+270
-0
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ G_BEGIN_DECLS
306306
* #GArrowPivotWiderOptions is a class to customize the `pivot_wider` and
307307
* `hash_pivot_wider` functions.
308308
*
309+
* #GArrowRankQuantileOptions is a class to customize the `rank_quantile` and
310+
* `rank_normal` functions.
311+
*
309312
* There are many functions to compute data on an array.
310313
*/
311314

@@ -8762,6 +8765,155 @@ garrow_pivot_wider_options_new(void)
87628765
g_object_new(GARROW_TYPE_PIVOT_WIDER_OPTIONS, nullptr));
87638766
}
87648767

8768+
enum {
8769+
PROP_RANK_QUANTILE_OPTIONS_NULL_PLACEMENT = 1,
8770+
};
8771+
8772+
G_DEFINE_TYPE(GArrowRankQuantileOptions,
8773+
garrow_rank_quantile_options,
8774+
GARROW_TYPE_FUNCTION_OPTIONS)
8775+
8776+
static void
8777+
garrow_rank_quantile_options_set_property(GObject *object,
8778+
guint prop_id,
8779+
const GValue *value,
8780+
GParamSpec *pspec)
8781+
{
8782+
auto options =
8783+
garrow_rank_quantile_options_get_raw(GARROW_RANK_QUANTILE_OPTIONS(object));
8784+
8785+
switch (prop_id) {
8786+
case PROP_RANK_QUANTILE_OPTIONS_NULL_PLACEMENT:
8787+
options->null_placement =
8788+
static_cast<arrow::compute::NullPlacement>(g_value_get_enum(value));
8789+
break;
8790+
default:
8791+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
8792+
break;
8793+
}
8794+
}
8795+
8796+
static void
8797+
garrow_rank_quantile_options_get_property(GObject *object,
8798+
guint prop_id,
8799+
GValue *value,
8800+
GParamSpec *pspec)
8801+
{
8802+
auto options =
8803+
garrow_rank_quantile_options_get_raw(GARROW_RANK_QUANTILE_OPTIONS(object));
8804+
8805+
switch (prop_id) {
8806+
case PROP_RANK_QUANTILE_OPTIONS_NULL_PLACEMENT:
8807+
g_value_set_enum(value, static_cast<GArrowNullPlacement>(options->null_placement));
8808+
break;
8809+
default:
8810+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
8811+
break;
8812+
}
8813+
}
8814+
8815+
static void
8816+
garrow_rank_quantile_options_init(GArrowRankQuantileOptions *object)
8817+
{
8818+
auto priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
8819+
priv->options = static_cast<arrow::compute::FunctionOptions *>(
8820+
new arrow::compute::RankQuantileOptions());
8821+
}
8822+
8823+
static void
8824+
garrow_rank_quantile_options_class_init(GArrowRankQuantileOptionsClass *klass)
8825+
{
8826+
auto gobject_class = G_OBJECT_CLASS(klass);
8827+
8828+
gobject_class->set_property = garrow_rank_quantile_options_set_property;
8829+
gobject_class->get_property = garrow_rank_quantile_options_get_property;
8830+
8831+
auto options = arrow::compute::RankQuantileOptions::Defaults();
8832+
8833+
GParamSpec *spec;
8834+
/**
8835+
* GArrowRankQuantileOptions:null-placement:
8836+
*
8837+
* Whether nulls and NaNs are placed at the start or at the end.
8838+
*
8839+
* Since: 23.0.0
8840+
*/
8841+
spec = g_param_spec_enum("null-placement",
8842+
"Null placement",
8843+
"Whether nulls and NaNs are placed "
8844+
"at the start or at the end.",
8845+
GARROW_TYPE_NULL_PLACEMENT,
8846+
static_cast<GArrowNullPlacement>(options.null_placement),
8847+
static_cast<GParamFlags>(G_PARAM_READWRITE));
8848+
g_object_class_install_property(gobject_class,
8849+
PROP_RANK_QUANTILE_OPTIONS_NULL_PLACEMENT,
8850+
spec);
8851+
}
8852+
8853+
/**
8854+
* garrow_rank_quantile_options_new:
8855+
*
8856+
* Returns: A newly created #GArrowRankQuantileOptions.
8857+
*
8858+
* Since: 23.0.0
8859+
*/
8860+
GArrowRankQuantileOptions *
8861+
garrow_rank_quantile_options_new(void)
8862+
{
8863+
return GARROW_RANK_QUANTILE_OPTIONS(
8864+
g_object_new(GARROW_TYPE_RANK_QUANTILE_OPTIONS, nullptr));
8865+
}
8866+
8867+
/**
8868+
* garrow_rank_quantile_options_get_sort_keys:
8869+
* @options: A #GArrowRankQuantileOptions.
8870+
*
8871+
* Returns: (transfer full) (element-type GArrowSortKey):
8872+
* The sort keys to be used.
8873+
*
8874+
* Since: 23.0.0
8875+
*/
8876+
GList *
8877+
garrow_rank_quantile_options_get_sort_keys(GArrowRankQuantileOptions *options)
8878+
{
8879+
auto arrow_options = garrow_rank_quantile_options_get_raw(options);
8880+
return garrow_sort_keys_new_raw(arrow_options->sort_keys);
8881+
}
8882+
8883+
/**
8884+
* garrow_rank_quantile_options_set_sort_keys:
8885+
* @options: A #GArrowRankQuantileOptions.
8886+
* @sort_keys: (element-type GArrowSortKey): The sort keys to be used.
8887+
*
8888+
* Set sort keys to be used.
8889+
*
8890+
* Since: 23.0.0
8891+
*/
8892+
void
8893+
garrow_rank_quantile_options_set_sort_keys(GArrowRankQuantileOptions *options,
8894+
GList *sort_keys)
8895+
{
8896+
auto arrow_options = garrow_rank_quantile_options_get_raw(options);
8897+
garrow_raw_sort_keys_set(arrow_options->sort_keys, sort_keys);
8898+
}
8899+
8900+
/**
8901+
* garrow_rank_quantile_options_add_sort_key:
8902+
* @options: A #GArrowRankQuantileOptions.
8903+
* @sort_key: The sort key to be added.
8904+
*
8905+
* Add a sort key to be used.
8906+
*
8907+
* Since: 23.0.0
8908+
*/
8909+
void
8910+
garrow_rank_quantile_options_add_sort_key(GArrowRankQuantileOptions *options,
8911+
GArrowSortKey *sort_key)
8912+
{
8913+
auto arrow_options = garrow_rank_quantile_options_get_raw(options);
8914+
garrow_raw_sort_keys_add(arrow_options->sort_keys, sort_key);
8915+
}
8916+
87658917
G_END_DECLS
87668918

87678919
arrow::Result<arrow::FieldRef>
@@ -8981,6 +9133,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt
89819133
static_cast<const arrow::compute::PivotWiderOptions *>(arrow_options);
89829134
auto options = garrow_pivot_wider_options_new_raw(arrow_pivot_wider_options);
89839135
return GARROW_FUNCTION_OPTIONS(options);
9136+
} else if (arrow_type_name == "RankQuantileOptions") {
9137+
const auto arrow_rank_quantile_options =
9138+
static_cast<const arrow::compute::RankQuantileOptions *>(arrow_options);
9139+
auto options = garrow_rank_quantile_options_new_raw(arrow_rank_quantile_options);
9140+
return GARROW_FUNCTION_OPTIONS(options);
89849141
} else {
89859142
auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
89869143
return GARROW_FUNCTION_OPTIONS(options);
@@ -9870,3 +10027,24 @@ garrow_pivot_wider_options_get_raw(GArrowPivotWiderOptions *options)
987010027
return static_cast<arrow::compute::PivotWiderOptions *>(
987110028
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
987210029
}
10030+
10031+
GArrowRankQuantileOptions *
10032+
garrow_rank_quantile_options_new_raw(
10033+
const arrow::compute::RankQuantileOptions *arrow_options)
10034+
{
10035+
auto options =
10036+
GARROW_RANK_QUANTILE_OPTIONS(g_object_new(GARROW_TYPE_RANK_QUANTILE_OPTIONS,
10037+
"null-placement",
10038+
arrow_options->null_placement,
10039+
nullptr));
10040+
auto arrow_new_options = garrow_rank_quantile_options_get_raw(options);
10041+
arrow_new_options->sort_keys = arrow_options->sort_keys;
10042+
return options;
10043+
}
10044+
10045+
arrow::compute::RankQuantileOptions *
10046+
garrow_rank_quantile_options_get_raw(GArrowRankQuantileOptions *options)
10047+
{
10048+
return static_cast<arrow::compute::RankQuantileOptions *>(
10049+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
10050+
}

c_glib/arrow-glib/compute.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,4 +1540,30 @@ GARROW_AVAILABLE_IN_23_0
15401540
GArrowPivotWiderOptions *
15411541
garrow_pivot_wider_options_new(void);
15421542

1543+
#define GARROW_TYPE_RANK_QUANTILE_OPTIONS (garrow_rank_quantile_options_get_type())
1544+
GARROW_AVAILABLE_IN_23_0
1545+
G_DECLARE_DERIVABLE_TYPE(GArrowRankQuantileOptions,
1546+
garrow_rank_quantile_options,
1547+
GARROW,
1548+
RANK_QUANTILE_OPTIONS,
1549+
GArrowFunctionOptions)
1550+
struct _GArrowRankQuantileOptionsClass
1551+
{
1552+
GArrowFunctionOptionsClass parent_class;
1553+
};
1554+
1555+
GArrowRankQuantileOptions *
1556+
garrow_rank_quantile_options_new(void);
1557+
GARROW_AVAILABLE_IN_23_0
1558+
GList *
1559+
garrow_rank_quantile_options_get_sort_keys(GArrowRankQuantileOptions *options);
1560+
GARROW_AVAILABLE_IN_23_0
1561+
void
1562+
garrow_rank_quantile_options_set_sort_keys(GArrowRankQuantileOptions *options,
1563+
GList *sort_keys);
1564+
GARROW_AVAILABLE_IN_23_0
1565+
void
1566+
garrow_rank_quantile_options_add_sort_key(GArrowRankQuantileOptions *options,
1567+
GArrowSortKey *sort_key);
1568+
15431569
G_END_DECLS

c_glib/arrow-glib/compute.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,9 @@ garrow_pivot_wider_options_new_raw(
274274
const arrow::compute::PivotWiderOptions *arrow_options);
275275
arrow::compute::PivotWiderOptions *
276276
garrow_pivot_wider_options_get_raw(GArrowPivotWiderOptions *options);
277+
278+
GArrowRankQuantileOptions *
279+
garrow_rank_quantile_options_new_raw(
280+
const arrow::compute::RankQuantileOptions *arrow_options);
281+
arrow::compute::RankQuantileOptions *
282+
garrow_rank_quantile_options_get_raw(GArrowRankQuantileOptions *options);
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 TestRankQuantileOptions < Test::Unit::TestCase
19+
include Helper::Buildable
20+
21+
def setup
22+
@options = Arrow::RankQuantileOptions.new
23+
end
24+
25+
def test_sort_keys
26+
sort_keys = [
27+
Arrow::SortKey.new("column1", :ascending),
28+
Arrow::SortKey.new("column2", :descending),
29+
]
30+
@options.sort_keys = sort_keys
31+
assert_equal(sort_keys, @options.sort_keys)
32+
end
33+
34+
def test_add_sort_key
35+
@options.add_sort_key(Arrow::SortKey.new("column1", :ascending))
36+
@options.add_sort_key(Arrow::SortKey.new("column2", :descending))
37+
assert_equal([
38+
Arrow::SortKey.new("column1", :ascending),
39+
Arrow::SortKey.new("column2", :descending),
40+
],
41+
@options.sort_keys)
42+
end
43+
44+
def test_null_placement
45+
assert_equal(Arrow::NullPlacement::AT_END, @options.null_placement)
46+
@options.null_placement = :at_start
47+
assert_equal(Arrow::NullPlacement::AT_START, @options.null_placement)
48+
end
49+
50+
def test_rank_quantile_function
51+
args = [
52+
Arrow::ArrayDatum.new(build_int32_array([nil, 1, nil, 2, nil])),
53+
]
54+
@options.null_placement = :at_start
55+
rank_quantile_function = Arrow::Function.find("rank_quantile")
56+
result = rank_quantile_function.execute(args, @options).value
57+
expected = build_double_array([0.3, 0.7, 0.3, 0.9, 0.3])
58+
assert_equal(expected, result)
59+
end
60+
end

0 commit comments

Comments
 (0)