Skip to content

Commit bd65669

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

File tree

4 files changed

+244
-0
lines changed

4 files changed

+244
-0
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ G_BEGIN_DECLS
321321
* #GArrowSelectKOptions is a class to customize the
322322
* `select_k_unstable` function.
323323
*
324+
* #GArrowSkewOptions is a class to customize the `skew` and
325+
* `kurtosis` functions.
326+
*
324327
* There are many functions to compute data on an array.
325328
*/
326329

@@ -9501,6 +9504,143 @@ garrow_select_k_options_add_sort_key(GArrowSelectKOptions *options,
95019504
garrow_raw_sort_keys_add(arrow_options->sort_keys, sort_key);
95029505
}
95039506

9507+
enum {
9508+
PROP_SKEW_OPTIONS_SKIP_NULLS = 1,
9509+
PROP_SKEW_OPTIONS_BIASED,
9510+
PROP_SKEW_OPTIONS_MIN_COUNT,
9511+
};
9512+
9513+
G_DEFINE_TYPE(GArrowSkewOptions, garrow_skew_options, GARROW_TYPE_FUNCTION_OPTIONS)
9514+
9515+
static void
9516+
garrow_skew_options_set_property(GObject *object,
9517+
guint prop_id,
9518+
const GValue *value,
9519+
GParamSpec *pspec)
9520+
{
9521+
auto options = garrow_skew_options_get_raw(GARROW_SKEW_OPTIONS(object));
9522+
9523+
switch (prop_id) {
9524+
case PROP_SKEW_OPTIONS_SKIP_NULLS:
9525+
options->skip_nulls = g_value_get_boolean(value);
9526+
break;
9527+
case PROP_SKEW_OPTIONS_BIASED:
9528+
options->biased = g_value_get_boolean(value);
9529+
break;
9530+
case PROP_SKEW_OPTIONS_MIN_COUNT:
9531+
options->min_count = g_value_get_uint(value);
9532+
break;
9533+
default:
9534+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
9535+
break;
9536+
}
9537+
}
9538+
9539+
static void
9540+
garrow_skew_options_get_property(GObject *object,
9541+
guint prop_id,
9542+
GValue *value,
9543+
GParamSpec *pspec)
9544+
{
9545+
auto options = garrow_skew_options_get_raw(GARROW_SKEW_OPTIONS(object));
9546+
9547+
switch (prop_id) {
9548+
case PROP_SKEW_OPTIONS_SKIP_NULLS:
9549+
g_value_set_boolean(value, options->skip_nulls);
9550+
break;
9551+
case PROP_SKEW_OPTIONS_BIASED:
9552+
g_value_set_boolean(value, options->biased);
9553+
break;
9554+
case PROP_SKEW_OPTIONS_MIN_COUNT:
9555+
g_value_set_uint(value, options->min_count);
9556+
break;
9557+
default:
9558+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
9559+
break;
9560+
}
9561+
}
9562+
9563+
static void
9564+
garrow_skew_options_init(GArrowSkewOptions *object)
9565+
{
9566+
auto priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
9567+
priv->options =
9568+
static_cast<arrow::compute::FunctionOptions *>(new arrow::compute::SkewOptions());
9569+
}
9570+
9571+
static void
9572+
garrow_skew_options_class_init(GArrowSkewOptionsClass *klass)
9573+
{
9574+
auto gobject_class = G_OBJECT_CLASS(klass);
9575+
9576+
gobject_class->set_property = garrow_skew_options_set_property;
9577+
gobject_class->get_property = garrow_skew_options_get_property;
9578+
9579+
arrow::compute::SkewOptions options;
9580+
9581+
GParamSpec *spec;
9582+
/**
9583+
* GArrowSkewOptions:skip-nulls:
9584+
*
9585+
* Whether NULLs are skipped or not.
9586+
*
9587+
* Since: 23.0.0
9588+
*/
9589+
spec = g_param_spec_boolean("skip-nulls",
9590+
"Skip NULLs",
9591+
"Whether NULLs are skipped or not",
9592+
options.skip_nulls,
9593+
static_cast<GParamFlags>(G_PARAM_READWRITE));
9594+
g_object_class_install_property(gobject_class, PROP_SKEW_OPTIONS_SKIP_NULLS, spec);
9595+
9596+
/**
9597+
* GArrowSkewOptions:biased:
9598+
*
9599+
* Whether the calculated value is biased.
9600+
* If false, the value computed includes a correction factor to reduce bias.
9601+
*
9602+
* Since: 23.0.0
9603+
*/
9604+
spec =
9605+
g_param_spec_boolean("biased",
9606+
"Biased",
9607+
"Whether the calculated value is biased. If false, the value "
9608+
"computed includes a correction factor to reduce bias",
9609+
options.biased,
9610+
static_cast<GParamFlags>(G_PARAM_READWRITE));
9611+
g_object_class_install_property(gobject_class, PROP_SKEW_OPTIONS_BIASED, spec);
9612+
9613+
/**
9614+
* GArrowSkewOptions:min-count:
9615+
*
9616+
* If less than this many non-null values are observed, emit null.
9617+
*
9618+
* Since: 23.0.0
9619+
*/
9620+
spec =
9621+
g_param_spec_uint("min-count",
9622+
"Min count",
9623+
"If less than this many non-null values are observed, emit null",
9624+
0,
9625+
G_MAXUINT,
9626+
options.min_count,
9627+
static_cast<GParamFlags>(G_PARAM_READWRITE));
9628+
g_object_class_install_property(gobject_class, PROP_SKEW_OPTIONS_MIN_COUNT, spec);
9629+
}
9630+
9631+
/**
9632+
* garrow_skew_options_new:
9633+
*
9634+
* Returns: A newly created #GArrowSkewOptions.
9635+
*
9636+
* Since: 23.0.0
9637+
*/
9638+
GArrowSkewOptions *
9639+
garrow_skew_options_new(void)
9640+
{
9641+
return GARROW_SKEW_OPTIONS(g_object_new(GARROW_TYPE_SKEW_OPTIONS, nullptr));
9642+
}
9643+
95049644
G_END_DECLS
95059645

