Skip to content

Commit e15dcee

Browse files
authored
Fix: select * from external models for dbt, skip private variables when deep copying JinjaMacroRegistry, and properly parse jinja pre and post expressions (#943)
1 parent 9fc9f20 commit e15dcee

File tree

7 files changed

+41
-5
lines changed

7 files changed

+41
-5
lines changed

sqlmesh/core/model/common.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import typing as t
44

55
from pydantic import validator
6-
from sqlglot import exp, maybe_parse
6+
from sqlglot import exp
77
from sqlglot.helper import seq_get, split_num_words
88

99
from sqlmesh.core.dialect import parse
@@ -33,7 +33,11 @@ def parse_expression(
3333
return v
3434

3535
if isinstance(v, list):
36-
return [e for e in (maybe_parse(i) for i in v) if e]
36+
return [
37+
e
38+
for expressions in (parse(i) if not isinstance(i, exp.Expression) else [i] for i in v)
39+
for e in expressions
40+
]
3741

3842
if isinstance(v, str):
3943
return seq_get(parse(v), 0)

sqlmesh/core/model/definition.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,8 +623,8 @@ def render_post_statements(
623623
def render_definition(self, include_python: bool = True) -> t.List[exp.Expression]:
624624
result = super().render_definition(include_python=include_python)
625625
result.extend(self.pre_statements)
626-
result.append(self.query)
627626
result.extend(self.post_statements)
627+
result.append(self.query)
628628
return result
629629

630630
@property
@@ -1480,4 +1480,5 @@ def _single_value_or_tuple(values: t.Sequence) -> exp.Identifier | exp.Tuple:
14801480
),
14811481
"tags": _single_value_or_tuple,
14821482
"grain": _single_value_or_tuple,
1483+
"hash_raw_query": exp.convert,
14831484
}

sqlmesh/core/renderer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ def _optimize_query(self, query: exp.Expression) -> exp.Expression:
275275
# We don't want to normalize names in the schema because that's handled by the optimizer
276276
schema = ensure_schema(self.schema, dialect=self._dialect, normalize=False)
277277
query = t.cast(exp.Subqueryable, query.copy())
278+
for node, *_ in query.walk():
279+
node.type = None
278280

279281
try:
280282
qualify(

sqlmesh/utils/jinja.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ def merge(self, other: JinjaMacroRegistry) -> JinjaMacroRegistry:
309309
top_level_packages=[*self.top_level_packages, *other.top_level_packages],
310310
)
311311

312+
def __deepcopy__(self, memo: t.Dict[int, t.Any]) -> JinjaMacroRegistry:
313+
return JinjaMacroRegistry.parse_obj(self.dict())
314+
312315
def _parse_macro(self, name: str, package: t.Optional[str]) -> Template:
313316
cache_key = (package, name)
314317
if cache_key not in self._parser_cache:

tests/core/test_model.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
load_model,
2222
model,
2323
)
24+
from sqlmesh.core.model.common import parse_expression
2425
from sqlmesh.utils.date import to_date, to_datetime, to_timestamp
2526
from sqlmesh.utils.errors import ConfigError
2627
from sqlmesh.utils.metaprogramming import Executable
@@ -1185,3 +1186,8 @@ def test_is_breaking_change():
11851186
)
11861187
is None
11871188
)
1189+
1190+
1191+
def test_parse_expression_list_with_jinja():
1192+
input = ["{{ log('log message') }}", "GRANT SELECT ON TABLE foo TO DEV"]
1193+
assert input == [val.sql() for val in parse_expression(input)]

tests/fixtures/dbt/sushi_test/models/waiters.sql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
}}
66

77
SELECT DISTINCT
8-
waiter_id::INT AS waiter_id,
9-
ds::TEXT AS ds
8+
*
109
FROM {{ source('streaming', 'orders') }}
1110
{{ incremental_by_time('ds', 'ds') }}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
- name: raw.items
2+
columns:
3+
id: BIGINT
4+
name: VARCHAR
5+
price: DOUBLE
6+
ds: DATE
7+
- name: raw.order_items
8+
columns:
9+
id: BIGINT
10+
order_id: BIGINT
11+
item_id: BIGINT
12+
quantity: BIGINT
13+
ds: DATE
14+
- name: raw.orders
15+
columns:
16+
id: BIGINT
17+
customer_id: BIGINT
18+
waiter_id: BIGINT
19+
start_ts: TIMESTAMP
20+
end_ts: TIMESTAMP
21+
ds: DATE

0 commit comments

Comments
 (0)