Skip to content

Commit 604a164

Browse files
authored
Chore: refactor the detection of SQLMESH_MOCKED_STAR (#1554)
* Chore: refactor the detection of SQLMESH_MOCKED_STAR * Improve error message, switch to named_selects
1 parent 79fff7f commit 604a164

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

sqlmesh/core/macros.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def columns_to_types(self, model_name: str) -> t.Dict[str, exp.DataType]:
273273

274274
columns_to_types = self._schema.find(exp.to_table(model_name))
275275
if columns_to_types is None:
276-
raise SQLMeshError(f"Model '{model_name}' not found in the macro evaluator's context.")
276+
raise SQLMeshError(f"Schema for model '{model_name}' can't be statically determined.")
277277

278278
return columns_to_types # type: ignore
279279

sqlmesh/core/model/definition.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from sqlglot import diff, exp
1818
from sqlglot.diff import Insert, Keep
1919
from sqlglot.helper import ensure_list
20-
from sqlglot.optimizer.normalize_identifiers import normalize_identifiers
2120
from sqlglot.schema import MappingSchema, nested_set
2221
from sqlglot.time import format_time
2322

@@ -1028,8 +1027,10 @@ def update_schema(
10281027
schema, default_schema=default_schema, default_catalog=default_catalog
10291028
)
10301029

1031-
mocked_star = normalize_identifiers(SQLMESH_MOCKED_STAR, dialect=self.dialect)
1032-
if mocked_star.name in (self.columns_to_types or {}):
1030+
query = self._query_renderer.render(optimize=False)
1031+
if isinstance(query, exp.Subqueryable) and any(
1032+
name.upper() == SQLMESH_MOCKED_STAR for name in query.named_selects
1033+
):
10331034
# We reset the unoptimized query cache here as well to allow the model's query
10341035
# to be re-rendered so that the MacroEvaluator can resolve columns_to_types calls
10351036
# and get rid of the mocked star column

0 commit comments

Comments
 (0)