Skip to content

Commit 8866161

Browse files
authored
branch-3.1:[fix](mtmv) Fix nested mv rewritten fail when bottom mv is partitioned #57558 (#58276)
picked from #57558
1 parent ad1047e commit 8866161

File tree

2 files changed

+102
-26
lines changed

2 files changed

+102
-26
lines changed

fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/PartitionCompensator.java

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.apache.doris.catalog.Partition;
2222
import org.apache.doris.catalog.PartitionInfo;
2323
import org.apache.doris.catalog.PartitionType;
24-
import org.apache.doris.catalog.TableIf;
2524
import org.apache.doris.common.AnalysisException;
2625
import org.apache.doris.common.Pair;
2726
import org.apache.doris.mtmv.BaseTableInfo;
@@ -55,9 +54,10 @@
5554
public class PartitionCompensator {
5655

5756
public static final Logger LOG = LogManager.getLogger(PartitionCompensator.class);
58-
// if partition pair is null which means can not get partitions from table in QueryPartitionCollector,
59-
// we think this table scan query all partitions default
57+
// if the partition pair is null which means could not get partitions from table in QueryPartitionCollector,
58+
// we think the table scans query all-partitions default
6059
public static final Pair<RelationId, Set<String>> ALL_PARTITIONS = Pair.of(null, null);
60+
// It means all partitions are used when query
6161
public static final Collection<Pair<RelationId, Set<String>>> ALL_PARTITIONS_LIST =
6262
ImmutableList.of(ALL_PARTITIONS);
6363

@@ -180,33 +180,24 @@ public static Map<List<String>, Set<String>> getQueryUsedPartitions(StatementCon
180180
// if value is not empty, means query some partitions
181181
Map<List<String>, Set<String>> queryUsedRelatedTablePartitionsMap = new HashMap<>();
182182
tableLoop:
183-
for (Map.Entry<List<String>, TableIf> queryUsedTableEntry : statementContext.getTables().entrySet()) {
183+
for (List<String> queryUsedTable : tableUsedPartitionNameMap.keySet()) {
184184
Set<String> usedPartitionSet = new HashSet<>();
185185
Collection<Pair<RelationId, Set<String>>> tableUsedPartitions =
186-
tableUsedPartitionNameMap.get(queryUsedTableEntry.getKey());
187-
if (!tableUsedPartitions.isEmpty()) {
188-
if (ALL_PARTITIONS_LIST.equals(tableUsedPartitions)) {
189-
queryUsedRelatedTablePartitionsMap.put(queryUsedTableEntry.getKey(), null);
190-
continue;
191-
}
192-
for (Pair<RelationId, Set<String>> partitionPair : tableUsedPartitions) {
193-
if (!customRelationIdSet.isEmpty()) {
194-
if (ALL_PARTITIONS.equals(partitionPair)) {
195-
continue;
196-
}
197-
if (customRelationIdSet.get(partitionPair.key().asInt())) {
198-
usedPartitionSet.addAll(partitionPair.value());
199-
}
200-
} else {
201-
if (ALL_PARTITIONS.equals(partitionPair)) {
202-
queryUsedRelatedTablePartitionsMap.put(queryUsedTableEntry.getKey(), null);
203-
continue tableLoop;
204-
}
205-
usedPartitionSet.addAll(partitionPair.value());
206-
}
186+
tableUsedPartitionNameMap.get(queryUsedTable);
187+
if (ALL_PARTITIONS_LIST.equals(tableUsedPartitions)) {
188+
// It means all partitions are used when query
189+
queryUsedRelatedTablePartitionsMap.put(queryUsedTable, null);
190+
continue;
191+
}
192+
for (Pair<RelationId, Set<String>> tableUsedPartitionPair : tableUsedPartitions) {
193+
if (ALL_PARTITIONS.equals(tableUsedPartitionPair)) {
194+
// It means all partitions are used when query
195+
queryUsedRelatedTablePartitionsMap.put(queryUsedTable, null);
196+
continue tableLoop;
207197
}
198+
usedPartitionSet.addAll(tableUsedPartitionPair.value());
208199
}
209-
queryUsedRelatedTablePartitionsMap.put(queryUsedTableEntry.getKey(), usedPartitionSet);
200+
queryUsedRelatedTablePartitionsMap.put(queryUsedTable, usedPartitionSet);
210201
}
211202
return queryUsedRelatedTablePartitionsMap;
212203
}

regression-test/suites/nereids_rules_p0/mv/nested_mtmv/nested_mtmv.groovy

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,4 +753,89 @@ suite("nested_mtmv") {
753753
mv_rewrite_any_success(sql_5, [mv_3, mv_4, mv_5])
754754
compare_res(sql_5 + " order by 1,2,3,4,5,6,7,8,9,10,11,12,13")
755755

756+
sql """
757+
drop table if exists sales_partitioned
758+
"""
759+
760+
sql """
761+
CREATE TABLE sales_partitioned (
762+
product_id INT NOT NULL,
763+
city VARCHAR(50) NOT NULL,
764+
sale_date DATE NOT NULL,
765+
amount DECIMAL(18, 2) NOT NULL
766+
)
767+
DUPLICATE KEY(product_id, city, sale_date)
768+
PARTITION BY RANGE(sale_date) (
769+
PARTITION p20251001 VALUES [('2025-10-01'), ('2025-10-02')),
770+
PARTITION p20251002 VALUES [('2025-10-02'), ('2025-10-03')),
771+
PARTITION p20251003 VALUES [('2025-10-03'), ('2025-10-04')),
772+
PARTITION p_other VALUES [('2025-10-04'), ('2025-11-01'))
773+
)
774+
DISTRIBUTED BY HASH(product_id) BUCKETS 10
775+
PROPERTIES (
776+
"replication_num" = "1"
777+
);
778+
"""
779+
780+
781+
sql """
782+
INSERT INTO sales_partitioned (product_id, city, sale_date, amount) VALUES
783+
(101, 'Beijing', '2025-10-01', 100.00), -- p20251001
784+
(101, 'Shanghai', '2025-10-01', 150.00), -- p20251001
785+
(102, 'Beijing', '2025-10-02', 200.00), -- p20251002
786+
(102, 'Shanghai', '2025-10-02', 250.00), -- p20251002
787+
(101, 'Beijing', '2025-10-03', 120.00), -- p20251003
788+
(102, 'Shanghai', '2025-10-03', 300.00); -- p20251003
789+
"""
790+
791+
create_async_partition_mv(db, "zz_mtmv1", """
792+
SELECT
793+
city,
794+
sale_date,
795+
SUM(amount) AS daily_city_amount
796+
FROM
797+
sales_partitioned
798+
GROUP BY
799+
city, sale_date;
800+
""", "(sale_date)")
801+
mv_rewrite_success("""
802+
SELECT
803+
city,
804+
SUM(amount) AS total_city_amount
805+
FROM
806+
sales_partitioned
807+
WHERE
808+
sale_date >= '2025-10-01' AND sale_date <= '2025-10-03'
809+
GROUP BY
810+
city;
811+
""", "zz_mtmv1", true, is_partition_statistics_ready(db, ["zz_mtmv1"]))
812+
813+
create_async_partition_mv(db, "zz_mtmv2", """
814+
SELECT
815+
city,
816+
date_trunc(sale_date, 'MONTH') AS sale_date,
817+
SUM(daily_city_amount) AS monthly_city_amount
818+
FROM
819+
zz_mtmv1
820+
GROUP BY
821+
city,
822+
date_trunc(sale_date, 'MONTH')
823+
""", "(sale_date)")
824+
825+
mv_rewrite_all_success_without_check_chosen("""
826+
SELECT
827+
date_trunc(sale_date, 'MONTH') AS sale_date,
828+
SUM(daily_city_amount) AS monthly_city_amount
829+
FROM
830+
(SELECT
831+
city,
832+
sale_date,
833+
SUM(amount) AS daily_city_amount
834+
FROM
835+
sales_partitioned
836+
GROUP BY
837+
city, sale_date) as t
838+
GROUP BY
839+
date_trunc(sale_date, 'MONTH');
840+
""", ["zz_mtmv1", "zz_mtmv2"])
756841
}

0 commit comments

Comments
 (0)