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 7ba05121c7..3573d5620b 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 @@ -34,6 +34,7 @@ import org.apache.doris.thrift.TSlotRef; import com.google.common.base.MoreObjects; import com.google.common.base.Objects; import com.google.common.base.Preconditions; +import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -189,6 +190,10 @@ public class SlotRef extends Expr { if (type.equals(Type.BOOLEAN)) { selectivity = DEFAULT_SELECTIVITY; } + if (tblName == null && StringUtils.isNotEmpty(desc.getParent().getLastAlias()) + && !desc.getParent().getLastAlias().equals(desc.getParent().getTable().getName())) { + tblName = new TableName(null, null, desc.getParent().getLastAlias()); + } } @Override diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/TupleDescriptor.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/TupleDescriptor.java index 55f7d141f5..df50dddffd 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/TupleDescriptor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/TupleDescriptor.java @@ -192,6 +192,10 @@ public class TupleDescriptor { return (aliases != null) ? aliases[0] : null; } + public String getLastAlias() { + return (aliases != null) ? aliases[aliases.length - 1] : null; + } + public TableName getAliasAsName() { return (aliases != null) ? new TableName(null, null, aliases[0]) : null; } diff --git a/fe/fe-core/src/test/java/org/apache/doris/policy/PolicyTest.java b/fe/fe-core/src/test/java/org/apache/doris/policy/PolicyTest.java index 5fb6d3dd5e..e5734c32a2 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/policy/PolicyTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/policy/PolicyTest.java @@ -189,7 +189,7 @@ public class PolicyTest extends TestWithFeService { String subQuerySql = "select * from table2 where k1 in (select k1 from table1)"; Assertions.assertTrue(getSQLPlanOrErrorMsg(subQuerySql).contains("PREDICATES: `k1` = 1, `k2` = 1")); String aliasSql = "select * from table1 t1 join table2 t2 on t1.k1=t2.k1"; - Assertions.assertTrue(getSQLPlanOrErrorMsg(aliasSql).contains("PREDICATES: `k1` = 1, `k2` = 1")); + Assertions.assertTrue(getSQLPlanOrErrorMsg(aliasSql).contains("PREDICATES: `t1`.`k1` = 1, `t1`.`k2` = 1")); dropPolicy("DROP ROW POLICY test_row_policy1 ON test.table1"); dropPolicy("DROP ROW POLICY test_row_policy2 ON test.table1"); } diff --git a/regression-test/suites/correctness/test_mv_alias_table_name.groovy b/regression-test/suites/correctness/test_mv_alias_table_name.groovy new file mode 100644 index 0000000000..c588d002dc --- /dev/null +++ b/regression-test/suites/correctness/test_mv_alias_table_name.groovy @@ -0,0 +1,63 @@ +// 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. + + +/* +exception throw before bug fix: Unknown column 'mv_bitmap_union_mh' in 'default_cluster:test.original_table' +*/ +suite("test_mv_alias_table_name") { + sql """ + DROP TABLE IF EXISTS original_table; + """ + sql """ + CREATE TABLE original_table + ( + day date, + aid bigint, + lid bigint, + yid bigint, + mh bigint, + my bigint + ) + DUPLICATE KEY(`day`, `aid`, `lid`) + DISTRIBUTED BY HASH(aid) + PROPERTIES ("replication_num" = "1" ); + """ + + sql """ + create materialized view mv_table as + select day,aid,lid, + bitmap_union(to_bitmap(mh)) as wu, + bitmap_union(to_bitmap(my)) as mu + from original_table + group by day, aid, lid; + """ + + sql """ + insert into original_table values('2022-10-16', 1665710553, 1665710553, 1665710553, 1665700553, 1665700553); + """ + + sleep(2000) + + sql """ + select t0.aid, t0.lid, count(distinct mh), count(distinct my) + from original_table t0 + where t0.day = '2022-10-16' and t0.lid > 0 group by t0.aid, t0.lid; + """ + + } +