From 143edc15346a1d3ba9fc9cfac995688fbf6ee28c Mon Sep 17 00:00:00 2001 From: Eduardo Blancas Date: Fri, 11 Aug 2023 13:19:48 -0600 Subject: [PATCH] raise error if trying to run a cte and using --with --- src/sql/magic.py | 5 +++++ src/tests/test_magic.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/sql/magic.py b/src/sql/magic.py index 1fd70e179..56c51769d 100644 --- a/src/sql/magic.py +++ b/src/sql/magic.py @@ -426,6 +426,11 @@ def interactive_execute_wrapper(**kwargs): else: with_ = None else: + if args.with_: + raise exceptions.UsageError( + "Cannot use --with with CTEs, remove --with and re-run the cell" + ) + with_ = None # Create the interactive slider diff --git a/src/tests/test_magic.py b/src/tests/test_magic.py index 8ced1344a..710832f83 100644 --- a/src/tests/test_magic.py +++ b/src/tests/test_magic.py @@ -1700,3 +1700,36 @@ def test_can_run_cte_that_references_a_table_whose_name_is_the_same_as_a_snippet "last_name": ("Shakespeare",), "year_of_death": (1616,), } + + +def test_error_when_running_a_cte_and_passing_with_argument(ip): + # randomize the name to avoid collisions + identifier = "shakespeare_" + str(uuid.uuid4())[:8] + + # create table + ip.run_cell( + f"""%%sql +create table {identifier} as select * from author where last_name = 'Shakespeare' +""" + ) + + # store a snippet with the same name + ip.run_cell( + f"""%%sql --save {identifier} +select * from author where last_name = 'some other last name' +""" + ) + + with pytest.raises(UsageError) as excinfo: + ip.run_cell( + f"""%%sql --with {identifier} +with author_subset as ( + select * from {identifier} +) +select * from author_subset +""" + ) + + assert "Cannot use --with with CTEs, remove --with and re-run the cell" in str( + excinfo.value + )