Skip to content

Commit 4618045

Browse files
committed
Fix ARRAY_SUBLINK and ARRAY[] for int2vector and oidvector input.
If the given input_type yields valid results from both get_element_type and get_array_type, initArrayResultAny believed the former and treated the input as an array type. However this is inconsistent with what get_promoted_array_type does, leading to situations where the output of an ARRAY() subquery is labeled with the wrong type: it's labeled as oidvector[] but is really a 2-D array of OID. That at least results in strange output, and can result in crashes if further processing such as unnest() is applied. AFAIK this is only possible with the int2vector and oidvector types, which are special-cased to be treated mostly as true arrays even though they aren't quite. Fix by switching the logic to match get_promoted_array_type by testing get_array_type not get_element_type, and remove an Assert thereby made pointless. (We need not introduce a symmetrical check for get_element_type in the other if-branch, because initArrayResultArr will check it.) This restores the behavior that existed before bac2739 introduced initArrayResultAny: the output really is int2vector[] or oidvector[]. Comparable confusion exists when an input of an ARRAY[] construct is int2vector or oidvector: transformArrayExpr decides it's dealing with a multidimensional array constructor, and we end up with something that's a multidimensional OID array but is alleged to be of type oidvector. I have not found a crashing case here, but it's easy to demonstrate totally-wrong results. Adjust that code so that what you get is an oidvector[] instead, for consistency with ARRAY() subqueries. (This change also makes these types work like domains-over-arrays in this context, which seems correct.) Bug: #18840 Reported-by: yang lei <[email protected]> Author: Tom Lane <[email protected]> Discussion: https://postgr.es/m/[email protected] Backpatch-through: 13
1 parent c7fc880 commit 4618045

File tree

4 files changed

+166
-8
lines changed

4 files changed

+166
-8
lines changed

src/backend/parser/parse_expr.c

+11-3
Original file line numberDiff line numberDiff line change
@@ -2053,10 +2053,18 @@ transformArrayExpr(ParseState *pstate, A_ArrayExpr *a,
20532053

20542054
/*
20552055
* Check for sub-array expressions, if we haven't already found
2056-
* one.
2056+
* one. Note we don't accept domain-over-array as a sub-array,
2057+
* nor int2vector nor oidvector; those have constraints that don't
2058+
* map well to being treated as a sub-array.
20572059
*/
2058-
if (!newa->multidims && type_is_array(exprType(newe)))
2059-
newa->multidims = true;
2060+
if (!newa->multidims)
2061+
{
2062+
Oid newetype = exprType(newe);
2063+
2064+
if (newetype != INT2VECTOROID && newetype != OIDVECTOROID &&
2065+
type_is_array(newetype))
2066+
newa->multidims = true;
2067+
}
20602068
}
20612069

20622070
newelems = lappend(newelems, newe);

