Class: Arrow::DataType

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.resolve(data_type) ⇒ Arrow::DataType .resolve(name) ⇒ Object .resolve(name_with_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) ⇒ 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)

    Parameters:

    • name (String, Symbol)

      The type name of the data type.

  • .resolve(name_with_arguments) ⇒ Object

    Creates a suitable data type from type name with arguments.

    Examples:

    Create a boolean data type

    Arrow::DataType.resolve([:boolean])

    Create a milliseconds unit timestamp data type

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

    Parameters:

    • name_with_arguments (::Array<String, ...>)

      The type name of the data type as the first element.

      The rest elements are 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.



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
114
115
116
117
118
119
120
121
122
# File 'lib/arrow/data-type.rb', line 83

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

.sub_typesObject



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/arrow/data-type.rb', line 124

def sub_types
  types = {}
  gtype.children.each do |child|
    sub_type = child.to_class
    types[sub_type] = true
    sub_type.sub_types.each do |sub_sub_type|
      types[sub_sub_type] = true
    end
  end
  types.keys
end

.try_convert(value) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/arrow/data-type.rb', line 136

def try_convert(value)
  begin
    resolve(value)
  rescue ArgumentError
    nil
  end
end

Instance Method Details

#build_array(values) ⇒ Object



181
182
183
184
185
186
187
# File 'lib/arrow/data-type.rb', line 181

def build_array(values)
  base_name = self.class.name.gsub(/DataType\z/, "")
  builder_class = self.class.const_get("#{base_name}ArrayBuilder")
  args = [values]
  args.unshift(self) unless builder_class.buildable?(args)
  builder_class.build(*args)
end