Class: Arrow::RecordBatchBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/arrow/record-batch-builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ RecordBatchBuilder

Returns a new instance of RecordBatchBuilder.



31
32
33
34
35
36
37
38
39
40
# File 'lib/arrow/record-batch-builder.rb', line 31

def initialize(schema)
  unless schema.is_a?(Schema)
    schema = Schema.new(schema)
  end
  initialize_raw(schema)
  @name_to_index = {}
  schema.fields.each_with_index do |field, i|
    @name_to_index[field.name] = i
  end
end

Class Method Details

.build(schema, data) ⇒ Object

Since:

  • 0.12.0



22
23
24
25
26
# File 'lib/arrow/record-batch-builder.rb', line 22

def build(schema, data)
  builder = new(schema)
  builder.append(data)
  builder.flush
end

Instance Method Details

#[](name_or_index) ⇒ Object

Since:

  • 0.12.0



43
44
45
46
47
48
49
50
51
52
# File 'lib/arrow/record-batch-builder.rb', line 43

def [](name_or_index)
  case name_or_index
  when String, Symbol
    name = name_or_index
    self[resolve_name(name)]
  else
    index = name_or_index
    column_builders[index]
  end
end

#append(*values) ⇒ Object

Since:

  • 0.12.0



55
56
57
58
59
60
61
62
63
64
# File 'lib/arrow/record-batch-builder.rb', line 55

def append(*values)
  values.each do |value|
    case value
    when Hash
      append_columns(value)
    else
      append_records(value)
    end
  end
end

#append_columns(columns) ⇒ Object

Since:

  • 0.12.0



96
97
98
99
100
# File 'lib/arrow/record-batch-builder.rb', line 96

def append_columns(columns)
  columns.each do |name, values|
    self[name].append(*values)
  end
end

#append_records(records) ⇒ Object

Since:

  • 0.12.0



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/arrow/record-batch-builder.rb', line 67

def append_records(records)
  n = n_columns
  columns = n.times.collect do
    []
  end
  records.each_with_index do |record, nth_record|
    case record
    when nil
    when Hash
      record.each do |name, value|
        nth_column = resolve_name(name)
        next if nth_column.nil?
        columns[nth_column] << value
      end
    else
      record.each_with_index do |value, nth_column|
        columns[nth_column] << value
      end
    end
    columns.each do |column|
      column << nil if column.size != (nth_record + 1)
    end
  end
  columns.each_with_index do |column, i|
    self[i].append(*column)
  end
end

#column_buildersObject

Since:

  • 0.13.0



103
104
105
106
107
# File 'lib/arrow/record-batch-builder.rb', line 103

def column_builders
  @column_builders ||= n_columns.times.collect do |i|
    get_column_builder(i)
  end
end