Skip to content

Commit 3dc9821

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

File tree

7 files changed

+591
-0
lines changed

7 files changed

+591
-0
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,9 @@ G_BEGIN_DECLS
282282
* #GArrowMapLookupOptions is a class to customize the `map_lookup`
283283
* function.
284284
*
285+
* #GArrowListSliceOptions is a class to customize the `list_slice`
286+
* function.
287+
*
285288
* There are many functions to compute data on an array.
286289
*/
287290

@@ -7664,6 +7667,217 @@ garrow_map_lookup_options_new(GArrowScalar *query_key,
76647667
NULL));
76657668
}
76667669

7670+
enum {
7671+
PROP_LIST_SLICE_OPTIONS_START = 1,
7672+
PROP_LIST_SLICE_OPTIONS_STOP,
7673+
PROP_LIST_SLICE_OPTIONS_STEP,
7674+
PROP_LIST_SLICE_OPTIONS_RETURN_FIXED_SIZE_LIST,
7675+
};
7676+
7677+
G_DEFINE_TYPE(GArrowListSliceOptions,
7678+
garrow_list_slice_options,
7679+
GARROW_TYPE_FUNCTION_OPTIONS)
7680+
7681+
static void
7682+
garrow_list_slice_options_set_property(GObject *object,
7683+
guint prop_id,
7684+
const GValue *value,
7685+
GParamSpec *pspec)
7686+
{
7687+
auto options = garrow_list_slice_options_get_raw(GARROW_LIST_SLICE_OPTIONS(object));
7688+
7689+
switch (prop_id) {
7690+
case PROP_LIST_SLICE_OPTIONS_START:
7691+
options->start = g_value_get_int64(value);
7692+
break;
7693+
case PROP_LIST_SLICE_OPTIONS_STOP:
7694+
{
7695+
auto stop_value = g_value_get_int64(value);
7696+
if (stop_value == GARROW_LIST_SLICE_OPTIONS_STOP_UNSPECIFIED) {
7697+
options->stop = std::nullopt;
7698+
} else {
7699+
options->stop = stop_value;
7700+
}
7701+
}
7702+
break;
7703+
case PROP_LIST_SLICE_OPTIONS_STEP:
7704+
options->step = g_value_get_int64(value);
7705+
break;
7706+
case PROP_LIST_SLICE_OPTIONS_RETURN_FIXED_SIZE_LIST:
7707+
{
7708+
auto return_fixed_size_list_value =
7709+
static_cast<GArrowListSliceReturnFixedSizeList>(g_value_get_enum(value));
7710+
switch (return_fixed_size_list_value) {
7711+
case GARROW_LIST_SLICE_RETURN_FIXED_SIZE_LIST_AUTO:
7712+
options->return_fixed_size_list = std::nullopt;
7713+
break;
7714+
case GARROW_LIST_SLICE_RETURN_FIXED_SIZE_LIST_FALSE:
7715+
options->return_fixed_size_list = false;
7716+
break;
7717+
case GARROW_LIST_SLICE_RETURN_FIXED_SIZE_LIST_TRUE:
7718+
options->return_fixed_size_list = true;
7719+
break;
7720+
default:
7721+
options->return_fixed_size_list = std::nullopt;
7722+
break;
7723+
}
7724+
}
7725+
break;
7726+
default:
7727+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
7728+
break;
7729+
}
7730+
}
7731+
7732+
static void
7733+
garrow_list_slice_options_get_property(GObject *object,
7734+
guint prop_id,
7735+
GValue *value,
7736+
GParamSpec *pspec)
7737+
{
7738+
auto options = garrow_list_slice_options_get_raw(GARROW_LIST_SLICE_OPTIONS(object));
7739+
7740+
switch (prop_id) {
7741+
case PROP_LIST_SLICE_OPTIONS_START:
7742+
g_value_set_int64(value, options->start);
7743+
break;
7744+
case PROP_LIST_SLICE_OPTIONS_STOP:
7745+
if (options->stop.has_value()) {
7746+
g_value_set_int64(value, options->stop.value());
7747+
} else {
7748+
g_value_set_int64(value, GARROW_LIST_SLICE_OPTIONS_STOP_UNSPECIFIED);
7749+
}
7750+
break;
7751+
case PROP_LIST_SLICE_OPTIONS_STEP:
7752+
g_value_set_int64(value, options->step);
7753+
break;
7754+
case PROP_LIST_SLICE_OPTIONS_RETURN_FIXED_SIZE_LIST:
7755+
if (options->return_fixed_size_list.has_value()) {
7756+
if (options->return_fixed_size_list.value()) {
7757+
g_value_set_enum(value, GARROW_LIST_SLICE_RETURN_FIXED_SIZE_LIST_TRUE);
7758+
} else {
7759+
g_value_set_enum(value, GARROW_LIST_SLICE_RETURN_FIXED_SIZE_LIST_FALSE);
7760+
}
7761+
} else {
7762+
// When not set (nullopt), return AUTO (default)
7763+
g_value_set_enum(value, GARROW_LIST_SLICE_RETURN_FIXED_SIZE_LIST_AUTO);
7764+
}
7765+
break;
7766+
default:
7767+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
7768+
break;
7769+
}
7770+
}
7771+
7772+
static void
7773+
garrow_list_slice_options_init(GArrowListSliceOptions *object)
7774+
{
7775+
auto priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
7776+
priv->options = static_cast<arrow::compute::FunctionOptions *>(
7777+
new arrow::compute::ListSliceOptions());
7778+
}
7779+
7780+
static void
7781+
garrow_list_slice_options_class_init(GArrowListSliceOptionsClass *klass)
7782+
{
7783+
auto gobject_class = G_OBJECT_CLASS(klass);
7784+
7785+
gobject_class->set_property = garrow_list_slice_options_set_property;
7786+
gobject_class->get_property = garrow_list_slice_options_get_property;
7787+
7788+
arrow::compute::ListSliceOptions options;
7789+
7790+
GParamSpec *spec;
7791+
/**
7792+
* GArrowListSliceOptions:start:
7793+
*
7794+
* The start of list slicing.
7795+
*
7796+
* Since: 23.0.0
7797+
*/
7798+
spec = g_param_spec_int64("start",
7799+
"Start",
7800+
"The start of list slicing",
7801+
G_MININT64,
7802+
G_MAXINT64,
7803+
options.start,
7804+
static_cast<GParamFlags>(G_PARAM_READWRITE));
7805+
g_object_class_install_property(gobject_class, PROP_LIST_SLICE_OPTIONS_START, spec);
7806+
7807+
/**
7808+
* GArrowListSliceOptions:stop:
7809+
*
7810+
* Optional stop of list slicing. If not set (value is
7811+
* %GARROW_LIST_SLICE_OPTIONS_STOP_UNSPECIFIED), then slice to end.
7812+
*
7813+
* Since: 23.0.0
7814+
*/
7815+
spec =
7816+
g_param_spec_int64("stop",
7817+
"Stop",
7818+
"Optional stop of list slicing. If not set (value is "
7819+
"GARROW_LIST_SLICE_OPTIONS_STOP_UNSPECIFIED), then slice to end",
7820+
GARROW_LIST_SLICE_OPTIONS_STOP_UNSPECIFIED,
7821+
G_MAXINT64,
7822+
GARROW_LIST_SLICE_OPTIONS_STOP_UNSPECIFIED,
7823+
static_cast<GParamFlags>(G_PARAM_READWRITE));
7824+
g_object_class_install_property(gobject_class, PROP_LIST_SLICE_OPTIONS_STOP, spec);
7825+
7826+
/**
7827+
* GArrowListSliceOptions:step:
7828+
*
7829+
* Slicing step.
7830+
*
7831+
* Since: 23.0.0
7832+
*/
7833+
spec = g_param_spec_int64("step",
7834+
"Step",
7835+
"Slicing step",
7836+
G_MININT64,
7837+
G_MAXINT64,
7838+
options.step,
7839+
static_cast<GParamFlags>(G_PARAM_READWRITE));
7840+
g_object_class_install_property(gobject_class, PROP_LIST_SLICE_OPTIONS_STEP, spec);
7841+
7842+
/**
7843+
* GArrowListSliceOptions:return-fixed-size-list:
7844+
*
7845+
* Whether to return a FixedSizeListArray. If
7846+
* #GARROW_LIST_SLICE_RETURN_FIXED_SIZE_LIST_TRUE and stop is after a list element's
7847+
* length, nulls will be appended to create the requested slice size. If
7848+
* #GARROW_LIST_SLICE_RETURN_FIXED_SIZE_LIST_AUTO (default), will return whatever type
7849+
* it got in.
7850+
*
7851+
* Since: 23.0.0
7852+
*/
7853+
spec = g_param_spec_enum(
7854+
"return-fixed-size-list",
7855+
"Return fixed size list",
7856+
"Whether to return a FixedSizeListArray. If TRUE and stop is after a list element's "
7857+
"length, nulls will be appended to create the requested slice size. If AUTO "
7858+
"(default), will return whatever type it got in",
7859+
GARROW_TYPE_LIST_SLICE_RETURN_FIXED_SIZE_LIST,
7860+
GARROW_LIST_SLICE_RETURN_FIXED_SIZE_LIST_AUTO,
7861+
static_cast<GParamFlags>(G_PARAM_READWRITE));
7862+
g_object_class_install_property(gobject_class,
7863+
PROP_LIST_SLICE_OPTIONS_RETURN_FIXED_SIZE_LIST,
7864+
spec);
7865+
}
7866+
7867+
/**
7868+
* garrow_list_slice_options_new:
7869+
*
7870+
* Returns: A newly created #GArrowListSliceOptions.
7871+
*
7872+
* Since: 23.0.0
7873+
*/
7874+
GArrowListSliceOptions *
7875+
garrow_list_slice_options_new(void)
7876+
{
7877+
auto options = g_object_new(GARROW_TYPE_LIST_SLICE_OPTIONS, nullptr);
7878+
return GARROW_LIST_SLICE_OPTIONS(options);
7879+
}
7880+
76677881
G_END_DECLS
76687882

