Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ public String getDb() {
}

// TODO(zc): used to get all dbs for lock
public void getDbs(Analyzer analyzer, Map<String, Database> dbs) throws AnalysisException {
public void getDbs(Analyzer analyzer, Map<String, Database> dbs, Set<String> parentViewNameSet) throws AnalysisException {
// get dbs of statement
queryStmt.getDbs(analyzer, dbs);
queryStmt.getDbs(analyzer, dbs, parentViewNameSet);
// get db of target table
tblName.analyze(analyzer);
String dbName = tblName.getDb();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,14 +427,20 @@ private Expr trySubstituteOrdinal(Expr expr, String errorPrefix,
return resultExprs.get((int) pos - 1).clone();
}

public void getWithClauseDbs(Analyzer analyzer, Map<String, Database> dbs) throws AnalysisException {
public void getWithClauseDbs(Analyzer analyzer, Map<String, Database> dbs, Set<String> parentViewNameSet) throws AnalysisException {
if (withClause_ != null) {
withClause_.getDbs(analyzer, dbs);
withClause_.getDbs(analyzer, dbs, parentViewNameSet);
}
}

// get database used by this query.
public abstract void getDbs(Analyzer analyzer, Map<String, Database> dbs) throws AnalysisException;
// Set<String> parentViewNameSet contain parent stmt view name
// to make sure query like "with tmp as (select * from db1.table1) " +
// "select a.siteid, b.citycode, a.siteid from (select siteid, citycode from tmp) a " +
// "left join (select siteid, citycode from tmp) b on a.siteid = b.siteid;";
// tmp in child stmt "(select siteid, citycode from tmp)" do not contain with_Clause
// so need to check is view name by parentViewNameSet. issue link: https://github.com/apache/incubator-doris/issues/4598
public abstract void getDbs(Analyzer analyzer, Map<String, Database> dbs, Set<String> parentViewNameSet) throws AnalysisException;

/**
* UnionStmt and SelectStmt have different implementations.
Expand Down
28 changes: 18 additions & 10 deletions fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,22 +276,21 @@ public ExprSubstitutionMap getBaseTblSmap() {
}

@Override
public void getDbs(Analyzer analyzer, Map<String, Database> dbs) throws AnalysisException {
getWithClauseDbs(analyzer, dbs);
public void getDbs(Analyzer analyzer, Map<String, Database> dbs, Set<String> parentViewNameSet) throws AnalysisException {
getWithClauseDbs(analyzer, dbs, parentViewNameSet);
for (TableRef tblRef : fromClause_) {
if (tblRef instanceof InlineViewRef) {
// Inline view reference
QueryStmt inlineStmt = ((InlineViewRef) tblRef).getViewStmt();
inlineStmt.withClause_ = this.withClause_;
inlineStmt.getDbs(analyzer, dbs);
inlineStmt.getDbs(analyzer, dbs, parentViewNameSet);
} else {
String dbName = tblRef.getName().getDb();
if (Strings.isNullOrEmpty(dbName)) {
dbName = analyzer.getDefaultDb();
} else {
dbName = ClusterNamespace.getFullName(analyzer.getClusterName(), tblRef.getName().getDb());
}
if(withClause_ != null && isViewTableRef(tblRef)){
if (isViewTableRef(tblRef.getName().toString(), parentViewNameSet)) {
continue;
}
if (Strings.isNullOrEmpty(dbName)) {
Expand All @@ -318,13 +317,22 @@ public void getDbs(Analyzer analyzer, Map<String, Database> dbs) throws Analysis
}
}

private boolean isViewTableRef(TableRef tblRef) {
List<View> views = withClause_.getViews();
for(View view : views){
if(view.getName().equals(tblRef.getName().toString())){
return true;
// if tableName in parentViewNameSetor tableName in withClause views
// means this tableref is inlineview, no need check dbname again
private boolean isViewTableRef(String tblName, Set<String> parentViewNameSet) {
if (parentViewNameSet.contains(tblName)) {
return true;
}

if (withClause_ != null) {
List<View> views = withClause_.getViews();
for (View view : views) {
if (view.getName().equals(tblName)) {
return true;
}
}
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Representation of a set ops with its list of operands, and optional order by and limit.
Expand Down Expand Up @@ -171,10 +172,10 @@ public void removeAllOperands() {
public List<Expr> getSetOpsResultExprs() { return setOpsResultExprs_; }

@Override
public void getDbs(Analyzer analyzer, Map<String, Database> dbs) throws AnalysisException {
getWithClauseDbs(analyzer, dbs);
public void getDbs(Analyzer analyzer, Map<String, Database> dbs, Set<String> parentViewNameSet) throws AnalysisException {
getWithClauseDbs(analyzer, dbs, parentViewNameSet);
for (SetOperand op : operands) {
op.getQueryStmt().getDbs(analyzer, dbs);
op.getQueryStmt().getDbs(analyzer, dbs, parentViewNameSet);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.doris.catalog.Database;
import org.apache.doris.catalog.View;
Expand Down Expand Up @@ -105,10 +106,11 @@ public void reset() {
for (View view: views_) view.getQueryStmt().reset();
}

public void getDbs(Analyzer analyzer, Map<String, Database> dbs) throws AnalysisException {
public void getDbs(Analyzer analyzer, Map<String, Database> dbs, Set<String> parentViewNameSet) throws AnalysisException {
for (View view : views_) {
QueryStmt stmt = view.getQueryStmt();
stmt.getDbs(analyzer, dbs);
parentViewNameSet.add(view.getName());
stmt.getDbs(analyzer, dbs, parentViewNameSet);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,14 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.glassfish.jersey.internal.guava.Sets;

import java.io.IOException;
import java.io.StringReader;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;

Expand Down Expand Up @@ -446,17 +448,18 @@ public void analyze(TQueryOptions tQueryOptions) throws UserException {
|| parsedStmt instanceof CreateTableAsSelectStmt) {
Map<String, Database> dbs = Maps.newTreeMap();
QueryStmt queryStmt;
Set<String> parentViewNameSet = Sets.newHashSet();
if (parsedStmt instanceof QueryStmt) {
queryStmt = (QueryStmt) parsedStmt;
queryStmt.getDbs(analyzer, dbs);
queryStmt.getDbs(analyzer, dbs, parentViewNameSet);
} else {
InsertStmt insertStmt;
if (parsedStmt instanceof InsertStmt) {
insertStmt = (InsertStmt) parsedStmt;
} else {
insertStmt = ((CreateTableAsSelectStmt) parsedStmt).getInsertStmt();
}
insertStmt.getDbs(analyzer, dbs);
insertStmt.getDbs(analyzer, dbs, parentViewNameSet);
}

lock(dbs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,4 +545,10 @@ public void testWithWithoutDatabase() throws Exception {
dorisAssert.withoutUseDatabase();
dorisAssert.query(sql).explainQuery();
}

@Test
public void testWithInNestedQueryStmt() throws Exception {
String sql = "select 1 from (with w as (select 1 from db1.table1) select 1 from w) as tt";
dorisAssert.query(sql).explainQuery();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

import com.google.common.collect.Lists;

import org.glassfish.jersey.internal.guava.Sets;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -195,7 +196,7 @@ public void testSelect(@Mocked QueryStmt queryStmt,
minTimes = 0;
result = false;

queryStmt.getDbs((Analyzer) any, (SortedMap) any);
queryStmt.getDbs((Analyzer) any, (SortedMap) any, Sets.newHashSet());
minTimes = 0;

queryStmt.getRedirectStatus();
Expand Down