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(mssql): translates null booleans incorrectly #9109

Closed
1 task done
NickCrews opened this issue May 2, 2024 · 4 comments
Closed
1 task done

bug(mssql): translates null booleans incorrectly #9109

NickCrews opened this issue May 2, 2024 · 4 comments
Labels
bug Incorrect behavior inside of ibis

Comments

@NickCrews
Copy link
Contributor

What happened?

import ibis

e = ibis.null().cast(bool)
ibis.to_sql(e, "mssql")

Generated SQL:

SELECT
  IIF([t0].[Cast(None, boolean)] <> 0, 1, 0) AS [Cast(None, boolean)]
FROM (
  SELECT
    NULL AS [Cast(None, boolean)]
) AS [t0]

which evaluates to 0, not None.

This is caused by

def _to_sqlglot(
self, expr: ir.Expr, *, limit: str | None = None, params=None, **_: Any
):
"""Compile an Ibis expression to a sqlglot object."""
table_expr = expr.as_table()
conversions = {
name: ibis.ifelse(table_expr[name], 1, 0).cast("boolean")
for name, typ in table_expr.schema().items()
if typ.is_boolean()
}
if conversions:
table_expr = table_expr.mutate(**conversions)
return super()._to_sqlglot(table_expr, limit=limit, params=params)

I discovered this in #9097, but I don't want to have to fix it there.

What version of ibis are you using?

main

What backend(s) are you using, if any?

mssql

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@NickCrews NickCrews added the bug Incorrect behavior inside of ibis label May 2, 2024
@cpcloud
Copy link
Member

cpcloud commented May 2, 2024

I don't think this is possible to fix. MS SQL doesn't have a boolean type.

@NickCrews
Copy link
Contributor Author

Would changing ibis.ifelse(table_expr[name], 1, 0).cast("boolean") to something like ibis.case().when(x.isnull(), ibis.null()).when(x, 1).else_(0).end() be the right direction?

@cpcloud
Copy link
Member

cpcloud commented May 2, 2024

Perhaps. Might be worth trying that out in a PR and seeing if it works!

@NickCrews
Copy link
Contributor Author

Ok, to be honest I don't use mssql so I'm not motivated to fix this myself. Feel free to close or move to backlog or whatever you think is best. Thanks!

@cpcloud cpcloud closed this as not planned Won't fix, can't repro, duplicate, stale Jun 2, 2024
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
Status: done
Development

No branches or pull requests

2 participants