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

bug(duckdb): duckdb backend should add in CAST for some bind parameters #3629

Closed
gforsyth opened this issue Mar 22, 2022 · 2 comments · Fixed by #3630
Closed

bug(duckdb): duckdb backend should add in CAST for some bind parameters #3629

gforsyth opened this issue Mar 22, 2022 · 2 comments · Fixed by #3630
Labels
bug Incorrect behavior inside of ibis
Milestone

Comments

@gforsyth
Copy link
Member

DuckDB casts bind parameters ? to strings which leads to binder errors with some queries

If we have a small tpch dataset:

import duckdb
con = duckdb.connect("tpch.ddb")
con.execute("CALL dbgen(sf=0.1)")

import ibis
con = ibis.duckdb.connect("tpch.ddb")

t = con.table('orders')
expr = t.aggregate(high_line_count=(t.o_orderpriority.case().when('1-URGENT', 1).else_(0).end().sum()

expr.execute()

raises

RuntimeError: Binder Error: No function matches the given name and argument types 'sum(VARCHAR)'. You might need to add explicit type casts.
	Candidate functions:
	sum(DECIMAL) -> DECIMAL
	sum(SMALLINT) -> HUGEINT
	sum(INTEGER) -> HUGEINT
	sum(BIGINT) -> HUGEINT
	sum(HUGEINT) -> HUGEINT
	sum(DOUBLE) -> DOUBLE

LINE 1: SELECT sum(CASE WHEN (t0.o_orderpriority = ?) ...

because our generated SQL doesn't have explicit casts:

print(expr.compile())
SELECT sum(CASE WHEN (t0.o_orderpriority = ?) THEN ? ELSE ? END) AS high_line_count 
FROM orders AS t0

we want to generate

SELECT sum(CASE WHEN (t0.o_orderpriority = ?) THEN cast(? as INTEGER) ELSE cast(? as INTEGER) END) AS high_line_count FROM orders as t0
@cpcloud
Copy link
Member

cpcloud commented Mar 22, 2022

Interestingly enough, postgres doesn't seem to have a problem with a similar query:

def test_case_agg(alltypes):
    expr = alltypes.aggregate(
        high_line_count=(
            alltypes.string_col.case().when("1", 1).else_(0).end().sum()
        )
    )
    expr.execute()

@cpcloud
Copy link
Member

cpcloud commented Mar 22, 2022

Ah that's probably because of duckdb/duckdb#2972 (comment)

gforsyth added a commit to gforsyth/ibis that referenced this issue Mar 22, 2022
@cpcloud cpcloud added the bug Incorrect behavior inside of ibis label Mar 22, 2022
@cpcloud cpcloud added this to the 3.0.0 milestone Mar 22, 2022
gforsyth added a commit to gforsyth/ibis that referenced this issue Mar 22, 2022
gforsyth added a commit to gforsyth/ibis that referenced this issue Mar 22, 2022
gforsyth added a commit to gforsyth/ibis that referenced this issue Mar 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Incorrect behavior inside of ibis
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants