From 6a3796d3855af0f087afd912029eff59f2a585fe Mon Sep 17 00:00:00 2001 From: Joel Natividad <1980690+jqnatividad@users.noreply.github.com> Date: Sat, 18 Jan 2025 17:15:53 -0500 Subject: [PATCH 1/4] fix: replace `unreachable` with `polars_bail` as its actually reachable and causes a panic when attempting unsupported ops like INSERT, UPDATE, DELETE, etc. --- crates/polars-sql/src/context.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/polars-sql/src/context.rs b/crates/polars-sql/src/context.rs index 6a76f290d4dd..f2e9b05b05d7 100644 --- a/crates/polars-sql/src/context.rs +++ b/crates/polars-sql/src/context.rs @@ -349,7 +349,7 @@ impl SQLContext { op => { let op = match op { SetExpr::SetOperation { op, .. } => op, - _ => unreachable!(), + _ => polars_bail!(SQLInterface: "'{}' is currently unsupported", op), }; polars_bail!(SQLInterface: "'{}' operation is currently unsupported", op) }, From 8e58e2398d470f8467d52d5ae79785221365ee5e Mon Sep 17 00:00:00 2001 From: Joel Natividad <1980690+jqnatividad@users.noreply.github.com> Date: Wed, 22 Jan 2025 16:56:10 -0500 Subject: [PATCH 2/4] fix: simplify unsupported SQL query handling in `process_query` --- crates/polars-sql/src/context.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/crates/polars-sql/src/context.rs b/crates/polars-sql/src/context.rs index f2e9b05b05d7..0cd3c50950ba 100644 --- a/crates/polars-sql/src/context.rs +++ b/crates/polars-sql/src/context.rs @@ -347,10 +347,6 @@ impl SQLContext { } }, op => { - let op = match op { - SetExpr::SetOperation { op, .. } => op, - _ => polars_bail!(SQLInterface: "'{}' is currently unsupported", op), - }; polars_bail!(SQLInterface: "'{}' operation is currently unsupported", op) }, } From 97986a5a8b0658a98e6225237ae0d55ae1a1f09b Mon Sep 17 00:00:00 2001 From: Joel Natividad <1980690+jqnatividad@users.noreply.github.com> Date: Wed, 22 Jan 2025 16:58:11 -0500 Subject: [PATCH 3/4] add test for unsupported update query the test may be more complicated than required, but I wanted to base the test case on the bug report here - https://github.com/dathere/qsv/issues/2450 --- py-polars/tests/unit/sql/test_set_ops.py | 48 ++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/py-polars/tests/unit/sql/test_set_ops.py b/py-polars/tests/unit/sql/test_set_ops.py index f148d561c31b..1a6d362ae884 100644 --- a/py-polars/tests/unit/sql/test_set_ops.py +++ b/py-polars/tests/unit/sql/test_set_ops.py @@ -89,6 +89,54 @@ def test_except_intersect_all_unsupported(op: str, op_subtype: str) -> None: pl.sql(f"SELECT * FROM df1 {op} {op_subtype} SELECT * FROM df2") +def test_update_statement_error() -> None: + df_large = pl.DataFrame( + { # noqa: F841 + "FQDN": ["c.ORG.na", "a.COM.na"], + "NS1": ["ns1.c.org.na", "ns1.d.net.na"], + "NS2": ["ns2.c.org.na", "ns2.d.net.na"], + "NS3": ["ns3.c.org.na", "ns3.d.net.na"], + } + ) + df_small = pl.DataFrame( + { # noqa: F841 + "FQDN": ["c.org.na"], + "NS1": ["ns1.c.org.na|127.0.0.1"], + "NS2": ["ns2.c.org.na|127.0.0.1"], + "NS3": ["ns3.c.org.na|127.0.0.1"], + } + ) + + # Create a context and register the tables + ctx = pl.SQLContext() + ctx.register("large", df_large) + ctx.register("small", df_small) + + with pytest.raises( + SQLInterfaceError, + match="'UPDATE large SET FQDN = u.FQDN, NS1 = u.NS1, NS2 = u.NS2, NS3 = u.NS3 FROM u WHERE large.FQDN = u.FQDN' operation is currently unsupported", + ): + ctx.execute(""" + WITH u AS ( + SELECT + small.FQDN, + small.NS1, + small.NS2, + small.NS3 + FROM small + INNER JOIN large ON small.FQDN = large.FQDN + ) + UPDATE large + SET + FQDN = u.FQDN, + NS1 = u.NS1, + NS2 = u.NS2, + NS3 = u.NS3 + FROM u + WHERE large.FQDN = u.FQDN + """) + + @pytest.mark.parametrize("op", ["EXCEPT", "INTERSECT", "UNION"]) def test_except_intersect_errors(op: str) -> None: df1 = pl.DataFrame({"x": [1, 9, 1, 1], "y": [2, 3, 4, 4], "z": [5, 5, 5, 5]}) # noqa: F841 From 319842d8b24cc3404398f027e518e2b59a29ac43 Mon Sep 17 00:00:00 2001 From: Joel Natividad <1980690+jqnatividad@users.noreply.github.com> Date: Wed, 22 Jan 2025 17:04:33 -0500 Subject: [PATCH 4/4] ruff --- py-polars/tests/unit/sql/test_set_ops.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/py-polars/tests/unit/sql/test_set_ops.py b/py-polars/tests/unit/sql/test_set_ops.py index 1a6d362ae884..d56b56f213a0 100644 --- a/py-polars/tests/unit/sql/test_set_ops.py +++ b/py-polars/tests/unit/sql/test_set_ops.py @@ -91,7 +91,7 @@ def test_except_intersect_all_unsupported(op: str, op_subtype: str) -> None: def test_update_statement_error() -> None: df_large = pl.DataFrame( - { # noqa: F841 + { "FQDN": ["c.ORG.na", "a.COM.na"], "NS1": ["ns1.c.org.na", "ns1.d.net.na"], "NS2": ["ns2.c.org.na", "ns2.d.net.na"], @@ -99,7 +99,7 @@ def test_update_statement_error() -> None: } ) df_small = pl.DataFrame( - { # noqa: F841 + { "FQDN": ["c.org.na"], "NS1": ["ns1.c.org.na|127.0.0.1"], "NS2": ["ns2.c.org.na|127.0.0.1"], @@ -118,7 +118,7 @@ def test_update_statement_error() -> None: ): ctx.execute(""" WITH u AS ( - SELECT + SELECT small.FQDN, small.NS1, small.NS2, @@ -126,8 +126,8 @@ def test_update_statement_error() -> None: FROM small INNER JOIN large ON small.FQDN = large.FQDN ) - UPDATE large - SET + UPDATE large + SET FQDN = u.FQDN, NS1 = u.NS1, NS2 = u.NS2,