Skip to content

branch-4.2 [Enhancement](optimizer) Support multi-mode CTE inline strategy - #68211

Open
englefly wants to merge 2 commits into
apache:branch-4.2from
englefly:pick-60601-4.2
Open

englefly wants to merge 2 commits into
apache:branch-4.2from
englefly:pick-60601-4.2

Conversation

@englefly

Copy link
Copy Markdown
Contributor

Cherry-pick of #60601 to branch-4.2

Enhancement Support multi-mode CTE inline strategy

Replaces the boolean enableCboCteInline behaviour with an integer cte_inline_mode
(<0 disable, 0 selective inline, >=1 full CBO comparison, default 0).

Adaptations for branch-4.2: the new session variable uses branch-4.2's
@VariableMgr.VarAttr annotation, and the shape_check baselines for tpcds queries
4/11/74 were regenerated with -forceGenOut on branch-4.2 (tpcDS/TPC-H plan shapes
differ from master).

Tests: new suites test_cte_multiuse_no_union_not_inline and
test_cte_nondeterministic_not_inline pass, plus distinct_agg_strategy_selector and
the regenerated shape_check suites (12+6+3+1 suites, 0 failures).

…ategy (apache#60601)

### What problem does this PR solve?
Previously, CTE inlining was controlled by a simple boolean flag
(enableCboCteInline), which either fully enabled or disabled CBO-based
CTE inline comparison. This was too coarse-grained — in some cases, full
CBO comparison is unnecessary overhead, while in others (e.g., TPC-DS
query74), a UNION ALL CTE can be selectively inlined when consumer
filters eliminate entire union branches, producing a clearly better plan
without needing full cost comparison.

This PR replaces the boolean enableCboCteInline with an integer
CteInlineMode that supports three modes:

- mode < 0: Disable CTE inlining entirely.

- mode = 0: Selective inline — only inline CTEs whose body contains a
UNION ALL, and only when consumer-side filters can eliminate some union
branches (i.e., produce EmptyRelation after filter pushdown). When this
condition is met, the inlined plan is directly selected without CBO cost
comparison.

- mode >= 1 (default): Full CBO comparison — both materialized and
inlined alternatives are added to the Memo for cost-based selection
(original behavior).
…CTE inline

Regenerated regression-test/data/shape_check baselines with -forceGenOut on
branch-4.2 + apache#60601: tpcds_sf100/{shape,no_stats_shape,noStatsRfPrune,rf_prune},
tpcds_sf1000/{shape,hint}, tpcds_sf10t_orc/shape and
tpcds_sf1000_constraints/shape for queries 4, 11 and 74.
@englefly
englefly requested a review from yiguolei as a code owner September 18, 2026 16:06
@hello-stephen

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Apache Doris.
Don't know what should be done next? See How to process your PR.

Please clearly describe your PR:

  1. What problem was fixed (it's best to include specific error reporting information). How it was fixed.
  2. Which behaviors were modified. What was the previous behavior, what is it now, why was it modified, and what possible impacts might there be.
  3. What features were added. Why was this function added?
  4. Which code was refactored and why was this part of the code refactored?
  5. Which functions were optimized and what is the difference before and after the optimization?

@englefly

Copy link
Copy Markdown
Contributor Author

run buildall

@hello-stephen

Copy link
Copy Markdown
Contributor

FE UT Coverage Report

Increment line coverage 43.15% (63/146) 🎉
Increment coverage report
Complete coverage report

@hello-stephen

Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 84.93% (124/146) 🎉
Increment coverage report
Complete coverage report

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants