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

panxiaolei pushed a commit to branch branch-3.0
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-3.0 by this push:
     new 37e5ce372af branch-3.0: [Bug](explain) fix wrong display of 
SortAlgorithm on explain #46559 (#46979)
37e5ce372af is described below

commit 37e5ce372af298042a08cc98f759ee2f6506994a
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Tue Jan 14 22:11:49 2025 +0800

    branch-3.0: [Bug](explain) fix wrong display of SortAlgorithm on explain 
#46559 (#46979)
    
    Cherry-picked from #46559
    
    Co-authored-by: Pxl <[email protected]>
---
 be/src/pipeline/exec/sort_sink_operator.cpp        |   5 ++
 .../org/apache/doris/planner/AnalyticPlanner.java  |   1 -
 .../java/org/apache/doris/planner/PlanNode.java    |   7 --
 .../apache/doris/planner/SingleNodePlanner.java    |   1 -
 .../java/org/apache/doris/planner/SortNode.java    | 100 ++++++++++-----------
 5 files changed, 51 insertions(+), 63 deletions(-)

diff --git a/be/src/pipeline/exec/sort_sink_operator.cpp 
b/be/src/pipeline/exec/sort_sink_operator.cpp
index f7295431be7..c6ab99b5bcb 100644
--- a/be/src/pipeline/exec/sort_sink_operator.cpp
+++ b/be/src/pipeline/exec/sort_sink_operator.cpp
@@ -71,6 +71,11 @@ Status SortSinkLocalState::open(RuntimeState* state) {
     _shared_state->sorter->init_profile(_profile);
 
     _profile->add_info_string("TOP-N", p._limit == -1 ? "false" : "true");
+    _profile->add_info_string(
+            "SortAlgorithm",
+            p._algorithm == TSortAlgorithm::HEAP_SORT
+                    ? "HEAP_SORT"
+                    : (p._algorithm == TSortAlgorithm::TOPN_SORT ? "TOPN_SORT" 
: "FULL_SORT"));
     return Status::OK();
 }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/planner/AnalyticPlanner.java 
