This is an automated email from the ASF dual-hosted git repository. morningman pushed a commit to branch branch-1.2-lts in repository https://gitbox.apache.org/repos/asf/doris.git
commit e48404e1c08f4887506181a4b6dd577cc4dc534e Author: morrySnow <101034200+morrys...@users.noreply.github.com> AuthorDate: Tue Jan 31 23:42:53 2023 +0800 [fix](planner) create view generate wrong sql when sql contains multi count distinct (#16092) If sql in create view has more than one count distinct, and write column name explicitly. We will generate sql contains function multi_count_distinct. It cannot be analyzed and all query containing this view will fail. --- .../org/apache/doris/analysis/BaseViewStmt.java | 1 + .../java/org/apache/doris/analysis/SelectStmt.java | 8 ++- .../suites/ddl_p0/test_create_view.groovy | 72 ++++++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/BaseViewStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/BaseViewStmt.java index 477e440f5e..8114448f0d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/BaseViewStmt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/BaseViewStmt.java @@ -117,6 +117,7 @@ public class BaseViewStmt extends DdlStmt { Analyzer tmpAnalyzer = new Analyzer(analyzer); List<String> colNames = cols.stream().map(c -> c.getColName()).collect(Collectors.toList()); + cloneStmt.setNeedToSql(true); cloneStmt.substituteSelectList(tmpAnalyzer, colNames); try (ToSqlContext toSqlContext = ToSqlContext.getOrNewThreadLocalContext()) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java index 1aae7c324f..8f47a9ed04 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java @@ -1898,7 +1898,7 @@ public class SelectStmt extends QueryStmt { if (i != 0) { strBuilder.append(", "); } - if (needToSql) { + if (needToSql && CollectionUtils.isNotEmpty(originalExpr)) { strBuilder.append(originalExpr.get(i).toSql()); } else { strBuilder.append(resultExprs.get(i).toSql()); @@ -2072,6 +2072,9 @@ public class SelectStmt extends QueryStmt { // Resolve and replace non-InlineViewRef table refs with a BaseTableRef or ViewRef. TableRef tblRef = fromClause.get(i); tblRef = analyzer.resolveTableRef(tblRef); + if (tblRef instanceof InlineViewRef) { + ((InlineViewRef) tblRef).setNeedToSql(needToSql); + } Preconditions.checkNotNull(tblRef); fromClause.set(i, tblRef); tblRef.setLeftTblRef(leftTblRef); @@ -2101,6 +2104,9 @@ public class SelectStmt extends QueryStmt { resultExprs.add(item.getExpr()); } } + if (needToSql) { + originalExpr = Expr.cloneList(resultExprs); + } // substitute group by if (groupByClause != null) { boolean aliasFirst = false; diff --git a/regression-test/suites/ddl_p0/test_create_view.groovy b/regression-test/suites/ddl_p0/test_create_view.groovy new file mode 100644 index 0000000000..4c401017ee --- /dev/null +++ b/regression-test/suites/ddl_p0/test_create_view.groovy @@ -0,0 +1,72 @@ +// 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("test_create_view") { + + sql """DROP TABLE IF EXISTS count_distinct""" + sql """ + CREATE TABLE IF NOT EXISTS count_distinct + ( + RQ DATE NOT NULL COMMENT "日期", + v1 VARCHAR(100) NOT NULL COMMENT "字段1", + v2 VARCHAR(100) NOT NULL COMMENT "字段2", + v3 VARCHAR(100) REPLACE_IF_NOT_NULL COMMENT "字段3" + ) + AGGREGATE KEY(RQ,v1,v2) + PARTITION BY RANGE(RQ) + ( + PARTITION p20220908 VALUES LESS THAN ('2022-09-09') + ) + DISTRIBUTED BY HASH(v1,v2) BUCKETS 3 + PROPERTIES( + "replication_num" = "1", + "dynamic_partition.enable" = "true", + "dynamic_partition.time_unit" = "DAY", + "dynamic_partition.start" = "-3", + "dynamic_partition.end" = "3", + "dynamic_partition.prefix" = "p", + "dynamic_partition.buckets" = "3" + ); + """ + sql """ + CREATE VIEW IF NOT EXISTS test_count_distinct + ( + RQ comment "日期", + v1 comment "v1", + v2 comment "v2", + v3 comment "v3" + ) + AS + select aa.RQ as RQ, aa.v1 as v1,aa.v2 as v2 , bb.v3 as v3 from + ( + select RQ, count(distinct v1) as v1 , count(distinct v2 ) as v2 + from count_distinct + group by RQ + ) aa + LEFT JOIN + ( + select RQ, max(v3) as v3 + from count_distinct + group by RQ + ) bb + on aa.RQ = bb.RQ; + """ + + sql """select * from test_count_distinct""" + sql """DROP VIEW IF EXISTS test_count_distinct""" + sql """DROP TABLE IF EXISTS count_distinct""" +} --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org