test: Pin v2 plan shapes in innerJoinOnSubquery - #1783
Open
mbasmanova wants to merge 3 commits into
Open
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
Summary:
Six of the seven assertions in `innerJoinOnSubquery` pinned v1's plan and skipped the comparison under v2. v2 keeps the same joins but stops carrying the redundant equi-join column through them, reconstructing it in a Project at the top:
Project[..., (r_regionkey, "n_regionkey"), ...]
HashJoin[INNER r_regionkey=n_regionkey] -> r_name, r_comment, n_nationkey, n_name, n_regionkey, n_comment
The join payload loses a column for the cost of one Project. In the NOT IN and NOT EXISTS cases v2 also probes with `region` and builds on the semi-join subtree, dropping the mark column on the way out. Those six now assert with `AXIOM_ASSERT_PLAN_V2`.
The correlated-scalar case stays on v1: v2 joins `region` first and only then LEFT JOINs the aggregate and applies `n_nationkey > coalesce(cnt, 0)`, so the region join sees rows the filter would have removed. v1 filters before that join.
Differential Revision: D117592298
|
@mbasmanova has exported this pull request. If you are a Meta employee, you can view the originating Diff in D117592298. |
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:
Six of the seven assertions in
innerJoinOnSubquerypinned v1's plan and skipped the comparison under v2. v2 keeps the same joins but stops carrying the redundant equi-join column through them, reconstructing it in a Project at the top:The join payload loses a column for the cost of one Project. In the NOT IN and NOT EXISTS cases v2 also probes with
regionand builds on the semi-join subtree, dropping the mark column on the way out. Those six now assert withAXIOM_ASSERT_PLAN_V2.The correlated-scalar case stays on v1: v2 joins
regionfirst and only then LEFT JOINs the aggregate and appliesn_nationkey > coalesce(cnt, 0), so the region join sees rows the filter would have removed. v1 filters before that join.Differential Revision: D117592298