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 0741175d12 ARROW-16875: [Ruby] Add Column#cast and ChunkedArray#cast 
(#13418)
0741175d12 is described below

commit 0741175d124ed1227d984a28db6592aec800aac8
Author: Sutou Kouhei <[email protected]>
AuthorDate: Thu Jun 23 05:58:30 2022 +0900

    ARROW-16875: [Ruby] Add Column#cast and ChunkedArray#cast (#13418)
    
    Authored-by: Sutou Kouhei <[email protected]>
    Signed-off-by: Sutou Kouhei <[email protected]>
---
 ruby/red-arrow/lib/arrow/array.rb         | 14 ++++++++++++++
 ruby/red-arrow/lib/arrow/chunked-array.rb |  7 +++++++
 ruby/red-arrow/lib/arrow/column.rb        |  4 ++++
 ruby/red-arrow/test/test-chunked-array.rb |  6 ++++++
 ruby/red-arrow/test/test-column.rb        | 16 +++++++++++-----
 5 files changed, 42 insertions(+), 5 deletions(-)

diff --git a/ruby/red-arrow/lib/arrow/array.rb 
b/ruby/red-arrow/lib/arrow/array.rb
index 5100674f14..d7ce4458b0 100644
--- a/ruby/red-arrow/lib/arrow/array.rb
+++ b/ruby/red-arrow/lib/arrow/array.rb
@@ -36,6 +36,20 @@ module Arrow
         return nil unless const_defined?(builder_class_name)
         const_get(builder_class_name)
       end
+
+      # @api private
+      def try_convert(value)
+        case value
+        when ::Array
+          begin
+            new(value)
+          rescue ArgumentError
+            nil
+          end
+        else
+          nil
+        end
+      end
     end
 
     # @param i [Integer]
diff --git a/ruby/red-arrow/lib/arrow/chunked-array.rb 
b/ruby/red-arrow/lib/arrow/chunked-array.rb
index 4acb1eedef..fb18a1baff 100644
--- a/ruby/red-arrow/lib/arrow/chunked-array.rb
+++ b/ruby/red-arrow/lib/arrow/chunked-array.rb
@@ -101,5 +101,12 @@ module Arrow
     def unique
       compute("unique")
     end
+
+    def cast(target_data_type, options: nil)
+      casted_chunks = chunks.collect do |chunk|
+        chunk.cast(target_data_type, options)
+      end
+      self.class.new(casted_chunks)
+    end
   end
 end
diff --git a/ruby/red-arrow/lib/arrow/column.rb 
b/ruby/red-arrow/lib/arrow/column.rb
index 435be55c50..2e54bfc596 100644
--- a/ruby/red-arrow/lib/arrow/column.rb
+++ b/ruby/red-arrow/lib/arrow/column.rb
@@ -96,5 +96,9 @@ module Arrow
     def uniq
       @data.uniq
     end
+
+    def cast(target_data_type, options: nil)
+      @data.cast(target_data_type, options: options)
+    end
   end
 end
diff --git a/ruby/red-arrow/test/test-chunked-array.rb 
b/ruby/red-arrow/test/test-chunked-array.rb
index 3785e9868e..c08613823b 100644
--- a/ruby/red-arrow/test/test-chunked-array.rb
+++ b/ruby/red-arrow/test/test-chunked-array.rb
@@ -180,4 +180,10 @@ class ChunkedArrayTest < Test::Unit::TestCase
                    @chunked_array.take(indices))
     end
   end
+
+  test("#cast") do
+    chunked_array = Arrow::ChunkedArray.new([[1, nil, 3]])
+    assert_equal(Arrow::ChunkedArray.new([["1", nil, "3"]]),
+                 chunked_array.cast(:string))
+  end
 end
diff --git a/ruby/red-arrow/test/test-column.rb 
b/ruby/red-arrow/test/test-column.rb
index 53ec4a3123..f78377e363 100644
--- a/ruby/red-arrow/test/test-column.rb
+++ b/ruby/red-arrow/test/test-column.rb
@@ -92,26 +92,32 @@ class ColumnTest < Test::Unit::TestCase
 
   test("#count") do
     table = Arrow::Table.new("revenue" => [1, nil, 3])
-    assert_equal(2, table['revenue'].count)
+    assert_equal(2, table["revenue"].count)
   end
 
   test("#min") do
     table = Arrow::Table.new("revenue" => [1, 2, 3])
-    assert_equal(1, table['revenue'].min)
+    assert_equal(1, table["revenue"].min)
   end
 
   test("#max") do
     table = Arrow::Table.new("revenue" => [1, 2, 3])
-    assert_equal(3, table['revenue'].max)
+    assert_equal(3, table["revenue"].max)
   end
 
   test("#sum") do
     table = Arrow::Table.new("revenue" => [1, 2, 3])
-    assert_equal(6, table['revenue'].sum)
+    assert_equal(6, table["revenue"].sum)
   end
 
   test("#uniq") do
     table = Arrow::Table.new("revenue" => [1, 2, 2])
-    assert_equal([1, 2], table['revenue'].uniq)
+    assert_equal([1, 2], table["revenue"].uniq)
+  end
+
+  test("#cast") do
+    table = Arrow::Table.new("revenue" => [1, nil, 3])
+    assert_equal(Arrow::ChunkedArray.new([["1", nil, "3"]]),
+                 table["revenue"].cast(:string))
   end
 end

Reply via email to