englefly opened a new pull request, #70894: URL: https://github.com/apache/doris/pull/70894
### Background A semi or anti join returns rows from one input. Nereids propagates the number of distinct values for the retained key to grouping and other operators above that join. ### What problem does this PR solve? Issue Number: None Related PR: #40762 (introduced the shared NDV update) PR Type: Bugfix Problem Summary: The common join updater can read the discarded key from the output map and use the unknown statistics placeholder NDV of 1 as a real bound. A grouped semi join in the regression case was estimated to return 1 row for 10 matching keys. Use the matching input to bound the output key of a semi join and keep the retained-side estimate for anti and mark joins. ### Details - **Frequency:** Always when this updater reads an omitted key and the retained key has an estimated NDV above 1. - **Root cause:** JoinEstimation reused the general equality-key updater after building semi and anti join statistics. That updater reads both keys from the output even when the output statistics contain only the retained input. - **Why existing tests missed it:** StatsCalculatorTest.testLeftSemiJoinSkew asserts hot values. Existing semi and anti SQL suites check returned rows or plan shape. Neither asserts the NDV propagated to an aggregate. - **Fix:** Read the matching input key from its input statistics and cap a known retained slot by that known NDV. Apply the bound only to non-mark semi joins with slot equalities. A missing estimate leaves the retained NDV available downstream. For TPC-H Q16, the corrected anti-join key estimate changes the DISTINCT aggregate from three hash aggregate stages to two in all four affected snapshots. - **Other paths and branches:** Logical and physical joins share JoinEstimation. The output-only update is present in branch-3.0, branch-3.1, branch-4.0, branch-4.1, and branch-4.2. branch-2.1 lacks this semi/anti call. - **Behavior change:** The optimizer propagates a different retained-key NDV for affected semi and anti joins. ### Testing - The semi_join_output_ndv suite checks the 10 matching keys via the optimized plan and query result. Local FE unit tests passed for StatsCalculatorTest. - Regenerated 1,204 shape snapshots and verified 1,205 shape_check suites with the regression runner. The generated diff updates 4 snapshots. - Built the FE with `build.sh --fe`. Local SQL regression used the Doris 4.1.5 BE with compatibility settings for data execution. ### Release note Nereids improves distinct-value estimates above semi and anti joins. ### Check List (For Author) - Behavior changed: Yes (see Details) - Does this need documentation: No - Pre-submission status: Ready ### AI Disclosure - Tool: OpenAI Codex (GPT-6) - Generated: Code, tests, and PR description - Human review: Pending -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