76697883
arrow::Result<arrow::FieldRef>
@@ -8566,3 +8780,38 @@ garrow_map_lookup_options_get_raw(GArrowMapLookupOptions *options)
85668780
return static_cast<arrow::compute::MapLookupOptions *>(
85678781
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
85688782
}
8783+
8784+
GArrowListSliceOptions *
8785+
garrow_list_slice_options_new_raw(const arrow::compute::ListSliceOptions *arrow_options)
8786+
{
8787+
gint64 stop_value = GARROW_LIST_SLICE_OPTIONS_STOP_UNSPECIFIED;
8788+
if (arrow_options->stop.has_value()) {
8789+
stop_value = arrow_options->stop.value();
8790+
}
8791+
GArrowListSliceReturnFixedSizeList return_fixed_size_list_value =
8792+
GARROW_LIST_SLICE_RETURN_FIXED_SIZE_LIST_AUTO;
8793+
if (arrow_options->return_fixed_size_list.has_value()) {
8794+
if (arrow_options->return_fixed_size_list.value()) {
8795+
return_fixed_size_list_value = GARROW_LIST_SLICE_RETURN_FIXED_SIZE_LIST_TRUE;
8796+
} else {
8797+
return_fixed_size_list_value = GARROW_LIST_SLICE_RETURN_FIXED_SIZE_LIST_FALSE;
8798+
}
8799+
}
8800+
return GARROW_LIST_SLICE_OPTIONS(g_object_new(GARROW_TYPE_LIST_SLICE_OPTIONS,
8801+
"start",
8802+
arrow_options->start,
8803+
"stop",
8804+
stop_value,
8805+
"step",
8806+
arrow_options->step,
8807+
"return-fixed-size-list",
8808+
return_fixed_size_list_value,
8809+
NULL));
8810+
}
8811+
8812+
arrow::compute::ListSliceOptions *
8813+
garrow_list_slice_options_get_raw(GArrowListSliceOptions *options)
8814+
{
8815+
return static_cast<arrow::compute::ListSliceOptions *>(
8816+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
8817+
}

c_glib/arrow-glib/compute.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,4 +1372,52 @@ GArrowMapLookupOptions *
13721372
garrow_map_lookup_options_new(GArrowScalar *query_key,
13731373
GArrowMapLookupOccurrence occurrence);
13741374

1375+
/**
1376+
* GArrowListSliceReturnFixedSizeList:
1377+
* @GARROW_LIST_SLICE_RETURN_FIXED_SIZE_LIST_AUTO: Return the same type which was passed
1378+
* in (default).
1379+
* @GARROW_LIST_SLICE_RETURN_FIXED_SIZE_LIST_FALSE: Explicitly return the same type which
1380+
* was passed in.
1381+
* @GARROW_LIST_SLICE_RETURN_FIXED_SIZE_LIST_TRUE: Return a FixedSizeListArray. If stop is
1382+
* after a list element's length, nulls will be appended to create the requested slice
1383+
* size.
1384+
*
1385+
* They correspond to the values of
1386+
* `std::optional<bool>` for `arrow::compute::ListSliceOptions::return_fixed_size_list`.
1387+
*
1388+
* Since: 23.0.0
1389+
*/
1390+
typedef enum {
1391+
GARROW_LIST_SLICE_RETURN_FIXED_SIZE_LIST_AUTO,
1392+
GARROW_LIST_SLICE_RETURN_FIXED_SIZE_LIST_FALSE,
1393+
GARROW_LIST_SLICE_RETURN_FIXED_SIZE_LIST_TRUE,
1394+
} GArrowListSliceReturnFixedSizeList;
1395+
1396+
/**
1397+
* GARROW_LIST_SLICE_OPTIONS_STOP_UNSPECIFIED:
1398+
*
1399+
* Sentinel value for the stop property in #GArrowListSliceOptions indicating
1400+
* that the stop value is not set. When this value is used, the slice will
1401+
* continue to the end of the list.
1402+
*
1403+
* Since: 23.0.0
1404+
*/
1405+
#define GARROW_LIST_SLICE_OPTIONS_STOP_UNSPECIFIED -1
1406+
1407+
#define GARROW_TYPE_LIST_SLICE_OPTIONS (garrow_list_slice_options_get_type())
1408+
GARROW_AVAILABLE_IN_23_0
1409+
G_DECLARE_DERIVABLE_TYPE(GArrowListSliceOptions,
1410+
garrow_list_slice_options,
1411+
GARROW,
1412+
LIST_SLICE_OPTIONS,
1413+
GArrowFunctionOptions)
1414+
struct _GArrowListSliceOptionsClass
1415+
{
1416+
GArrowFunctionOptionsClass parent_class;
1417+
};
1418+
1419+
GARROW_AVAILABLE_IN_23_0
1420+
GArrowListSliceOptions *
1421+
garrow_list_slice_options_new(void);
1422+
13751423
G_END_DECLS

c_glib/arrow-glib/compute.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,8 @@ GArrowMapLookupOptions *
231231
garrow_map_lookup_options_new_raw(const arrow::compute::MapLookupOptions *arrow_options);
232232
arrow::compute::MapLookupOptions *
233233
garrow_map_lookup_options_get_raw(GArrowMapLookupOptions *options);
234+
235+
GArrowListSliceOptions *
236+
garrow_list_slice_options_new_raw(const arrow::compute::ListSliceOptions *arrow_options);
237+
arrow::compute::ListSliceOptions *
238+
garrow_list_slice_options_get_raw(GArrowListSliceOptions *options);

0 commit comments

Comments
 (0)