Skip to content

Commit 46b2b82

Browse files
committed
ORCA: Fix window function cost model producing zero local cost when no ORDER BY
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.
1 parent 98240f9 commit 46b2b82

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

src/backend/gporca/libgpdbcost/src/CCostModelGPDB.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,21 +1622,24 @@ CCostModelGPDB::CostSequenceProject(CMemoryPool *mp, CExpressionHandle &exprhdl,
16221622
}
16231623

16241624
// we process (sorted window of) input tuples to compute window function values
1625+
// Use at least 1 to account for the base cost of evaluating window functions
1626+
// even when there are no sort columns (e.g., no ORDER BY in the window spec)
1627+
ULONG ulCostFactor = std::max(ulSortCols, (ULONG) 1);
16251628
CCost costLocal =
1626-
CCost(pci->NumRebinds() * (ulSortCols * num_rows_outer * dWidthOuter *
1629+
CCost(pci->NumRebinds() * (ulCostFactor * num_rows_outer * dWidthOuter *
16271630
dTupDefaultProcCostUnit));
16281631
CCost costChild =
16291632
CostChildren(mp, exprhdl, pci, pcmgpdb->GetCostModelParams());
1630-
1633+
16311634
return costLocal + costChild;
16321635
}
16331636

16341637
//---------------------------------------------------------------------------
16351638
// @function:
1636-
// CCostModelGPDB::CostSequenceProject
1639+
// CCostModelGPDB::CostHashSequenceProject
16371640
//
16381641
// @doc:
1639-
// Cost of sequence project
1642+
// Cost of hash sequence project
16401643
//
16411644
//---------------------------------------------------------------------------
16421645
CCost
@@ -1675,8 +1678,11 @@ CCostModelGPDB::CostHashSequenceProject(CMemoryPool *mp, CExpressionHandle &expr
16751678
}
16761679

16771680
// we process (sorted window of) input tuples to compute window function values
1681+
// Use at least 1 to account for the base cost of evaluating window functions
1682+
// even when there are no sort columns (e.g., no ORDER BY in the window spec)
1683+
ULONG ulCostFactor = std::max(ulSortCols, (ULONG) 1);
16781684
CCost costLocal =
1679-
CCost(pci->NumRebinds() * (ulSortCols * num_rows_outer * dWidthOuter *
1685+
CCost(pci->NumRebinds() * (ulCostFactor * num_rows_outer * dWidthOuter *
16801686
dTupDefaultProcCostUnit));
16811687
CCost costChild =
16821688
CostChildren(mp, exprhdl, pci, pcmgpdb->GetCostModelParams());

0 commit comments

Comments
 (0)