Skip to content

Commit b91a3b5

Browse files
authored
[fix](planner) should not bind slot on brother's tuple in subquery (apache#17813)
consider the query like this: ```sql SELECT k3, k4 FROM test WHERE EXISTS( SELECT d.* FROM (SELECT k1 AS _1234, SUM(k2) FROM `test` d GROUP BY _1234) d LEFT JOIN (SELECT k1 AS _1234, SUM(k2) FROM `test` GROUP BY _1234) temp ON d._1234 = temp._1234) ORDER BY k3, k4 ``` when we analyze group by exprs in `temp` inline view. we bind the `_1234` on `d._1234` by mistake. that because, when we do analyze in a **SUB-QUERY**, we will resolve SlotRef by itself **AND** parent's tuple. in the meanwhile, we register child's tuple to parent's analyzer. So, in a **SUB-QUERY**, the brother's tuple will affect the resolve result of current inlineview's slot. This PR: 1. add a flag on the function `resolveColumnRef` in `Analyzer` ```java private TupleDescriptor resolveColumnRef(String colName, boolean requestFromChild); private TupleDescriptor resolveColumnRef(TableName tblName, String colName, boolean requestByChild); ``` 2. add a flag to specify whether the tuple is from child. ```java // alias name -> <from child, tupleDesc> private final Multimap<String, Pair<Boolean, TupleDescriptor>> tupleByAlias; ``` when `requestByChild == true`, we **SKIP** the tuple from other child to avoid resolve error.
1 parent a4b151e commit b91a3b5

5 files changed

Lines changed: 67 additions & 14 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java‎

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ public class Analyzer {
113113
// UniqueAlias used to check whether the table ref or the alias is unique
114114
// table/view used db.table, inline use alias
115115
private final Set<String> uniqueTableAliasSet = Sets.newHashSet();
116-
private final Multimap<String, TupleDescriptor> tupleByAlias = ArrayListMultimap.create();
116+
117+
// alias name -> <from child, tupleDesc>
118+
private final Multimap<String, Pair<Boolean, TupleDescriptor>> tupleByAlias = ArrayListMultimap.create();
117119

118120
// NOTE: Alias of column is case ignorance
119121
// map from lowercase table alias to descriptor.
@@ -624,10 +626,14 @@ public void substitute(ExprSubstitutionMap sMap) {
624626
if (globalState.conjuncts.get(id).substitute(sMap) instanceof BoolLiteral) {
625627
continue;
626628
}
627-
globalState.conjuncts.put(id, (Predicate) globalState.conjuncts.get(id).substitute(sMap));
629+
globalState.conjuncts.put(id, globalState.conjuncts.get(id).substitute(sMap));
628630
}
629631
}
630632

633+
public TupleDescriptor registerTableRef(TableRef ref) throws AnalysisException {
634+
return registerTableRef(ref, false);
635+
}
636+
631637
/**
632638
* Creates an returns an empty TupleDescriptor for the given table ref and registers
633639
* it against all its legal aliases. For tables refs with an explicit alias, only the
@@ -638,7 +644,7 @@ public void substitute(ExprSubstitutionMap sMap) {
638644
* Throws if an existing explicit alias or implicit fully-qualified alias
639645
* has already been registered for another table ref.
640646
*/
641-
public TupleDescriptor registerTableRef(TableRef ref) throws AnalysisException {
647+
public TupleDescriptor registerTableRef(TableRef ref, boolean fromChild) throws AnalysisException {
642648
String uniqueAlias = ref.getUniqueAlias();
643649
if (uniqueTableAliasSet.contains(uniqueAlias)) {
644650
ErrorReport.reportAnalysisException(ErrorCode.ERR_NONUNIQ_TABLE, uniqueAlias);
@@ -671,7 +677,7 @@ public TupleDescriptor registerTableRef(TableRef ref) throws AnalysisException {
671677
for (String alias : aliases) {
672678
// TODO(zc)
673679
// aliasMap_.put(alias, result);
674-
tupleByAlias.put(alias, result);
680+
tupleByAlias.put(alias, Pair.of(fromChild, result));
675681
}
676682

677683
tableRefMap.put(result.getId(), ref);
@@ -715,7 +721,7 @@ public TupleDescriptor registerOlapTable(Table table, TableName tableName, List<
715721
globalState.descTbl.computeStatAndMemLayout();
716722
tableRefMap.put(result.getId(), ref);
717723
for (String alias : tableRef.getAliases()) {
718-
tupleByAlias.put(alias, result);
724+
tupleByAlias.put(alias, Pair.of(false, result));
719725
}
720726
return result;
721727
}
@@ -829,7 +835,7 @@ public ExprRewriter getMVExprRewriter() {
829835
* @return null if not registered.
830836
*/
831837
public Collection<TupleDescriptor> getDescriptor(TableName name) {
832-
return tupleByAlias.get(name.toString());
838+
return tupleByAlias.get(name.toString()).stream().map(p -> p.second).collect(Collectors.toList());
833839
}
834840

835841
public TupleDescriptor getTupleDesc(TupleId id) {
@@ -896,9 +902,9 @@ public SlotDescriptor registerColumnRef(TableName tblName, String colName) throw
896902
if (d == null && hasAncestors() && isSubquery) {
897903
// analyzer father for subquery
898904
if (newTblName == null) {
899-
d = getParentAnalyzer().resolveColumnRef(colName);
905+
d = getParentAnalyzer().resolveColumnRef(colName, true);
900906
} else {
901-
d = getParentAnalyzer().resolveColumnRef(newTblName, colName);
907+
d = getParentAnalyzer().resolveColumnRef(newTblName, colName, true);
902908
}
903909
}
904910
if (d == null) {
@@ -955,15 +961,24 @@ public SlotDescriptor registerVirtualColumnRef(String colName, Type type, TupleD
955961
return result;
956962
}
957963

964+
TupleDescriptor resolveColumnRef(TableName tblName, String colName) throws AnalysisException {
965+
return resolveColumnRef(tblName, colName, false);
966+
}
967+
958968
/**
959969
* Resolves column name in context of any of the registered table aliases.
960970
* Returns null if not found or multiple bindings to different tables exist,
961971
* otherwise returns the table alias.
962972
*/
963-
private TupleDescriptor resolveColumnRef(TableName tblName, String colName) throws AnalysisException {
973+
private TupleDescriptor resolveColumnRef(TableName tblName, String colName,
974+
boolean requestByChild) throws AnalysisException {
964975
TupleDescriptor result = null;
965976
// find table all name
966-
for (TupleDescriptor desc : tupleByAlias.get(tblName.toString())) {
977+
for (Pair<Boolean, TupleDescriptor> p : tupleByAlias.get(tblName.toString())) {
978+
if (p.first && requestByChild) {
979+
continue;
980+
}
981+
TupleDescriptor desc = p.second;
967982
//result = desc;
968983
if (!colName.equalsIgnoreCase(Column.DELETE_SIGN) && !isVisible(desc.getId())) {
969984
ErrorReport.reportAnalysisException(ErrorCode.ERR_ILLEGAL_COLUMN_REFERENCE_ERROR,
@@ -982,8 +997,16 @@ private TupleDescriptor resolveColumnRef(TableName tblName, String colName) thro
982997
}
983998

984999
private TupleDescriptor resolveColumnRef(String colName) throws AnalysisException {
1000+
return resolveColumnRef(colName, false);
1001+
}
1002+
1003+
private TupleDescriptor resolveColumnRef(String colName, boolean requestFromChild) throws AnalysisException {
9851004
TupleDescriptor result = null;
986-
for (TupleDescriptor desc : tupleByAlias.values()) {
1005+
for (Pair<Boolean, TupleDescriptor> p : tupleByAlias.values()) {
1006+
if (p.first && requestFromChild) {
1007+
continue;
1008+
}
1009+
TupleDescriptor desc = p.second;
9871010
if (!isVisible(desc.getId())) {
9881011
continue;
9891012
}

‎fe/fe-core/src/main/java/org/apache/doris/analysis/InlineViewRef.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public void analyze(Analyzer analyzer) throws AnalysisException, UserException {
200200
}
201201
//TODO(chenhao16): fix TableName in Db.Table style
202202
// name.analyze(analyzer);
203-
desc = analyzer.registerTableRef(this);
203+
desc = analyzer.registerTableRef(this, true);
204204
isAnalyzed = true; // true now that we have assigned desc
205205

206206
// For constant selects we materialize its exprs into a tuple.

‎fe/fe-core/src/test/java/org/apache/doris/analysis/GroupByClauseTest.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.doris.analysis;
1919

2020
import org.apache.doris.common.AnalysisException;
21+
import org.apache.doris.common.Pair;
2122
import org.apache.doris.datasource.InternalCatalog;
2223

2324
import com.google.common.collect.ArrayListMultimap;
@@ -46,10 +47,10 @@ public void setUp() throws AnalysisException {
4647
try {
4748
Field f = analyzer.getClass().getDeclaredField("tupleByAlias");
4849
f.setAccessible(true);
49-
Multimap<String, TupleDescriptor> tupleByAlias = ArrayListMultimap.create();
50+
Multimap<String, Pair<Boolean, TupleDescriptor>> tupleByAlias = ArrayListMultimap.create();
5051
TupleDescriptor td = new TupleDescriptor(new TupleId(0));
5152
td.setTable(analyzerBase.getTableOrAnalysisException(new TableName(internalCtl, "testdb", "t")));
52-
tupleByAlias.put("testdb.t", td);
53+
tupleByAlias.put("testdb.t", Pair.of(false, td));
5354
f.set(analyzer, tupleByAlias);
5455
} catch (NoSuchFieldException e) {
5556
e.printStackTrace();

‎regression-test/data/query_p0/subquery/test_subquery.out‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,8 @@ true 9 1991 -2147483647 11011902 -654.654 true 1991-08-11 1989-03-21T13:11 wangj
3636

3737
-- !sql8 --
3838

39+
-- !sql_same_alias_in_subquery --
40+
1001 11011902
41+
1001 11011903
42+
1002 11011905
43+

‎regression-test/suites/query_p0/subquery/test_subquery.groovy‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,28 @@ suite("test_subquery") {
5454
select * from (select k1, -1 as c from test_query_db.test union all select k1, -2 as c from test_query_db.baseall) t
5555
where t.c > 0;
5656
"""
57+
58+
qt_sql_same_alias_in_subquery """
59+
SELECT
60+
k3, k4
61+
FROM
62+
test_query_db.test
63+
WHERE
64+
EXISTS( SELECT
65+
d.*
66+
FROM
67+
(SELECT
68+
k1 AS _1234, SUM(k2)
69+
FROM
70+
test_query_db.`test` d
71+
GROUP BY _1234) d
72+
LEFT JOIN
73+
(SELECT
74+
k1 AS _1234,
75+
SUM(k2)
76+
FROM
77+
test_query_db.`test`
78+
GROUP BY _1234) temp ON d._1234 = temp._1234)
79+
ORDER BY k3, k4
80+
"""
5781
}

0 commit comments

Comments
 (0)