Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 163 additions & 0 deletions c_glib/arrow-glib/compute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ G_BEGIN_DECLS
* #GArrowSortOptions is a class to customize the `sort_indices`
* function.
*
* #GArrowSetLookupOptions is a class to customize the `is_in` function
* and `index_in` function.
*
* There are many functions to compute data on an array.
*/

Expand Down Expand Up @@ -2417,6 +2420,157 @@ garrow_sort_options_set_sort_keys(GArrowSortOptions *options,
}


typedef struct GArrowSetLookupOptionsPrivate_ {
GArrowDatum *value_set;
} GArrowSetLookupOptionsPrivate;

enum {
PROP_SET_LOOKUP_OPTIONS_VALUE_SET = 1,
PROP_SET_LOOKUP_OPTIONS_SKIP_NULLS,
};

G_DEFINE_TYPE_WITH_PRIVATE(GArrowSetLookupOptions,
garrow_set_lookup_options,
GARROW_TYPE_FUNCTION_OPTIONS)

#define GARROW_SET_LOOKUP_OPTIONS_GET_PRIVATE(object) \
static_cast<GArrowSetLookupOptionsPrivate *>( \
garrow_set_lookup_options_get_instance_private( \
GARROW_SET_LOOKUP_OPTIONS(object)))

static void
garrow_set_lookup_options_dispose(GObject *object)
{
auto priv = GARROW_SET_LOOKUP_OPTIONS_GET_PRIVATE(object);

if (priv->value_set) {
g_object_unref(priv->value_set);
priv->value_set = NULL;
}

G_OBJECT_CLASS(garrow_set_lookup_options_parent_class)->dispose(object);
}

