You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by sh...@apache.org on 2019/11/29 23:33:46 UTC
[arrow] branch master updated: ARROW-7275: [Ruby] Add support for
Arrow::ListDataType.new(data_type)
This is an automated email from the ASF dual-hosted git repository.
shiro 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 03874ef ARROW-7275: [Ruby] Add support for Arrow::ListDataType.new(data_type)
03874ef is described below
commit 03874ef5c66f49370218ae60d048e7bf33561c6a
Author: Sutou Kouhei <ko...@clear-code.com>
AuthorDate: Sat Nov 30 08:33:25 2019 +0900
ARROW-7275: [Ruby] Add support for Arrow::ListDataType.new(data_type)
"item" is used as the default field name for this case.
Closes #5924 from kou/ruby-list-data-type-new and squashes the following commits:
0a4cfb7a8 <Sutou Kouhei> Add support for Arrow::ListDataType.new(data_type)
Authored-by: Sutou Kouhei <ko...@clear-code.com>
Signed-off-by: Yosuke Shiro <yo...@gmail.com>
---
ruby/red-arrow/lib/arrow/list-data-type.rb | 66 ++++++++++++++++++++++++++----
ruby/red-arrow/test/test-list-data-type.rb | 28 ++++++++++++-
2 files changed, 85 insertions(+), 9 deletions(-)
diff --git a/ruby/red-arrow/lib/arrow/list-data-type.rb b/ruby/red-arrow/lib/arrow/list-data-type.rb
index c097da4..cfcdd2a 100644
--- a/ruby/red-arrow/lib/arrow/list-data-type.rb
+++ b/ruby/red-arrow/lib/arrow/list-data-type.rb
@@ -53,16 +53,66 @@ module Arrow
#
# @example Create a list data type with field description
# Arrow::ListDataType.new(field: {name: "visible", type: :boolean})
- def initialize(field)
- if field.is_a?(Hash) and field.key?(:field)
- description = field
- field = description[:field]
- end
- if field.is_a?(Hash)
- field_description = field
- field = Field.new(field_description)
+ #
+ # @overload initialize(data_type)
+ #
+ # @param data_type [Arrow::DataType, String, Symbol,
+ # ::Array<String>, ::Array<Symbol>, Hash] The element data
+ # type of the list data type. A field is created with the
+ # default name `"item"` from the data type automatically.
+ #
+ # See {Arrow::DataType.resolve} how to specify data type.
+ #
+ # @example Create a list data type with {Arrow::DataType}
+ # Arrow::ListDataType.new(Arrow::BooleanDataType.new)
+ #
+ # @example Create a list data type with data type name as String
+ # Arrow::ListDataType.new("boolean")
+ #
+ # @example Create a list data type with data type name as Symbol
+ # Arrow::ListDataType.new(:boolean)
+ #
+ # @example Create a list data type with data type as Array
+ # Arrow::ListDataType.new([:time32, :milli])
+ def initialize(arg)
+ data_type = resolve_data_type(arg)
+ if data_type
+ field = Field.new(default_field_name, data_type)
+ else
+ field = resolve_field(arg)
end
initialize_raw(field)
end
+
+ private
+ def resolve_data_type(arg)
+ case arg
+ when DataType, String, Symbol, ::Array
+ DataType.resolve(arg)
+ when Hash
+ return nil if arg[:name]
+ return nil unless arg[:type]
+ DataType.resolve(arg)
+ else
+ nil
+ end
+ end
+
+ def default_field_name
+ "item"
+ end
+
+ def resolve_field(arg)
+ if arg.is_a?(Hash) and arg.key?(:field)
+ description = arg
+ arg = description[:field]
+ end
+ if arg.is_a?(Hash)
+ field_description = arg
+ Field.new(field_description)
+ else
+ arg
+ end
+ end
end
end
diff --git a/ruby/red-arrow/test/test-list-data-type.rb b/ruby/red-arrow/test/test-list-data-type.rb
index cca6ca3..ada4639 100644
--- a/ruby/red-arrow/test/test-list-data-type.rb
+++ b/ruby/red-arrow/test/test-list-data-type.rb
@@ -23,7 +23,7 @@ class ListDataTypeTest < Test::Unit::TestCase
Arrow::ListDataType.new(field).to_s)
end
- test("Hash") do
+ test("name: String") do
assert_equal("list<tag: string>",
Arrow::ListDataType.new(name: "tag", type: :string).to_s)
end
@@ -39,5 +39,31 @@ class ListDataTypeTest < Test::Unit::TestCase
assert_equal("list<tag: string>",
Arrow::ListDataType.new(field: field_description).to_s)
end
+
+ test("Arrow::DataType") do
+ data_type = Arrow::BooleanDataType.new
+ assert_equal("list<item: bool>",
+ Arrow::ListDataType.new(data_type).to_s)
+ end
+
+ test("String") do
+ assert_equal("list<item: bool>",
+ Arrow::ListDataType.new("boolean").to_s)
+ end
+
+ test("Symbol") do
+ assert_equal("list<item: bool>",
+ Arrow::ListDataType.new(:boolean).to_s)
+ end
+
+ test("[data type name, additional information]") do
+ assert_equal("list<item: time32[ms]>",
+ Arrow::ListDataType.new([:time32, :milli]).to_s)
+ end
+
+ test("type: Symbol") do
+ assert_equal("list<item: bool>",
+ Arrow::ListDataType.new(type: :boolean).to_s)
+ end
end
end