Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ruby/red-arrow-format/lib/arrow-format/field.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
Expand All @@ -23,7 +24,8 @@ def initialize(name,
type,
nullable: true,
metadata: nil)
@name = name
name = name.to_s if name.is_a?(Symbol)
@name = name.to_str
@type = type
@nullable = nullable
@metadata = metadata
Expand Down
58 changes: 52 additions & 6 deletions ruby/red-arrow-format/lib/arrow-format/record-batch.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
Expand All @@ -15,6 +16,7 @@
# under the License.

require_relative "buffer-alignable"
require_relative "record"

module ArrowFormat
class RecordBatch
Expand Down Expand Up @@ -44,12 +46,43 @@ def empty?
@n_rows.zero?
end

def to_h
hash = {}
@schema.fields.zip(@columns) do |field, column|
hash[field.name] = column
def find_column(name_or_index)
case name_or_index
when Integer
@columns[name_or_index]
when Symbol
name_to_column[name_or_index.to_s]
else
name_to_column[name_or_index.to_str]
end
hash
end

def each_record(reuse_record: false)
unless block_given?
return to_enum(__method__, reuse_record: reuse_record)
end

if reuse_record
record = Record.new(self, nil)
@n_rows.times do |i|
record.index = i
yield(record)
end
else
@n_rows.times do |i|
yield(Record.new(self, i))
end
end
end

def records
size.times.collect do |i|
Record.new(self, i)
end
end

def to_h
name_to_column.dup
end

def to_flatbuffers
Expand Down Expand Up @@ -158,7 +191,20 @@ def build(data)
raise ArgumentError, message
end

return Schema.new(fields), n_rows, columns
return Schema.new(fields), all_n_rows[0], columns
end

def name_to_column
@name_to_column ||= build_name_to_column
end

def build_name_to_column
name_to_column = {}
@schema.fields.zip(@columns) do |field, column|
name_to_column[field.name] = column
end
name_to_column.freeze
name_to_column
end
end
end
60 changes: 60 additions & 0 deletions ruby/red-arrow-format/lib/arrow-format/record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

module ArrowFormat
class Record
attr_reader :record_batch
attr_accessor :index
def initialize(record_batch, index)
@record_batch = record_batch
@index = index
end

def [](name_or_index)
column = @record_batch.find_column(name_or_index)
return nil if column.nil?
column[@index]
end

def to_a
@record_batch.columns.collect do |column|
column[@index]
end
end

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

def respond_to_missing?(name, include_private)
return true if @record_batch.find_column(name)
super
end

def method_missing(name, *args, &block)
if args.empty?
column = @record_batch.find_column(name)
return column[@index] if column
end
super
end
end
end
25 changes: 25 additions & 0 deletions ruby/red-arrow-format/lib/arrow-format/schema.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
Expand Down Expand Up @@ -25,6 +26,17 @@ def initialize(fields, metadata: nil, message_metadata: nil)
@message_metadata = message_metadata
end

def [](name_or_index)
case name_or_index
when Integer
@fields[name_or_index]
when Symbol
name_to_field[name_or_index.to_s]
else
name_to_field[name_or_index.to_str]
end
end

def to_flatbuffers
fb_schema = FB::Schema::Data.new
fb_schema.endianness = FB::Endianness::LITTLE
Expand All @@ -33,5 +45,18 @@ def to_flatbuffers
# fb_schema.features = @features
fb_schema
end

private
def name_to_field
@name_to_field ||= build_name_to_field
end

def build_name_to_field
name_to_field = {}
@fields.each do |field|
name_to_field[field.name] = field
end
name_to_field
end
end
end
2 changes: 1 addition & 1 deletion ruby/red-arrow-format/test/test-binary-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_sliced_different_content

sub_test_case("#[]") do
def test_valid
assert_equal(@values[3], @array[3])
assert_equal(@values[2], @array[2])
end

def test_null
Expand Down
2 changes: 1 addition & 1 deletion ruby/red-arrow-format/test/test-boolean-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_sliced_different_content

sub_test_case("#[]") do
def test_valid
assert_equal(@values[3], @array[3])
assert_equal(@values[2], @array[2])
end

def test_null
Expand Down
2 changes: 1 addition & 1 deletion ruby/red-arrow-format/test/test-date32-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_sliced_different_content

sub_test_case("#[]") do
def test_valid
assert_equal(@values[3], @array[3])
assert_equal(@values[2], @array[2])
end

def test_null
Expand Down
2 changes: 1 addition & 1 deletion ruby/red-arrow-format/test/test-date64-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_sliced_different_content

sub_test_case("#[]") do
def test_valid
assert_equal(@values[3], @array[3])
assert_equal(@values[2], @array[2])
end

def test_null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def test_sliced_different_content

sub_test_case("#[]") do
def test_valid
assert_equal(@values[3], @array[3])
assert_equal(@values[2], @array[2])
end

def test_null
Expand Down
2 changes: 1 addition & 1 deletion ruby/red-arrow-format/test/test-duration-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_sliced_different_content

sub_test_case("#[]") do
def test_valid
assert_equal(@values[3], @array[3])
assert_equal(@values[2], @array[2])
end

def test_null
Expand Down
2 changes: 1 addition & 1 deletion ruby/red-arrow-format/test/test-float32-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_sliced_different_content

sub_test_case("#[]") do
def test_valid
assert_equal(@values[3], @array[3])
assert_equal(@values[2], @array[2])
end

def test_null
Expand Down
2 changes: 1 addition & 1 deletion ruby/red-arrow-format/test/test-float64-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_sliced_different_content

sub_test_case("#[]") do
def test_valid
assert_equal(@values[3], @array[3])
assert_equal(@values[2], @array[2])
end

def test_null
Expand Down
2 changes: 1 addition & 1 deletion ruby/red-arrow-format/test/test-int16-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_sliced_different_content

sub_test_case("#[]") do
def test_valid
assert_equal(@values[3], @array[3])
assert_equal(@values[2], @array[2])
end

def test_null
Expand Down
2 changes: 1 addition & 1 deletion ruby/red-arrow-format/test/test-int32-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_sliced_different_content

sub_test_case("#[]") do
def test_valid
assert_equal(@values[3], @array[3])
assert_equal(@values[2], @array[2])
end

def test_null
Expand Down
2 changes: 1 addition & 1 deletion ruby/red-arrow-format/test/test-int64-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_sliced_different_content

sub_test_case("#[]") do
def test_valid
assert_equal(@values[3], @array[3])
assert_equal(@values[2], @array[2])
end

def test_null
Expand Down
2 changes: 1 addition & 1 deletion ruby/red-arrow-format/test/test-int8-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_sliced_different_content

sub_test_case("#[]") do
def test_valid
assert_equal(@values[3], @array[3])
assert_equal(@values[2], @array[2])
end

def test_null
Expand Down
2 changes: 1 addition & 1 deletion ruby/red-arrow-format/test/test-large-binary-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_sliced_different_content

sub_test_case("#[]") do
def test_valid
assert_equal(@values[3], @array[3])
assert_equal(@values[2], @array[2])
end

def test_null
Expand Down
2 changes: 1 addition & 1 deletion ruby/red-arrow-format/test/test-large-utf8-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_sliced_different_content

sub_test_case("#[]") do
def test_valid
assert_equal(@values[3], @array[3])
assert_equal(@values[2], @array[2])
end

def test_null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_sliced_different_content

sub_test_case("#[]") do
def test_valid
assert_equal(@values[3], @array[3])
assert_equal(@values[2], @array[2])
end

def test_null
Expand Down
Loading