Skip to content
Open
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
54 changes: 53 additions & 1 deletion ruby/red-arrow-format/lib/arrow-format/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,21 @@ def type
end

class FixedSizeBinaryArray < Array
def initialize(type, size, validity_buffer, values_buffer)
include BufferAlignable

def initialize(type, *args)
unless type.is_a?(Type)
Comment on lines 854 to +858

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ArrowFormat::Array#== compares only the type, the size, and the validity structure. ArrowFormat::BinaryArray and ArrowFormat::UTF8Array behave the same, so this isn't specific to fixed-size binary.
Comparing values needs #each, which is missing. I'll handle it in the same follow-up issue.

type = FixedSizeBinaryType.try_convert(type) || type
end
if args.size == 1
args = build_data(args.first, type)
elsif args.size != 3
raise ArgumentError,
"wrong number of arguments (given #{args.size + 1}, expected 2 or 4)"
end

size, validity_buffer, values_buffer = args

super(type, size, validity_buffer)
@values_buffer = values_buffer
end
Expand All @@ -875,6 +889,44 @@ def to_a
end
apply_validity(values)
end

private
def build_data(data, type)
n = 0
validity_buffer_builder = nil

values = +"".b
byte_width = type.byte_width
null_value = "\x00" * byte_width

data.each_with_index do |value, i|
if value.nil?
validity_buffer_builder ||= SparseBitmapBuilder.new
validity_buffer_builder.unset(i)
values.append_as_bytes(null_value)
else
unless value.bytesize == byte_width
message = "value size must be #{byte_width}: #{value.inspect}"
raise ArgumentError, message
end
values.append_as_bytes(value)
end

n += 1
end

validity_buffer = validity_buffer_builder&.finish(n)

pad!(values, buffer_padding_size(values))
values.freeze
values_buffer = IO::Buffer.for(values)

[
n,
validity_buffer,
values_buffer,
]
end
end

class DecimalArray < FixedSizeBinaryArray
Expand Down
14 changes: 14 additions & 0 deletions ruby/red-arrow-format/lib/arrow-format/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,20 @@ def to_flatbuffers
end

class FixedSizeBinaryType < Type
class << self
def try_convert(value)
case value
when Integer
byte_width = value
new(byte_width)
when self
value
else
nil
end
end
end

attr_reader :byte_width
def initialize(byte_width)
super()
Expand Down
79 changes: 79 additions & 0 deletions ruby/red-arrow-format/test/test-fixed-size-binary-array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# 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.

class TestFixedSizeBinaryArray < Test::Unit::TestCase
sub_test_case("#initialize") do
def test_no_null
values = ["0124".b, "abcd".b]
assert_equal(values,
ArrowFormat::FixedSizeBinaryArray.new(4, values).to_a)
end

def test_mixed
values = ["0124".b, nil, "abcd".b]
assert_equal(values,
ArrowFormat::FixedSizeBinaryArray.new(4, values).to_a)
end

def test_type
type = ArrowFormat::FixedSizeBinaryType.new(4)
values = ["0124".b, nil, "abcd".b]
assert_equal(values,
ArrowFormat::FixedSizeBinaryArray.new(type, values).to_a)
end

def test_too_small_value_size
error = ArgumentError.new("value size must be 4: \"012\"")
assert_raise(error) do
ArrowFormat::FixedSizeBinaryArray.new(4, ["012".b])
end
end

def test_too_large_value_size
error = ArgumentError.new("value size must be 4: \"01245\"")
assert_raise(error) do
ArrowFormat::FixedSizeBinaryArray.new(4, ["01245".b])
end
end
end

sub_test_case("#==") do
def test_no_slice
values = ["0124".b, nil, "abcd".b]
array1 = ArrowFormat::FixedSizeBinaryArray.new(4, values)
array2 = ArrowFormat::FixedSizeBinaryArray.new(4, values)
assert_equal(array1, array2)
end

def test_sliced
pad = "0000".b
values = ["0124".b, nil, "abcd".b]
array1 = ArrowFormat::FixedSizeBinaryArray.new(4, values)
array2 = ArrowFormat::FixedSizeBinaryArray.new(4, [pad, *values, pad])
assert_equal(array1, array2.slice(1, 3))
end

def test_sliced_different_content
pad = "0000".b
values = ["0124".b, nil, "abcd".b]
array1 = ArrowFormat::FixedSizeBinaryArray.new(4, values)
array2 = ArrowFormat::FixedSizeBinaryArray.new(4,
[pad, pad, *values, pad])
assert_not_equal(array1, array2.slice(1, 3))
end
end
end
Loading