95069646
arrow::Result<arrow::FieldRef>
@@ -9746,6 +9886,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt
97469886
static_cast<const arrow::compute::SelectKOptions *>(arrow_options);
97479887
auto options = garrow_select_k_options_new_raw(arrow_select_k_options);
97489888
return GARROW_FUNCTION_OPTIONS(options);
9889+
} else if (arrow_type_name == "SkewOptions") {
9890+
const auto arrow_skew_options =
9891+
static_cast<const arrow::compute::SkewOptions *>(arrow_options);
9892+
auto options = garrow_skew_options_new_raw(arrow_skew_options);
9893+
return GARROW_FUNCTION_OPTIONS(options);
97499894
} else {
97509895
auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
97519896
return GARROW_FUNCTION_OPTIONS(options);
@@ -10739,3 +10884,24 @@ garrow_select_k_options_get_raw(GArrowSelectKOptions *options)
1073910884
return static_cast<arrow::compute::SelectKOptions *>(
1074010885
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
1074110886
}
10887+
10888+
GArrowSkewOptions *
10889+
garrow_skew_options_new_raw(const arrow::compute::SkewOptions *arrow_options)
10890+
{
10891+
auto options = g_object_new(GARROW_TYPE_SKEW_OPTIONS,
10892+
"skip-nulls",
10893+
arrow_options->skip_nulls,
10894+
"biased",
10895+
arrow_options->biased,
10896+
"min-count",
10897+
arrow_options->min_count,
10898+
nullptr);
10899+
return GARROW_SKEW_OPTIONS(options);
10900+
}
10901+
10902+
arrow::compute::SkewOptions *
10903+
garrow_skew_options_get_raw(GArrowSkewOptions *options)
10904+
{
10905+
return static_cast<arrow::compute::SkewOptions *>(
10906+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
10907+
}

c_glib/arrow-glib/compute.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,4 +1678,17 @@ void
16781678
garrow_select_k_options_add_sort_key(GArrowSelectKOptions *options,
16791679
GArrowSortKey *sort_key);
16801680

1681+
#define GARROW_TYPE_SKEW_OPTIONS (garrow_skew_options_get_type())
1682+
GARROW_AVAILABLE_IN_23_0
1683+
G_DECLARE_DERIVABLE_TYPE(
1684+
GArrowSkewOptions, garrow_skew_options, GARROW, SKEW_OPTIONS, GArrowFunctionOptions)
1685+
struct _GArrowSkewOptionsClass
1686+
{
1687+
GArrowFunctionOptionsClass parent_class;
1688+
};
1689+
1690+
GARROW_AVAILABLE_IN_23_0
1691+
GArrowSkewOptions *
1692+
garrow_skew_options_new(void);
1693+
16811694
G_END_DECLS

c_glib/arrow-glib/compute.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,8 @@ GArrowSelectKOptions *
303303
garrow_select_k_options_new_raw(const arrow::compute::SelectKOptions *arrow_options);
304304
arrow::compute::SelectKOptions *
305305
garrow_select_k_options_get_raw(GArrowSelectKOptions *options);
306+
307+
GArrowSkewOptions *
308+
garrow_skew_options_new_raw(const arrow::compute::SkewOptions *arrow_options);
309+
arrow::compute::SkewOptions *
310+
garrow_skew_options_get_raw(GArrowSkewOptions *options);

c_glib/test/test-skew-options.rb

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 TestSkewOptions < Test::Unit::TestCase
19+
include Helper::Buildable
20+
21+
def setup
22+
@options = Arrow::SkewOptions.new
23+
end
24+
25+
def test_skip_nulls
26+
assert do
27+
@options.skip_nulls?
28+
end
29+
@options.skip_nulls = false
30+
assert do
31+
not @options.skip_nulls?
32+
end
33+
end
34+
35+
def test_biased
36+
assert do
37+
@options.biased?
38+
end
39+
@options.biased = false
40+
assert do
41+
not @options.biased?
42+
end
43+
end
44+
45+
def test_min_count
46+
assert_equal(0, @options.min_count)
47+
@options.min_count = 1
48+
assert_equal(1, @options.min_count)
49+
end
50+
51+
def test_skew_function
52+
args = [
53+
Arrow::ArrayDatum.new(build_double_array([1.0, 1.0, 2.0])),
54+
]
55+
@options.min_count = 4
56+
skew_function = Arrow::Function.find("skew")
57+
result = skew_function.execute(args, @options).value
58+
assert_equal(0.0, result.value)
59+
end
60+
end

0 commit comments

Comments
 (0)