Skip to content

Commit

Permalink
fix: Decimal precision problem when converting BigDecimal to Arrow::D…
Browse files Browse the repository at this point in the history
…ecimalXXX
  • Loading branch information
saluzafa committed Oct 17, 2021
1 parent 1c5eab8 commit 9fbc6f7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 16 deletions.
16 changes: 8 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
PATH
remote: .
specs:
parqueteur (1.3.0)
parqueteur (1.3.1)
red-parquet (~> 5.0)

GEM
remote: https://rubygems.org/
specs:
bigdecimal (3.0.0)
extpp (0.0.9)
gio2 (3.4.6)
gobject-introspection (= 3.4.6)
glib2 (3.4.6)
bigdecimal (3.0.2)
extpp (0.1.0)
gio2 (3.4.9)
gobject-introspection (= 3.4.9)
glib2 (3.4.9)
native-package-installer (>= 1.0.3)
pkg-config (>= 1.3.5)
gobject-introspection (3.4.6)
glib2 (= 3.4.6)
gobject-introspection (3.4.9)
glib2 (= 3.4.9)
native-package-installer (1.1.1)
pkg-config (1.4.6)
rake (13.0.6)
Expand Down
3 changes: 2 additions & 1 deletion examples/hello-world.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class FooParquetConverter < Parqueteur::Converter
'id' => i,
'my_string_array' => %w[a b c],
'my_date' => Date.today,
'my_decimal' => BigDecimal('789000.5678'),
'my_decimal' => BigDecimal('0.03'),
'my_int' => rand(1..10),
'my_map' => { 'a' => 'b' },
'my_string' => 'Hello World',
Expand Down Expand Up @@ -52,5 +52,6 @@ class FooParquetConverter < Parqueteur::Converter
# Arrow Table
table = converter.to_arrow_table
table.each_record do |record|
# pp record['my_decimal'].to_f
pp record.to_h
end
17 changes: 14 additions & 3 deletions lib/parqueteur/types/decimal128_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@
module Parqueteur
module Types
class Decimal128Type < Parqueteur::Type
def initialize(options = {}, &block)
@scale = options.fetch(:scale)
@precision = options.fetch(:precision)
@format_str = "%.#{@scale}f"
super(options, &block)
end

def build_value_array(values)
Arrow::Decimal128ArrayBuilder.build(@arrow_type, values)
Arrow::Decimal128ArrayBuilder.build(
@arrow_type,
values.map do |value|
Arrow::Decimal128.new(format(@format_str, BigDecimal(value)))
end
)
end

def arrow_type_builder
Arrow::Decimal128DataType.new(
precision: @options.fetch(:precision),
scale: @options.fetch(:scale)
@precision, @scale
)
end
end
Expand Down
17 changes: 14 additions & 3 deletions lib/parqueteur/types/decimal256_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@
module Parqueteur
module Types
class Decimal256Type < Parqueteur::Type
def initialize(options = {}, &block)
@scale = options.fetch(:scale)
@precision = options.fetch(:precision)
@format_str = "%.#{@scale}f"
super(options, &block)
end

def build_value_array(values)
Arrow::Decimal256ArrayBuilder.build(@arrow_type, values)
Arrow::Decimal256ArrayBuilder.build(
@arrow_type,
values.map do |value|
Arrow::Decimal256.new(format(@format_str, BigDecimal(value)))
end
)
end

def arrow_type_builder
Arrow::Decimal256DataType.new(
precision: @options.fetch(:precision),
scale: @options.fetch(:scale)
@precision, @scale
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/parqueteur/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Parqueteur
VERSION = '1.3.0'
VERSION = '1.3.1'
end

0 comments on commit 9fbc6f7

Please sign in to comment.