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 467241e  ARROW-4600: [Ruby] Arrow::DictionaryArray#[] returns 
dictionary value
467241e is described below

commit 467241e4b092784e6dd454427b56d58af4220702
Author: Sutou Kouhei <[email protected]>
AuthorDate: Tue Jul 7 04:56:33 2020 +0900

    ARROW-4600: [Ruby] Arrow::DictionaryArray#[] returns dictionary value
    
    Closes #7639 from kou/ruby-dictionary-array-value
    
    Authored-by: Sutou Kouhei <[email protected]>
    Signed-off-by: Sutou Kouhei <[email protected]>
---
 ruby/red-arrow/ext/arrow/converters.hpp      | 41 +++++++++++++++++++++++-----
 ruby/red-arrow/lib/arrow/dictionary-array.rb | 24 ++++++++++++++++
 ruby/red-arrow/lib/arrow/loader.rb           |  1 +
 ruby/red-arrow/test/test-dictionary-array.rb | 41 ++++++++++++++++++++++++++++
 4 files changed, 100 insertions(+), 7 deletions(-)

diff --git a/ruby/red-arrow/ext/arrow/converters.hpp 
b/ruby/red-arrow/ext/arrow/converters.hpp
index 3a19c8c..4f9e6c0 100644
--- a/ruby/red-arrow/ext/arrow/converters.hpp
+++ b/ruby/red-arrow/ext/arrow/converters.hpp
@@ -560,30 +560,57 @@ namespace red_arrow {
   public:
     explicit DictionaryArrayValueConverter(ArrayValueConverter* converter)
       : array_value_converter_(converter),
-        index_(0),
+        value_index_(0),
         result_(Qnil) {
     }
 
     VALUE convert(const arrow::DictionaryArray& array,
                   const int64_t index) {
-      index_ = index;
-      auto indices = array.indices().get();
-      check_status(indices->Accept(this),
+      value_index_ = array.GetValueIndex(index);
+      auto dictionary = array.dictionary().get();
+      check_status(dictionary->Accept(this),
                    "[raw-records][dictionary-array]");
       return result_;
     }
 
-    // TODO: Convert to real value.
 #define VISIT(TYPE)                                                     \
     arrow::Status Visit(const arrow::TYPE ## Array& array) override {   \
-      result_ = convert_value(array, index_);                           \
+      result_ = convert_value(array, value_index_);                     \
       return arrow::Status::OK();                                       \
       }
 
+    VISIT(Null)
+    VISIT(Boolean)
     VISIT(Int8)
     VISIT(Int16)
     VISIT(Int32)
     VISIT(Int64)
+    VISIT(UInt8)
+    VISIT(UInt16)
+    VISIT(UInt32)
+    VISIT(UInt64)
+    // TODO
+    // VISIT(HalfFloat)
+    VISIT(Float)
+    VISIT(Double)
+    VISIT(Binary)
+    VISIT(String)
+    VISIT(FixedSizeBinary)
+    VISIT(Date32)
+    VISIT(Date64)
+    VISIT(Time32)
+    VISIT(Time64)
+    VISIT(Timestamp)
+    // TODO
+    // VISIT(Interval)
+    VISIT(List)
+    VISIT(Struct)
+    VISIT(SparseUnion)
+    VISIT(DenseUnion)
+    VISIT(Dictionary)
+    VISIT(Decimal128)
+    // TODO
+    // VISIT(Extension)
 
 #undef VISIT
 
@@ -595,7 +622,7 @@ namespace red_arrow {
     }
 
     ArrayValueConverter* array_value_converter_;
-    int64_t index_;
+    int64_t value_index_;
     VALUE result_;
   };
 
diff --git a/ruby/red-arrow/lib/arrow/dictionary-array.rb 
b/ruby/red-arrow/lib/arrow/dictionary-array.rb
new file mode 100644
index 0000000..70591ab
--- /dev/null
+++ b/ruby/red-arrow/lib/arrow/dictionary-array.rb
@@ -0,0 +1,24 @@
+# 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 DictionaryArray
+    def get_value(i)
+      dictionary[indices[i]]
+    end
+  end
+end
diff --git a/ruby/red-arrow/lib/arrow/loader.rb 
b/ruby/red-arrow/lib/arrow/loader.rb
index c8724cf..8560b02 100644
--- a/ruby/red-arrow/lib/arrow/loader.rb
+++ b/ruby/red-arrow/lib/arrow/loader.rb
@@ -57,6 +57,7 @@ module Arrow
       require "arrow/decimal128-array-builder"
       require "arrow/decimal128-data-type"
       require "arrow/dense-union-data-type"
+      require "arrow/dictionary-array"
       require "arrow/dictionary-data-type"
       require "arrow/field"
       require "arrow/file-output-stream"
diff --git a/ruby/red-arrow/test/test-dictionary-array.rb 
b/ruby/red-arrow/test/test-dictionary-array.rb
new file mode 100644
index 0000000..83368e9
--- /dev/null
+++ b/ruby/red-arrow/test/test-dictionary-array.rb
@@ -0,0 +1,41 @@
+# 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 DictionaryArrayTest < Test::Unit::TestCase
+  sub_test_case("instance methods") do
+    def setup
+      @values = ["a", "b", "c", "b", "a"]
+      @string_array = Arrow::StringArray.new(@values)
+      @array = @string_array.dictionary_encode
+    end
+
+    test("#[]") do
+      assert_equal(@values, @array.to_a)
+    end
+
+    test("#get_value") do
+      assert_equal([
+                     @values[0],
+                     @values[3],
+                   ],
+                   [
+                     @array.get_value(0),
+                     @array.get_value(3),
+                   ])
+    end
+  end
+end

Reply via email to