Skip to content
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

fix handle_decimal when nil in the data #103

Merged
merged 2 commits into from
Sep 12, 2024
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
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