Skip to content

Commit 5f180c5

Browse files
wuyuhao28my-ship-it
authored andcommitted
Fix CDatumSortedSet handling of empty arrays causing errors in ORCA
Previously, when constructing CDatumSortedSet from an array expression with all NULL elements, the aprngdatum was becoming an empty array. This caused unexpected errors that could lead to ORCA fallback to the planner, or even coredump. The bug has been fixed by ensuring that no unnecessary operations are performed on the empty aprngdatum array.
1 parent a099d24 commit 5f180c5

4 files changed

Lines changed: 37 additions & 0 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ CDatumSortedSet::CDatumSortedSet(CMemoryPool *mp, CExpression *pexprArray,
3636
aprngdatum->Append(datum);
3737
}
3838
}
39+
40+
// ALL NULLs, just return empty set
41+
if (aprngdatum->Size() == 0)
42+
{
43+
return;
44+
}
3945
aprngdatum->Sort(&CUtils::IDatumCmp);
4046

4147
// de-duplicate

src/test/regress/expected/gporca.out

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14328,3 +14328,16 @@ ON t1.tradingday = t2.tradingday;
1432814328
(1 row)
1432914329

1433014330
DROP TABLE t_clientinstrumentind2, t_clientproductind2;
14331+
---------------------------------------------------------------------------------
14332+
-- Test ALL NULL scalar array compare
14333+
create table DatumSortedSet_core (a int, b character varying NOT NULL) distributed by (a);
14334+
explain select * from DatumSortedSet_core where b in (NULL, NULL);
14335+
QUERY PLAN
14336+
-------------------------------------------------------------------------------
14337+
Gather Motion 3:1 (slice1; segments: 3) (cost=0.00..240.69 rows=1 width=36)
14338+
-> Seq Scan on datumsortedset_core (cost=0.00..240.67 rows=1 width=36)
14339+
Filter: ((b)::text = ANY ('{NULL,NULL}'::text[]))
14340+
Optimizer: Postgres-based planner
14341+
(4 rows)
14342+
14343+
---------------------------------------------------------------------------------

src/test/regress/expected/gporca_optimizer.out

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14671,3 +14671,15 @@ ON t1.tradingday = t2.tradingday;
1467114671
(1 row)
1467214672

1467314673
DROP TABLE t_clientinstrumentind2, t_clientproductind2;
14674+
---------------------------------------------------------------------------------
14675+
-- Test ALL NULL scalar array compare
14676+
create table DatumSortedSet_core (a int, b character varying NOT NULL) distributed by (a);
14677+
explain select * from DatumSortedSet_core where b in (NULL, NULL);
14678+
QUERY PLAN
14679+
-------------------------------------------
14680+
Result (cost=0.00..0.00 rows=0 width=12)
14681+
One-Time Filter: false
14682+
Optimizer: GPORCA
14683+
(3 rows)
14684+
14685+
---------------------------------------------------------------------------------

src/test/regress/sql/gporca.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3518,6 +3518,12 @@ ON t1.tradingday = t2.tradingday;
35183518
DROP TABLE t_clientinstrumentind2, t_clientproductind2;
35193519

35203520

3521+
---------------------------------------------------------------------------------
3522+
-- Test ALL NULL scalar array compare
3523+
create table DatumSortedSet_core (a int, b character varying NOT NULL) distributed by (a);
3524+
explain select * from DatumSortedSet_core where b in (NULL, NULL);
3525+
---------------------------------------------------------------------------------
3526+
35213527
-- start_ignore
35223528
DROP SCHEMA orca CASCADE;
35233529
-- end_ignore

0 commit comments

Comments
 (0)