From a94a8441d4465565a29c715e5624dad5895e589f Mon Sep 17 00:00:00 2001 From: Will Bryant Date: Thu, 20 Apr 2023 12:25:11 +1200 Subject: [PATCH] Fix CTE insertion position when the model uses WITH RECURSIVE (#7350) --- .changes/unreleased/Fixes-20230420-123221.yaml | 6 ++++++ core/dbt/compilation.py | 4 ++++ third-party-stubs/sqlparse/sql.pyi | 1 + 3 files changed, 11 insertions(+) create mode 100644 .changes/unreleased/Fixes-20230420-123221.yaml diff --git a/.changes/unreleased/Fixes-20230420-123221.yaml b/.changes/unreleased/Fixes-20230420-123221.yaml new file mode 100644 index 00000000000..3cdfb568cc3 --- /dev/null +++ b/.changes/unreleased/Fixes-20230420-123221.yaml @@ -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" diff --git a/core/dbt/compilation.py b/core/dbt/compilation.py index e5503af462e..b7244642696 100644 --- a/core/dbt/compilation.py +++ b/core/dbt/compilation.py @@ -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: diff --git a/third-party-stubs/sqlparse/sql.pyi b/third-party-stubs/sqlparse/sql.pyi index d4700ea71f0..09ea56990f8 100644 --- a/third-party-stubs/sqlparse/sql.pyi +++ b/third-party-stubs/sqlparse/sql.pyi @@ -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):