Skip to content

Commit

Permalink
Fix CTE insertion position when the model uses WITH RECURSIVE (#7350)
Browse files Browse the repository at this point in the history
  • Loading branch information
willbryant committed Apr 20, 2023
1 parent ada8860 commit a94a844
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20230420-123221.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Fix CTE insertion position when the model uses WITH RECURSIVE
time: 2023-04-20T12:32:21.432848+12:00
custom:
Author: willbryant
Issue: "7350"
4 changes: 4 additions & 0 deletions core/dbt/compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ def _inject_ctes_into_sql(self, sql: str, ctes: List[InjectedCTE]) -> str:
for token in parsed.tokens:
if token.is_keyword and token.normalized == "WITH":
with_stmt = token
elif token.is_keyword and token.normalized == "RECURSIVE" and with_stmt is not None:
with_stmt = token
break
elif not token.is_whitespace and with_stmt is not None:
break

if with_stmt is None:
Expand Down
1 change: 1 addition & 0 deletions third-party-stubs/sqlparse/sql.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ from typing import Tuple, Iterable
class Token:
def __init__(self, ttype, value): ...
is_keyword: bool
is_whitespace: bool
normalized: str

class TokenList(Token):
Expand Down

0 comments on commit a94a844

Please sign in to comment.