Skip to content

Commit

Permalink
Fix precision loss due to JSON float parsing (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
jenskdsgn authored Apr 3, 2024
1 parent d98c938 commit 0e43bd7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,11 @@ def format_body_response(body, format)
end

def format_from_json_compact(body)
JSON.parse(body)
parse_json_payload(body)
end

def format_from_json_compact_each_row_with_names_and_types(body)
rows = body.split("\n").map { |row| JSON.parse(row) }
rows = body.split("\n").map { |row| parse_json_payload(row) }
names, types, *data = rows

meta = names.zip(types).map do |name, type|
Expand All @@ -252,6 +252,10 @@ def format_from_json_compact_each_row_with_names_and_types(body)
'data' => data
}
end

def parse_json_payload(payload)
JSON.parse(payload, decimal_class: BigDecimal)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def up
t.datetime :datetime64, precision: 3, null: true
t.string :byte_array, null: true
t.uuid :relation_uuid
t.decimal :decimal_value, precision: 38, scale: 16, null: true
end
end
end
13 changes: 13 additions & 0 deletions spec/single/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,19 @@ class ModelPk < ActiveRecord::Base
end
end

describe 'decimal column type' do
let!(:record1) do
Model.create!(event_name: 'some event', decimal_value: BigDecimal('95891.74'))
end

# If converted to float, the value would be 9589174.000000001. This happened previously
# due to JSON parsing of numeric values to floats.
it 'keeps precision' do
decimal_value = Model.first.decimal_value
expect(decimal_value).to eq(BigDecimal('95891.74'))
end
end

describe '#settings' do
it 'works' do
sql = Model.settings(optimize_read_in_order: 1, cast_keep_nullable: 1).to_sql
Expand Down

0 comments on commit 0e43bd7

Please sign in to comment.