Skip to content

bug: MSSQL syntax error #11021

Open
Open
@albersonmiranda

Description

@albersonmiranda

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:

# %%
import ibis
import pandas as pd

ibis.options.interactive = True

# dataframe
data = {"col1": [1, 2, 3], "col2": ["A", "B", "C"]}
df = pd.DataFrame(data)

# mssql con
con = ibis.mssql.connect(
    user=input("User: "),
    password=input("Password: "),
    host=input("Host: "),
    driver="SQL Server",
    database=input("db: ")
)

con.create_table("test_table", df)
t = con.table("test_table")

na_pass = False
column = "col1"
compare = 0

# %%
tbl = t.mutate(
    pb_is_good_1=t[column].isnull() & ibis.literal(na_pass),
    pb_is_good_2=t[column] > ibis.literal(compare),
)

tbl = tbl.mutate(pb_is_good_2=ibis.ifelse(tbl.pb_is_good_2.notnull(), tbl.pb_is_good_2, False))

result_tbl = tbl.mutate(pb_is_good_=tbl.pb_is_good_1 | tbl.pb_is_good_2).drop(
    "pb_is_good_1", "pb_is_good_2"
)

sql_query = ibis.to_sql(result_tbl)
print(sql_query)
# %%

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

Here's SQL generated:


SELECT
  [t1].[col1],
  [t1].[col2],
  IIF([t1].[pb_is_good_1] <> 0 OR [t1].[pb_is_good_2] <> 0, 1, 0) AS [pb_is_good_]
FROM (
  SELECT
    [t0].[col1],
    [t0].[col2],
    (
      [t0].[col1] IS NULL -------------------------------------- Invalid syntax
    ) AND (1 = 0) AS [pb_is_good_1],---------------------------- Invalid syntax
    IIF((
      [t0].[col1] > 0
    ) IS NOT NULL, [t0].[col1] > 0, (1 = 0)) AS [pb_is_good_2]
  FROM [test_table] AS [t0]
) AS [t1]

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIncorrect behavior inside of ibismssqlThe Microsoft SQL Server backend

    Type

    No type

    Projects

    Status

    backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions