Skip to content

Commit

Permalink
ARROW-16874: [Ruby] Use more .try_convert for auto data type conversi…
Browse files Browse the repository at this point in the history
…on (apache#13417)

Authored-by: Sutou Kouhei <kou@clear-code.com>
Signed-off-by: Sutou Kouhei <kou@clear-code.com>
  • Loading branch information
kou authored and vibhatha committed Jul 4, 2022
1 parent e39bb7c commit f1d0ae9
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 163 deletions.
3 changes: 2 additions & 1 deletion ruby/red-arrow/lib/arrow/data-type.rb
Expand Up @@ -110,7 +110,7 @@ def resolve(data_type)
description[key] = value
end
end
if type.nil?
if type.nil? and self == DataType
message =
"data type description must have :type value: #{data_type.inspect}"
raise ArgumentError, message
Expand Down Expand Up @@ -152,6 +152,7 @@ def try_convert(value)

private
def resolve_class(data_type)
return self if data_type.nil?
components = data_type.to_s.split("_").collect(&:capitalize)
data_type_name = components.join.gsub(/\AUint/, "UInt")
data_type_class_name = "#{data_type_name}DataType"
Expand Down
16 changes: 16 additions & 0 deletions ruby/red-arrow/lib/arrow/field.rb
Expand Up @@ -17,6 +17,22 @@

module Arrow
class Field
class << self
# @api private
def try_convert(value)
case value
when Hash
begin
new(value)
rescue ArgumentError
nil
end
else
nil
end
end
end

alias_method :initialize_raw, :initialize
private :initialize_raw

Expand Down
7 changes: 1 addition & 6 deletions ruby/red-arrow/lib/arrow/list-data-type.rb
Expand Up @@ -107,12 +107,7 @@ def resolve_field(arg)
description = arg
arg = description[:field]
end
if arg.is_a?(Hash)
field_description = arg
Field.new(field_description)
else
arg
end
arg
end
end
end
1 change: 1 addition & 0 deletions ruby/red-arrow/lib/arrow/loader.rb
Expand Up @@ -119,6 +119,7 @@ def require_libraries
require "arrow/table-saver"
require "arrow/tensor"
require "arrow/time"
require "arrow/time-unit"
require "arrow/time32-array"
require "arrow/time32-array-builder"
require "arrow/time32-data-type"
Expand Down
31 changes: 31 additions & 0 deletions ruby/red-arrow/lib/arrow/time-unit.rb
@@ -0,0 +1,31 @@
# 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 TimeUnit
class << self
# @api private
def try_convert(value)
if value.is_a?(Hash) and value.size == 1 and value[:unit]
super(value[:unit])
else
super
end
end
end
end
end
16 changes: 2 additions & 14 deletions ruby/red-arrow/lib/arrow/time32-array-builder.rb
Expand Up @@ -18,24 +18,12 @@
module Arrow
class Time32ArrayBuilder
class << self
def build(unit_or_data_type, values)
builder = new(unit_or_data_type)
def build(data_type, values)
builder = new(data_type)
builder.build(values)
end
end

alias_method :initialize_raw, :initialize
def initialize(unit_or_data_type)
case unit_or_data_type
when DataType
data_type = unit_or_data_type
else
unit = unit_or_data_type
data_type = Time32DataType.new(unit)
end
initialize_raw(data_type)
end

def unit
@unit ||= value_data_type.unit
end
Expand Down
47 changes: 9 additions & 38 deletions ruby/red-arrow/lib/arrow/time32-data-type.rb
Expand Up @@ -17,45 +17,16 @@

module Arrow
class Time32DataType
alias_method :initialize_raw, :initialize
private :initialize_raw

# Creates a new {Arrow::Time32DataType}.
#
# @overload initialize(unit)
#
# @param unit [Arrow::TimeUnit, Symbol] The unit of the
# time32 data type.
#
# The unit must be second or millisecond.
#
# @example Create a time32 data type with Arrow::TimeUnit
# Arrow::Time32DataType.new(Arrow::TimeUnit::MILLI)
#
# @example Create a time32 data type with Symbol
# Arrow::Time32DataType.new(:milli)
#
# @overload initialize(description)
#
# @param description [Hash] The description of the time32 data
# type. It must have `:unit` value.
#
# @option description [Arrow::TimeUnit, Symbol] :unit The unit of
# the time32 data type.
#
# The unit must be second or millisecond.
#
# @example Create a time32 data type with Arrow::TimeUnit
# Arrow::Time32DataType.new(unit: Arrow::TimeUnit::MILLI)
#
# @example Create a time32 data type with Symbol
# Arrow::Time32DataType.new(unit: :milli)
def initialize(unit)
if unit.is_a?(Hash)
description = unit
unit = description[:unit]
class << self
# @api private
def try_convert(value)
case value
when Symbol, Arrow::TimeUnit
new(value)
else
super
end
end
initialize_raw(unit)
end
end
end
16 changes: 2 additions & 14 deletions ruby/red-arrow/lib/arrow/time64-array-builder.rb
Expand Up @@ -18,24 +18,12 @@
module Arrow
class Time64ArrayBuilder
class << self
def build(unit_or_data_type, values)
builder = new(unit_or_data_type)
def build(data_type, values)
builder = new(data_type)
builder.build(values)
end
end

alias_method :initialize_raw, :initialize
def initialize(unit_or_data_type)
case unit_or_data_type
when DataType
data_type = unit_or_data_type
else
unit = unit_or_data_type
data_type = Time64DataType.new(unit)
end
initialize_raw(data_type)
end

def unit
@unit ||= value_data_type.unit
end
Expand Down
47 changes: 9 additions & 38 deletions ruby/red-arrow/lib/arrow/time64-data-type.rb
Expand Up @@ -17,45 +17,16 @@

module Arrow
class Time64DataType
alias_method :initialize_raw, :initialize
private :initialize_raw

# Creates a new {Arrow::Time64DataType}.
#
# @overload initialize(unit)
#
# @param unit [Arrow::TimeUnit, Symbol] The unit of the
# time64 data type.
#
# The unit must be microsecond or nanosecond.
#
# @example Create a time64 data type with Arrow::TimeUnit
# Arrow::Time64DataType.new(Arrow::TimeUnit::NANO)
#
# @example Create a time64 data type with Symbol
# Arrow::Time64DataType.new(:nano)
#
# @overload initialize(description)
#
# @param description [Hash] The description of the time64 data
# type. It must have `:unit` value.
#
# @option description [Arrow::TimeUnit, Symbol] :unit The unit of
# the time64 data type.
#
# The unit must be microsecond or nanosecond.
#
# @example Create a time64 data type with Arrow::TimeUnit
# Arrow::Time64DataType.new(unit: Arrow::TimeUnit::NANO)
#
# @example Create a time64 data type with Symbol
# Arrow::Time64DataType.new(unit: :nano)
def initialize(unit)
if unit.is_a?(Hash)
description = unit
unit = description[:unit]
class << self
# @api private
def try_convert(value)
case value
when Symbol, Arrow::TimeUnit
new(value)
else
super
end
end
initialize_raw(unit)
end
end
end
16 changes: 2 additions & 14 deletions ruby/red-arrow/lib/arrow/timestamp-array-builder.rb
Expand Up @@ -18,24 +18,12 @@
module Arrow
class TimestampArrayBuilder
class << self
def build(unit_or_data_type, values)
builder = new(unit_or_data_type)
def build(data_type, values)
builder = new(data_type)
builder.build(values)
end
end

alias_method :initialize_raw, :initialize
def initialize(unit_or_data_type)
case unit_or_data_type
when DataType
data_type = unit_or_data_type
else
unit = unit_or_data_type
data_type = TimestampDataType.new(unit)
end
initialize_raw(data_type)
end

private
def unit_id
@unit_id ||= value_data_type.unit.nick.to_sym
Expand Down
43 changes: 9 additions & 34 deletions ruby/red-arrow/lib/arrow/timestamp-data-type.rb
Expand Up @@ -17,41 +17,16 @@

module Arrow
class TimestampDataType
alias_method :initialize_raw, :initialize
private :initialize_raw

# Creates a new {Arrow::TimestampDataType}.
#
# @overload initialize(unit)
#
# @param unit [Arrow::TimeUnit, Symbol] The unit of the
# timestamp data type.
#
# @example Create a timestamp data type with Arrow::TimeUnit
# Arrow::TimestampDataType.new(Arrow::TimeUnit::MILLI)
#
# @example Create a timestamp data type with Symbol
# Arrow::TimestampDataType.new(:milli)
#
# @overload initialize(description)
#
# @param description [Hash] The description of the timestamp data
# type. It must have `:unit` value.
#
# @option description [Arrow::TimeUnit, Symbol] :unit The unit of
# the timestamp data type.
#
# @example Create a timestamp data type with Arrow::TimeUnit
# Arrow::TimestampDataType.new(unit: Arrow::TimeUnit::MILLI)
#
# @example Create a timestamp data type with Symbol
# Arrow::TimestampDataType.new(unit: :milli)
def initialize(unit)
if unit.is_a?(Hash)
description = unit
unit = description[:unit]
class << self
# @api private
def try_convert(value)
case value
when Symbol, Arrow::TimeUnit
new(value)
else
super
end
end
initialize_raw(unit)
end
end
end
3 changes: 1 addition & 2 deletions ruby/red-arrow/test/test-decimal128-array.rb
Expand Up @@ -18,14 +18,13 @@
class Decimal128ArrayTest < Test::Unit::TestCase
sub_test_case(".new") do
test("build") do
data_type = Arrow::Decimal128DataType.new(3, 1)
values = [
10.1,
nil,
"10.1",
BigDecimal("10.1"),
]
array = Arrow::Decimal128Array.new(data_type, values)
array = Arrow::Decimal128Array.new({precision: 3, scale: 1}, values)
assert_equal([
BigDecimal("10.1"),
nil,
Expand Down
3 changes: 1 addition & 2 deletions ruby/red-arrow/test/test-decimal256-array.rb
Expand Up @@ -18,14 +18,13 @@
class Decimal256ArrayTest < Test::Unit::TestCase
sub_test_case(".new") do
test("build") do
data_type = Arrow::Decimal256DataType.new(3, 1)
values = [
10.1,
nil,
"10.1",
BigDecimal("10.1"),
]
array = Arrow::Decimal256Array.new(data_type, values)
array = Arrow::Decimal256Array.new({precision: 3, scale: 1}, values)
assert_equal([
BigDecimal("10.1"),
nil,
Expand Down

0 comments on commit f1d0ae9

Please sign in to comment.