Class: ArrowFormat::RecordBatch

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

Constant Summary

Constants included from BufferAlignable

BufferAlignable::BUFFER_ALIGNMENT_SIZE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, n_rows, columns, message_metadata: nil) ⇒ RecordBatch

Returns a new instance of RecordBatch.



29
30
31
32
33
34
# File 'lib/arrow-format/record-batch.rb', line 29

def initialize(schema, n_rows, columns, message_metadata: nil)
  @schema = schema
  @n_rows = n_rows
  @columns = columns
  @message_metadata = 
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



27
28
29
# File 'lib/arrow-format/record-batch.rb', line 27

def columns
  @columns
end

#message_metadataObject (readonly)

Returns the value of attribute message_metadata.



28
29
30
# File 'lib/arrow-format/record-batch.rb', line 28

def 
  @message_metadata
end

#n_rowsObject (readonly) Also known as: size, length

Returns the value of attribute n_rows.



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

def n_rows
  @n_rows
end

#schemaObject (readonly)

Returns the value of attribute schema.



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

def schema
  @schema
end

Instance Method Details

#all_buffers_enumeratorObject



95
96
97
98
99
100
101
102
103
# File 'lib/arrow-format/record-batch.rb', line 95

def all_buffers_enumerator
  Enumerator.new do |yielder|
    all_columns_enumerator.each do |array|
      array.each_buffer do |buffer|
        yielder << buffer
      end
    end
  end
end

#all_columns_enumeratorObject

Pre-order depth-first traversal



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/arrow-format/record-batch.rb', line 77

def all_columns_enumerator
  Enumerator.new do |yielder|
    traverse = lambda do |array|
      yielder << array
      if array.respond_to?(:child)
        traverse.call(array.child)
      elsif array.respond_to?(:children)
        array.children.each do |child_array|
          traverse.call(child_array)
        end
      end
    end
    @columns.each do |array|
      traverse.call(array)
    end
  end
end

#empty?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/arrow-format/record-batch.rb', line 36

def empty?
  @n_rows.zero?
end

#to_flatbuffersObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/arrow-format/record-batch.rb', line 48

def to_flatbuffers
  fb_record_batch = FB::RecordBatch::Data.new
  fb_record_batch.length = @n_rows
  fb_record_batch.nodes = all_columns_enumerator.collect do |array|
    field_node = FB::FieldNode::Data.new
    field_node.length = array.size
    field_node.null_count = array.n_nulls
    field_node
  end
  offset = 0
  fb_record_batch.buffers = all_buffers_enumerator.collect do |buffer|
    fb_buffer = FB::Buffer::Data.new
    fb_buffer.offset = offset
    if buffer
      aligned_size = aligned_buffer_size(buffer)
      offset += aligned_size
      fb_buffer.length = aligned_size
    else
      fb_buffer.length = 0
    end
    fb_buffer
  end
  # body_compression = FB::BodyCompression::Data.new
  # body_compression.codec = ...
  # fb_record_batch.compression = body_compression
  fb_record_batch
end

#to_hObject



40
41
42
43
44
45
46
# File 'lib/arrow-format/record-batch.rb', line 40

def to_h
  hash = {}
  @schema.fields.zip(@columns) do |field, column|
    hash[field.name] = column
  end
  hash
end