Skip to content

branch-4.1 [fix](fd) Propagate constants out of one row relation to enable partition pruning - #67400

Draft
englefly wants to merge 3 commits into
apache:branch-4.1from
englefly:pick-66905-4.1
Draft

branch-4.1 [fix](fd) Propagate constants out of one row relation to enable partition pruning#67400
englefly wants to merge 3 commits into
apache:branch-4.1from
englefly:pick-66905-4.1

Conversation

@englefly

@englefly englefly commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Cherry-pick of #66905 to branch-4.1

fix Propagate constants out of one row relation to enable partition pruning

…e partition pruning

Problem Summary:
A CTE that defines a single constant row (e.g. `WITH params AS (SELECT CAST(...) AS
begin_time, ...)`) is inlined into its consumers as a one-row relation, but the constant
projects were only registered as uniform slots without values. Constant propagation and
predicate inference outside the relation therefore could not substitute the constant
values into predicates over the CTE columns, so predicates like
`dt BETWEEN DATE_SUB(params.begin_time, INTERVAL params.period_days DAY) AND
DATE_SUB(params.begin_time, INTERVAL 1 DAY)` were not folded, the `dt` predicates were
left as nested-loop-join conjuncts with runtime filters, and partition pruning failed:
the scan read all 526/537 partitions instead of the single needed partition.

Fix:
`LogicalOneRowRelation.computeUniform` now registers the values of constant projects as
uniform constants, so constant propagation can substitute them into predicates over the
CTE columns, fold functions such as DATE_SUB over them, and push the resulting
predicates into the scan for partition pruning.

None

- Test: FE unit test ConstantCteTest (asserts both scans prune to the expected single
  partition); regression test test_constant_cte_partition_prune.
- Behavior changed: No
- Does this need documentation: No
…ghJoin for one-row-relation constant propagation

### What problem does this PR solve?

Problem Summary:
LogicalOneRowRelation.computeUniform now registers the values of a one-row relation's
constant projects as uniform literals (constant CTE propagation). For the query
`select coalesce(element_at(s, 'city'), 'abc') from (select * from tbl)a join
(select 100 id, 'f1' name)b on a.id=b.id`, the join condition `a.id = b.id` now folds
to `a.id = 100`, the predicate is pushed into a filter above the scan, and the join
becomes a cross join. The left project therefore no longer needs to output the join key
`id` and keeps only the pushed-down nested column access
`element_at(s, 'city')` (1 project instead of 2). The old test asserted the previous
plan shape and failed; update the assertion to the new expected plan, which is
semantically equivalent and enables pushing `id = 100` into the scan.

### Release note

None

### Check List (For Author)

- Test: PruneNestedColumnTest (50/50) and ConstantCteTest pass
- Behavior changed: No
- Does this need documentation: No
…relation constant propagation

### What problem does this PR solve?

Problem Summary:
- pull_up_predicate_literal: after LogicalOneRowRelation.computeUniform registers the
  values of a one-row relation's constant projects as uniform literals, the join condition
  `tmp.col1 = ds.col1 AND tmp.col2 = ds.col2` (tmp being a one-row relation with the
  literals 'abc'/'def') is folded into `ds.col1 = 'abc' AND ds.col2 = 'def'` and pushed
  into a filter below the scan; the join itself keeps no condition and becomes a nested
  loop join. Regenerate the .out file for the two view-based queries; the remaining
  queries in the suite use project-over-scan constant subqueries and are unaffected.
- test_constant_cte_partition_prune: the new test explains the expected single-partition
  pruning of a constant CTE, but on an empty table PRUNE_EMPTY_PARTITION rewrites the
  scans into empty relations, so the `partitions=1/6 (p20260728)` info never appears.
  Seed each partition with one row so the scans stay and the asserted pruning shows up.

### Release note

None

### Check List (For Author)

- Test: regression tests pull_up_predicate_literal and test_constant_cte_partition_prune pass
- Behavior changed: No
- Does this need documentation: No
@englefly

englefly commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

run buildall

@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?

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