Skip to content

branch-4.2: [fix](mtmv) Keep constant-propagated predicates in materialized view rewrite #69464 - #69539

Open
github-actions[bot] wants to merge 1 commit into
branch-4.2from
auto-pick-69464-branch-4.2
Open

github-actions[bot] wants to merge 1 commit into
branch-4.2from
auto-pick-69464-branch-4.2

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

Cherry-picked from #69464

…rewrite (#69464)

Constant propagation rewrites a constant grouping key above the
aggregate and reuses the exprId of the replaced column:

```
LogicalProject (projects=[2026 AS dt_year#4, org_group#7, ...])
+-- LogicalAggregate (groupByExpr=[org_group#7], ...)
    +-- LogicalFilter (predicates=[dt_year#4 = 2026, ...])
```

The filter which constrains that column is still below the aggregate, so
one exprId now describes two different values in two scopes. When the
query is rewritten by a materialized view,
`AbstractMaterializedViewRule#rewriteExpression` shuttles the
compensation predicates through the query top plan with
`shuttleExpressionWithLineage`, which resolves slots by exprId only. It
picked up the constant alias above the aggregate and turned `dt_year#4 =
2026` into `2026 = 2026`; the tautology is folded away, so the rewritten
MV scan loses the year predicate. The scan then reads and aggregates the
rows of every year while the top projection still outputs the constant
`2026`, i.e. a silent wrong result. This matches the reported symptom:
the MV scan keeps the other filters (`org_group IN (...)`, `dt_quarter
<= 3`, `account_type <> ...`) but not `dt_year = 2026`, and the query
result is a multiple of the correct one.

Fix: `ExpressionLineageReplacer` no longer accepts a constant alias as
the lineage of an exprId which is still produced by the plan below that
alias. Such an alias shadows the lower column instead of defining it for
the expressions evaluated there, so the compensation predicate keeps
referencing the column and is pushed to the MV scan.

A query of this shape on a partitioned MV returns the aggregation of
every year before the change and the correct per-year result after it.

### Release note

Fixed a wrong-result issue where a materialized view rewrite could drop
a filter predicate whose column was materialized as a constant by
constant propagation.
@github-actions
github-actions Bot requested a review from yiguolei as a code owner October 23, 2026 11:27
@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?

@hello-stephen

Copy link
Copy Markdown
Contributor

run buildall

@hello-stephen

Copy link
Copy Markdown
Contributor

FE UT Coverage Report

Increment line coverage 90.91% (10/11) 🎉
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