englefly opened a new pull request, #70893:
URL: https://github.com/apache/doris/pull/70893

   ### Background
   
   Nereids uses input statistics to estimate parent operators. The row-count 
derivation helper normalizes column NDVs and NULL counts to the new 
cardinality. INTERSECT additionally maps its output slots to corresponding 
columns of the left input.
   
   ### What problem does this PR solve?
   
   Issue Number: None
   Related PR: #18035 (introduced in-place normalization), #17637 (introduced 
the intersect input write), #24997 (renamed the derivation helper)
   PR Type: Bugfix
   
   Problem Summary:
   
   Row-count derivation shares the column map with its input and changes the 
input when normalizing it. For example, deriving one row from input NDV 1,000 
also sets the input NDV to 1. INTERSECT also writes its output slots into the 
left input. Give both derived statistics their own maps so sibling and memo 
estimates can continue using the original input.
   
   ### Details
   
   - **Frequency:** Always when a derived row count requires column 
normalization. INTERSECT also inserts output keys on every derivation.
   - **Root cause:** Statistics.withRowCountAndEnforceValid passes its input 
map to the new estimate, and StatsCalculator.computeIntersect adds output keys 
before constructing its result.
   - **Why existing tests missed it:** StatisticsTest checks in-place 
normalization, and set_operation_exprid_reuse checks returned SQL rows. They do 
not check if the optimizer changed a child estimate during derivation.
   - **Fix:** Use the existing copying row-count helper and build INTERSECT 
statistics on a copied map before normalizing. Preserve the result's existing 
column mappings, width, delta-row reset, and HBO flag.
   - **Other paths and branches:** The helper change covers its Limit, TopN, 
AssertNumRows, join, memo and HBO callers. Logical and physical INTERSECT share 
the changed derivation. Both patterns are present from branch-2.1 through 
branch-4.2.
   - **Behavior change:** Later optimizer estimates continue to see the input 
statistics that were produced for the input operator.
   
   ### Testing
   
   - A baseline FE probe reproduced NDV 1 on the shared input and output-slot 
leakage from INTERSECT. The fixed FE reproducer preserves both input NDVs, and 
the set_operation_exprid_reuse regression passes. The unit tests also cover the 
HBO path. Local FE unit tests passed for StatsCalculatorTest and StatisticsTest.
   - Regenerated 1,204 shape snapshots and verified 1,205 shape_check suites 
with the regression runner. The regenerated snapshots match the checked-in 
files.
   - 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 preserves input statistics when estimating operators such as 
INTERSECT, LIMIT, and TOP-N.
   
   ### 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]

Reply via email to