This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 8ffc85b6ff [fix](planner)project should be done inside inlineview 
(#17831)
8ffc85b6ff is described below

commit 8ffc85b6fff692a956a4f6bc806ba468176897d0
Author: starocean999 <[email protected]>
AuthorDate: Mon Mar 20 21:12:45 2023 +0800

    [fix](planner)project should be done inside inlineview (#17831)
    
    * [fix](planner)project should be done inside inlineview
    
    * add src column for slots in scan node's output tuple
---
 .../org/apache/doris/analysis/SlotDescriptor.java  |  2 +-
 .../org/apache/doris/planner/JoinNodeBase.java     | 12 ++---
 .../org/apache/doris/planner/OlapScanNode.java     | 42 -----------------
 .../java/org/apache/doris/planner/ScanNode.java    | 55 ++++++++++++++++++++++
 .../test_inlineview_with_project.out               |  3 ++
 .../test_inlineview_with_project.groovy            | 26 ++++++++++
 6 files changed, 91 insertions(+), 49 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/analysis/SlotDescriptor.java 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/SlotDescriptor.java
index 6335a7c5fe..cc19f94fa7 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/SlotDescriptor.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/SlotDescriptor.java
@@ -81,7 +81,7 @@ public class SlotDescriptor {
         this.isMultiRef = false;
     }
 
-    SlotDescriptor(SlotId id, TupleDescriptor parent, SlotDescriptor src) {
+    public SlotDescriptor(SlotId id, TupleDescriptor parent, SlotDescriptor 
src) {
         this.id = id;
         this.parent = parent;
         this.byteOffset = src.byteOffset;
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/planner/JoinNodeBase.java 
b/fe/fe-core/src/main/java/org/apache/doris/planner/JoinNodeBase.java
index 10e7444190..14aafb4a84 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/planner/JoinNodeBase.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/planner/JoinNodeBase.java
@@ -597,19 +597,19 @@ public abstract class JoinNodeBase extends PlanNode {
                 Lists.newArrayList(vSrcToOutputSMap.getLhs()));
         List<Expr> newRhs = Lists.newArrayList();
         boolean bSmapChanged = false;
-        for (Expr expr : smap.getRhs()) {
-            if (expr instanceof SlotRef) {
-                newRhs.add(expr);
+        for (Expr rhsExpr : smap.getRhs()) {
+            if (rhsExpr instanceof SlotRef || 
!rhsExpr.isBound(vOutputTupleDesc.getId())) {
+                newRhs.add(rhsExpr);
             } else {
                 // we need do project in the join node
                 // add a new slot for projection result and add the project 
expr to vSrcToOutputSMap
                 SlotDescriptor slotDesc = 
analyzer.addSlotDescriptor(vOutputTupleDesc);
-                slotDesc.initFromExpr(expr);
+                slotDesc.initFromExpr(rhsExpr);
                 slotDesc.setIsMaterialized(true);
                 // the project expr is from smap, which use the slots of hash 
join node's output tuple
                 // we need substitute it to make sure the project expr use 
slots from intermediate tuple
-                expr.substitute(tmpSmap);
-                vSrcToOutputSMap.getLhs().add(expr);
+                rhsExpr = rhsExpr.substitute(tmpSmap);
+                vSrcToOutputSMap.getLhs().add(rhsExpr);
                 SlotRef slotRef = new SlotRef(slotDesc);
                 vSrcToOutputSMap.getRhs().add(slotRef);
                 newRhs.add(slotRef);
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/planner/OlapScanNode.java 
b/fe/fe-core/src/main/java/org/apache/doris/planner/OlapScanNode.java
index 0c1b545fce..e5ee3d2962 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/planner/OlapScanNode.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/planner/OlapScanNode.java
@@ -24,7 +24,6 @@ import org.apache.doris.analysis.CastExpr;
 import org.apache.doris.analysis.CreateMaterializedViewStmt;
 import org.apache.doris.analysis.DescriptorTable;
 import org.apache.doris.analysis.Expr;
-import org.apache.doris.analysis.ExprSubstitutionMap;
 import org.apache.doris.analysis.InPredicate;
 import org.apache.doris.analysis.IntLiteral;
 import org.apache.doris.analysis.PartitionNames;
@@ -1391,45 +1390,4 @@ public class OlapScanNode extends ScanNode {
             }
         }
     }
-
-    public void setOutputSmap(ExprSubstitutionMap smap, Analyzer analyzer) {
-        if (smap.getRhs().stream().anyMatch(expr -> !(expr instanceof 
SlotRef))) {
-            if (outputTupleDesc == null) {
-                outputTupleDesc = 
analyzer.getDescTbl().createTupleDescriptor("OlapScanNode");
-                outputTupleDesc.setTable(this.olapTable);
-            }
-            if (projectList == null) {
-                projectList = Lists.newArrayList();
-            }
-            List<Expr> newRhs = Lists.newArrayList();
-            List<Expr> newLhs = Lists.newArrayList();
-            for (Expr expr : smap.getRhs()) {
-                if (expr instanceof SlotRef && !((SlotRef) 
expr).getDesc().isMaterialized()) {
-                    continue;
-                }
-                if (outputSmap.mappingForRhsExpr(expr) != null) {
-                    newLhs.add(outputSmap.mappingForRhsExpr(expr));
-                    newRhs.add(expr);
-                } else {
-                    SlotDescriptor slotDesc = 
analyzer.addSlotDescriptor(outputTupleDesc);
-                    slotDesc.initFromExpr(expr);
-                    slotDesc.setIsMaterialized(true);
-                    slotDesc.materializeSrcExpr();
-                    projectList.add(expr);
-                    newLhs.add(smap.mappingForRhsExpr(expr));
-                    newRhs.add(new SlotRef(slotDesc));
-                }
-            }
-            outputSmap = new ExprSubstitutionMap(newLhs, newRhs);
-        } else {
-            outputSmap = smap;
-        }
-    }
-
-    public List<TupleId> getOutputTupleIds() {
-        if (outputTupleDesc != null) {
-            return Lists.newArrayList(outputTupleDesc.getId());
-        }
-        return tupleIds;
-    }
 }
diff --git a/fe/fe-core/src/main/java/org/apache/doris/planner/ScanNode.java 
b/fe/fe-core/src/main/java/org/apache/doris/planner/ScanNode.java
index 8726bb0240..888c8fdc3d 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/planner/ScanNode.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/planner/ScanNode.java
@@ -24,6 +24,7 @@ import org.apache.doris.analysis.Analyzer;
 import org.apache.doris.analysis.BinaryPredicate;
 import org.apache.doris.analysis.CompoundPredicate;
 import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.ExprSubstitutionMap;
 import org.apache.doris.analysis.InPredicate;
 import org.apache.doris.analysis.IsNullPredicate;
 import org.apache.doris.analysis.LiteralExpr;
@@ -33,6 +34,7 @@ import org.apache.doris.analysis.SlotDescriptor;
 import org.apache.doris.analysis.SlotId;
 import org.apache.doris.analysis.SlotRef;
 import org.apache.doris.analysis.TupleDescriptor;
+import org.apache.doris.analysis.TupleId;
 import org.apache.doris.catalog.Column;
 import org.apache.doris.catalog.PrimitiveType;
 import org.apache.doris.common.UserException;
@@ -54,6 +56,7 @@ import org.apache.logging.log4j.Logger;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.stream.Collectors;
 
 /**
  * Representation of the common elements of all scan nodes.
@@ -494,4 +497,56 @@ public abstract class ScanNode extends PlanNode {
     public boolean needToCheckColumnPriv() {
         return true;
     }
+
+    public void setOutputSmap(ExprSubstitutionMap smap, Analyzer analyzer) {
+        outputSmap = smap;
+        if (smap.getRhs().stream().anyMatch(expr -> !(expr instanceof 
SlotRef))) {
+            if (outputTupleDesc == null) {
+                outputTupleDesc = 
analyzer.getDescTbl().createTupleDescriptor("OlapScanNode");
+            }
+            if (projectList == null) {
+                projectList = Lists.newArrayList();
+            }
+            // setOutputSmap may be called multiple times
+            // this happens if the olap table is in the most inner sub-query 
block in the cascades sub-queries
+            // create a tmpSmap for the later setOutputSmap call
+            ExprSubstitutionMap tmpSmap = new ExprSubstitutionMap(
+                    
Lists.newArrayList(outputTupleDesc.getSlots().stream().map(slot -> new 
SlotRef(slot)).collect(
+                            Collectors.toList())), 
Lists.newArrayList(projectList));
+            Set<SlotId> allOutputSlotIds = 
outputTupleDesc.getSlots().stream().map(slot -> slot.getId())
+                    .collect(Collectors.toSet());
+            List<Expr> newRhs = Lists.newArrayList();
+            List<Expr> rhs = smap.getRhs();
+            for (int i = 0; i < smap.size(); ++i) {
+                Expr rhsExpr = rhs.get(i);
+                if (!(rhsExpr instanceof SlotRef) || 
!(allOutputSlotIds.contains(((SlotRef) rhsExpr).getSlotId()))) {
+                    rhsExpr = rhsExpr.substitute(tmpSmap);
+                    if (rhsExpr.isBound(desc.getId())) {
+                        SlotDescriptor slotDesc = 
analyzer.addSlotDescriptor(outputTupleDesc);
+                        slotDesc.initFromExpr(rhsExpr);
+                        if (rhsExpr instanceof SlotRef) {
+                            slotDesc.setSrcColumn(((SlotRef) 
rhsExpr).getColumn());
+                        }
+                        slotDesc.setIsMaterialized(true);
+                        slotDesc.materializeSrcExpr();
+                        projectList.add(rhsExpr);
+                        newRhs.add(new SlotRef(slotDesc));
+                        allOutputSlotIds.add(slotDesc.getId());
+                    } else {
+                        newRhs.add(rhs.get(i));
+                    }
+                } else {
+                    newRhs.add(rhsExpr);
+                }
+            }
+            outputSmap.updateRhsExprs(newRhs);
+        }
+    }
+
+    public List<TupleId> getOutputTupleIds() {
+        if (outputTupleDesc != null) {
+            return Lists.newArrayList(outputTupleDesc.getId());
+        }
+        return tupleIds;
+    }
 }
diff --git 
a/regression-test/data/correctness_p0/test_inlineview_with_project.out 
b/regression-test/data/correctness_p0/test_inlineview_with_project.out
index 72d126351a..0a5825a7bb 100644
--- a/regression-test/data/correctness_p0/test_inlineview_with_project.out
+++ b/regression-test/data/correctness_p0/test_inlineview_with_project.out
@@ -2,3 +2,6 @@
 -- !select --
 1
 
+-- !select2 --
+1
+
diff --git 
a/regression-test/suites/correctness_p0/test_inlineview_with_project.groovy 
b/regression-test/suites/correctness_p0/test_inlineview_with_project.groovy
index dbf7cd348a..41de08d082 100644
--- a/regression-test/suites/correctness_p0/test_inlineview_with_project.groovy
+++ b/regression-test/suites/correctness_p0/test_inlineview_with_project.groovy
@@ -70,6 +70,32 @@ suite("test_inlineview_with_project") {
         ) tx;
     """
 
+    qt_select2 """
+        SELECT count(*) AS count
+        FROM (with t0 AS 
+            (SELECT report.date AS date,
+                max(date)- min(date) imp_price
+            FROM 
+                (SELECT DATE_FORMAT(date,
+                '%Y%m%d') AS date
+                FROM cir_1756_t1 ) report
+                GROUP BY  date ), t3 AS 
+                    (SELECT date_format(date,
+                '%Y%m%d') AS `date`
+                    FROM cir_1756_t2 )
+                    SELECT date 1account_id_num
+                    FROM 
+                        (SELECT date,
+                dense_rank() over(partition by date
+                        ORDER BY  date desc) 1account_id_num
+                        FROM 
+                            (SELECT t0.date
+                            FROM t0
+                            LEFT JOIN t3
+                                ON t0.date=t3.date )t0 )tb
+                            WHERE 1account_id_num <= 3 ) t;
+    """
+
     sql """
         drop table if exists cir_1756_t1;
     """


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

Reply via email to