diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateMaterializedViewStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateMaterializedViewStmt.java index bc1364a7d686b1..f5ba40bbf0e105 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateMaterializedViewStmt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateMaterializedViewStmt.java @@ -225,7 +225,7 @@ public void analyze(Analyzer analyzer) throws UserException { mvKeysType = KeysType.AGG_KEYS; } if (selectStmt.getWhereClause() != null) { - if (!isReplay && selectStmt.getWhereClause().hasAggregateSlot()) { + if (!isReplay && selectStmt.getWhereClause().hasAggregateSlot(getMVKeysType())) { throw new AnalysisException( "The where clause contained aggregate column is not supported, expr:" + selectStmt.getWhereClause().toSql()); @@ -585,7 +585,7 @@ private MVColumnItem buildMVColumnItem(Analyzer analyzer, FunctionCallExpr funct mvAggregateType = AggregateType.valueOf(functionName.toUpperCase()); } - if (!isReplay && defineExpr.hasAggregateSlot()) { + if (!isReplay && defineExpr.hasAggregateSlot(getMVKeysType())) { SlotRef slot = null; if (defineExpr instanceof SlotRef) { slot = (SlotRef) defineExpr; diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java index 653fb92003a67e..3091a7695683a9 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java @@ -28,6 +28,7 @@ import org.apache.doris.catalog.Function; import org.apache.doris.catalog.Function.NullableMode; import org.apache.doris.catalog.FunctionSet; +import org.apache.doris.catalog.KeysType; import org.apache.doris.catalog.MapType; import org.apache.doris.catalog.MaterializedIndexMeta; import org.apache.doris.catalog.PrimitiveType; @@ -2271,9 +2272,9 @@ protected static boolean hasNullableChild(List children) { return false; } - public boolean hasAggregateSlot() { + public boolean hasAggregateSlot(KeysType keysType) { for (Expr expr : children) { - if (expr.hasAggregateSlot()) { + if (expr.hasAggregateSlot(keysType)) { return true; } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/SlotRef.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/SlotRef.java index 3e8117e365b950..485934af18e05b 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/SlotRef.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/SlotRef.java @@ -22,6 +22,7 @@ import org.apache.doris.catalog.Column; import org.apache.doris.catalog.JdbcTable; +import org.apache.doris.catalog.KeysType; import org.apache.doris.catalog.MaterializedIndexMeta; import org.apache.doris.catalog.OdbcTable; import org.apache.doris.catalog.TableIf; @@ -499,7 +500,13 @@ public boolean isBoundByTupleIds(List tids) { } @Override - public boolean hasAggregateSlot() { + public boolean hasAggregateSlot(KeysType keysType) { + if (KeysType.UNIQUE_KEYS.equals(keysType)) { + Column column = getColumn(); + if (column != null && !column.isKey()) { + return true; + } + } return desc.getColumn().isAggregated(); } diff --git a/regression-test/suites/mv_p0/where/k123/k123.groovy b/regression-test/suites/mv_p0/where/k123/k123.groovy index 366fc76580ff48..3527a889f83187 100644 --- a/regression-test/suites/mv_p0/where/k123/k123.groovy +++ b/regression-test/suites/mv_p0/where/k123/k123.groovy @@ -87,4 +87,27 @@ suite ("k123p") { mv_rewrite_success("""select k1,k2+k3 from d_table where k1 = 2 and k4 = "b";""", "k123p4w") + sql """ DROP TABLE IF EXISTS u_table; """ + sql """ + create table u_table( + k1 int null, + k2 int not null, + k3 bigint null, + k4 varchar(100) null + ) + unique key (k1,k2) + distributed BY hash(k1) buckets 3 + properties("replication_num" = "1"); + """ + + sql "insert into u_table select 1,1,1,'a';" + sql "insert into u_table select 2,2,2,'bb';" + sql "insert into u_table select 3,-3,null,'c';" + + test { + sql """create materialized view k123p4w as select k1 as aa1,k2 as aa2,k3 as aa3 from u_table where k4 = "b";""" + exception "The where clause contained aggregate column is not supported" + } + + createMV ("""create materialized view k123p1w as select k1 as a1,k2 as a2,k3 as a3 from u_table where k1 = 1;""") }