morningman commented on a change in pull request #4000:
URL: https://github.com/apache/incubator-doris/pull/4000#discussion_r449881612



##########
File path: fe/src/main/java/org/apache/doris/analysis/SelectStmt.java
##########
@@ -816,9 +816,103 @@ protected void reorderTable(Analyzer analyzer) throws 
AnalysisException {
 
         // can not get AST only with equal join, MayBe cross join can help
         fromClause_.clear();
+        List<Pair<Expr, Expr>> misMatchJoinConditions = Lists.newArrayList();
+        List<TupleId> tupleIds = Lists.newArrayList();
+
         for (Pair<TableRef, Long> candidate : candidates) {
-            fromClause_.add(candidate.first);
+            TableRef tableRef = candidate.first;
+            fromClause_.add(tableRef);
+            tupleIds.add(tableRef.desc.getId());
+            applyInnerJoinConditionReorganizeRule(tableRef, tupleIds, 
misMatchJoinConditions);
+        }
+
+        Preconditions.checkState(misMatchJoinConditions.isEmpty());
+    }
+
+    /**
+     *  After adjusting the order of tables, it is necessary to reselect the 
join condition of each table,
+     *  otherwise some columns will not be recognized by the current table 
space
+     * @param tableRef:
+     * @param tupleIds: TableSpaces visible up to now
+     * @param misMatchJoinConditions: join conditions no matching yet, the 
first is fatherExpr, the second is sonExpr need to replace
+     */
+    void applyInnerJoinConditionReorganizeRule(TableRef tableRef, 
List<TupleId> tupleIds,
+                List<Pair<Expr, Expr>> misMatchJoinConditions) {
+        List<Pair<Expr, Expr>> tempMisMatchConjuncts = Lists.newArrayList();
+
+        // Extract all join conditions from joinOnClause to 
misMatchJoinConditions,
+        // which cannot be bound by existing tableSpaces(tupleIds)
+        findUnboundedJoinConditions(tableRef.getOnClause(), tupleIds, 
misMatchJoinConditions);
+
+        for (Pair<Expr, Expr> exprPair : misMatchJoinConditions) {
+            Expr father = exprPair.first;
+            Expr expr = exprPair.second;
+            if (!expr.isBoundByTupleIds(tupleIds)) {
+                tempMisMatchConjuncts.add(new Pair<Expr, Expr>(null, expr));
+                if (father.getChild(0) == expr) {
+                    // We just replace the unresolved predicates with 
TrueBoolLiteral expr,
+                    // so that the tree structure of joinOnClasue can not be 
changed.
+                    // TrueBoolLiteral will be removed in later analyze.
+                    father.setChild(0, new BoolLiteral(true));
+                } else if (father.getChild(1) == expr) {
+                    father.setChild(1, new BoolLiteral(true));
+                }
+            } else {
+                if (tableRef.getOnClause() == null) {
+                    tableRef.setOnClause(expr);
+                } else {
+                    tableRef.setOnClause(new CompoundPredicate(
+                            CompoundPredicate.Operator.AND,
+                            expr,
+                            tableRef.getOnClause()));
+                    // no need to reAnalysis
+                    tableRef.getOnClause().analysisDone();
+                }
+            }
+        }
+        misMatchJoinConditions.clear();
+        misMatchJoinConditions.addAll(tempMisMatchConjuncts);
+    }
+
+    boolean ifExprIsCompandAndPredicate(Expr expr) {

Review comment:
       ```suggestion
       boolean ifExprIsCompoundAndPredicate(Expr expr) {
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to