Repository navigation
fix: make kl_divergence and chi_square audits run on DuckDB and Trino - #6129
Open
shinichitazawa wants to merge 1 commit into
Open
shinichitazawa wants to merge 1 commit into
shinichitazawa wants to merge 1 commit into
Conversation
kl_divergence failed on every engine:
- table_a_with_q joined USING (@source_column) against a CTE that only has
@target_column, so different columns failed ("missing from right side of join")
- with the same column on both sides, num_rows became ambiguous
- the final SELECT grouped by the column values, so the divergence was
compared per value instead of summed over the distribution
- single-argument LOG() is log10 on DuckDB and does not exist on Trino
Rename the value column to x in both distributions, join them on it, sum
(p - q) * LN(p / q) over the values present in both (the same terms the old
query kept), and compare the single result with the threshold.
chi_square referenced the chi_square select alias in HAVING, which Trino
rejects ("Column 'chi_square' cannot be resolved"). Compute the statistic in a
CTE and filter it with WHERE.
kl_divergence also used HAVING on its alias; the rewrite filters with WHERE.
Signed-off-by: shinichitazawa <shinichitazawa.aws@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The built-in
kl_divergenceaudit fails on every engine I tried, andchi_squarefails on Trino. Found while testing statistical audits on Trino 483 + Iceberg (follow-up to the docs fix in #6128).kl_divergence
table_a_with_qjoinsUSING (@source_column)againsttable_b_with_q, which only has@target_columnColumn 'bytes' is missing from right side of join(Trino) /Column "bytes" does not exist on right side of join(DuckDB)num_rowsAMBIGUOUS_NAME(Trino) /Ambiguous reference to column name "num_rows"(DuckDB)SELECT ... GROUP BY @source_column, @target_columnLOG(x)logThe rewrite names the value column
xin both distributions, joins them on it, computesSUM((p - q) * LN(p / q))over the values present in both columns, and compares that single number with@threshold.(p - q) * ln(p / q)equalsp ln(p/q) + q ln(q/p), i.e. the same terms the old query tried to add. Values present in only one column are skipped, as before (their terms were NULL and dropped bySUM).Behaviour change: results now use the natural log (as in the linked definition of the symmetrised divergence). On DuckDB the old query, where it ran, used log10, so the same threshold now means something different.
chi_square
HAVINGreferenced thechi_squareselect alias. DuckDB accepts that, Trino does not:Column 'chi_square' cannot be resolved. The statistic is now computed in astatisticCTE and filtered withWHERE. The computation itself is unchanged.Test Plan
Ran each audit (
_non_blocking) on anINCREMENTAL_BY_TIME_RANGEmodel for one hourly interval (328,846 rows, two categorical columns with 27 distinct values each), with SQLMesh 0.236.3 and thisbuiltin.pyswapped in (the file is identical between v0.236.3 and main). The reference KL value was computed separately in Python with the natural log: 0.0373.kl_divergence, same column on both sideskl_divergence,threshold := 0.0372kl_divergence,threshold := 0.0374chi_square, defaultdependent := truechi_square,dependent := falseUnit tests:
tests/core/test_audit.pyupdates the expected SQL fortest_chi_square_auditand addstest_kl_divergence_audit. Result: 43 passed (42 before). One test,test_model_inline_audits, errors both before and after this change in my environment because the BigQuery extra is not installed.Checklist
make styleand fixed any issues: ranruff checkandruff format --checkon both changed files (clean). mypy was not run.make fast-test): not run. Onlytests/core/test_audit.pywas run, as described above.git commit -s) per the DCO🤖 Generated with Claude Code