Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix](planner) inlineView alias error #13600

Merged
merged 3 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
englefly marked this conversation as resolved.
Show resolved Hide resolved
"""
sql "DROP VIEW ${viewName}"
sql "DROP TABLE ${tableName}"
Expand Down
47 changes: 47 additions & 0 deletions regression-test/suites/correctness/test_table_alias.groovy
Original file line number Diff line number Diff line change
@@ -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"
}
}