Skip to content

Commit

Permalink
raise error if trying to run a cte and using --with
Browse files Browse the repository at this point in the history
  • Loading branch information
edublancas committed Aug 11, 2023
1 parent 7c749e9 commit 143edc1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/sql/magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions src/tests/test_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

0 comments on commit 143edc1

Please # to comment.