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

Hugeint: SUM functions return tuples #31

Open
mpope9 opened this issue Jan 16, 2025 · 4 comments
Open

Hugeint: SUM functions return tuples #31

mpope9 opened this issue Jan 16, 2025 · 4 comments

Comments

@mpope9
Copy link

mpope9 commented Jan 16, 2025

Thanks for the library! I'm seeing odd behaviour when using SUM functions, tuples are returned. They always seem to start with 0 as well.

Duckdbex.fetch_all("SELECT SUM(1);")
> [[{0, 1}]]

This doesn't happen with MAX:

Duckdbex.fetch_all("SELECT MAX(1);")
> [[1]]
@03juan
Copy link

03juan commented Jan 19, 2025

It seems duckdb preemptively casts a SUM(param::INT) to a (signed) int128 HUGEINT.

https://github.com/duckdb/duckdb/blob/0024e5d4beba0185733df68642775e3f38e089cb/extension/core_functions/aggregate/distributive/sum.cpp#L31

which is represented as a tuple of signed and unsigned 64bit integers, {int64, uint64}.

case duckdb::LogicalTypeId::HUGEINT: {
duckdb::hugeint_t hugeint = duckdb::HugeIntValue::Get(value);
sink = enif_make_tuple2(env,
enif_make_int64(env, hugeint.upper),
enif_make_uint64(env, hugeint.lower)
);
return true;
}

@AlexR2D2
Copy link
Owner

It seems duckdb preemptively casts a SUM(param::INT) to a (signed) int128 HUGEINT.

looks like you are right

Image

so, i think, if you are expecting an integer number but got tuple/2 you can try this 'workaround'

iex > {:ok, res_ref} = Duckdbex.query(conn, "SELECT SUM(1);")
iex > [[res]] = Duckdbex.fetch_all(res_ref)
[[{0, 1}]]
iex [Вс :: 14] > Duckdbex.hugeint_to_integer(res)
1

@mpope9
Copy link
Author

mpope9 commented Jan 19, 2025

Neat! I think that workaround is good with me. Might be worth adding that as an example to the readme? Right now I'm just iterating over every cell and stripping the tuple.

@03juan
Copy link

03juan commented Jan 20, 2025

@AlexR2D2 I came across various issues in other language's libraries using duckdb, where people are confused and surprised by this behaviour.

Since Erlang natively support arbitrary size integers, would you consider automatically converting these for the user?

I understand that you would want to maintain feature parity but maybe it can live behind a config flag and function opts.

@AlexR2D2 AlexR2D2 changed the title SUM functions return tuples Hugeint: SUM functions return tuples Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants