-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for encoding and decoding Decimals #93
Changes from all commits
11f0eed
c223c94
feaa179
9af7a3d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -316,6 +316,34 @@ defmodule AvroEx.Decode.Test do | |
|
||
assert Time.truncate(time, :millisecond) == now | ||
end | ||
|
||
test "decimal" do | ||
schema = "test/fixtures/decimal.avsc" |> File.read!() |> AvroEx.decode_schema!() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it would be useful to test if round tripping the value through the encoder results in the same result There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed. There are now some round-trip tests in the |
||
# This reference file was encoded using avro's reference implementation: | ||
# | ||
# ```java | ||
# Conversions.DecimalConversion conversion = new Conversions.DecimalConversion(); | ||
# BigDecimal bigDecimal = new BigDecimal(valueInString); | ||
# return conversion.toBytes(bigDecimal, schema, logicalType); | ||
# ``` | ||
result = AvroEx.decode!(schema, File.read!("test/fixtures/decimal.avro"), decimals: :exact) | ||
|
||
assert result == %{ | ||
"decimalField1" => Decimal.new("1.23456789E-7"), | ||
"decimalField2" => Decimal.new("4.54545454545E-35"), | ||
"decimalField3" => Decimal.new("-111111111.1"), | ||
"decimalField4" => Decimal.new("5.3E-11") | ||
} | ||
|
||
result_approximate_values = AvroEx.decode!(schema, File.read!("test/fixtures/decimal.avro")) | ||
|
||
assert result_approximate_values == %{ | ||
"decimalField1" => 1.2345678900000002e-7, | ||
"decimalField2" => 4.54545454545e-35, | ||
"decimalField3" => -111_111_111.10000001, | ||
"decimalField4" => 5.3e-11 | ||
} | ||
end | ||
end | ||
|
||
describe "DecodingError" do | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[Í | ||
iÕ5ѽÅÊ95 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"namespace": "example.avro", | ||
"type": "record", | ||
"name": "decimalContainer", | ||
"fields": [ | ||
{ | ||
"name": "decimalField1", | ||
"type": { | ||
"type": "bytes", | ||
"scale": 15, | ||
"precision": 11, | ||
"logicalType": "decimal" | ||
} | ||
}, | ||
{ | ||
"name": "decimalField2", | ||
"type": { | ||
"type": "bytes", | ||
"scale": 46, | ||
"precision": 46, | ||
"logicalType": "decimal" | ||
} | ||
}, | ||
{ | ||
"name": "decimalField3", | ||
"type": { | ||
"type": "bytes", | ||
"scale": 1, | ||
"precision": 46, | ||
"logicalType": "decimal" | ||
} | ||
}, | ||
{ | ||
"name": "decimalField4", | ||
"type": { | ||
"type": "bytes", | ||
"scale": 12, | ||
"precision": 46, | ||
"logicalType": "decimal" | ||
} | ||
} | ||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are you sure that this will terminate for all input values?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hum, I am not. Should we stop when we reach 64 bytes ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On second thought: I don't actually see a situation where this won't terminate. The value of
bits
can only by positive multiples of 8, so we're always bound to eventually get a2^bits
that's bigger than any arbitrary value.WDYT ?