fix(optimizer): Drop a redundant COALESCE in decorrelation - #1782
Open
mbasmanova wants to merge 2 commits into
Open
fix(optimizer): Drop a redundant COALESCE in decorrelation#1782mbasmanova wants to merge 2 commits into
mbasmanova wants to merge 2 commits into
Conversation
Summary:
Both tests pinned v1's plan and skipped the comparison under v2, which hid two places where v2 plans better. For
SELECT * FROM region WHERE r_regionkey > (SELECT n_regionkey FROM nation)
v2 carries the comparison as the nested loop join condition, where v1 emits a cross join followed by a Filter and a Project. For the scalar subquery in a projection, v2 puts the one-row `EnforceSingleRow` side on the join's build input; v1 builds on the driving table instead, and Velox buffers the entire build side, so v1's memory grows with that table.
Both tests now assert with `AXIOM_ASSERT_PLAN_V2`. v1 still has to produce a plan, but its shape is no longer compared.
Differential Revision: D117583558
Summary:
Decorrelating a correlated `count(*)` subquery wrapped the aggregate in `COALESCE(count, 0)` on every path, so a filter over it planned as
gt(coalesce("count15", 0), 3)
In the lifted shape that wrap is dead work: the aggregate runs above a LEFT join and groups by the outer row id, so an outer row with no matches still forms a group, and a masked `count` over that empty group already returns 0. The filter now plans as `gt("count", 3)`.
`buildAggregateWraps` takes `everyOuterRowHasGroup`. The three lifted shapes pass true and skip the wrap; the join-back shape keeps it, because there an unmatched outer row has no group at all and the LEFT rejoin pads it with NULL.
With the wrap gone, six assertions across the `nonEqui*` subquery tests now pin v2 instead of v1. Three stay on v1 where v2 is worse: the equi-and-non-equi correlation builds the hash join on the outer relation and loses the streaming aggregation, and both distributed cases split the per-outer-row aggregation into PARTIAL and FINAL around a `HASH(__rownum)` shuffle, though each group holds one row.
Found along the way and not fixed here, v1 answers this query wrong:
SELECT a, (SELECT count(*) FROM u WHERE u.a = t.a HAVING count(*) = 0) AS c FROM t
For an outer row whose subquery matched but failed the HAVING, v1 returns 0 where the answer is NULL. Its own COALESCE on the join-back path cannot tell that row apart from one with no matches at all, since both reach the rejoin as NULL. v2 and DuckDB agree on NULL. A fix needs the HAVING applied above the restored empty-input value, so it is not a local change.
Differential Revision: D117590410
|
@mbasmanova has exported this pull request. If you are a Meta employee, you can view the originating Diff in D117590410. |
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.
Summary:
Decorrelating a correlated
count(*)subquery wrapped the aggregate inCOALESCE(count, 0)on every path, so a filter over it planned asIn the lifted shape that wrap is dead work: the aggregate runs above a LEFT join and groups by the outer row id, so an outer row with no matches still forms a group, and a masked
countover that empty group already returns 0. The filter now plans asgt("count", 3).buildAggregateWrapstakeseveryOuterRowHasGroup. The three lifted shapes pass true and skip the wrap; the join-back shape keeps it, because there an unmatched outer row has no group at all and the LEFT rejoin pads it with NULL.With the wrap gone, six assertions across the
nonEqui*subquery tests now pin v2 instead of v1. Three stay on v1 where v2 is worse: the equi-and-non-equi correlation builds the hash join on the outer relation and loses the streaming aggregation, and both distributed cases split the per-outer-row aggregation into PARTIAL and FINAL around aHASH(__rownum)shuffle, though each group holds one row.Found along the way and not fixed here, v1 answers this query wrong:
For an outer row whose subquery matched but failed the HAVING, v1 returns 0 where the answer is NULL. Its own COALESCE on the join-back path cannot tell that row apart from one with no matches at all, since both reach the rejoin as NULL. v2 and DuckDB agree on NULL. A fix needs the HAVING applied above the restored empty-input value, so it is not a local change.
Differential Revision: D117590410