Skip to content

fix(audits): apply condition to the rows checked by z_score and valid_http_method - #6156

Open
DG47 wants to merge 1 commit into
SQLMesh:mainfrom
DG47:fix/audit-condition-z-score
Open

DG47 wants to merge 1 commit into
SQLMesh:mainfrom
DG47:fix/audit-condition-z-score

Conversation

@DG47

@DG47 DG47 commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #6155

Two built-in audits did not apply the optional condition argument to the rows they check:

  • z_score applied condition only in the stats CTE that computes the mean and standard deviation. The outer query read every row of the model, so rows excluded by the condition were compared against statistics from a different subset and reported as failures. It also scanned the full model instead of the filtered rows.
  • valid_http_method had no condition default and did not reference @condition, so a condition passed to it was silently ignored. Every other built-in audit supports it.

This change adds @condition to the outer WHERE clause of z_score. It also gives valid_http_method the same condition default (TRUE) and @AND(..., @condition) filter that the other built-in audits use. Without a condition, both queries return the same rows as before. The _non_blocking variants are copies of these audits, so they get the fix too.

Test Plan

  • Added test_z_score_audit and test_valid_http_method_audit in tests/core/test_audit.py. They check the rendered SQL with and without a condition.
  • Added test_builtin_audits_condition_filters_audited_rows, which runs both audits against an in-memory DuckDB table where the condition excludes some rows. Before this change, z_score flagged the excluded rows and valid_http_method flagged rows outside the condition.
  • All three tests fail on main and pass with this change.
  • pytest tests/core/test_audit.py: 46 passed.
  • pytest -n 8 -m "fast and not cicdonly and not isolated" tests/core: 2105 passed, 2 skipped. 6 failures in tests/core/test_connection_config.py (mssql/fabric pyodbc) come from pyodbc not being installed locally, and they fail the same way on main.
  • SKIP=prettier,eslint pre-commit run --all-files: ruff and ruff-format pass. mypy passes on the changed files; across the tree it only reports import-not-found for optional extras (web, lsp) that are not installed in my local environment.

Checklist

  • I have run make style and fixed any issues
  • I have added tests for my changes (if applicable)
  • All existing tests pass (make fast-test) (ran the tests/core fast subset locally, see above)
  • My commits are signed off (git commit -s) per the DCO

…_http_method

The z_score audit only applied `condition` to the CTE that computes the
mean and standard deviation. The outer query scanned every row of the
model, so rows excluded by the condition were still compared against
statistics computed from a different subset and reported as failures.

The valid_http_method audit had no `condition` default and did not
reference it in its query, so a `condition` passed to it was silently
ignored, unlike every other built-in audit.

Apply `condition` in the WHERE clause of both queries.

Signed-off-by: Dhruv Gupta <todhruvg@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Built-in z_score audit ignores condition for the rows it checks, and valid_http_method ignores condition entirely

1 participant