Skip to content

Commit bffdd7d

Browse files
yjhjstzhanwei
authored andcommitted
ORCA: Optimize CDatumSortedSet by checking IsSorted before sorting
Check IsSorted before Sort to reduce O(n log n) to O(n-1) comparisons for pre-sorted IN lists, improving ORCA optimization time.
1 parent 58cc529 commit bffdd7d

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/backend/gporca/libgpopt/src/base/CDatumSortedSet.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ CDatumSortedSet::CDatumSortedSet(CMemoryPool *mp, CExpression *pexprArray,
4242
{
4343
return;
4444
}
45-
aprngdatum->Sort(&CUtils::IDatumCmp);
45+
// Only sort if not already sorted - IsSorted is O(n-1) comparisons,
46+
// while Sort is O(n log n), so this optimization helps when data
47+
// is already sorted (common case for IN lists in queries)
48+
if (!aprngdatum->IsSorted(&CUtils::IDatumCmp))
49+
{
50+
aprngdatum->Sort(&CUtils::IDatumCmp);
51+
}
4652

4753
// de-duplicate
4854
const ULONG ulRangeArrayArity = aprngdatum->Size();

0 commit comments

Comments
 (0)