Skip to content

[Bug]: RIGHT SEMI statistics use the non-preserved child after physical swap #29534

Closed

Description

@XuPeng-SH

Fix in the existing unmerged PR

The preserved-side statistics defect already exists on main at c2; the Cartesian correction amplifies it. Both findings are repaired in PR #29527, not deferred to a separate PR.

The existing post-swap owner now uses physical child 1 for RIGHT SEMI output and block counts while preserving ordinary selectivity, costs, hash-map ownership and limits. Typed boundary/recalculation tests and public DELETE/BVT oracles pass. The earlier review at aa83017933 was reopened for three concrete follow-up findings; these are also repaired in the same PR. Commit 91e413abca893f1337495ebdce45e85251f24237 preserves the remote main merge and passes the final GPT-6.1-sol / xhigh review. See updated validation and provenance. Public SQL verifies a flushed five-row source plus five own writes yields 80 CROSS rows, wide growth preserves its 8200-byte estimate, and a finite 4.29B auto-ID hint reserves only 10,000 IDs with a successful cold allocator write after service restart. No exact row scans or extra state are added. Fresh normal controls show no repeated slowdown; performance limits remain explicit. This issue remains open until the fixing PR merges.

Original diagnosis at f93762e

Description

RIGHT SEMI output statistics use the non-preserved physical child after swapJoinChildren. A SEMI join can emit only rows from its preserved logical-left relation; after physical swap to RIGHT SEMI that relation is child 1. With exact statistics for a 5-row target and a 40-row matching product, current EXPLAIN estimates 40 output rows instead of respecting the 5-row bound.

This defect exists on the matched main baseline and is amplified by the corrected Cartesian estimation in PR #29527. It changes downstream plan selection and write costs; the tested query's actual rows remain correct.

Environment

  • Main baseline c2abd6a54b7cd3e13c1b1494388cd7a81b8369d4.
  • PR head f93762ed90e618f477a37ecee640ce8395c72d8e.
  • Isolated one LOG + one TN + one CN, local NVMe DISK-V2; 64 MiB process limit, max_dop=2, join_spill_mem=0.

Reduced public reproduction

CREATE DATABASE right_semi_stats;
USE right_semi_stats;
SET max_dop=2;
SET join_spill_mem=0;
CREATE TABLE a(n INT PRIMARY KEY);
CREATE TABLE b(n INT PRIMARY KEY);
CREATE TABLE ti(id INT PRIMARY KEY,payload VARCHAR(8192));
INSERT INTO a VALUES(0),(1),(2),(3),(4);
INSERT INTO b VALUES(0),(1),(2),(3),(4),(5),(6),(7);
INSERT INTO ti VALUES(0,'p'),(1,'p'),(2,'p'),(3,'p'),(4,'p');
SELECT table_cnt FROM table_stats('right_semi_stats.a','patch',
 '{"table_cnt":5,"block_number":1,"accurate_object_number":1,"ndv_map":{"n":5}}') g;
SELECT table_cnt FROM table_stats('right_semi_stats.b','patch',
 '{"table_cnt":8,"block_number":1,"accurate_object_number":1,"ndv_map":{"n":8}}') g;
SELECT table_cnt FROM table_stats('right_semi_stats.ti','patch',
 '{"table_cnt":5,"block_number":1,"accurate_object_number":1,"ndv_map":{"id":5}}') g;
EXPLAIN VERBOSE DELETE FROM ti
WHERE id IN(SELECT a.n FROM a CROSS JOIN b);
DELETE FROM ti WHERE id IN(SELECT a.n FROM a CROSS JOIN b);
SELECT COUNT(*) FROM ti; -- 0; only the five target rows can be deleted

PR-head plan excerpt with these exact counts:

Multi Update (... outcnt=40 ...)
  Lock (... outcnt=40 ...)
    Join (... outcnt=40 ... hashmapSize=5 ...)
      Join Type: RIGHT SEMI hashOnPK
      Join Cond: (a.n = ti.id)
      child 0: Cartesian product (... outcnt=40 ...)
      child 1: Table Scan ti (... outcnt=5 ...)

Clean baseline proof: the same exact-count case at c2abd6a selects RIGHT SEMI and estimates eight output rows over the same five-row preserved target (8 > 5). Its old Cartesian estimate is eight. The PR fixes that child estimate to 40; the existing SEMI preserved-side defect then estimates 40 (40 > 5). The larger underlying product is not a valid reason to inflate the SEMI result to its non-preserved side's row count.

