sqlmesh icon indicating copy to clipboard operation
sqlmesh copied to clipboard

Should python macro functions work in audits?

Open patricksurry opened this issue 2 years ago • 2 comments

In the example project, I added macros/constants.py containing

from sqlmesh import macro
from sqlmesh.core.macros import MacroEvaluator

@macro()
def min_item(
    evaluator: MacroEvaluator
) -> int:
    return 0

I modified models/incremental_model.sql to reference it like this:

...
WHERE
  event_date BETWEEN @start_date AND @end_date
  and item_id > @min_item()

which works just fine. But if I also modify audits/assert_positive_order_ids.sql to reference it:

...
SELECT
  *
FROM @this_model
WHERE
  item_id < @min_item()

then it fails to find the macro:

sqlmesh-example % sqlmesh audit                                   
Error: Macro 'min_item' does not exist.

sqlmesh-example % sqlmesh --version
0.113.0

patricksurry avatar Jul 26 '24 13:07 patricksurry

I believe we don't scan audits for macro references when constructing the model's python environment. This is why the macro can't be found.

@georgesittas , @Themiscodes can either of you please have a look when you get a chance.

izeigerman avatar Jul 26 '24 16:07 izeigerman

Sure, I will look into it

themisvaltinos avatar Jul 26 '24 16:07 themisvaltinos