This is an automated email from the ASF dual-hosted git repository.
kou pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/master by this push:
new 4c2294c ARROW-15274: [Ruby] Improve Arrow::Function#execute usability
4c2294c is described below
commit 4c2294c7173abf6a9920f09520d8cbc56c361ddc
Author: Sutou Kouhei <[email protected]>
AuthorDate: Sun Jan 9 05:56:06 2022 +0900
ARROW-15274: [Ruby] Improve Arrow::Function#execute usability
* Raw Hash is accepted as options
* #call-able
Closes #12101 from kou/ruby-function-call
Authored-by: Sutou Kouhei <[email protected]>
Signed-off-by: Sutou Kouhei <[email protected]>
---
ruby/red-arrow/lib/arrow/function.rb | 52 +++++++++++++++++++++++++
ruby/red-arrow/lib/arrow/loader.rb | 2 +
ruby/red-arrow/lib/arrow/set-lookup-options.rb | 26 +++++++++++++
ruby/red-arrow/test/test-function.rb | 54 +++++++++++++++++++-------
4 files changed, 120 insertions(+), 14 deletions(-)
diff --git a/ruby/red-arrow/lib/arrow/function.rb
b/ruby/red-arrow/lib/arrow/function.rb
new file mode 100644
index 0000000..a253860
--- /dev/null
+++ b/ruby/red-arrow/lib/arrow/function.rb
@@ -0,0 +1,52 @@
+# 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.
+
+module Arrow
+ class Function
+ alias_method :execute_raw, :execute
+ def execute(args, options=nil, context=nil)
+ options = resolve_options(options)
+ execute_raw(args, options, context)
+ end
+ alias_method :call, :execute
+
+ private
+ def resolve_options(options)
+ return nil if options.nil?
+ return options if options.is_a?(FunctionOptions)
+
+ arrow_options_class = options_type&.to_class
+ if arrow_options_class
+ if arrow_options_class.respond_to?(:try_convert)
+ arrow_options = arrow_options_class.try_convert(options)
+ return arrow_options if arrow_options
+ end
+ arrow_options = (default_options || arrow_options_class.new)
+ else
+ arrow_options = default_options
+ end
+ return arrow_options if arrow_options.nil?
+
+ options.each do |key, value|
+ setter = :"#{key}="
+ next unless arrow_options.respond_to?(setter)
+ arrow_options.__send__(setter, value)
+ end
+ arrow_options
+ end
+ end
+end
diff --git a/ruby/red-arrow/lib/arrow/loader.rb
b/ruby/red-arrow/lib/arrow/loader.rb
index 3e9a976..fb11afa 100644
--- a/ruby/red-arrow/lib/arrow/loader.rb
+++ b/ruby/red-arrow/lib/arrow/loader.rb
@@ -76,6 +76,7 @@ module Arrow
require "arrow/file-system"
require "arrow/fixed-size-binary-array"
require "arrow/fixed-size-binary-array-builder"
+ require "arrow/function"
require "arrow/group"
require "arrow/list-array-builder"
require "arrow/list-data-type"
@@ -96,6 +97,7 @@ module Arrow
require "arrow/s3-global-options"
require "arrow/scalar"
require "arrow/schema"
+ require "arrow/set-lookup-options"
require "arrow/slicer"
require "arrow/sort-key"
require "arrow/sort-options"
diff --git a/ruby/red-arrow/lib/arrow/set-lookup-options.rb
b/ruby/red-arrow/lib/arrow/set-lookup-options.rb
new file mode 100644
index 0000000..6e40950
--- /dev/null
+++ b/ruby/red-arrow/lib/arrow/set-lookup-options.rb
@@ -0,0 +1,26 @@
+# 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.
+
+module Arrow
+ class SetLookupOptions
+ # TODO: Remove this when we require glib2 gem 3.5.0 or later.
+ undef_method :value_set=
+ def value_set=(value_set)
+ set_value_set(Datum.try_convert(value_set))
+ end
+ end
+end
diff --git a/ruby/red-arrow/test/test-function.rb
b/ruby/red-arrow/test/test-function.rb
index 95667e6..b4fc9af 100644
--- a/ruby/red-arrow/test/test-function.rb
+++ b/ruby/red-arrow/test/test-function.rb
@@ -116,12 +116,13 @@ class FunctionTest < Test::Unit::TestCase
cast_function = Arrow::Function.find("cast")
date = Date.new(2021, 6, 12)
args = [date]
- options = Arrow::CastOptions.new
- options.to_data_type = Arrow::TimestampDataType.new(:second)
+ options = {
+ to_data_type: Arrow::TimestampDataType.new(:second),
+ }
time = Time.utc(date.year,
date.month,
date.day)
- assert_equal(Arrow::TimestampScalar.new(options.to_data_type,
+ assert_equal(Arrow::TimestampScalar.new(options[:to_data_type],
time.to_i),
cast_function.execute(args, options).value)
end
@@ -132,9 +133,10 @@ class FunctionTest < Test::Unit::TestCase
# 00:10:00
60 * 10)
args = [arrow_time]
- options = Arrow::CastOptions.new
- options.to_data_type = Arrow::Time64DataType.new(:micro)
- assert_equal(Arrow::Time64Scalar.new(options.to_data_type,
+ options = {
+ to_data_type: Arrow::Time64DataType.new(:micro),
+ }
+ assert_equal(Arrow::Time64Scalar.new(options[:to_data_type],
# 00:10:00.000000
60 * 10 * 1000 * 1000),
cast_function.execute(args, options).value)
@@ -146,10 +148,11 @@ class FunctionTest < Test::Unit::TestCase
# 00:10:00.000000
60 * 10 * 1000 * 1000)
args = [arrow_time]
- options = Arrow::CastOptions.new
- options.to_data_type = Arrow::Time32DataType.new(:second)
- options.allow_time_truncate = true
- assert_equal(Arrow::Time32Scalar.new(options.to_data_type,
+ options = {
+ to_data_type: Arrow::Time32DataType.new(:second),
+ allow_time_truncate: true,
+ }
+ assert_equal(Arrow::Time32Scalar.new(options[:to_data_type],
# 00:10:00
60 * 10),
cast_function.execute(args, options).value)
@@ -159,18 +162,41 @@ class FunctionTest < Test::Unit::TestCase
cast_function = Arrow::Function.find("cast")
time = Time.utc(2021, 6, 12, 1, 2, 3, 1)
args = [time]
- options = Arrow::CastOptions.new
- options.to_data_type = Arrow::TimestampDataType.new(:second)
- options.allow_time_truncate = true
+ options = {
+ to_data_type: Arrow::TimestampDataType.new(:second),
+ allow_time_truncate: true,
+ }
time = Time.utc(time.year,
time.month,
time.day,
time.hour,
time.min,
time.sec)
- assert_equal(Arrow::TimestampScalar.new(options.to_data_type,
+ assert_equal(Arrow::TimestampScalar.new(options[:to_data_type],
time.to_i),
cast_function.execute(args, options).value)
end
+
+ test("SetLookupOptions") do
+ is_in_function = Arrow::Function.find("is_in")
+ args = [
+ Arrow::Int16Array.new([1, 0, 1, 2]),
+ ]
+ options = {
+ value_set: Arrow::Int16Array.new([2, 0]),
+ }
+ assert_equal(Arrow::BooleanArray.new([false, true, false, true]),
+ is_in_function.execute(args, options).value)
+ end
+ end
+
+ def test_call
+ or_function = Arrow::Function.find("or")
+ args = [
+ Arrow::BooleanArray.new([true, false, false]),
+ Arrow::BooleanArray.new([true, false, true]),
+ ]
+ assert_equal([true, false, true],
+ or_function.call(args).value.to_a)
end
end