From 4284e05fdcb587f14b394d072585dde4a1b9151c Mon Sep 17 00:00:00 2001 From: Nick Muoh Date: Fri, 4 Dec 2026 00:56:25 -0800 Subject: [PATCH 1/2] test(macros): show annotation aliases lose SQL coercion Model rendering re-executes serialized macros. An alias used only in a type annotation, such as SQL imported as SQLType, is left out of that environment. Hint resolution then fails and the macro receives the raw column instead of a SQL string. Reported in nickmuoh/sqlmesh#4. Signed-off-by: Nick Muoh Co-authored-by: Nick Muoh --- tests/core/test_macros.py | 71 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/tests/core/test_macros.py b/tests/core/test_macros.py index fd5a6daf7e..e7babf3060 100644 --- a/tests/core/test_macros.py +++ b/tests/core/test_macros.py @@ -1,5 +1,9 @@ +import importlib.util +import sys +import textwrap import typing as t from datetime import datetime, date +from pathlib import Path import pytest from sqlglot import MappingSchema, ParseError, exp, parse_one @@ -1362,3 +1366,70 @@ def test_safe_arithmetic_macros_keep_precedence_after_optimization( rendered_projection = model.render_query_or_raise().selects[0] assert rendered_projection.sql() == f'{expected} AS "result"' + + +def test_macro_annotation_alias_coerces_after_serialization(tmp_path: Path) -> None: + # Rendering re-executes the serialized macro. `SQL` is injected into that + # environment, but an alias used only in an annotation is not, so coercion + # is skipped and the macro receives the raw expression. + macro_path = tmp_path / "macros" / "annotation_alias.py" + macro_path.parent.mkdir() + macro_path.write_text( + textwrap.dedent( + """ + from __future__ import annotations + + from sqlglot import exp + from sqlmesh import SQL as SQLType, macro + from sqlmesh.core.macros import MacroEvaluator, SQL + + @macro() + def plain_sql_annotation(evaluator: MacroEvaluator, fallback_value: SQL): + if not isinstance(fallback_value, str): + raise TypeError( + f"plain expected SQL string, got {type(fallback_value).__name__}" + ) + return exp.Literal.string(fallback_value) + + @macro() + def alias_sql_annotation(evaluator: MacroEvaluator, fallback_value: SQLType): + if not isinstance(fallback_value, str): + got = type(fallback_value).__name__ + raise TypeError(f"alias expected SQL string, got {got}: {fallback_value}") + return exp.Literal.string(fallback_value) + """ + ) + ) + + module_name = "annotation_alias_macros" + spec = importlib.util.spec_from_file_location(module_name, macro_path) + assert spec is not None and spec.loader is not None + module = importlib.util.module_from_spec(spec) + sys.modules[module_name] = module + + def load(name: str, macro_name: str): + return load_sql_based_model( + d.parse( + f""" + MODEL (name db.{name}); + SELECT @{macro_name}(es.seat_id) AS result + """ + ), + module_path=tmp_path, + path=tmp_path / "models" / f"{name}.sql", + dialect="duckdb", + ) + + try: + spec.loader.exec_module(module) + expected = "SELECT 'es.seat_id' AS \"result\"" + assert load("plain_annotation", "plain_sql_annotation").render_query_or_raise().sql() == ( + expected + ) + assert load("alias_annotation", "alias_sql_annotation").render_query_or_raise().sql() == ( + expected + ) + finally: + for name in ("plain_sql_annotation", "alias_sql_annotation"): + macro.get_registry().pop(name, None) + sys.modules.pop(module_name, None) From 28259b46ab9c3552760691236c93269b8eaee33c Mon Sep 17 00:00:00 2001 From: Nick Muoh Date: Fri, 4 Dec 2026 01:23:22 -0800 Subject: [PATCH 2/2] test(macros): pop temporary macros from the live registry get_registry() returns a copy, so popping it left the serialized annotation macros registered until the module fixture restored state. Signed-off-by: Nick Muoh Co-authored-by: Nick Muoh --- tests/core/test_macros.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/core/test_macros.py b/tests/core/test_macros.py index e7babf3060..d7f8bd29d8 100644 --- a/tests/core/test_macros.py +++ b/tests/core/test_macros.py @@ -1431,5 +1431,5 @@ def load(name: str, macro_name: str): ) finally: for name in ("plain_sql_annotation", "alias_sql_annotation"): - macro.get_registry().pop(name, None) + macro.registry().pop(name, None) sys.modules.pop(module_name, None)