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) ⇒ Arrow::DataType .resolve(name_with_arguments) ⇒ Arrow::DataType .resolve(description) ⇒ Arrow::DataType

Ensure returning 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) ⇒ Arrow::DataType

    Creates a suitable data type from the given 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.

    Returns:

  • .resolve(name_with_arguments) ⇒ Arrow::DataType

    Creates a new suitable data type from the given 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.

    Returns:

  • .resolve(description) ⇒ Arrow::DataType

    Creates a new suitable data type from the given 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.

    Returns:



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
123
124
125
126
127
128
129
130
131
# File 'lib/arrow/data-type.rb', line 92

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? and self == DataType
      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



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

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



145
146
147
148
149
150
151
# File 'lib/arrow/data-type.rb', line 145

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

Instance Method Details

#array_classObject



191
192
193
194
# File 'lib/arrow/data-type.rb', line 191

def array_class
  base_name = self.class.name.gsub(/DataType\z/, "")
  ::Arrow.const_get("#{base_name}Array")
end

#build_array(values) ⇒ Object



196
197
198
199
200
201
# File 'lib/arrow/data-type.rb', line 196

def build_array(values)
  builder_class = array_class.builder_class
  args = [values]
  args.unshift(self) unless builder_class.buildable?(args)
  builder_class.build(*args)
end

#scalar_classArrow::Scalar

Returns A corresponding Scalar class for this data type.

Returns:

Since:

  • 12.0.0



207
208
209
210
# File 'lib/arrow/data-type.rb', line 207

def scalar_class
  base_name = self.class.name.gsub(/DataType\z/, "")
  ::Arrow.const_get("#{base_name}Scalar")
end