Skip to content

test: Pin v2 plan shapes in innerJoinOnSubquery - #1783

Open
mbasmanova wants to merge 3 commits into
facebookincubator:mainfrom
mbasmanova:export-D117592298
Open

test: Pin v2 plan shapes in innerJoinOnSubquery#1783
mbasmanova wants to merge 3 commits into
facebookincubator:mainfrom
mbasmanova:export-D117592298

Conversation

@mbasmanova

Copy link
Copy Markdown
Contributor

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:
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
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 26, 2026
@meta-codesync

meta-codesync Bot commented Aug 26, 2026

Copy link
Copy Markdown

@mbasmanova has exported this pull request. If you are a Meta employee, you can view the originating Diff in D117592298.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant