Skip to content

Commit 0405982

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 <ylshiyu@126.com> Author: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/18840-fbc9505f066e50d6@postgresql.org Backpatch-through: 13
1 parent 004dbbd commit 0405982

File tree

4 files changed

+166
-8
lines changed

4 files changed

+166
-8
lines changed

src/backend/parser/parse_expr.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,10 +1992,18 @@ transformArrayExpr(ParseState *pstate, A_ArrayExpr *a,
19921992

19931993
/*
19941994
* Check for sub-array expressions, if we haven't already found
1995-
* one.
1995+
* one. Note we don't accept domain-over-array as a sub-array,
1996+
* nor int2vector nor oidvector; those have constraints that don't
1997+
* map well to being treated as a sub-array.
19961998
*/
1997-
if (!newa->multidims && type_is_array(exprType(newe)))
1998-
newa->multidims = true;
1999+
if (!newa->multidims)
2000+
{
2001+
Oid newetype = exprType(newe);
2002+
2003+
if (newetype != INT2VECTOROID && newetype != OIDVECTOROID &&
2004+
type_is_array(newetype))
2005+
newa->multidims = true;
2006+
}
19992007
}
20002008

20012009
newelems = lappend(newelems, newe);

src/backend/utils/adt/arrayfuncs.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5800,9 +5800,14 @@ ArrayBuildStateAny *
58005800
initArrayResultAny(Oid input_type, MemoryContext rcontext, bool subcontext)
58015801
{
58025802
ArrayBuildStateAny *astate;
5803-
Oid element_type = get_element_type(input_type);
58045803

5805-
if (OidIsValid(element_type))
5804+
/*
5805+
* int2vector and oidvector will satisfy both get_element_type and
5806+
* get_array_type. We prefer to treat them as scalars, to be consistent
5807+
* with get_promoted_array_type. Hence, check get_array_type not
5808+
* get_element_type.
5809+
*/
5810+
if (!OidIsValid(get_array_type(input_type)))
58065811
{
58075812
/* Array case */
58085813
ArrayBuildStateArr *arraystate;
@@ -5819,9 +5824,6 @@ initArrayResultAny(Oid input_type, MemoryContext rcontext, bool subcontext)
58195824
/* Scalar case */
58205825
ArrayBuildState *scalarstate;
58215826

5822-
/* Let's just check that we have a type that can be put into arrays */
5823-
Assert(OidIsValid(get_array_type(input_type)));
5824-
58255827
scalarstate = initArrayResult(input_type, rcontext, subcontext);
58265828
astate = (ArrayBuildStateAny *)
58275829
MemoryContextAlloc(scalarstate->mcontext,

src/test/regress/expected/arrays.out

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,6 +2301,132 @@ select array(select array['Hello', i::text] from generate_series(9,11) i);
23012301
{{Hello,9},{Hello,10},{Hello,11}}
23022302
(1 row)
23032303

2304+
-- int2vector and oidvector should be treated as scalar types for this purpose
2305+
select pg_typeof(array(select '11 22 33'::int2vector from generate_series(1,5)));
2306+
pg_typeof
2307+
--------------
2308+
int2vector[]
2309+
(1 row)
2310+
2311+
select array(select '11 22 33'::int2vector from generate_series(1,5));
2312+
array
2313+
----------------------------------------------------------
2314+
{"11 22 33","11 22 33","11 22 33","11 22 33","11 22 33"}
2315+
(1 row)
2316+
2317+
select unnest(array(select '11 22 33'::int2vector from generate_series(1,5)));
2318+
unnest
2319+
----------
2320+
11 22 33
2321+
11 22 33
2322+
11 22 33
2323+
11 22 33
2324+
11 22 33
2325+
(5 rows)
2326+
2327+
select pg_typeof(array(select '11 22 33'::oidvector from generate_series(1,5)));
2328+
pg_typeof
2329+
-------------
2330+
oidvector[]
2331+
(1 row)
2332+
2333+
select array(select '11 22 33'::oidvector from generate_series(1,5));
2334+
array
2335+
----------------------------------------------------------
2336+
{"11 22 33","11 22 33","11 22 33","11 22 33","11 22 33"}
2337+
(1 row)
2338+
2339+
select unnest(array(select '11 22 33'::oidvector from generate_series(1,5)));
2340+
unnest
2341+
----------
2342+
11 22 33
2343+
11 22 33
2344+
11 22 33
2345+
11 22 33
2346+
11 22 33
2347+
(5 rows)
2348+
2349+
-- array[] should do the same
2350+
select pg_typeof(array['11 22 33'::int2vector]);
2351+
pg_typeof
2352+
--------------
2353+
int2vector[]
2354+
(1 row)
2355+
2356+
select array['11 22 33'::int2vector];
2357+
array
2358+
--------------
2359+
{"11 22 33"}
2360+
(1 row)
2361+
2362+
select pg_typeof(unnest(array['11 22 33'::int2vector]));
2363+
pg_typeof
2364+
------------
2365+
int2vector
2366+
(1 row)
2367+
2368+
select unnest(array['11 22 33'::int2vector]);
2369+
unnest
2370+
----------
2371+
11 22 33
2372+
(1 row)
2373+
2374+
select pg_typeof(unnest('11 22 33'::int2vector));
2375+
pg_typeof
2376+
-----------
2377+
smallint
2378+
smallint
2379+
smallint
2380+
(3 rows)
2381+
2382+
select unnest('11 22 33'::int2vector);
2383+
unnest
2384+
--------
2385+
11
2386+
22
2387+
33
2388+
(3 rows)
2389+
2390+
select pg_typeof(array['11 22 33'::oidvector]);
2391+
pg_typeof
2392+
-------------
2393+
oidvector[]
2394+
(1 row)
2395+
2396+
select array['11 22 33'::oidvector];
2397+
array
2398+
--------------
2399+
{"11 22 33"}
2400+
(1 row)
2401+
2402+
select pg_typeof(unnest(array['11 22 33'::oidvector]));
2403+
pg_typeof
2404+
-----------
2405+
oidvector
2406+
(1 row)
2407+
2408+
select unnest(array['11 22 33'::oidvector]);
2409+
unnest
2410+
----------
2411+
11 22 33
2412+
(1 row)
2413+
2414+
select pg_typeof(unnest('11 22 33'::oidvector));
2415+
pg_typeof
2416+
-----------
2417+
oid
2418+
oid
2419+
oid
2420+
(3 rows)
2421+
2422+
select unnest('11 22 33'::oidvector);
2423+
unnest
2424+
--------
2425+
11
2426+
22
2427+
33
2428+
(3 rows)
2429+
23042430
-- Insert/update on a column that is array of composite
23052431
create temp table t1 (f1 int8_tbl[]);
23062432
insert into t1 (f1[5].q1) values(42);

src/test/regress/sql/arrays.sql

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,28 @@ select array_replace(array['AB',NULL,'CDE'],NULL,'12');
682682
select array(select array[i,i/2] from generate_series(1,5) i);
683683
select array(select array['Hello', i::text] from generate_series(9,11) i);
684684

685+
-- int2vector and oidvector should be treated as scalar types for this purpose
686+
select pg_typeof(array(select '11 22 33'::int2vector from generate_series(1,5)));
687+
select array(select '11 22 33'::int2vector from generate_series(1,5));
688+
select unnest(array(select '11 22 33'::int2vector from generate_series(1,5)));
689+
select pg_typeof(array(select '11 22 33'::oidvector from generate_series(1,5)));
690+
select array(select '11 22 33'::oidvector from generate_series(1,5));
691+
select unnest(array(select '11 22 33'::oidvector from generate_series(1,5)));
692+
693+
-- array[] should do the same
694+
select pg_typeof(array['11 22 33'::int2vector]);
695+
select array['11 22 33'::int2vector];
696+
select pg_typeof(unnest(array['11 22 33'::int2vector]));
697+
select unnest(array['11 22 33'::int2vector]);
698+
select pg_typeof(unnest('11 22 33'::int2vector));
699+
select unnest('11 22 33'::int2vector);
700+
select pg_typeof(array['11 22 33'::oidvector]);
701+
select array['11 22 33'::oidvector];
702+
select pg_typeof(unnest(array['11 22 33'::oidvector]));
703+
select unnest(array['11 22 33'::oidvector]);
704+
select pg_typeof(unnest('11 22 33'::oidvector));
705+
select unnest('11 22 33'::oidvector);
706+
685707
-- Insert/update on a column that is array of composite
686708

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

0 commit comments

Comments
 (0)