Class: Arrow::Array

Inherits:
Object
  • Object
show all
Includes:
GenericFilterable, GenericTakeable, Enumerable
Defined in:
lib/arrow/array.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GenericTakeable

included, #take_generic

Methods included from GenericFilterable

#filter_generic, included

Class Method Details

.builder_classObject



32
33
34
35
36
# File 'lib/arrow/array.rb', line 32

def builder_class
  builder_class_name = "#{name}Builder"
  return nil unless const_defined?(builder_class_name)
  const_get(builder_class_name)
end

.new(*args) ⇒ Object



25
26
27
28
29
30
# File 'lib/arrow/array.rb', line 25

def new(*args)
  _builder_class = builder_class
  return super if _builder_class.nil?
  return super unless _builder_class.buildable?(args)
  _builder_class.build(*args)
end

Instance Method Details

#+(other_array) ⇒ Object

Concatenates the given other array to the array.

If you have multiple arrays to be concatenated, you should use #concatenate to concatenate multiple arrays at once.

Examples:

Raw Ruby Array

Arrow::Int32Array.new([1]) + [2, 3] # => Arrow::Int32Array.new([1, 2, 3])

Arrow::Array

Arrow::Int32Array.new([1]) +
  Arrow::Int32Array.new([2, 3]) # => Arrow::Int32Array.new([1, 2, 3])

Parameters:

  • other_array (::Array, Arrow::Array)

    The array to be concatenated.

    ‘@other_array` is processed by #resolve before it’s concatenated.

Since:

  • 4.0.0



149
150
151
# File 'lib/arrow/array.rb', line 149

def +(other_array)
  concatenate(other_array)
end

#[](i) ⇒ Object?

Returns The i-th value.

nil for NULL value or out of range i.

Parameters:

  • i (Integer)

    The index of the value to be gotten.

    You can specify negative index like for ‘::Array#[]`.

Returns:

  • (Object, nil)

    The i-th value.

    nil for NULL value or out of range i.



48
49
50
51
52
53
54
55
56
# File 'lib/arrow/array.rb', line 48

def [](i)
  i += length if i < 0
  return nil if i < 0 or i >= length
  if null?(i)
    nil
  else
    get_value(i)
  end
end

#concatenate(*other_arrays) ⇒ Object

Concatenates the given other arrays to the array.

Examples:

Raw Ruby Array

array = Arrow::Int32Array.new([1])
array.concatenate([2, 3], [4]) # => Arrow::Int32Array.new([1, 2, 3, 4])

Arrow::Array

array = Arrow::Int32Array.new([1])
array.concatenate(Arrow::Int32Array.new([2, 3]),
                  Arrow::Int8Array.new([4])) # => Arrow::Int32Array.new([1, 2, 3, 4])

Parameters:

  • other_arrays (::Array, Arrow::Array)

    The arrays to be concatenated.

    Each other array is processed by #resolve before they’re concatenated.

Since:

  • 4.0.0



124
125
126
127
128
129
# File 'lib/arrow/array.rb', line 124

def concatenate(*other_arrays)
  other_arrays = other_arrays.collect do |other_array|
    resolve(other_array)
  end
  concatenate_raw(other_arrays)
end

#concatenate_rawObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



105
# File 'lib/arrow/array.rb', line 105

alias_method :concatenate_raw, :concatenate

#eachObject



58
59
60
61
62
63
64
# File 'lib/arrow/array.rb', line 58

def each
  return to_enum(__method__) unless block_given?

  length.times do |i|
    yield(self[i])
  end
end

#is_in(values) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/arrow/array.rb', line 88

def is_in(values)
  case values
  when ::Array
    if self.class.builder_class.buildable?([values])
      values = self.class.new(values)
    else
      values = self.class.new(value_data_type, values)
    end
    is_in_raw(values)
  when ChunkedArray
    is_in_chunked_array(values)
  else
    is_in_raw(values)
  end
end

#is_in_rawObject



87
# File 'lib/arrow/array.rb', line 87

alias_method :is_in_raw, :is_in

#resolve(other_raw_array) ⇒ Arrow::Array #resolve(other_array) ⇒ Arrow::Array

Ensures returning the same data type array from the given array.

Overloads:

  • #resolve(other_raw_array) ⇒ Arrow::Array

    Examples:

    Raw Ruby Array

    int32_array = Arrow::Int32Array.new([1])
    other_array = int32_array.resolve([2, 3, 4])
    other_array # => Arrow::Int32Array.new([2, 3, 4])
    

    Parameters:

    • other_raw_array (::Array)

      A raw Ruby Array. A new Arrow::Array is built by self.class.new.

  • #resolve(other_array) ⇒ Arrow::Array

    Examples:

    Same data type

    int32_array = Arrow::Int32Array.new([1])
    other_int32_array = Arrow::Int32Array.new([2, 3, 4])
    other_array = int32_array.resolve(other_int32_array)
    other_array.object_id == other_int32_array.object_id
    

    Other data type

    int32_array = Arrow::Int32Array.new([1])
    other_int8_array = Arrow::Int8Array.new([2, 3, 4])
    other_array = int32_array.resolve(other_int32_array)
    other_array #=> Arrow::Int32Array.new([2, 3, 4])
    

    Parameters:

    • other_array (Arrow::Array)

      Another Arrow::Array.

      If the given other array is an same data type array of self, the given other array is returned as-is.

      If the given other array isn’t an same data type array of self, the given other array is casted.

Returns:

Since:

  • 4.0.0



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/arrow/array.rb', line 190

def resolve(other_array)
  if other_array.is_a?(::Array)
    builder_class = self.class.builder_class
    if builder_class.nil?
      message =
        "[array][resolve] can't build #{value_data_type} array " +
        "from raw Ruby Array"
      raise ArgumentError, message
    end
    if builder_class.buildable?([other_array])
      other_array = builder_class.build(other_array)
    elsif builder_class.buildable?([value_data_type, other_array])
      other_array = builder_class.build(value_data_type, other_array)
    else
      message =
        "[array][resolve] need to implement " +
        "a feature that building #{value_data_type} array " +
        "from raw Ruby Array"
      raise NotImpelemented, message
    end
    other_array
  elsif other_array.respond_to?(:value_data_type)
    return other_array if value_data_type == other_array.value_data_type
    other_array.cast(value_data_type)
  else
    message =
      "[array][resolve] can't build #{value_data_type} array: " +
      "#{other_array.inspect}"
    raise ArgumentError, message
  end
end

#reverse_eachObject



66
67
68
69
70
71
72
# File 'lib/arrow/array.rb', line 66

def reverse_each
  return to_enum(__method__) unless block_given?

  (length - 1).downto(0) do |i|
    yield(self[i])
  end
end

#to_aObject



83
84
85
# File 'lib/arrow/array.rb', line 83

def to_a
  values
end

#to_arrowObject



74
75
76
# File 'lib/arrow/array.rb', line 74

def to_arrow
  self
end

#value_data_typeObject



79
80
81
# File 'lib/arrow/array.rb', line 79

def value_data_type
  @value_data_type ||= value_data_type_raw
end

#value_data_type_rawObject



78
# File 'lib/arrow/array.rb', line 78

alias_method :value_data_type_raw, :value_data_type