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

yjhjstz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git


The following commit(s) were added to refs/heads/main by this push:
     new 3bf5b2a9903 ORCA: Fix window function cost model producing zero local 
cost when no ORDER BY (#1573)
3bf5b2a9903 is described below

commit 3bf5b2a9903d849595078af838fc418eec3ca17b
Author: Jianghua.yjh <[email protected]>
AuthorDate: Tue Feb 24 13:08:00 2026 -0800

    ORCA: Fix window function cost model producing zero local cost when no 
ORDER BY (#1573)
    
    When a window function has no ORDER BY clause (empty Order Spec),
    ulSortCols is 0, causing the local cost of SequenceProject operators
    to be zero. This makes CostHashSequenceProject and
    CPhysicalSequenceProject have identical costs.
    
    Fix by using max(ulSortCols, 1) so the window function evaluation
    cost is never zero.
---
 src/backend/gporca/libgpdbcost/src/CCostModelGPDB.cpp | 16 +++++++++++-----
 src/test/regress/expected/cte_prune_optimizer.out     | 10 +++++-----
 2 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/src/backend/gporca/libgpdbcost/src/CCostModelGPDB.cpp 
b/src/backend/gporca/libgpdbcost/src/CCostModelGPDB.cpp
index 73330059c72..0bf5e167469 100644
--- a/src/backend/gporca/libgpdbcost/src/CCostModelGPDB.cpp
+++ b/src/backend/gporca/libgpdbcost/src/CCostModelGPDB.cpp
@@ -1622,21 +1622,24 @@ CCostModelGPDB::CostSequenceProject(CMemoryPool *mp, 
CExpressionHandle &exprhdl,
        }
 
        // we process (sorted window of) input tuples to compute window 
function values
+       // Use at least 1 to account for the base cost of evaluating window 
functions
+       // even when there are no sort columns (e.g., no ORDER BY in the window 
spec)
+       ULONG ulCostFactor = std::max(ulSortCols, (ULONG) 1);
        CCost costLocal =
-               CCost(pci->NumRebinds() * (ulSortCols * num_rows_outer * 
dWidthOuter *
+               CCost(pci->NumRebinds() * (ulCostFactor * num_rows_outer * 
dWidthOuter *
                                                                   
dTupDefaultProcCostUnit));
        CCost costChild =
                CostChildren(mp, exprhdl, pci, pcmgpdb->GetCostModelParams());
-       
+
        return costLocal + costChild;
 }
 
 //---------------------------------------------------------------------------
 //     @function:
-//             CCostModelGPDB::CostSequenceProject
+//             CCostModelGPDB::CostHashSequenceProject
 //
 //     @doc:
-//             Cost of sequence project
+//             Cost of hash sequence project
 //
 //---------------------------------------------------------------------------
 CCost
@@ -1675,8 +1678,11 @@ CCostModelGPDB::CostHashSequenceProject(CMemoryPool *mp, 
CExpressionHandle &expr
        }
 
        // we process (sorted window of) input tuples to compute window 
function values
+       // Use at least 1 to account for the base cost of evaluating window 
functions
+       // even when there are no sort columns (e.g., no ORDER BY in the window 
spec)
+       ULONG ulCostFactor = std::max(ulSortCols, (ULONG) 1);
        CCost costLocal =
-               CCost(pci->NumRebinds() * (ulSortCols * num_rows_outer * 
dWidthOuter *
+               CCost(pci->NumRebinds() * (ulCostFactor * num_rows_outer * 
dWidthOuter *
                                                                   
dTupDefaultProcCostUnit));
        CCost costChild =
                CostChildren(mp, exprhdl, pci, pcmgpdb->GetCostModelParams());
diff --git a/src/test/regress/expected/cte_prune_optimizer.out 
b/src/test/regress/expected/cte_prune_optimizer.out
index e7f2339a0eb..1b0833673e8 100644
--- a/src/test/regress/expected/cte_prune_optimizer.out
+++ b/src/test/regress/expected/cte_prune_optimizer.out
@@ -1306,16 +1306,16 @@ LIMIT 10;
                                              Output: t4.d
                ->  Redistribute Motion 1:3  (slice4)  
(cost=0.00..1356696139.21 rows=4 width=24)
                      Output: t4_1.c, t4_1.d, (avg(share0_ref3.b) OVER (?)), 
(sum(share0_ref2.d) OVER (?))
-                     ->  Limit  (cost=0.00..1356696139.20 rows=10 width=24)
+                     ->  Limit  (cost=0.00..1356696139.21 rows=10 width=24)
                            Output: t4_1.c, t4_1.d, (avg(share0_ref3.b) OVER 
(?)), (sum(share0_ref2.d) OVER (?))
-                           ->  Gather Motion 3:1  (slice5; segments: 3)  
(cost=0.00..1356696139.20 rows=10 width=24)
+                           ->  Gather Motion 3:1  (slice5; segments: 3)  
(cost=0.00..1356696139.21 rows=10 width=24)
                                  Output: t4_1.c, t4_1.d, (avg(share0_ref3.b) 
OVER (?)), (sum(share0_ref2.d) OVER (?))
                                  Merge Key: t4_1.c, t4_1.d, 
(avg(share0_ref3.b) OVER (?)), (sum(share0_ref2.d) OVER (?))
-                                 ->  Limit  (cost=0.00..1356696139.20 rows=4 
width=24)
+                                 ->  Limit  (cost=0.00..1356696139.21 rows=4 
width=24)
                                        Output: t4_1.c, t4_1.d, 
(avg(share0_ref3.b) OVER (?)), (sum(share0_ref2.d) OVER (?))
-                                       ->  Result  (cost=0.00..1356696139.20 
rows=134 width=24)
+                                       ->  Result  (cost=0.00..1356696139.21 
rows=134 width=24)
                                              Output: t4_1.c, t4_1.d, 
(avg(share0_ref3.b) OVER (?)), (sum(share0_ref2.d) OVER (?))
-                                             ->  Sort  
(cost=0.00..1356696139.20 rows=134 width=24)
+                                             ->  Sort  
(cost=0.00..1356696139.21 rows=134 width=24)
                                                    Output: t4_1.c, t4_1.d, 
(avg(share0_ref3.b) OVER (?)), (sum(share0_ref2.d) OVER (?)), share0_ref2.b
                                                    Sort Key: t4_1.c, t4_1.d, 
(avg(share0_ref3.b) OVER (?)), (sum(share0_ref2.d) OVER (?))
                                                    ->  WindowAgg  
(cost=0.00..1356696139.08 rows=134 width=24)


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

Reply via email to