-
Notifications
You must be signed in to change notification settings - Fork 635
bug: MSSQL syntax error #11021
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
Comments
Hey @albersonmiranda -- can you confirm that Ibis version number? |
@gforsyth I'm sorry, user mistake (I've previously installed |
@albersonmiranda -- that version is several years old. Current release is 10.3.1 -- is there something in your environment preventing an upgrade? |
Took from wrong venv again... @gforsyth here's correct output:
And query again:
|
Thanks @albersonmiranda ! This does look like a bug and a moderately gnarly one. I don't think your usage is incorrect. A simplified example shows that we're initially doing the right thing: [ins] In [42]: t = ibis.table({'a': 'str', 'b': 'str'})
[ins] In [43]: expr = t.mutate(c=t.a.isnull() & ibis.literal(False))
[nav] In [44]: ibis.to_sql(expr, dialect="mssql")
Out[44]:
SELECT
[t0].[a],
[t0].[b],
IIF((
[t0].[a] IS NULL
) AND (1 = 0), 1, 0) AS [c]
FROM [unbound_table_3] AS [t0] but when we then use an [ins] In [45]: expr2 = expr.mutate(d=ibis.ifelse(expr.c.notnull(), expr.c, False))
[ins] In [46]: ibis.to_sql(expr2, dialect="mssql")
Out[46]:
SELECT
[t1].[a],
[t1].[b],
IIF([t1].[c] <> 0, 1, 0) AS [c],
IIF(IIF([t1].[c] IS NOT NULL, [t1].[c], (1 = 0)), 1, 0) AS [d]
FROM (
SELECT
[t0].[a],
[t0].[b],
(
[t0].[a] IS NULL
) AND (1 = 0) AS [c]
FROM [unbound_table_3] AS [t0]
) AS [t1] |
@gforsyth I see you've got a pretty big backlog. Any pointers on which file the bug might be in, so I can take a shot at fixing it? |
@albersonmiranda -- I think this is likely a pretty deep rabbit hole. I took a poke at it, and in the MSSQL compiler, we call https://github.com/ibis-project/ibis/blob/main/ibis/backends/sql/compilers/mssql.py#L159-L178 as a part of that, we run this conversion code: conversions = {
name: ibis.ifelse(table_expr[name], 1, 0).cast(dt.boolean)
for name, typ in table_expr.schema().items()
if typ.is_boolean()
} to convert any boolean types to explicit I don't think the issue is actually here, but if we compare the table expression after those conversions are applied, there's an indication of the problem:
Note that the final projection (r2) includes I'm fairly sure that's the problem -- I suspect we may need an explicit rule in the |
@gforsyth Thanks for the detailed instructions! I'm getting close but not there yet:
For my example, this generates:
|
@albersonmiranda -- another option that might work is to add a rewrite rule to the mssql compiler |
What happened?
Nested
mutate()
generates invalid SQL query. See reprex adapted from https://github.com/posit-dev/pointblank/blob/main/pointblank/_interrogation.py#L91-L152:Is this a bug or misuse of
mutate()
?What version of ibis are you using?
10.3.1
What backend(s) are you using, if any?
MSSQL
Relevant log output
Code of Conduct
The text was updated successfully, but these errors were encountered: