Skip to content

Commit

Permalink
fix handle_decimal when nil in the data (#103)
Browse files Browse the repository at this point in the history
* fix `handle_decimal` when `nil` in the data

Signed-off-by: Cocoa <[email protected]>

* fix ci

Signed-off-by: Cocoa <[email protected]>

---------

Signed-off-by: Cocoa <[email protected]>
  • Loading branch information
cocoa-xu authored Sep 12, 2024
1 parent a671464 commit 518723b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ jobs:
if: steps.cache-otp.outputs.cache-hit != 'true'
run: |
mkdir -p ./cache/otp
curl -fSL https://cocoa.build/otp/v${{ env.OTP_VERSION }}/otp-x86_64-apple-darwin.tar.gz -o ./cache/otp/otp-v${{ env.OTP_VERSION }}-x86_64-apple-darwin.tar.gz
curl -fSL https://github.com/cocoa-xu/otp-build/releases/download/v${{ env.OTP_VERSION }}/otp-x86_64-apple-darwin.tar.gz -o ./cache/otp/otp-v${{ env.OTP_VERSION }}-x86_64-apple-darwin.tar.gz
cd ./cache/otp
tar -xzf otp-v${{ env.OTP_VERSION }}-x86_64-apple-darwin.tar.gz
Expand Down
16 changes: 9 additions & 7 deletions lib/adbc_column.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1198,14 +1198,16 @@ defmodule Adbc.Column do
end

defp handle_decimal(decimal_data, bits, scale) do
Enum.map(decimal_data, fn data ->
<<decimal::signed-integer-size(bits)-little>> = data
Enum.map(decimal_data, fn
<<decimal::signed-integer-size(bits)-little>> ->
if decimal < 0 do
Decimal.new(-1, -decimal, -scale)
else
Decimal.new(1, decimal, -scale)
end

if decimal < 0 do
Decimal.new(-1, -decimal, -scale)
else
Decimal.new(1, decimal, -scale)
end
nil ->
nil
end)
end

Expand Down
24 changes: 24 additions & 0 deletions test/adbc_column_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ defmodule Adbc.Column.Test do
assert value == decode2 / :math.pow(10, scale)
end

test "nil in data" do
bitwidth = 128
precision = 19
scale = 10

decimal_with_nil = %Adbc.Column{
data: [nil],
metadata: nil,
name: nil,
nullable: true,
type: {:decimal, bitwidth, precision, scale}
}

assert %Adbc.Column{
name: nil,
type: {:decimal, ^bitwidth, ^precision, ^scale},
nullable: true,
metadata: nil,
data: [nil],
length: nil,
offset: nil
} = Adbc.Column.materialize(decimal_with_nil)
end

test "floats" do
value = 12345

Expand Down

0 comments on commit 518723b

Please sign in to comment.