static void
garrow_set_lookup_options_set_property(GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
auto priv = GARROW_SET_LOOKUP_OPTIONS_GET_PRIVATE(object);
auto options =
garrow_set_lookup_options_get_raw(GARROW_SET_LOOKUP_OPTIONS(object));

switch (prop_id) {
case PROP_SET_LOOKUP_OPTIONS_VALUE_SET:
priv->value_set = GARROW_DATUM(g_value_dup_object(value));
options->value_set = garrow_datum_get_raw(priv->value_set);
break;
case PROP_SET_LOOKUP_OPTIONS_SKIP_NULLS:
options->skip_nulls = g_value_get_boolean(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}

static void
garrow_set_lookup_options_get_property(GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
auto priv = GARROW_SET_LOOKUP_OPTIONS_GET_PRIVATE(object);
auto options =
garrow_set_lookup_options_get_raw(GARROW_SET_LOOKUP_OPTIONS(object));

switch (prop_id) {
case PROP_SET_LOOKUP_OPTIONS_VALUE_SET:
g_value_set_object(value, priv->value_set);
break;
case PROP_SET_LOOKUP_OPTIONS_SKIP_NULLS:
g_value_set_boolean(value, options->skip_nulls);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}

static void
garrow_set_lookup_options_init(GArrowSetLookupOptions *object)
{
auto priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
priv->options = static_cast<arrow::compute::FunctionOptions *>(
new arrow::compute::SetLookupOptions());
}

static void
garrow_set_lookup_options_class_init(GArrowSetLookupOptionsClass *klass)
{
auto gobject_class = G_OBJECT_CLASS(klass);

gobject_class->dispose = garrow_set_lookup_options_dispose;
gobject_class->set_property = garrow_set_lookup_options_set_property;
gobject_class->get_property = garrow_set_lookup_options_get_property;


arrow::compute::SetLookupOptions options;

GParamSpec *spec;
/**
* GArrowSetLookupOptions:value-set:
*
* The set of values to look up input values into.
*
* Since: 6.0.0
*/
spec = g_param_spec_object("value-set",
"Value set",
"The set of values to look up input values into",
GARROW_TYPE_DATUM,
static_cast<GParamFlags>(G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property(gobject_class,
PROP_SET_LOOKUP_OPTIONS_VALUE_SET,
spec);

/**
* GArrowSetLookupOptions:skip-nulls:
*
* Whether NULLs are skipped or not.
*
* Since: 6.0.0
*/
spec = g_param_spec_boolean("skip-nulls",
"Skip NULLs",
"Whether NULLs are skipped or not",
options.skip_nulls,
static_cast<GParamFlags>(G_PARAM_READWRITE));
g_object_class_install_property(gobject_class,
PROP_SET_LOOKUP_OPTIONS_SKIP_NULLS,
spec);
}

/**
* garrow_set_lookup_options_new:
* @value_set: A #GArrowArrayDatum or #GArrowChunkedArrayDatum to be looked up.
*
* Returns: A newly created #GArrowSetLookupOptions.
*
* Since: 6.0.0
*/
GArrowSetLookupOptions *
garrow_set_lookup_options_new(GArrowDatum *value_set)
{
return GARROW_SET_LOOKUP_OPTIONS(
g_object_new(GARROW_TYPE_SET_LOOKUP_OPTIONS,
"value-set", value_set,
NULL));
}


/**
* garrow_array_cast:
* @array: A #GArrowArray.
Expand Down Expand Up @@ -3755,3 +3909,12 @@ garrow_sort_options_get_raw(GArrowSortOptions *options)
return static_cast<arrow::compute::SortOptions *>(
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
}

arrow::compute::SetLookupOptions *
garrow_set_lookup_options_get_raw(GArrowSetLookupOptions *options)
{
return static_cast<arrow::compute::SetLookupOptions *>(
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
}


16 changes: 16 additions & 0 deletions c_glib/arrow-glib/compute.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,22 @@ garrow_sort_options_add_sort_key(GArrowSortOptions *options,
GArrowSortKey *sort_key);


#define GARROW_TYPE_SET_LOOKUP_OPTIONS (garrow_set_lookup_options_get_type())
G_DECLARE_DERIVABLE_TYPE(GArrowSetLookupOptions,
garrow_set_lookup_options,
GARROW,
SET_LOOKUP_OPTIONS,
GArrowFunctionOptions)
struct _GArrowSetLookupOptionsClass
{
GArrowFunctionOptionsClass parent_class;
};

GARROW_AVAILABLE_IN_6_0
GArrowSetLookupOptions *
garrow_set_lookup_options_new(GArrowDatum *value_set);


GArrowArray *garrow_array_cast(GArrowArray *array,
GArrowDataType *target_data_type,
GArrowCastOptions *options,
Expand Down
4 changes: 4 additions & 0 deletions c_glib/arrow-glib/compute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,7 @@ garrow_sort_key_get_raw(GArrowSortKey *sort_key);

arrow::compute::SortOptions *
garrow_sort_options_get_raw(GArrowSortOptions *options);


arrow::compute::SetLookupOptions *
garrow_set_lookup_options_get_raw(GArrowSetLookupOptions *options);
24 changes: 24 additions & 0 deletions c_glib/test/test-is-in.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ def test_null_in_both
assert_equal(build_boolean_array([false, true, true, true]),
left.is_in(right))
end

def test_options
left = build_int16_array([1, 0, nil, 2])
right = build_int16_array([2, 0, nil])
is_in = Arrow::Function.find("is_in")
options = Arrow::SetLookupOptions.new(Arrow::ArrayDatum.new(right))
assert_equal(build_boolean_array([false, true, true, true]),
is_in.execute([Arrow::ArrayDatum.new(left)],
options).value)
end
end

sub_test_case("ChunkedArray") do
Expand Down Expand Up @@ -92,5 +102,19 @@ def test_null_in_both
assert_equal(build_boolean_array([false, true, true, true]),
left.is_in_chunked_array(right))
end

def test_options
left = build_int16_array([1, 0, nil, 2])
chunks = [
build_int16_array([2, 0]),
build_int16_array([3, nil])
]
right = Arrow::ChunkedArray.new(chunks)
is_in = Arrow::Function.find("is_in")
options = Arrow::SetLookupOptions.new(Arrow::ChunkedArrayDatum.new(right))
assert_equal(build_boolean_array([false, true, true, true]),
is_in.execute([Arrow::ArrayDatum.new(left)],
options).value)
end
end
end
43 changes: 43 additions & 0 deletions c_glib/test/test-set-lookup-options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

class TestSetLookupOptions < Test::Unit::TestCase
include Helper::Buildable

def test_new
value_set = Arrow::ArrayDatum.new(build_int8_array([1, 2, 3]))
options = Arrow::SetLookupOptions.new(value_set)
assert_equal(value_set, options.value_set)
end

sub_test_case("instance methods") do
def setup
value_set = Arrow::ArrayDatum.new(build_int8_array([1, 2, 3]))
@options = Arrow::SetLookupOptions.new(value_set)
end

def test_skip_nulls
assert do
not @options.skip_nulls?
end
@options.skip_nulls = true
assert do
@options.skip_nulls?
end
end
end
end
2 changes: 2 additions & 0 deletions ruby/red-arrow/lib/arrow/datum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class << self
# @api private
def try_convert(value)
case value
when Table
TableDatum.new(value)
when Array
ArrayDatum.new(value)
when ChunkedArray
Expand Down
Loading