|
46 | 46 | ) |
47 | 47 |
|
48 | 48 | if t.TYPE_CHECKING: |
| 49 | + from sqlglot.dialects.dialect import DialectType |
49 | 50 | from sqlmesh.core._typing import TableName |
50 | 51 | from sqlmesh.core.audit import ModelAudit, Audit |
51 | 52 | from sqlmesh.core.context import ExecutionContext |
@@ -1600,24 +1601,15 @@ def load_sql_based_model( |
1600 | 1601 | if prop.name.lower() == "audits": |
1601 | 1602 | model_audits = prop.args.get("value") |
1602 | 1603 |
|
1603 | | - meta_python_env = _python_env( |
1604 | | - expressions=meta, |
1605 | | - jinja_macro_references=None, |
| 1604 | + meta_renderer = _meta_renderer( |
| 1605 | + expression=meta, |
1606 | 1606 | module_path=module_path, |
1607 | | - macros=macros or macro.get_registry(), |
| 1607 | + macros=macros, |
| 1608 | + jinja_macros=jinja_macros, |
1608 | 1609 | variables=variables, |
1609 | 1610 | path=path, |
1610 | | - ) |
1611 | | - meta_renderer = ExpressionRenderer( |
1612 | | - meta, |
1613 | | - dialect, |
1614 | | - [], |
1615 | | - path=path, |
1616 | | - jinja_macro_registry=jinja_macros, |
1617 | | - python_env=meta_python_env, |
| 1611 | + dialect=dialect, |
1618 | 1612 | default_catalog=default_catalog, |
1619 | | - quote_identifiers=False, |
1620 | | - normalize_identifiers=False, |
1621 | 1613 | ) |
1622 | 1614 |
|
1623 | 1615 | rendered_meta_exprs = meta_renderer.render() |
@@ -1954,6 +1946,19 @@ def create_python_model( |
1954 | 1946 | ) |
1955 | 1947 | ) |
1956 | 1948 |
|
| 1949 | + dialect = kwargs.get("dialect") |
| 1950 | + name_renderer = _meta_renderer( |
| 1951 | + expression=d.parse_one(name, dialect=dialect), |
| 1952 | + module_path=module_path, |
| 1953 | + macros=macros, |
| 1954 | + jinja_macros=jinja_macros, |
| 1955 | + variables=variables, |
| 1956 | + path=path, |
| 1957 | + dialect=dialect, |
| 1958 | + default_catalog=kwargs.get("default_catalog"), |
| 1959 | + ) |
| 1960 | + name = t.cast(t.List[exp.Expression], name_renderer.render())[0].sql(dialect=dialect) |
| 1961 | + |
1957 | 1962 | parsed_depends_on, referenced_variables = ( |
1958 | 1963 | _parse_dependencies(python_env, entrypoint) if python_env is not None else (set(), set()) |
1959 | 1964 | ) |
@@ -2385,6 +2390,37 @@ def _refs_to_sql(values: t.Any) -> exp.Expression: |
2385 | 2390 | return exp.Tuple(expressions=values) |
2386 | 2391 |
|
2387 | 2392 |
|
| 2393 | +def _meta_renderer( |
| 2394 | + expression: exp.Expression, |
| 2395 | + module_path: Path, |
| 2396 | + path: Path, |
| 2397 | + jinja_macros: t.Optional[JinjaMacroRegistry] = None, |
| 2398 | + macros: t.Optional[MacroRegistry] = None, |
| 2399 | + dialect: DialectType = None, |
| 2400 | + variables: t.Optional[t.Dict[str, t.Any]] = None, |
| 2401 | + default_catalog: t.Optional[str] = None, |
| 2402 | +) -> ExpressionRenderer: |
| 2403 | + meta_python_env = _python_env( |
| 2404 | + expressions=expression, |
| 2405 | + jinja_macro_references=None, |
| 2406 | + module_path=module_path, |
| 2407 | + macros=macros or macro.get_registry(), |
| 2408 | + variables=variables, |
| 2409 | + path=path, |
| 2410 | + ) |
| 2411 | + return ExpressionRenderer( |
| 2412 | + expression, |
| 2413 | + dialect, |
| 2414 | + [], |
| 2415 | + path=path, |
| 2416 | + jinja_macro_registry=jinja_macros, |
| 2417 | + python_env=meta_python_env, |
| 2418 | + default_catalog=default_catalog, |
| 2419 | + quote_identifiers=False, |
| 2420 | + normalize_identifiers=False, |
| 2421 | + ) |
| 2422 | + |
| 2423 | + |
2388 | 2424 | META_FIELD_CONVERTER: t.Dict[str, t.Callable] = { |
2389 | 2425 | "start": lambda value: exp.Literal.string(value), |
2390 | 2426 | "cron": lambda value: exp.Literal.string(value), |
|
0 commit comments