From 04730d0211d014183ef75ea5f3de767e76ace978 Mon Sep 17 00:00:00 2001 From: englefly Date: Mon, 24 Oct 2022 17:19:47 +0800 Subject: [PATCH 1/3] fix --- .../org/apache/doris/analysis/Analyzer.java | 24 +++++++++++-------- .../apache/doris/analysis/InlineViewRef.java | 3 +++ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java index d3399be4672dbd..6777e3e0b2b824 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java @@ -152,18 +152,10 @@ public class Analyzer { // Flag indicating if this analyzer instance belongs to a subquery. private boolean isSubquery = false; - - public boolean isInlineView() { - return isInlineView; - } - - public void setInlineView(boolean inlineView) { - isInlineView = inlineView; - } - // Flag indicating if this analyzer instance belongs to an inlineview. private boolean isInlineView = false; + private String explicitViewAlias; // Flag indicating whether this analyzer belongs to a WITH clause view. private boolean isWithClause = false; @@ -519,6 +511,18 @@ public int getCallDepth() { return callDepth; } + public void setInlineView(boolean inlineView) { + isInlineView = inlineView; + } + + public void setExplicitViewAlias(String alias) { + explicitViewAlias = alias; + } + + public String getExplicitViewAlias() { + return explicitViewAlias; + } + /** * Registers a local view definition with this analyzer. Throws an exception if a view * definition with the same alias has already been registered or if the number of @@ -806,7 +810,7 @@ public SlotDescriptor registerColumnRef(TableName tblName, String colName) throw // =================================================== // Someone may concern that if t2 is not alias of t, this fix will cause incorrect resolve. In fact, // this does not happen, since we push t2.a in (1.2) down to this inline view, t2 must be alias of t. - if (d == null && isInlineView) { + if (d == null && isInlineView && newTblName.getTbl().equals(explicitViewAlias)) { d = resolveColumnRef(colName); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/InlineViewRef.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/InlineViewRef.java index 8adc8047f013c7..cf7b459005b1b2 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/InlineViewRef.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/InlineViewRef.java @@ -187,6 +187,9 @@ public void analyze(Analyzer analyzer) throws AnalysisException, UserException { // Analyze the inline view query statement with its own analyzer inlineViewAnalyzer = new Analyzer(analyzer); inlineViewAnalyzer.setInlineView(true); + if (hasExplicitAlias) { + inlineViewAnalyzer.setExplicitViewAlias(aliases[0]); + } queryStmt.analyze(inlineViewAnalyzer); correlatedTupleIds.addAll(queryStmt.getCorrelatedTupleIds(inlineViewAnalyzer)); From 448dd9fc18317bf5f7287a1170f281f4aa9b4f31 Mon Sep 17 00:00:00 2001 From: englefly Date: Mon, 24 Oct 2022 17:29:23 +0800 Subject: [PATCH 2/3] update regression --- .../suites/correctness/test_pushdown_pred_to_view.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/regression-test/suites/correctness/test_pushdown_pred_to_view.groovy b/regression-test/suites/correctness/test_pushdown_pred_to_view.groovy index 3cf285b2fd40d0..9b5556767e1252 100644 --- a/regression-test/suites/correctness/test_pushdown_pred_to_view.groovy +++ b/regression-test/suites/correctness/test_pushdown_pred_to_view.groovy @@ -54,7 +54,7 @@ The same resolve error occurs when re-analyze v2. """ qt_sql """ - select * from ${viewName} as v1 join ${viewName} as v2 on v1.id=v2.id and v1.id>0; + select * from ${viewName} as v1 join ${viewName} as v2 where v1.id=v2.id and v1.id>0; """ sql "DROP VIEW ${viewName}" sql "DROP TABLE ${tableName}" From db2d4d87cc3de172e1f06c4c325bf283e4ce8c6e Mon Sep 17 00:00:00 2001 From: englefly Date: Tue, 25 Oct 2022 16:19:29 +0800 Subject: [PATCH 3/3] add regression test --- .../correctness/test_table_alias.groovy | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 regression-test/suites/correctness/test_table_alias.groovy diff --git a/regression-test/suites/correctness/test_table_alias.groovy b/regression-test/suites/correctness/test_table_alias.groovy new file mode 100644 index 00000000000000..7c81a703d334c7 --- /dev/null +++ b/regression-test/suites/correctness/test_table_alias.groovy @@ -0,0 +1,47 @@ +// 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_table_alias") { + sql """ DROP TABLE IF EXISTS tbl_alias """ + sql """ + CREATE TABLE tbl_alias ( + `id` int + ) ENGINE=OLAP + AGGREGATE KEY(`id`) + COMMENT "OLAP" + DISTRIBUTED BY HASH(`id`) BUCKETS 1 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1", + "in_memory" = "false", + "storage_format" = "V2" + ); + """ + + try { + test { + sql """ + select * + from (select t3.id + from (select * from tbl_alias) t1 + ) t2 + """ + exception "errCode = 2, detailMessage = Unknown column 'id' in 't3'" + } + } finally { + sql "drop table if exists tbl_alias" + } +} \ No newline at end of file