Class: Arrow::ArrayBuilder

Inherits:
Object
  • Object
show all
Extended by:
ArrayBuildable
Defined in:
lib/arrow/array-builder.rb,
lib/arrow/jruby/array-builder.rb

Constant Summary

Constants included from ArrayBuildable

Arrow::ArrayBuildable::ValueVector

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ArrayBuildable

buildable?

Constructor Details

#initializeArrayBuilder

Returns a new instance of ArrayBuilder.



34
35
36
37
38
# File 'lib/arrow/jruby/array-builder.rb', line 34

def initialize
  @vector = self.class::Array::Vector.new("", Arrow.allocator)
  @vector.allocate_new
  @index = 0
end

Class Method Details

.build(values) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/arrow/array-builder.rb', line 23

def build(values)
  if self != ArrayBuilder
    builder = new
    return builder.build(values)
  end

  builder_info = nil
  values.each do |value|
    builder_info = detect_builder_info(value, builder_info)
    break if builder_info and builder_info[:detected]
  end
  if builder_info
    builder = builder_info[:builder]
    if builder.nil? and builder_info[:builder_type]
      builder = create_builder(builder_info)
    end
  end
  if builder
    builder.build(values)
  else
    Arrow::StringArray.new(values)
  end
end

.buildable?(args) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/arrow/array-builder.rb', line 47

def buildable?(args)
  args.size == method(:build).arity
end

Instance Method Details

#append(*values) ⇒ Object

Since:

  • 0.12.0



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/arrow/array-builder.rb', line 240

def append(*values)
  value_convertable = respond_to?(:convert_to_arrow_value, true)
  start_index = 0
  current_index = 0
  status = :value

  values.each do |value|
    if value.nil?
      if status == :value
        if start_index != current_index
          target_values = values[start_index...current_index]
          if value_convertable
            target_values = target_values.collect do |v|
              convert_to_arrow_value(v)
            end
          end
          append_values(target_values, nil)
          start_index = current_index
        end
        status = :null
      end
    else
      if status == :null
        append_nulls(current_index - start_index)
        start_index = current_index
        status = :value
      end
    end
    current_index += 1
  end
  if start_index != current_index
    if status == :value
      if start_index == 0 and current_index == values.size
        target_values = values
      else
        target_values = values[start_index...current_index]
      end
      if value_convertable
        target_values = target_values.collect do |v|
          convert_to_arrow_value(v)
        end
      end
      append_values(target_values, nil)
    else
      append_nulls(current_index - start_index)
    end
  end
end

#append_nulls(n) ⇒ Object



63
64
65
66
67
68
# File 'lib/arrow/jruby/array-builder.rb', line 63

def append_nulls(n)
  n.times do
    @vector.set_null(@index)
    @index += 1
  end
end

#append_value(value) ⇒ Object



40
41
42
43
# File 'lib/arrow/jruby/array-builder.rb', line 40

def append_value(value)
  @vector.set(@index, value)
  @index += 1
end

#append_values(values, is_valids = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/arrow/jruby/array-builder.rb', line 45

def append_values(values, is_valids=nil)
  if is_valids
    values.zip(is_valids) do |value, is_valid|
      if is_valid
        @vector.set(@index, value)
      else
        @vector.set_null(@index)
      end
      @index += 1
    end
  else
    values.each do |value|
      @vector.set(@index, value)
      @index += 1
    end
  end
end

#build(values) ⇒ Object



234
235
236
237
# File 'lib/arrow/array-builder.rb', line 234

def build(values)
  append(*values)
  finish
end

#finishObject



70
71
72
73
74
# File 'lib/arrow/jruby/array-builder.rb', line 70

def finish
  @vector.set_value_count(@index)
  vector, @vector = @vector, nil
  self.class::Array.new(vector)
end