fix(expressions)!: classify boolean binary operators as Predicate - #8073
fix(expressions)!: classify boolean binary operators as Predicate#8073georgesittas wants to merge 1 commit into
Conversation
| elif isinstance(predicate, exp.EQ): | ||
| parent_predicate = _replace( | ||
| parent_predicate, | ||
| f"({parent_predicate} AND ARRAY_CONTAINS({nested}, {column}))", | ||
| ) |
There was a problem hiding this comment.
This branch was a no-op due to how group_by is constructed:
- Two loops iterate over
keys: loop A at L197–208 decidesgroup_by, loop B at L293–316 reads it - To reach this branch, both
not (key in group_by)andisinstance(predicate, exp.EQ)must be true - The only way for a key to avoid being added in
group_byis by the corresponding predicate in the tuple not being anEQpredicate, see L207-208
SQLGlot Integration Test Results✅ All tests passedComparing:
Overallmain: 182937 total, 160861 passed (pass rate: 87.9%) sqlglot:jo/classify_boolean_predicates: 170743 total, 149709 passed (pass rate: 87.7%) Transitions: Dialect pair changes: 0 previous results not found, 3 current results not found ✅ All tests passed |
|
|
||
|
|
||
| class Overlaps(Expression, Binary): | ||
| class Overlaps(Expression, Binary, Predicate): |
There was a problem hiding this comment.
Adding Predicate here routes these operators into a parenthesis removal path in simplify_parens that mishandles ~:
Input query (with ArrayContainsAll)
SELECT ~(a @> b) FROM t
simplify() output
SELECT ~a @> b FROM t
~ binds tighter than @>.
There was a problem hiding this comment.
Hmm, it actually looks like the same path also mishandles on main: SELECT ~(a = b) simplifies to SELECT ~a = b. Maybe it's a different PR.
Various
Binaryclasses that entered the codebase as functions (RegexpLike,ArrayContains, etc.) now subclassPredicate, so_annotate_binaryshort-circuits them toBOOLEANand 14 redundantBOOLEANreturns entries drop out.Breaking:
simplify_parensnow strips redundant parens around these operators (NOT (a @> b)becomesNOT a @> b). Verified this is safe w.r.t. precedence on Postgres 18, DuckDB, MySQL, Snowflake and Databricks;nonnullpropagation is also restored.