test: Pin v2 shapes in the outer-join-on-subquery tests - #1785
Open
mbasmanova wants to merge 4 commits into
Open
test: Pin v2 shapes in the outer-join-on-subquery tests#1785mbasmanova wants to merge 4 commits into
mbasmanova wants to merge 4 commits into
Conversation
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
Summary:
Two of these assertions were never a real divergence. The matcher spelled the aggregate's output column as `count` / `approx_distinct`, which is v1's generated name; v2 generates `count13` and `approx_distinct13`. The plans are otherwise identical, so capturing an alias on the aggregate makes one expectation fit both, and those two dual-run again.
The other three pin v2, which plans them better. For
SELECT * FROM region WHERE r_regionkey = (SELECT min(n_nationkey) FROM nation WHERE n_regionkey = r_regionkey)
v1 builds a LEFT JOIN and filters `r_regionkey = min` above it. v2 folds both equalities into the join keys, making it an INNER join, and derives `n_regionkey = min` on the build side to cut the aggregate output before the join. The join type is safe to narrow because the filter rejects nulls on the aggregate — an outer row with no match reads `min` as NULL and drops out either way. Where the filter is `coalesce(cnt, 0)` and so keeps unmatched rows, v2 leaves the LEFT JOIN alone.
The two subqueries without aggregation now compute `c + d` above the join instead of projecting it on the build side, once per outer row rather than once per `u` row.
Also switches the join matchers this change touches to `hashJoinInner` and friends.
Differential Revision: D117594872
Summary:
Three assertions across `leftJoinOnSubquery` and `rightJoinOnSubquery` pinned v1's plan and skipped the comparison under v2, which plans all three better.
With an uncorrelated scalar in a LEFT JOIN's ON clause, v2 carries the comparison as the nested loop join's condition, where v1 cross joins and filters above it:
NestedLoopJoin[INNER, joinCondition: gt("r_regionkey","min")]
For the two RIGHT JOIN cases, v2 swaps the inputs to plan a LEFT join and pushes the IN or EXISTS into the null-supplying side, with no Project to rebuild the output. The inner-join tests do need that Project, because an INNER join lets the optimizer read one side of an equality off the other; an outer join cannot, since the key is null for unmatched rows.
The remaining pin, a correlated `count(*)` in a LEFT JOIN's ON clause, stays on v1: v1 semi-joins `supplier` with `region` before grouping, and v2 aggregates all of `supplier` and joins afterwards.
bypass-github-export-checks
___
Differential Revision: D117595746
|
@mbasmanova has exported this pull request. If you are a Meta employee, you can view the originating Diff in D117595746. |
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:
Three assertions across
leftJoinOnSubqueryandrightJoinOnSubquerypinned v1's plan and skipped the comparison under v2, which plans all three better.With an uncorrelated scalar in a LEFT JOIN's ON clause, v2 carries the comparison as the nested loop join's condition, where v1 cross joins and filters above it:
For the two RIGHT JOIN cases, v2 swaps the inputs to plan a LEFT join and pushes the IN or EXISTS into the null-supplying side, with no Project to rebuild the output. The inner-join tests do need that Project, because an INNER join lets the optimizer read one side of an equality off the other; an outer join cannot, since the key is null for unmatched rows.
The remaining pin, a correlated
count(*)in a LEFT JOIN's ON clause, stays on v1: v1 semi-joinssupplierwithregionbefore grouping, and v2 aggregates all ofsupplierand joins afterwards.bypass-github-export-checks
Differential Revision: D117595746