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
37 changes: 37 additions & 0 deletions ruby/red-arrow-format/lib/arrow-format/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# specific language governing permissions and limitations
# under the License.

require "bigdecimal"

require_relative "bitmap"

module ArrowFormat
Expand Down Expand Up @@ -306,6 +308,41 @@ def to_a
end
end

class DecimalArray < FixedSizeBinaryArray
end

class Decimal128Array < DecimalArray
def to_a
byte_width = @type.byte_width
buffer_types = [:u64, :s64] # TODO: big endian support
values = 0.step(@size * byte_width - 1, byte_width).collect do |offset|
@values_buffer.get_values(buffer_types, offset)
end
apply_validity(values).collect do |value|
if value.nil?
nil
else
low, high = value
BigDecimal(format_value(low, high))
end
end
end

private
def format_value(low, high)
width = @type.precision
width += 1 if high < 0
value = (high << 64) + low
string = value.to_s.ljust(width, "0")
if @type.scale < 0
string << ("0" * -@type.scale)
elsif @type.scale > 0
string[-@type.scale, 0] = "."
end
string
end
end

class VariableSizeListArray < Array
def initialize(type, size, validity_buffer, offsets_buffer, child)
super(type, size, validity_buffer)
Expand Down
6 changes: 6 additions & 0 deletions ruby/red-arrow-format/lib/arrow-format/readable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
require_relative "org/apache/arrow/flatbuf/bool"
require_relative "org/apache/arrow/flatbuf/date"
require_relative "org/apache/arrow/flatbuf/date_unit"
require_relative "org/apache/arrow/flatbuf/decimal"
require_relative "org/apache/arrow/flatbuf/duration"
require_relative "org/apache/arrow/flatbuf/fixed_size_binary"
require_relative "org/apache/arrow/flatbuf/floating_point"
Expand Down Expand Up @@ -166,6 +167,11 @@ def read_field(fb_field)
type = LargeUTF8Type.singleton
when Org::Apache::Arrow::Flatbuf::FixedSizeBinary
type = FixedSizeBinaryType.new(fb_type.byte_width)
when Org::Apache::Arrow::Flatbuf::Decimal
case fb_type.bit_width
when 128
type = Decimal128Type.new(fb_type.precision, fb_type.scale)
end
end
Field.new(fb_field.name, type, fb_field.nullable?)
end
Expand Down
Loading