diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java index 734f5d9bd67e83..41a00b422d4186 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java @@ -250,7 +250,9 @@ private LogicalPlan preAggForRandomDistribution(LogicalOlapScan olapScan) { List childOutputSlots = olapScan.computeOutput(); List groupByExpressions = new ArrayList<>(); List outputExpressions = new ArrayList<>(); - List columns = olapTable.getBaseSchema(); + List columns = olapScan.isIndexSelected() + ? olapTable.getSchemaByIndexId(olapScan.getSelectedIndexId()) + : olapTable.getBaseSchema(); for (Column col : columns) { // use exist slot in the plan diff --git a/regression-test/data/statistics/test_analyze_mv.out b/regression-test/data/statistics/test_analyze_mv.out new file mode 100644 index 00000000000000..a72493354303b5 --- /dev/null +++ b/regression-test/data/statistics/test_analyze_mv.out @@ -0,0 +1,6 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !test_agg -- +1 6 +2 9 +3 6 + diff --git a/regression-test/suites/statistics/test_analyze_mv.groovy b/regression-test/suites/statistics/test_analyze_mv.groovy index daa8d780aee130..aafac346f5a63a 100644 --- a/regression-test/suites/statistics/test_analyze_mv.groovy +++ b/regression-test/suites/statistics/test_analyze_mv.groovy @@ -789,6 +789,25 @@ suite("test_analyze_mv") { assertEquals("1", result[0][7]) assertEquals("5", result[0][8]) + sql """drop table if exists testMvDirectSelect""" + sql """ + CREATE TABLE testMvDirectSelect ( + key1 int NOT NULL, + key2 int NOT NULL, + value int SUM + )ENGINE=OLAP + AGGREGATE KEY(key1, key2) + COMMENT "OLAP" + DISTRIBUTED BY RANDOM BUCKETS 2 + PROPERTIES ( + "replication_num" = "1" + ); + """ + + createMV("CREATE MATERIALIZED VIEW aggMv as select key1, SUM(value) from testMvDirectSelect group by key1;") + sql """insert into testMvDirectSelect values (1, 1, 1), (1, 2, 2), (1, 3, 3), (2, 1, 4), (2, 2, 5), (3, 2, 6)""" + qt_test_agg """select * from testMvDirectSelect index aggMv order by mv_key1""" + sql """drop database if exists test_analyze_mv""" }