b/fe/fe-core/src/main/java/org/apache/doris/planner/AnalyticPlanner.java
index 7687b5812a4..3b29e8afb43 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/planner/AnalyticPlanner.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/planner/AnalyticPlanner.java
@@ -413,7 +413,6 @@ public class AnalyticPlanner {
 
             SortInfo sortInfo = createSortInfo(newRoot, sortExprs, isAsc, 
nullsFirst);
             SortNode sortNode = new SortNode(ctx.getNextNodeId(), newRoot, 
sortInfo, false);
-            sortNode.setDefaultLimit(false);
 
             // if this sort group does not have partitioning exprs, we want 
the sort
             // to be executed like a regular distributed sort
diff --git a/fe/fe-core/src/main/java/org/apache/doris/planner/PlanNode.java 
b/fe/fe-core/src/main/java/org/apache/doris/planner/PlanNode.java
index c83cd9cde99..c6b06e7576c 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/planner/PlanNode.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/planner/PlanNode.java
@@ -322,13 +322,6 @@ public abstract class PlanNode extends TreeNode<PlanNode> 
implements PlanStats {
         this.offset = offset;
     }
 
-    /**
-     * Used by new optimizer only.
-     */
-    public void setOffSetDirectly(long offset) {
-        this.offset = offset;
-    }
-
     public boolean hasLimit() {
         return limit > -1;
     }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/planner/SingleNodePlanner.java 
b/fe/fe-core/src/main/java/org/apache/doris/planner/SingleNodePlanner.java
index f29ff4dba32..1ec738eba47 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/planner/SingleNodePlanner.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/planner/SingleNodePlanner.java
@@ -313,7 +313,6 @@ public class SingleNodePlanner {
             boolean useTopN = true;
             root = new SortNode(ctx.getNextNodeId(), root, stmt.getSortInfo(),
                     useTopN);
-            ((SortNode) root).setDefaultLimit(limit == -1);
             root.setOffset(stmt.getOffset());
             if (useTopN) {
                 if (sqlSelectLimit >= 0) {
diff --git a/fe/fe-core/src/main/java/org/apache/doris/planner/SortNode.java 
b/fe/fe-core/src/main/java/org/apache/doris/planner/SortNode.java
index e3eb08c3e75..2bfa76eb513 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/planner/SortNode.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/planner/SortNode.java
@@ -71,11 +71,11 @@ public class SortNode extends PlanNode {
     // exchange node, and the sort node is used for the ORDER BY .
     private boolean mergeByexchange = false;
 
-    private boolean isDefaultLimit;
     // if true, the output of this node feeds an AnalyticNode
     private boolean isAnalyticSort;
     private boolean isColocate = false;
     private DataPartition inputPartition;
+    TSortAlgorithm algorithm;
 
     private boolean isUnusedExprRemoved = false;
 
@@ -84,42 +84,49 @@ public class SortNode extends PlanNode {
     /**
      * Constructor.
      */
-    public SortNode(PlanNodeId id, PlanNode input, SortInfo info, boolean 
useTopN,
-            boolean isDefaultLimit, long offset) {
+    public SortNode(PlanNodeId id, PlanNode input, SortInfo info, boolean 
useTopN, long offset) {
         super(id, useTopN ? "TOP-N" : "SORT", StatisticalType.SORT_NODE);
         this.info = info;
         this.useTopN = useTopN;
-        this.isDefaultLimit = isDefaultLimit;
         
this.tupleIds.addAll(Lists.newArrayList(info.getSortTupleDescriptor().getId()));
         
this.tblRefIds.addAll(Lists.newArrayList(info.getSortTupleDescriptor().getId()));
         this.nullableTupleIds.addAll(input.getNullableTupleIds());
         this.children.add(input);
         this.offset = offset;
         Preconditions.checkArgument(info.getOrderingExprs().size() == 
info.getIsAscOrder().size());
+        updateSortAlgorithm();
     }
 
     public SortNode(PlanNodeId id, PlanNode input, SortInfo info, boolean 
useTopN) {
-        this(id, input, info, useTopN, true, 0);
+        this(id, input, info, useTopN, 0);
     }
 
-    /**
-     * Clone 'inputSortNode' for distributed Top-N.
-     */
-    public SortNode(PlanNodeId id, SortNode inputSortNode, PlanNode child) {
-        super(id, inputSortNode, inputSortNode.useTopN ? "TOP-N" : "SORT", 
StatisticalType.SORT_NODE);
-        this.info = inputSortNode.info;
-        this.useTopN = inputSortNode.useTopN;
-        this.isDefaultLimit = inputSortNode.isDefaultLimit;
-        this.children.add(child);
-        this.offset = inputSortNode.offset;
-    }
-
-    /**
-     * set isDefaultLimit when translate PhysicalLimit
-     * @param defaultLimit
-     */
-    public void setDefaultLimit(boolean defaultLimit) {
-        isDefaultLimit = defaultLimit;
+    private void updateSortAlgorithm() {
+        ConnectContext connectContext = ConnectContext.get();
+        if (connectContext != null && 
!connectContext.getSessionVariable().forceSortAlgorithm.isEmpty()) {
+            String algo = 
connectContext.getSessionVariable().forceSortAlgorithm;
+            if (algo.equals("heap")) {
+                algorithm = TSortAlgorithm.HEAP_SORT;
+            } else if (algo.equals("topn")) {
+                algorithm = TSortAlgorithm.TOPN_SORT;
+            } else {
+                algorithm = TSortAlgorithm.FULL_SORT;
+            }
+        } else {
+            if (limit <= 0) {
+                algorithm = TSortAlgorithm.FULL_SORT;
+            } else if (hasRuntimePredicate || useTwoPhaseReadOpt) {
+                algorithm = TSortAlgorithm.HEAP_SORT;
+            } else {
+                if (limit + offset < 50000) {
+                    algorithm = TSortAlgorithm.HEAP_SORT;
+                } else if (limit + offset < 20000000) {
+                    algorithm = TSortAlgorithm.FULL_SORT;
+                } else {
+                    algorithm = TSortAlgorithm.TOPN_SORT;
+                }
+            }
+        }
     }
 
     public void setIsAnalyticSort(boolean v) {
@@ -160,6 +167,7 @@ public class SortNode extends PlanNode {
 
     public void setUseTwoPhaseReadOpt(boolean useTwoPhaseReadOpt) {
         this.useTwoPhaseReadOpt = useTwoPhaseReadOpt;
+        updateSortAlgorithm();
     }
 
     public List<Expr> getResolvedTupleExprs() {
@@ -201,12 +209,9 @@ public class SortNode extends PlanNode {
         }
 
         output.append(detailPrefix + "algorithm: ");
-        boolean isFixedLength = info.getOrderingExprs().stream().allMatch(e -> 
!e.getType().isStringType()
-                && !e.getType().isCollectionType());
-        if (limit > 0 && limit + offset < 1024 && (useTwoPhaseReadOpt || 
hasRuntimePredicate
-                || isFixedLength)) {
+        if (algorithm == TSortAlgorithm.HEAP_SORT) {
             output.append("heap sort\n");
-        } else if (limit > 0 && !isFixedLength && limit + offset < 256) {
+        } else if (algorithm == TSortAlgorithm.TOPN_SORT) {
             output.append("topn sort\n");
         } else {
             output.append("full sort\n");
@@ -340,32 +345,7 @@ public class SortNode extends PlanNode {
         msg.sort_node.setIsAnalyticSort(isAnalyticSort);
         msg.sort_node.setIsColocate(isColocate);
 
-        ConnectContext connectContext = ConnectContext.get();
-        TSortAlgorithm algorithm;
-        if (connectContext != null && 
!connectContext.getSessionVariable().forceSortAlgorithm.isEmpty()) {
-            String algo = 
connectContext.getSessionVariable().forceSortAlgorithm;
-            if (algo.equals("heap")) {
-                algorithm = TSortAlgorithm.HEAP_SORT;
-            } else if (algo.equals("topn")) {
-                algorithm = TSortAlgorithm.TOPN_SORT;
-            } else {
-                algorithm = TSortAlgorithm.FULL_SORT;
-            }
-        } else {
-            if (limit <= 0) {
-                algorithm = TSortAlgorithm.FULL_SORT;
-            } else if (hasRuntimePredicate || useTwoPhaseReadOpt) {
-                algorithm = TSortAlgorithm.HEAP_SORT;
-            } else {
-                if (limit + offset < 50000) {
-                    algorithm = TSortAlgorithm.HEAP_SORT;
-                } else if (limit + offset < 20000000) {
-                    algorithm = TSortAlgorithm.FULL_SORT;
-                } else {
-                    algorithm = TSortAlgorithm.TOPN_SORT;
-                }
-            }
-        }
+
         msg.sort_node.setAlgorithm(algorithm);
     }
 
@@ -401,5 +381,17 @@ public class SortNode extends PlanNode {
 
     public void setHasRuntimePredicate() {
         this.hasRuntimePredicate = true;
+        updateSortAlgorithm();
+    }
+
+    @Override
+    public void setLimit(long limit) {
+        super.setLimit(limit);
+        updateSortAlgorithm();
+    }
+
+    public void setOffset(long offset) {
+        super.setOffset(offset);
+        updateSortAlgorithm();
     }
 }


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

Reply via email to