Class: Arrow::DataType

Inherits:
Object
  • Object
show all
Defined in:
lib/arrow/data-type.rb

Class Method Summary collapse

Class Method Details

.resolve(data_type) ⇒ Arrow::DataType .resolve(name, *arguments) ⇒ Object .resolve(description) ⇒ Object

Creates a new suitable Arrow::DataType.

Overloads:

  • .resolve(data_type) ⇒ Arrow::DataType

    Returns the given data type itself. This is convenient to use this method as Arrow::DataType converter.

    Parameters:

    Returns:

  • .resolve(name, *arguments) ⇒ Object

    Creates a suitable data type from type name. For example, you can create BooleanDataType from ‘:boolean`.

    Examples:

    Create a boolean data type

    Arrow::DataType.resolve(:boolean)

    Create a milliseconds unit timestamp data type

    Arrow::DataType.resolve(:timestamp, :milli)

    Parameters:

    • name (String, Symbol)

      The type name of the data type.

    • arguments (::Array)

      The additional information of the data type.

      For example, TimestampDataType needs unit as additional information.

  • .resolve(description) ⇒ Object

    Creates a suitable data type from data type description.

    Data type description is a raw ‘Hash`. Data type description must have `:type` value. `:type` is the type of the data type.

    If the type needs additional information, you need to specify it. See constructor document what information is needed. For example, ListDataType#initialize needs ‘:field` value.

    Examples:

    Create a boolean data type

    Arrow::DataType.resolve(type: :boolean)

    Create a list data type

    Arrow::DataType.resolve(type: :list,
                            field: {name: "visible", type: :boolean})

    Parameters:

    • description (Hash)

      The description of the data type.

    Options Hash (description):

    • :type (String, Symbol)

      The type name of the data type.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/arrow/data-type.rb', line 74

def resolve(data_type)
  case data_type
  when DataType
    data_type
  when String, Symbol
    resolve_class(data_type).new
  when ::Array
    type, *arguments = data_type
    resolve_class(type).new(*arguments)
  when Hash
    type = nil
    description = {}
    data_type.each do |key, value|
      key = key.to_sym
      case key
      when :type
        type = value
      else
        description[key] = value
      end
    end
    if type.nil?
      message =
        "data type description must have :type value: #{data_type.inspect}"
      raise ArgumentError, message
    end
    data_type_class = resolve_class(type)
    if description.empty?
      data_type_class.new
    else
      data_type_class.new(description)
    end
  else
    message =
      "data type must be " +
      "Arrow::DataType, String, Symbol, [String, ...], [Symbol, ...] " +
      "{type: String, ...} or {type: Symbol, ...}: #{data_type.inspect}"
    raise ArgumentError, message
  end
end