Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 59d3789

Browse files
committed
Fix SIGSEGV in pruning for ScalarArrayOp with constant-null array.
Not much to be said here: commit 9fdb675 should have checked constisnull, didn't. Per report from Piotr Włodarczyk. Back-patch to v11 where bug was introduced. Discussion: https://postgr.es/m/CAP-dhMr+vRpwizEYjUjsiZ1vwqpohTm+3Pbdt6Pr7FEgPq9R0Q@mail.gmail.com
1 parent edc793d commit 59d3789

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

src/backend/partitioning/partprune.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2054,7 +2054,7 @@ match_clause_to_partition_key(GeneratePruningStepsContext *context,
20542054
* nodes, one for each array element (excepting nulls).
20552055
*/
20562056
Const *arr = (Const *) rightop;
2057-
ArrayType *arrval = DatumGetArrayTypeP(arr->constvalue);
2057+
ArrayType *arrval;
20582058
int16 elemlen;
20592059
bool elembyval;
20602060
char elemalign;
@@ -2063,6 +2063,11 @@ match_clause_to_partition_key(GeneratePruningStepsContext *context,
20632063
int num_elems,
20642064
i;
20652065

2066+
/* If the array itself is null, the saop returns null */
2067+
if (arr->constisnull)
2068+
return PARTCLAUSE_MATCH_CONTRADICT;
2069+
2070+
arrval = DatumGetArrayTypeP(arr->constvalue);
20662071
get_typlenbyvalalign(ARR_ELEMTYPE(arrval),
20672072
&elemlen, &elembyval, &elemalign);
20682073
deconstruct_array(arrval,

src/test/regress/expected/partition_prune.out

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,58 @@ explain (costs off) select * from coercepart where a !~ all ('{ab,bc}');
11681168
Filter: ((a)::text !~ ALL ('{ab,bc}'::text[]))
11691169
(7 rows)
11701170

1171+
explain (costs off) select * from coercepart where a = any ('{ab,bc}');
1172+
QUERY PLAN
1173+
-------------------------------------------------------
1174+
Append
1175+
-> Seq Scan on coercepart_ab
1176+
Filter: ((a)::text = ANY ('{ab,bc}'::text[]))
1177+
-> Seq Scan on coercepart_bc
1178+
Filter: ((a)::text = ANY ('{ab,bc}'::text[]))
1179+
(5 rows)
1180+
1181+
explain (costs off) select * from coercepart where a = any ('{ab,null}');
1182+
QUERY PLAN
1183+
---------------------------------------------------
1184+
Seq Scan on coercepart_ab
1185+
Filter: ((a)::text = ANY ('{ab,NULL}'::text[]))
1186+
(2 rows)
1187+
1188+
explain (costs off) select * from coercepart where a = any (null::text[]);
1189+
QUERY PLAN
1190+
--------------------------
1191+
Result
1192+
One-Time Filter: false
1193+
(2 rows)
1194+
1195+
explain (costs off) select * from coercepart where a = all ('{ab}');
1196+
QUERY PLAN
1197+
----------------------------------------------
1198+
Seq Scan on coercepart_ab
1199+
Filter: ((a)::text = ALL ('{ab}'::text[]))
1200+
(2 rows)
1201+
1202+
explain (costs off) select * from coercepart where a = all ('{ab,bc}');
1203+
QUERY PLAN
1204+
--------------------------
1205+
Result
1206+
One-Time Filter: false
1207+
(2 rows)
1208+
1209+
explain (costs off) select * from coercepart where a = all ('{ab,null}');
1210+
QUERY PLAN
1211+
--------------------------
1212+
Result
1213+
One-Time Filter: false
1214+
(2 rows)
1215+
1216+
explain (costs off) select * from coercepart where a = all (null::text[]);
1217+
QUERY PLAN
1218+
--------------------------
1219+
Result
1220+
One-Time Filter: false
1221+
(2 rows)
1222+
11711223
drop table coercepart;
11721224
CREATE TABLE part (a INT, b INT) PARTITION BY LIST (a);
11731225
CREATE TABLE part_p1 PARTITION OF part FOR VALUES IN (-2,-1,0,1,2);
@@ -3139,6 +3191,20 @@ select * from stable_qual_pruning
31393191
Filter: (a = ANY ('{"Tue Feb 01 00:00:00 2000 PST","Fri Jan 01 00:00:00 2010 PST"}'::timestamp with time zone[]))
31403192
(4 rows)
31413193

3194+
explain (analyze, costs off, summary off, timing off)
3195+
select * from stable_qual_pruning
3196+
where a = any(null::timestamptz[]);
3197+
QUERY PLAN
3198+
----------------------------------------------------------------
3199+
Append (actual rows=0 loops=1)
3200+
-> Seq Scan on stable_qual_pruning1 (actual rows=0 loops=1)
3201+
Filter: (a = ANY (NULL::timestamp with time zone[]))
3202+
-> Seq Scan on stable_qual_pruning2 (actual rows=0 loops=1)
3203+
Filter: (a = ANY (NULL::timestamp with time zone[]))
3204+
-> Seq Scan on stable_qual_pruning3 (actual rows=0 loops=1)
3205+
Filter: (a = ANY (NULL::timestamp with time zone[]))
3206+
(7 rows)
3207+
31423208
drop table stable_qual_pruning;
31433209
--
31443210
-- Check that pruning with composite range partitioning works correctly when

src/test/regress/sql/partition_prune.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ explain (costs off) select * from coercepart where a ~ any ('{ab}');
180180
explain (costs off) select * from coercepart where a !~ all ('{ab}');
181181
explain (costs off) select * from coercepart where a ~ any ('{ab,bc}');
182182
explain (costs off) select * from coercepart where a !~ all ('{ab,bc}');
183+
explain (costs off) select * from coercepart where a = any ('{ab,bc}');
184+
explain (costs off) select * from coercepart where a = any ('{ab,null}');
185+
explain (costs off) select * from coercepart where a = any (null::text[]);
186+
explain (costs off) select * from coercepart where a = all ('{ab}');
187+
explain (costs off) select * from coercepart where a = all ('{ab,bc}');
188+
explain (costs off) select * from coercepart where a = all ('{ab,null}');
189+
explain (costs off) select * from coercepart where a = all (null::text[]);
183190

184191
drop table coercepart;
185192

@@ -799,6 +806,9 @@ select * from stable_qual_pruning
799806
explain (analyze, costs off, summary off, timing off)
800807
select * from stable_qual_pruning
801808
where a = any(array['2000-02-01', '2010-01-01']::timestamptz[]);
809+
explain (analyze, costs off, summary off, timing off)
810+
select * from stable_qual_pruning
811+
where a = any(null::timestamptz[]);
802812

803813
drop table stable_qual_pruning;
804814

0 commit comments

Comments
 (0)