@@ -4274,6 +4274,96 @@ def prepare_reference_call_project(*, tmp_path: Path, staging_from: str, mart_fr
42744274 return project_dir
42754275
42764276
4277+ _MACRO_REFERENCE_CALL_MODELS : tuple [str , ...] = ("customer_orders" , "customer_returns" )
4278+ _DIAGNOSTIC_FIELDS : tuple [str , ...] = (
4279+ "code" ,
4280+ "message" ,
4281+ "resource_name" ,
4282+ "path" ,
4283+ "line" ,
4284+ "column" ,
4285+ "help" ,
4286+ )
4287+
4288+
4289+ class MacroReferenceCallRuns (NamedTuple ):
4290+ """Repeated compiles: exit codes, macro executions, and the diagnostics of each run."""
4291+
4292+ returncodes : tuple [int , ...]
4293+ logged_calls : tuple [int , ...]
4294+ diagnostics : tuple [tuple [tuple [object , ...], ...], ...]
4295+
4296+
4297+ def prepare_macro_reference_call_project (* , tmp_path : Path ) -> Path :
4298+ """Write a project whose models share a macro returning a reference call compile rejects."""
4299+
4300+ return prepare_inline_project (
4301+ tmp_path = tmp_path ,
4302+ project_name = "orders" ,
4303+ repo_files = {
4304+ "sqlbuild_project.toml" : _REFERENCE_CALL_PROJECT_TOML ,
4305+ "sources/raw.yml" : _REFERENCE_CALL_SOURCES_YML ,
4306+ "models/staging/customers.sql" : (
4307+ 'MODEL (description "Customers.", materialized view);\n \n '
4308+ 'SELECT order_id AS customer_id FROM __source("raw_orders")\n '
4309+ ),
4310+ "models/marts/_sqlbuild/_macros/customer_relation.py" : (
4311+ '"""A macro that returns an unquoted reference call."""\n \n '
4312+ "import os\n \n \n "
4313+ "def customer_relation() -> str:\n "
4314+ ' """Return the customers relation, logging each execution."""\n '
4315+ f' path = os.environ.get("{ MACRO_CALL_LOG_ENV_VAR } ")\n '
4316+ " if path:\n "
4317+ ' with open(path, "a", encoding="utf-8") as log:\n '
4318+ ' log.write("customer_relation\\ n")\n '
4319+ ' return "__ref(customers)"\n '
4320+ ),
4321+ ** {
4322+ f"models/marts/{ name } .sql" : (
4323+ f'MODEL (description "{ name } .", materialized view);\n \n '
4324+ "SELECT customer_id FROM @customer_relation()\n "
4325+ )
4326+ for name in _MACRO_REFERENCE_CALL_MODELS
4327+ },
4328+ },
4329+ )
4330+
4331+
4332+ def _diagnostic_fields (item : dict [str , object ]) -> tuple [object , ...]:
4333+ return tuple (item .get (field ) for field in _DIAGNOSTIC_FIELDS )
4334+
4335+
4336+ def macro_reference_call_runs (
4337+ * , project_dir : Path , log_path : Path , engine : str , runs : int
4338+ ) -> MacroReferenceCallRuns :
4339+ """Compile `runs` times in fresh processes, keeping the macro call store between runs."""
4340+
4341+ env : dict [str , str ] = {
4342+ COMPILER_ENGINE_ENV_VAR : engine ,
4343+ REUSE_DISABLE_ENV_VAR : "1" ,
4344+ MACRO_CALL_LOG_ENV_VAR : str (log_path ),
4345+ }
4346+ returncodes : list [int ] = []
4347+ logged_calls : list [int ] = []
4348+ diagnostics : list [tuple [tuple [object , ...], ...]] = []
4349+ for _ in range (runs ):
4350+ _ = log_path .write_text ("" , encoding = "utf-8" )
4351+ compiled : subprocess .CompletedProcess [str ] = run_installed_sqb (
4352+ project_dir = project_dir , args = ("compile" , "--json" ), env = env
4353+ )
4354+ returncodes .append (compiled .returncode )
4355+ logged_calls .append (len (log_path .read_text (encoding = "utf-8" ).splitlines ()))
4356+ items : list [dict [str , object ]] = cast (
4357+ list [dict [str , object ]], json .loads (compiled .stdout )["diagnostics" ]
4358+ )
4359+ diagnostics .append (tuple (_diagnostic_fields (item ) for item in items ))
4360+ return MacroReferenceCallRuns (
4361+ returncodes = tuple (returncodes ),
4362+ logged_calls = tuple (logged_calls ),
4363+ diagnostics = tuple (diagnostics ),
4364+ )
4365+
4366+
42774367MACRO_CALL_STORE_ENGINE : str = "native-preview"
42784368STORE_ENVIRONMENT_REGION_VAR : str = "SQB_STORE_TEST_REGION"
42794369STORE_ARGUMENT_ENV_VAR : str = "STORE_TEST_ARGUMENT"
0 commit comments