Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions sqlmesh/core/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,18 @@ def load(self, context: GenericContext, update_schemas: bool = True) -> LoadedPr
self._config_mtimes = {path: max(mtimes) for path, mtimes in config_mtimes.items()}

macros, jinja_macros = self._load_scripts()

# All macros need to be known at parse time
standard_macros = macro.get_registry()
macro.set_registry(macros)

audits = self._load_audits(macros=macros, jinja_macros=jinja_macros)
models = self._load_models(
macros, jinja_macros, context.gateway or context.config.default_gateway, audits or None
)

macro.set_registry(standard_macros)

for model in models.values():
self._add_model_to_dag(model)

Expand Down
35 changes: 35 additions & 0 deletions tests/core/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5922,3 +5922,38 @@ def test_cache():
model = load_sql_based_model(expressions)
assert model.depends_on == {'"y"'}
assert model.copy(update={"depends_on_": {'"z"'}}).depends_on == {'"z"', '"y"'}


def test_snowflake_macro_func_as_table(tmp_path: Path):
init_example_project(tmp_path, dialect="duckdb")

custom_macro_file = tmp_path / "macros/custom_macros.py"
custom_macro_file.parent.mkdir(parents=True, exist_ok=True)
custom_macro_file.write_text("""
from sqlmesh import macro

@macro()
def custom_macro(evaluator, arg1, arg2):
return f"{arg1}{arg2}"
""")

new_snowflake_model_file = tmp_path / "models/new_model.sql"
new_snowflake_model_file.parent.mkdir(parents=True, exist_ok=True)
new_snowflake_model_file.write_text("""
MODEL (
name sqlmesh_example.test,
dialect snowflake,
);

@DEF(foo, foo);
@DEF(bar, bar);

SELECT * FROM @custom_macro(@foo, @bar)
""")

config = Config(model_defaults=ModelDefaultsConfig(dialect="duckdb"))
context = Context(paths=tmp_path, config=config)

query = context.get_model("sqlmesh_example.test").render_query()

assert t.cast(exp.Query, query).sql("snowflake") == 'SELECT * FROM "FOOBAR" AS "FOOBAR"'