This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch branch-4.2
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-4.2 by this push:
     new 134637a4ab6 branch-4.2: [fix](mtmv) Check MV partition coverage when 
union compensation is disabled #71138 (#71178)
134637a4ab6 is described below

commit 134637a4ab668e12e767f5faab47e4bf31c086d9
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Fri Dec 4 09:58:36 2026 +0800

    branch-4.2: [fix](mtmv) Check MV partition coverage when union compensation 
is disabled #71138 (#71178)
    
    Cherry-picked from #71138
    
    Co-authored-by: yujun <[email protected]>
---
 .../mv/AbstractMaterializedViewRule.java           | 13 +++-
 .../partition_freshness_without_union.out          | 10 +++
 .../partition_freshness_without_union.groovy       | 77 ++++++++++++++++++++++
 3 files changed, 97 insertions(+), 3 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewRule.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewRule.java
index 87a1b40a24a..2f8cac78f85 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewRule.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewRule.java
@@ -320,9 +320,8 @@ public abstract class AbstractMaterializedViewRule 
implements ExplorationRuleFac
             if (rewrittenPlan == null) {
                 continue;
             }
-            Pair<Map<BaseTableInfo, Set<String>>, Map<BaseColInfo, 
Set<String>>> invalidPartitions;
-            if (PartitionCompensator.needUnionRewrite(materializationContext, 
cascadesContext.getStatementContext())
-                    && sessionVariable.isEnableMaterializedViewUnionRewrite()) 
{
+            // Validate partition coverage even when union compensation is 
disabled.
+            if (PartitionCompensator.needUnionRewrite(materializationContext, 
cascadesContext.getStatementContext())) {
                 MTMV mtmv = ((AsyncMaterializationContext) 
materializationContext).getMtmv();
                 Map<List<String>, Set<String>> queryUsedPartitions = 
PartitionCompensator.getQueryUsedPartitions(
                         cascadesContext.getStatementContext(), 
queryStructInfo.getRelationBitSet());
@@ -357,6 +356,7 @@ public abstract class AbstractMaterializedViewRule 
implements ExplorationRuleFac
                     }
                     return rewriteResults;
                 }
+                Pair<Map<BaseTableInfo, Set<String>>, Map<BaseColInfo, 
Set<String>>> invalidPartitions;
                 try {
                     invalidPartitions = 
calcInvalidPartitions(queryUsedPartitions, rewrittenPlan,
                             cascadesContext, (AsyncMaterializationContext) 
materializationContext);
@@ -377,6 +377,13 @@ public abstract class AbstractMaterializedViewRule 
implements ExplorationRuleFac
                 }
                 boolean needRemoveMvPartitions = 
!invalidPartitions.key().isEmpty();
                 boolean needBaseTableUnion = 
!invalidPartitions.value().isEmpty();
+                if (needBaseTableUnion && 
!sessionVariable.isEnableMaterializedViewUnionRewrite()) {
+                    materializationContext.recordFailReason(queryStructInfo,
+                            "Base table partition compensation is disabled",
+                            () -> String.format("base table partitions 
requiring compensation: %s",
+                                    invalidPartitions.value()));
+                    return rewriteResults;
+                }
                 if (needBaseTableUnion && !canUnionRewrite(queryPlan,
                         (AsyncMaterializationContext) materializationContext, 
cascadesContext)) {
                     materializationContext.recordFailReason(queryStructInfo,
diff --git 
a/regression-test/data/nereids_rules_p0/mv/availability/partition_freshness_without_union.out
 
b/regression-test/data/nereids_rules_p0/mv/availability/partition_freshness_without_union.out
new file mode 100644
index 00000000000..d331968ee23
--- /dev/null
+++ 
b/regression-test/data/nereids_rules_p0/mv/availability/partition_freshness_without_union.out
@@ -0,0 +1,10 @@
+-- This file is automatically generated. You should know what you did if you 
want to edit this
+-- !all_partitions --
+22
+
+-- !fresh_partition --
+1
+
+-- !stale_partition --
+21
+
diff --git 
a/regression-test/suites/nereids_rules_p0/mv/availability/partition_freshness_without_union.groovy
 
b/regression-test/suites/nereids_rules_p0/mv/availability/partition_freshness_without_union.groovy
new file mode 100644
index 00000000000..62273a239f0
--- /dev/null
+++ 
b/regression-test/suites/nereids_rules_p0/mv/availability/partition_freshness_without_union.groovy
@@ -0,0 +1,77 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+suite("partition_freshness_without_union") {
+    sql "SET enable_sql_cache = false"
+    sql "SET enable_materialized_view_rewrite = true"
+    sql "SET enable_materialized_view_union_rewrite = false"
+
+    sql "DROP MATERIALIZED VIEW IF EXISTS mv_partition_freshness_without_union"
+    sql "DROP TABLE IF EXISTS partition_freshness_base"
+    sql """
+        CREATE TABLE partition_freshness_base (k INT NOT NULL)
+        DUPLICATE KEY(k)
+        PARTITION BY RANGE(k) (
+            PARTITION p1 VALUES LESS THAN (10),
+            PARTITION p2 VALUES LESS THAN (20)
+        )
+        DISTRIBUTED BY HASH(k) BUCKETS 1
+        PROPERTIES ('replication_num' = '1')
+    """
+    sql "INSERT INTO partition_freshness_base VALUES (1), (10)"
+    sql """
+        CREATE MATERIALIZED VIEW mv_partition_freshness_without_union
+        BUILD IMMEDIATE REFRESH AUTO ON MANUAL
+        PARTITION BY (k)
+        DISTRIBUTED BY HASH(k) BUCKETS 1
+        PROPERTIES ('replication_num' = '1', 'grace_period' = '0')
+        AS SELECT k FROM partition_freshness_base
+    """
+    waitingMTMVTaskFinishedByMvName("mv_partition_freshness_without_union")
+
+    // Keep p1 fresh and make p2 stale without refreshing the view.
+    sql "INSERT INTO partition_freshness_base VALUES (11)"
+    sql "SYNC"
+
+    // Prefer the MV when it is eligible so plan assertions do not depend on 
small-table cost estimates.
+    def allQuery = """
+        SELECT /*+ USE_MV(mv_partition_freshness_without_union) */ SUM(k)
+        FROM partition_freshness_base
+    """
+    explain {
+        sql allQuery
+        contains "partition_freshness_base(partition_freshness_base)"
+        notContains 
"mv_partition_freshness_without_union(mv_partition_freshness_without_union)"
+    }
+    order_qt_all_partitions allQuery
+
+    def freshQuery = "${allQuery} WHERE k < 10"
+    explain {
+        sql freshQuery
+        contains 
"mv_partition_freshness_without_union(mv_partition_freshness_without_union)"
+        contains "partitions=1/2"
+    }
+    order_qt_fresh_partition freshQuery
+
+    def staleQuery = "${allQuery} WHERE k >= 10"
+    explain {
+        sql staleQuery
+        contains "partition_freshness_base(partition_freshness_base)"
+        notContains 
"mv_partition_freshness_without_union(mv_partition_freshness_without_union)"
+    }
+    order_qt_stale_partition staleQuery
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to