This is an automated email from the ASF dual-hosted git repository.
yujun777 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new 25a4027bd2f [fix](mtmv) Preserve child candidates in materialized view
struct info maps (#70347)
25a4027bd2f is described below
commit 25a4027bd2f249693b59e3c94c82044130c9c119
Author: yujun <[email protected]>
AuthorDate: Tue Nov 24 14:57:03 2026 +0800
[fix](mtmv) Preserve child candidates in materialized view struct info maps
(#70347)
### Background
Materialized view rewrite caches logical expressions by the tables or
relation IDs they contain. Each cached expression records a candidate
bitmap for each child, which is later used to reconstruct a complete
query plan.
### What problem does this PR solve?
Issue Number: None
Related PR: None
PR Type: Bugfix
Problem Summary: A join whose leading child has no struct info
candidates can cause a null pointer exception during materialized view
rewrite. The refresh traversal omits that child and associates a later
child's bitmap with the wrong group. Preserve one candidate set per
child so the existing Cartesian product rejects expressions with
unavailable children before plan reconstruction.
### Details
- **Frequency:** Deterministic for the covered plan shape; production
frequency is unknown.
- **Root cause:** `refresh` checks the cumulative `filteredTableMaps`
before adding each child's candidate set. An empty leading child is
skipped, shifting subsequent bitmaps away from their corresponding child
groups. `constructPlan` then dereferences a missing mapping.
- **Why existing tests missed it:** Existing coverage exercises complete
candidate sets and MV alternatives, but does not cover an unavailable
leading join child.
- **Fix:** Keep empty candidate sets in the child list. The Cartesian
product then produces no parent candidates if any child is unavailable.
Remove the now-unreachable empty-child-list check; leaf expressions
already exit before child enumeration.
- **Other paths and branches:** The shared refresh implementation serves
both table-ID and relation-ID modes. Tests exercise excluded children in
both modes and subsequent forced refresh. No backports are included.
- **Behavior change:** Incomplete struct info candidates are rejected
before plan construction; valid alternatives remain available.
---
.../apache/doris/nereids/memo/StructInfoMap.java | 9 +-
.../doris/nereids/memo/StructInfoMapTest.java | 52 ++++++++++
.../mv/nested_mtmv/nested_mtmv_struct_info.out | 63 +++++++++++++
.../mv/nested_mtmv/nested_mtmv_struct_info.groovy | 105 +++++++++++++++++++++
4 files changed, 223 insertions(+), 6 deletions(-)
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/memo/StructInfoMap.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/memo/StructInfoMap.java
index ac567c4bd34..7649af1f876 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/memo/StructInfoMap.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/memo/StructInfoMap.java
@@ -316,18 +316,15 @@ public class StructInfoMap {
groupTableSet.add(tableMaps);
filteredTableMaps.or(tableMaps);
}
- if (!filteredTableMaps.isEmpty()) {
- childrenTableMap.add(groupTableSet);
- }
+ // Keep one candidate set per child, including empty sets. The
Cartesian product
+ // must reject the whole expression if any child has no
candidates.
+ childrenTableMap.add(groupTableSet);
}
// filter the tableSet that used intersects with targetBitSet,
make sure the at least constructed
if (!structInfoMap.getTableMaps(tableIdMode).isEmpty() &&
!targetBitSet.isEmpty()
&& !filteredTableMaps.isEmpty() &&
!filteredTableMaps.intersects(targetBitSet)) {
continue;
}
- if (childrenTableMap.isEmpty()) {
- continue;
- }
// if groupExpression which has the same table set have refreshed,
continue
BitSet eachGroupExpressionTableSet = new BitSet();
for (Set<BitSet> groupExpressionBitSet : childrenTableMap) {
diff --git
a/fe/fe-core/src/test/java/org/apache/doris/nereids/memo/StructInfoMapTest.java
b/fe/fe-core/src/test/java/org/apache/doris/nereids/memo/StructInfoMapTest.java
index 6fbec7f2243..e6291daf725 100644
---
a/fe/fe-core/src/test/java/org/apache/doris/nereids/memo/StructInfoMapTest.java
+++
b/fe/fe-core/src/test/java/org/apache/doris/nereids/memo/StructInfoMapTest.java
@@ -33,6 +33,58 @@ import java.util.Set;
import java.util.stream.Collectors;
class StructInfoMapTest extends SqlTestBase {
+ @Test
+ void testTablelessJoinChild() {
+ for (String query : new String[] {
+ "select T1.id from (select 1 as k) c join T1 on c.k = T1.id",
+ "select T1.id from T1 join (select 1 as k) c on T1.id = c.k"})
{
+ CascadesContext context = createCascadesContext(query,
connectContext);
+ PlanChecker.from(context).analyze();
+ Group root = context.getMemo().getRoot();
+ BitSet target = new BitSet();
+
context.getRewritePlan().<CatalogRelation>collect(CatalogRelation.class::isInstance)
+ .forEach(relation ->
target.set(context.getStatementContext()
+ .getTableId(relation.getTable()).asInt()));
+ // A tableless child cannot supply a table-ID candidate. It must
not be omitted from
+ // the child list, otherwise the other child's bitmap is applied
to the wrong group.
+ Assertions.assertNull(root.getStructInfoMap().getStructInfo(
+ context, target, root, null, true, true));
+
Assertions.assertTrue(root.getStructInfoMap().getTableMaps(true).isEmpty());
+ }
+ }
+
+ @Test
+ void testExcludedJoinChild() {
+ for (boolean tableIdMode : new boolean[] {true, false}) {
+ for (String excludedTable : new String[] {"T1", "T2"}) {
+ CascadesContext context = createCascadesContext(
+ "select T1.id from T1 join T2 on T1.id = T2.id",
connectContext);
+ PlanChecker.from(context).analyze();
+ Set<CatalogRelation> relations = context.getRewritePlan()
+ .collect(CatalogRelation.class::isInstance);
+ BitSet target = new BitSet();
+ for (CatalogRelation relation : relations) {
+ target.set(tableIdMode ?
context.getStatementContext().getTableId(relation.getTable()).asInt()
+ : relation.getRelationId().asInt());
+ if
(relation.getTable().getName().equalsIgnoreCase(excludedTable)) {
+
context.getStatementContext().addMaterializationRewrittenSuccess(
+ relation.getTable().getFullQualifiers());
+ }
+ }
+ Group root = context.getMemo().getRoot();
+ StructInfoMap map = root.getStructInfoMap();
+ Assertions.assertNull(map.getStructInfo(context, target, root,
null, false, tableIdMode));
+ Assertions.assertTrue(map.getTableMaps(tableIdMode).isEmpty());
+
+ // Nested rewrite includes already rewritten materializations
again. A refresh must
+ // still be able to construct a complete plan after the
earlier candidate was rejected.
+ StructInfo structInfo = map.getStructInfo(context, target,
root, null, true, tableIdMode);
+ Assertions.assertNotNull(structInfo);
+ Assertions.assertEquals(relations, new
HashSet<>(structInfo.getRelations()));
+ }
+ }
+ }
+
@Test
void testTableMap() throws Exception {
connectContext.getSessionVariable().setDisableNereidsRules("PRUNE_EMPTY_PARTITION");
diff --git
a/regression-test/data/nereids_rules_p0/mv/nested_mtmv/nested_mtmv_struct_info.out
b/regression-test/data/nereids_rules_p0/mv/nested_mtmv/nested_mtmv_struct_info.out
new file mode 100644
index 00000000000..46049b073aa
--- /dev/null
+++
b/regression-test/data/nereids_rules_p0/mv/nested_mtmv/nested_mtmv_struct_info.out
@@ -0,0 +1,63 @@
+-- This file is automatically generated. You should know what you did if you
want to edit this
+-- !initial --
+1 1
+2 2
+3 3
+
+-- !initial_without_rewrite --
+1 1
+2 2
+3 3
+
+-- !stale_first_level --
+1 1
+2 2
+3 3
+4 \N
+
+-- !stale_first_level_without_rewrite --
+1 1
+2 2
+3 3
+4 \N
+
+-- !stale_nested --
+1 1
+2 2
+3 3
+4 \N
+
+-- !stale_nested_without_rewrite --
+1 1
+2 2
+3 3
+4 \N
+
+-- !stale_nested_cache_first --
+1 1
+2 2
+3 3
+4 \N
+
+-- !stale_nested_cache_repeat --
+1 1
+2 2
+3 3
+4 \N
+
+-- !refreshed_nested --
+1 1
+2 2
+3 3
+4 \N
+
+-- !cte_child --
+1 1
+1 1
+2 2
+2 2
+3 3
+3 3
+4 \N
+4 \N
+
diff --git
a/regression-test/suites/nereids_rules_p0/mv/nested_mtmv/nested_mtmv_struct_info.groovy
b/regression-test/suites/nereids_rules_p0/mv/nested_mtmv/nested_mtmv_struct_info.groovy
new file mode 100644
index 00000000000..de56d52c90e
--- /dev/null
+++
b/regression-test/suites/nereids_rules_p0/mv/nested_mtmv/nested_mtmv_struct_info.groovy
@@ -0,0 +1,105 @@
+// 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("nested_mtmv_struct_info") {
+ String db = context.config.getDbNameByFile(context.file)
+ sql "set enable_materialized_view_rewrite = true"
+ sql "set enable_materialized_view_nest_rewrite = true"
+ sql "set enable_sql_cache = false"
+
+ sql "drop materialized view if exists struct_info_nested_mv"
+ sql "drop materialized view if exists struct_info_left_mv"
+ sql "drop materialized view if exists struct_info_right_mv"
+ sql "drop table if exists struct_info_table1"
+ sql "drop table if exists struct_info_table2"
+ sql """
+ create table struct_info_table1 (id int, value int)
+ partition by range(id) (
+ partition p1 values [('1'), ('2')),
+ partition p2 values [('2'), ('3')),
+ partition p3 values [('3'), ('4'))
+ )
+ distributed by hash(id) buckets 2
+ properties ('replication_num' = '1')
+ """
+ sql "create table struct_info_table2 like struct_info_table1"
+ sql "insert into struct_info_table1 values (1, 1), (2, 2), (3, 3)"
+ sql "insert into struct_info_table2 values (1, 1), (2, 2), (3, 3)"
+
+ create_async_partition_mv(db, "struct_info_left_mv", """
+ select t1.id, t2.value
+ from struct_info_table1 t1 left join struct_info_table2 t2 on t1.id =
t2.id
+ """, "(id)")
+ create_async_partition_mv(db, "struct_info_right_mv", """
+ select t2.id, t2.value
+ from struct_info_table1 t1 right join struct_info_table2 t2 on t1.id =
t2.id
+ """, "(id)")
+ String query = """
+ select t1.id, t2.value
+ from struct_info_left_mv t1 left join struct_info_right_mv t2 on t1.id
= t2.id
+ """
+ create_async_partition_mv(db, "struct_info_nested_mv", query, "(id)")
+ String baseQuery = """
+ select t1.id, t2.value
+ from struct_info_table1 t1 left join struct_info_table2 t2 on t1.id =
t2.id
+ """
+
+ mv_rewrite_success_without_check_chosen(query, "struct_info_nested_mv")
+ order_qt_initial query
+ sql "set enable_materialized_view_rewrite = false"
+ order_qt_initial_without_rewrite query
+ sql "set enable_materialized_view_rewrite = true"
+
+ // The reported INSERT of id=4 needs a partition: the original p3 upper
bound is exclusive.
+ sql "alter table struct_info_table1 add partition p4 values [('4'), ('5'))"
+ sql "insert into struct_info_table1 values (4, 4)"
+ order_qt_stale_first_level baseQuery
+ sql "set enable_materialized_view_rewrite = false"
+ order_qt_stale_first_level_without_rewrite baseQuery
+ sql "set enable_materialized_view_rewrite = true"
+ sql "refresh materialized view struct_info_left_mv auto"
+ waitingMTMVTaskFinished(getJobName(db, "struct_info_left_mv"))
+ sql "sync"
+
+ // Refresh only the first-level MV. Exploring rewrite with the stale
nested MV
+ // must retain the correct child-to-bitmap correspondence.
+ order_qt_stale_nested query
+ sql "set enable_materialized_view_rewrite = false"
+ order_qt_stale_nested_without_rewrite query
+ sql "set enable_materialized_view_rewrite = true"
+ sql "set enable_sql_cache = true"
+ order_qt_stale_nested_cache_first query
+ order_qt_stale_nested_cache_repeat query
+ sql "set enable_sql_cache = false"
+
+ sql "refresh materialized view struct_info_nested_mv auto"
+ waitingMTMVTaskFinished(getJobName(db, "struct_info_nested_mv"))
+ sql "sync"
+ mv_rewrite_success_without_check_chosen(query, "struct_info_nested_mv")
+ order_qt_refreshed_nested query
+
+ // A materialized CTE consumer has no table-ID candidate. Keep it on the
leading
+ // side of a join during CBO MV exploration to exercise the missing-child
path.
+ sql "set inline_cte_referenced_threshold = 0"
+ sql "set pre_materialized_view_rewrite_strategy = 'NOT_IN_RBO'"
+ order_qt_cte_child """
+ with c as (select * from struct_info_left_mv)
+ select c.id, t.value from c left join struct_info_right_mv t on c.id =
t.id
+ union all
+ select c.id, t.value from c left join struct_info_right_mv t on c.id =
t.id
+ """
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]