src/backend/utils/adt/arrayfuncs.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -5782,9 +5782,14 @@ ArrayBuildStateAny *
57825782
initArrayResultAny(Oid input_type, MemoryContext rcontext, bool subcontext)
57835783
{
57845784
ArrayBuildStateAny *astate;
5785-
Oid element_type = get_element_type(input_type);
57865785

5787-
if (OidIsValid(element_type))
5786+
/*
5787+
* int2vector and oidvector will satisfy both get_element_type and
5788+
* get_array_type. We prefer to treat them as scalars, to be consistent
5789+
* with get_promoted_array_type. Hence, check get_array_type not
5790+
* get_element_type.
5791+
*/
5792+
if (!OidIsValid(get_array_type(input_type)))
57885793
{
57895794
/* Array case */
57905795
ArrayBuildStateArr *arraystate;
@@ -5801,9 +5806,6 @@ initArrayResultAny(Oid input_type, MemoryContext rcontext, bool subcontext)
58015806
/* Scalar case */
58025807
ArrayBuildState *scalarstate;
58035808

5804-
/* Let's just check that we have a type that can be put into arrays */
5805-
Assert(OidIsValid(get_array_type(input_type)));
5806-
58075809
scalarstate = initArrayResult(input_type, rcontext, subcontext);
58085810
astate = (ArrayBuildStateAny *)
58095811
MemoryContextAlloc(scalarstate->mcontext,

src/test/regress/expected/arrays.out

+126
Original file line numberDiff line numberDiff line change
@@ -2457,6 +2457,132 @@ select array(select array['Hello', i::text] from generate_series(9,11) i);
24572457
{{Hello,9},{Hello,10},{Hello,11}}
24582458
(1 row)
24592459

2460+
-- int2vector and oidvector should be treated as scalar types for this purpose
2461+
select pg_typeof(array(select '11 22 33'::int2vector from generate_series(1,5)));
2462+
pg_typeof
2463+
--------------
2464+
int2vector[]
2465+
(1 row)
2466+
2467+
select array(select '11 22 33'::int2vector from generate_series(1,5));
2468+
array
2469+
----------------------------------------------------------
2470+
{"11 22 33","11 22 33","11 22 33","11 22 33","11 22 33"}
2471+
(1 row)
2472+
2473+
select unnest(array(select '11 22 33'::int2vector from generate_series(1,5)));
2474+
unnest
2475+
----------
2476+
11 22 33
2477+
11 22 33
2478+
11 22 33
2479+
11 22 33
2480+
11 22 33
2481+
(5 rows)
2482+
2483+
select pg_typeof(array(select '11 22 33'::oidvector from generate_series(1,5)));
2484+
pg_typeof
2485+
-------------
2486+
oidvector[]
2487+
(1 row)
2488+
2489+
select array(select '11 22 33'::oidvector from generate_series(1,5));
2490+
array
2491+
----------------------------------------------------------
2492+
{"11 22 33","11 22 33","11 22 33","11 22 33","11 22 33"}
2493+
(1 row)
2494+
2495+
select unnest(array(select '11 22 33'::oidvector from generate_series(1,5)));
2496+
unnest
2497+
----------
2498+
11 22 33
2499+
11 22 33
2500+
11 22 33
2501+
11 22 33
2502+
11 22 33
2503+
(5 rows)
2504+
2505+
-- array[] should do the same
2506+
select pg_typeof(array['11 22 33'::int2vector]);
2507+
pg_typeof
2508+
--------------
2509+
int2vector[]
2510+
(1 row)
2511+
2512+
select array['11 22 33'::int2vector];
2513+
array
2514+
--------------
2515+
{"11 22 33"}
2516+
(1 row)
2517+
2518+
select pg_typeof(unnest(array['11 22 33'::int2vector]));
2519+
pg_typeof
2520+
------------
2521+
int2vector
2522+
(1 row)
2523+
2524+
select unnest(array['11 22 33'::int2vector]);
2525+
unnest
2526+
----------
2527+
11 22 33
2528+
(1 row)
2529+
2530+
select pg_typeof(unnest('11 22 33'::int2vector));
2531+
pg_typeof
2532+
-----------
2533+
smallint
2534+
smallint
2535+
smallint
2536+
(3 rows)
2537+
2538+
select unnest('11 22 33'::int2vector);
2539+
unnest
2540+
--------
2541+
11
2542+
22
2543+
33
2544+
(3 rows)
2545+
2546+
select pg_typeof(array['11 22 33'::oidvector]);
2547+
pg_typeof
2548+
-------------
2549+
oidvector[]
2550+
(1 row)
2551+
2552+
select array['11 22 33'::oidvector];
2553+
array
2554+
--------------
2555+
{"11 22 33"}
2556+
(1 row)
2557+
2558+
select pg_typeof(unnest(array['11 22 33'::oidvector]));
2559+
pg_typeof
2560+
-----------
2561+
oidvector
2562+
(1 row)
2563+
2564+
select unnest(array['11 22 33'::oidvector]);
2565+
unnest
2566+
----------
2567+
11 22 33
2568+
(1 row)
2569+
2570+
select pg_typeof(unnest('11 22 33'::oidvector));
2571+
pg_typeof
2572+
-----------
2573+
oid
2574+
oid
2575+
oid
2576+
(3 rows)
2577+
2578+
select unnest('11 22 33'::oidvector);
2579+
unnest
2580+
--------
2581+
11
2582+
22
2583+
33
2584+
(3 rows)
2585+
24602586
-- Insert/update on a column that is array of composite
24612587
create temp table t1 (f1 int8_tbl[]);
24622588
insert into t1 (f1[5].q1) values(42);

src/test/regress/sql/arrays.sql

+22
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,28 @@ select array_replace(array['AB',NULL,'CDE'],NULL,'12');
713713
select array(select array[i,i/2] from generate_series(1,5) i);
714714
select array(select array['Hello', i::text] from generate_series(9,11) i);
715715

716+
-- int2vector and oidvector should be treated as scalar types for this purpose
717+
select pg_typeof(array(select '11 22 33'::int2vector from generate_series(1,5)));
718+
select array(select '11 22 33'::int2vector from generate_series(1,5));
719+
select unnest(array(select '11 22 33'::int2vector from generate_series(1,5)));
720+
select pg_typeof(array(select '11 22 33'::oidvector from generate_series(1,5)));
721+
select array(select '11 22 33'::oidvector from generate_series(1,5));
722+
select unnest(array(select '11 22 33'::oidvector from generate_series(1,5)));
723+
724+
-- array[] should do the same
725+
select pg_typeof(array['11 22 33'::int2vector]);
726+
select array['11 22 33'::int2vector];
727+
select pg_typeof(unnest(array['11 22 33'::int2vector]));
728+
select unnest(array['11 22 33'::int2vector]);
729+
select pg_typeof(unnest('11 22 33'::int2vector));
730+
select unnest('11 22 33'::int2vector);
731+
select pg_typeof(array['11 22 33'::oidvector]);
732+
select array['11 22 33'::oidvector];
733+
select pg_typeof(unnest(array['11 22 33'::oidvector]));
734+
select unnest(array['11 22 33'::oidvector]);
735+
select pg_typeof(unnest('11 22 33'::oidvector));
736+
select unnest('11 22 33'::oidvector);
737+
716738
-- Insert/update on a column that is array of composite
717739

718740
create temp table t1 (f1 int8_tbl[]);

0 commit comments

Comments
 (0)