You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by ko...@apache.org on 2018/11/30 01:34:22 UTC

[arrow] branch master updated: ARROW-3905: [Ruby] Add StructDataType#[]

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 69d207f  ARROW-3905: [Ruby] Add StructDataType#[]
69d207f is described below

commit 69d207ff446c76f78fe27b960e7ebe89a607d992
Author: Yosuke Shiro <yo...@gmail.com>
AuthorDate: Fri Nov 30 10:34:07 2018 +0900

    ARROW-3905: [Ruby] Add StructDataType#[]
    
    Author: Yosuke Shiro <yo...@gmail.com>
    
    Closes #3053 from shiro615/ruby-add-convenient-method-to-struct-data-type and squashes the following commits:
    
    fdaf7b8b <Yosuke Shiro> Rename SchemaContainable to FieldContainable
    9d24d267 <Yosuke Shiro> Use alias method for #
    aef063fc <Yosuke Shiro>  Add StructDataType#
---
 .../lib/arrow/{schema.rb => field-containable.rb}  |  6 +--
 ruby/red-arrow/lib/arrow/loader.rb                 |  1 +
 ruby/red-arrow/lib/arrow/schema.rb                 | 19 +++-----
 .../lib/arrow/{schema.rb => struct-data-type.rb}   | 21 +++------
 ruby/red-arrow/test/test-struct-data-type.rb       | 50 ++++++++++++++++++++++
 5 files changed, 65 insertions(+), 32 deletions(-)

diff --git a/ruby/red-arrow/lib/arrow/schema.rb b/ruby/red-arrow/lib/arrow/field-containable.rb
similarity index 93%
copy from ruby/red-arrow/lib/arrow/schema.rb
copy to ruby/red-arrow/lib/arrow/field-containable.rb
index 09713b6..1956dde 100644
--- a/ruby/red-arrow/lib/arrow/schema.rb
+++ b/ruby/red-arrow/lib/arrow/field-containable.rb
@@ -16,15 +16,15 @@
 # under the License.
 
 module Arrow
-  class Schema
-    def [](name_or_index)
+  module FieldContainable
+    def find_field(name_or_index)
       case name_or_index
       when String, Symbol
         name = name_or_index
         get_field_by_name(name)
       when Integer
         index = name_or_index
-        fields[index]
+        get_field(index)
       else
         message = "field name or index must be String, Symbol or Integer"
         message << ": <#{name_or_index.inspect}>"
diff --git a/ruby/red-arrow/lib/arrow/loader.rb b/ruby/red-arrow/lib/arrow/loader.rb
index 15dd025..736f25b 100644
--- a/ruby/red-arrow/lib/arrow/loader.rb
+++ b/ruby/red-arrow/lib/arrow/loader.rb
@@ -53,6 +53,7 @@ module Arrow
       require "arrow/schema"
       require "arrow/slicer"
       require "arrow/struct-array"
+      require "arrow/struct-data-type"
       require "arrow/table"
       require "arrow/table-formatter"
       require "arrow/table-list-formatter"
diff --git a/ruby/red-arrow/lib/arrow/schema.rb b/ruby/red-arrow/lib/arrow/schema.rb
index 09713b6..2e6bad2 100644
--- a/ruby/red-arrow/lib/arrow/schema.rb
+++ b/ruby/red-arrow/lib/arrow/schema.rb
@@ -15,21 +15,12 @@
 # specific language governing permissions and limitations
 # under the License.
 
+require "arrow/field-containable"
+
 module Arrow
   class Schema
-    def [](name_or_index)
-      case name_or_index
-      when String, Symbol
-        name = name_or_index
-        get_field_by_name(name)
-      when Integer
-        index = name_or_index
-        fields[index]
-      else
-        message = "field name or index must be String, Symbol or Integer"
-        message << ": <#{name_or_index.inspect}>"
-        raise ArgumentError, message
-      end
-    end
+    include FieldContainable
+
+    alias_method :[], :find_field
   end
 end
diff --git a/ruby/red-arrow/lib/arrow/schema.rb b/ruby/red-arrow/lib/arrow/struct-data-type.rb
similarity index 66%
copy from ruby/red-arrow/lib/arrow/schema.rb
copy to ruby/red-arrow/lib/arrow/struct-data-type.rb
index 09713b6..7a59f1f 100644
--- a/ruby/red-arrow/lib/arrow/schema.rb
+++ b/ruby/red-arrow/lib/arrow/struct-data-type.rb
@@ -15,21 +15,12 @@
 # specific language governing permissions and limitations
 # under the License.
 
+require "arrow/field-containable"
+
 module Arrow
-  class Schema
-    def [](name_or_index)
-      case name_or_index
-      when String, Symbol
-        name = name_or_index
-        get_field_by_name(name)
-      when Integer
-        index = name_or_index
-        fields[index]
-      else
-        message = "field name or index must be String, Symbol or Integer"
-        message << ": <#{name_or_index.inspect}>"
-        raise ArgumentError, message
-      end
-    end
+  class StructDataType
+    include FieldContainable
+
+    alias_method :[], :find_field
   end
 end
diff --git a/ruby/red-arrow/test/test-struct-data-type.rb b/ruby/red-arrow/test/test-struct-data-type.rb
new file mode 100644
index 0000000..c802c44
--- /dev/null
+++ b/ruby/red-arrow/test/test-struct-data-type.rb
@@ -0,0 +1,50 @@
+# 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 StructDataTypeTest < Test::Unit::TestCase
+  def setup
+    @count_field = Arrow::Field.new("count", :uint32)
+    @visible_field = Arrow::Field.new("visible", :boolean)
+    @data_type = Arrow::StructDataType.new([@count_field, @visible_field])
+  end
+
+  sub_test_case("#[]") do
+    test("[String]") do
+      assert_equal([@count_field, @visible_field],
+                   [@data_type["count"], @data_type["visible"]])
+    end
+
+    test("[Symbol]") do
+      assert_equal([@count_field, @visible_field],
+                   [@data_type[:count], @data_type[:visible]])
+    end
+
+    test("[Integer]") do
+      assert_equal([@count_field, @visible_field],
+                   [@data_type[0], @data_type[1]])
+    end
+
+    test("[invalid]") do
+      invalid = []
+      message = "field name or index must be String, Symbol or Integer"
+      message << ": <#{invalid.inspect}>"
+      assert_raise(ArgumentError.new(message)) do
+        @data_type[invalid]
+      end
+    end
+  end
+end