Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Initialize sqlparse lexer and tweak order of setting compilation fields
  • Loading branch information
gshank committed Jul 26, 2023
commit 33d9437dcb6eebed21dec02a1173805cc342d60e
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20230726-104448.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Improve handling of CTE injection with ephemeral models
time: 2023-07-26T10:44:48.888451-04:00
custom:
Author: gshank
Issue: "8213"
18 changes: 12 additions & 6 deletions core/dbt/compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import networkx as nx # type: ignore
import os
import pickle
import sqlparse

from collections import defaultdict
from typing import List, Dict, Any, Tuple, Optional
Expand Down Expand Up @@ -36,6 +35,7 @@
from dbt.events.format import pluralize
import dbt.tracking
import dbt.task.list as list_task
import sqlparse

graph_file_name = "graph.gpickle"

Expand Down Expand Up @@ -378,16 +378,16 @@ def _recursively_prepend_ctes(

_add_prepended_cte(prepended_ctes, InjectedCTE(id=cte.id, sql=sql))

injected_sql = inject_ctes_into_sql(
model.compiled_code,
prepended_ctes,
)
# Check again before updating for multi-threading
if not model.extra_ctes_injected:
injected_sql = inject_ctes_into_sql(
model.compiled_code,
prepended_ctes,
)
model.extra_ctes_injected = True
model._pre_injected_sql = model.compiled_code
model.compiled_code = injected_sql
model.extra_ctes = prepended_ctes
model.extra_ctes_injected = True

# if model.extra_ctes is not set to prepended ctes, something went wrong
return model, model.extra_ctes
Expand Down Expand Up @@ -563,6 +563,12 @@ def inject_ctes_into_sql(sql: str, ctes: List[InjectedCTE]) -> str:
if len(ctes) == 0:
return sql

# Make sure Lexer for sqlparse 0.4.4 is initialized
from sqlparse.lexer import Lexer # type: ignore

if hasattr(Lexer, "get_default_instance"):
Lexer.get_default_instance()

parsed_stmts = sqlparse.parse(sql)
parsed = parsed_stmts[0]

Expand Down