Downstream effect with default statistics

Without the patches, the actual 5/8/5 rows can all be represented by default 1,000-row scan estimates. At c2 the DELETE plan is SEMI with 1,000 estimated output rows. At the PR head the matching product becomes 1,000,000 estimated rows, the join orients as RIGHT SEMI, and that product count propagates to MultiUpdate instead of the preserved target count (1,000 estimated, five actual).

That crosses the existing estimated rows * 300 > 10 MiB object-writer threshold. The measured DELETE of five actual rows changes from approximately 4.804 ms to 12.220 ms (2.54x). Both comparison rounds regress (2.40x and 2.83x), and captured plans add the existing WriteS3/FlushS3Info actions and merge. This is a local one-CN measurement, not a multi-CN throughput claim.

Root cause and responsibility

  • ReCalcNodeStats SEMI case always derives Outcnt and BlockNum from leftStats.
  • reCalcNodeStatsAfterSwap knows the physical child order and repairs the preserved side for SINGLE and ANTI, but omits SEMI.
  • IsRightJoin is selected before physical swapping. A blanket change in the ordinary statistics pass based only on IsRightJoin would choose the wrong side during earlier optimizer phases. The existing post-swap owner is the phase-safe place to repair this contract.
  • Do not add a DELETE-specific estimate clamp, modify Cartesian arithmetic or duplicate preserved-side bookkeeping in the compiler.

Expected behavior and regression coverage

  1. SEMI output estimate cannot exceed its preserved-side estimate, before or after physical orientation; matching-side duplicates cannot multiply output rows.
  2. Preserve HashmapSize/build-side accounting and logical filtering semantics. Apply LIMIT once in the existing owner, and retain the established zero/singleton behavior.
  3. Cover right orientation after swap, left-orientation control, repeated stats recalculation, empty/duplicate matching input and a parent DML/downstream join consuming the estimate.
  4. Verify actual result and atomic DELETE behavior independently of EXPLAIN; keep the data set small.
  5. Reuse the existing post-swap fixture/owner rather than a new stats model or a duplicate test cluster.

Related

PR #29527 and issue #29497 expose the larger cost consequence. #25862 is a different right-SINGLE runtime-filter optimization, not this RIGHT SEMI cardinality defect. Existing exact-row tests and CI success do not demonstrate this estimate bound. No fix is claimed here.

#29533 tracks the independent small-Cartesian INSERT writer/shuffle regression with missing statistics.

Activity

  1. added
    kind/bug
    on Sep 30, 2026
  2. self-assigned this
    on Sep 30, 2026
  3. added a commit that references this issue on Oct 1, 2026
    f6eb974
  4. Ariznawlll commented on Oct 2, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:main
    • Commit:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 部署方式:干净源码 worktree;定向 Go UT/集成回归(涉及 Proxy/CN 的路径使用对应组件级并发与协议模拟)
    • 测试日期:2026-10-02

    原问题验证

    在上述 commit 运行 issue 对应的定向回归。RIGHT SEMI 物理 swap 后统计仍使用语义 preserved side,HashmapSize 未取错 child。

    Happy Path 覆盖

    覆盖 left/right semi、带 limit 与普通 inner 控制。

    Unhappy Path 覆盖

    覆盖物理交换和不同 child cardinality;统计稳定。

    白盒验证

    关联修复 #29527 已进入当前 main。直接执行并检查的定向测试为:pkg/sql/plan/runtime_filter_test.go::TestSemiJoinStatsUseSemanticPreservedSide。

    回归测试

    本问题依赖内部故障注入、并发时序、catalog/optimizer 状态或多组件协议边界,无法由普通 SQL golden 稳定构造,因此不放入 MOTR;回归保留在 MatrixOne UT/BVT 层。对应定向测试在精确 commit 上通过,且未修改用户工作区。

    测试证据

    • 精确测试版本:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 定向测试:pkg/sql/plan/runtime_filter_test.go::TestSemiJoinStatsUseSemanticPreservedSide
    • 当前结果:RIGHT SEMI 物理 swap 后统计仍使用语义 preserved side,HashmapSize 未取错 child。
    • 关联 PR:#29527

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions