[fix](optimizer) Fix the default join reorder algorithm (#10174)

Default join reorder algorithm not working for the most cases.
This commit is contained in:
Kikyou1997
2022-06-17 10:59:33 +08:00
committed by GitHub
parent fd0bd395ac
commit 67e95276fb
3 changed files with 33 additions and 16 deletions

View File

@ -178,7 +178,6 @@ under the License.
<module name="UnusedLocalVariable">
<property name="severity" value="error"/>
</module>
<module name="VariableDeclarationUsageDistance"/>
<!-- Headers -->
<!-- Imports -->

View File

@ -735,7 +735,7 @@ public class SelectStmt extends QueryStmt {
protected void reorderTable(Analyzer analyzer) throws AnalysisException {
List<Pair<TableRef, Long>> candidates = Lists.newArrayList();
List<TableRef> originOrderBackUp = Lists.newArrayList(fromClause.getTableRefs());
// New pair of table ref and row count
for (TableRef tblRef : fromClause) {
if (tblRef.getJoinOp() != JoinOperator.INNER_JOIN || tblRef.hasJoinHints()) {
@ -773,8 +773,8 @@ public class SelectStmt extends QueryStmt {
// can not get AST only with equal join, MayBe cross join can help
fromClause.clear();
for (Pair<TableRef, Long> candidate : candidates) {
fromClause.add(candidate.first);
for (TableRef tableRef : originOrderBackUp) {
fromClause.add(tableRef);
}
}
@ -819,19 +819,21 @@ public class SelectStmt extends QueryStmt {
// is being added.
Preconditions.checkState(tid == candidateTableRef.getId());
List<Expr> candidateEqJoinPredicates = analyzer.getEqJoinConjunctsExcludeAuxPredicates(tid);
List<TupleId> candidateTupleList = Lists.newArrayList();
Expr.getIds(candidateEqJoinPredicates, candidateTupleList, null);
int count = candidateTupleList.size();
for (TupleId tupleId : candidateTupleList) {
if (validTupleId.contains(tupleId) || tid == tupleId) {
count--;
for (Expr candidateEqJoinPredicate : candidateEqJoinPredicates) {
List<TupleId> candidateTupleList = Lists.newArrayList();
Expr.getIds(Lists.newArrayList(candidateEqJoinPredicate), candidateTupleList, null);
int count = candidateTupleList.size();
for (TupleId tupleId : candidateTupleList) {
if (validTupleId.contains(tupleId) || tid.equals(tupleId)) {
count--;
}
}
if (count == 0) {
fromClause.add(candidateTableRef);
validTupleId.add(tid);
tableRefMap.remove(tid);
break;
}
}
if (count == 0) {
fromClause.add(candidateTableRef);
validTupleId.add(tid);
tableRefMap.remove(tid);
}
}
}

View File

@ -2118,4 +2118,20 @@ public class QueryPlanTest extends TestWithFeService {
Assert.assertFalse(explainString.contains("non-equal FULL OUTER JOIN is not supported"));
}
@Test
public void testDefaultJoinReorder() throws Exception {
connectContext.setDatabase("default_cluster:test");
createTable("CREATE TABLE t1 (col1 varchar, col2 varchar, col3 int)\n" + "DISTRIBUTED BY HASH(col3)\n"
+ "BUCKETS 3\n" + "PROPERTIES(\n" + " \"replication_num\"=\"1\"\n" + ");");
createTable("CREATE TABLE t2 (col1 varchar, col2 varchar, col3 int)\n" + "DISTRIBUTED BY HASH(col3)\n"
+ "BUCKETS 3\n" + "PROPERTIES(\n" + " \"replication_num\"=\"1\"\n" + ");");
createTable("CREATE TABLE t3 (col1 varchar, col2 varchar, col3 int)\n" + "DISTRIBUTED BY HASH(col3)\n"
+ "BUCKETS 3\n" + "PROPERTIES(\n" + " \"replication_num\"=\"1\"\n" + ");");
String sql = "explain select x.col2 from t1,t2,t3 x,t3 y "
+ "where x.col1=t2.col1 and y.col1=t2.col2 and t1.col1=y.col1";
String explainString = getSQLPlanOrErrorMsg(sql);
Assert.assertFalse(explainString.contains("CROSS JOIN"));
}
}