Commit 0a665bbc authored by Tom Lane's avatar Tom Lane

Add a couple of regression test cases related to array subscripting.

Exercise some error cases that were never reached in the existing
regression tests.  This is partly for code-coverage reasons, and
partly to memorialize the current behavior in advance of planned
changes for generic subscripting.

Also, I noticed that type_sanity's check to verify that all standard
types have array types was never extended when we added arrays for
all system catalog rowtypes (f7f70d5e), nor when we added arrays
over domain types (c12d570f).  So do that.  Also, since the query's
expected output isn't empty, it seems like a good idea to add an
ORDER BY to make sure the result stays stable.
parent 6ba581cf
...@@ -26,6 +26,16 @@ INSERT INTO arrtest (a, b[1:2][1:2], c, d, e, f, g) ...@@ -26,6 +26,16 @@ INSERT INTO arrtest (a, b[1:2][1:2], c, d, e, f, g)
'{"abc","abcde"}', '{"abc","abcde"}'); '{"abc","abcde"}', '{"abc","abcde"}');
INSERT INTO arrtest (a, b[1:2], c, d[1:2]) INSERT INTO arrtest (a, b[1:2], c, d[1:2])
VALUES ('{}', '{3,4}', '{foo,bar}', '{bar,foo}'); VALUES ('{}', '{3,4}', '{foo,bar}', '{bar,foo}');
INSERT INTO arrtest (b[2]) VALUES(now()); -- error, type mismatch
ERROR: array assignment to "b" requires type integer but expression is of type timestamp with time zone
LINE 1: INSERT INTO arrtest (b[2]) VALUES(now());
^
HINT: You will need to rewrite or cast the expression.
INSERT INTO arrtest (b[1:2]) VALUES(now()); -- error, type mismatch
ERROR: array assignment to "b" requires type integer[] but expression is of type timestamp with time zone
LINE 1: INSERT INTO arrtest (b[1:2]) VALUES(now());
^
HINT: You will need to rewrite or cast the expression.
SELECT * FROM arrtest; SELECT * FROM arrtest;
a | b | c | d | e | f | g a | b | c | d | e | f | g
-------------+-----------------+-----------+---------------+-----------------+-----------------+------------- -------------+-----------------+-----------+---------------+-----------------+-----------------+-------------
...@@ -225,6 +235,9 @@ UPDATE arrtest ...@@ -225,6 +235,9 @@ UPDATE arrtest
SET c[1:NULL] = '{"can''t assign"}' SET c[1:NULL] = '{"can''t assign"}'
WHERE array_dims(c) is not null; WHERE array_dims(c) is not null;
ERROR: array subscript in assignment must not be null ERROR: array subscript in assignment must not be null
-- Un-subscriptable type
SELECT (now())[1];
ERROR: cannot subscript type timestamp with time zone because it is not an array
-- test slices with empty lower and/or upper index -- test slices with empty lower and/or upper index
CREATE TEMP TABLE arrtest_s ( CREATE TEMP TABLE arrtest_s (
a int2[], a int2[],
......
...@@ -118,12 +118,18 @@ select * from people; ...@@ -118,12 +118,18 @@ select * from people;
(1 row) (1 row)
insert into quadtable (f1, q.c1.r, q.c2.i) values(44,55,66); insert into quadtable (f1, q.c1.r, q.c2.i) values(44,55,66);
update quadtable set q.c1.r = 12 where f1 = 2;
update quadtable set q.c1 = 12; -- error, type mismatch
ERROR: subfield "c1" is of type complex but expression is of type integer
LINE 1: update quadtable set q.c1 = 12;
^
HINT: You will need to rewrite or cast the expression.
select * from quadtable; select * from quadtable;
f1 | q f1 | q
----+--------------------------- ----+---------------------------
1 | ("(3.3,4.4)","(5.5,6.6)") 1 | ("(3.3,4.4)","(5.5,6.6)")
2 | ("(,4.4)","(5.5,6.6)")
44 | ("(55,)","(,66)") 44 | ("(55,)","(,66)")
2 | ("(12,4.4)","(5.5,6.6)")
(3 rows) (3 rows)
-- The object here is to ensure that toasted references inside -- The object here is to ensure that toasted references inside
......
...@@ -56,17 +56,17 @@ WHERE (p1.typtype = 'c' AND p1.typrelid = 0) OR ...@@ -56,17 +56,17 @@ WHERE (p1.typtype = 'c' AND p1.typrelid = 0) OR
-----+--------- -----+---------
(0 rows) (0 rows)
-- Look for types that should have an array type according to their typtype, -- Look for types that should have an array type but don't.
-- but don't. We exclude composites here because we have not bothered to -- Generally anything that's not a pseudotype should have an array type.
-- make array types corresponding to the system catalogs' rowtypes. -- However, we do have a small number of exceptions.
-- NOTE: as of v10, this check finds pg_node_tree, pg_ndistinct, smgr.
SELECT p1.oid, p1.typname SELECT p1.oid, p1.typname
FROM pg_type as p1 FROM pg_type as p1
WHERE p1.typtype not in ('c','d','p') AND p1.typname NOT LIKE E'\\_%' WHERE p1.typtype not in ('p') AND p1.typname NOT LIKE E'\\_%'
AND NOT EXISTS AND NOT EXISTS
(SELECT 1 FROM pg_type as p2 (SELECT 1 FROM pg_type as p2
WHERE p2.typname = ('_' || p1.typname)::name AND WHERE p2.typname = ('_' || p1.typname)::name AND
p2.typelem = p1.oid and p1.typarray = p2.oid); p2.typelem = p1.oid and p1.typarray = p2.oid)
ORDER BY p1.oid;
oid | typname oid | typname
------+----------------- ------+-----------------
194 | pg_node_tree 194 | pg_node_tree
......
...@@ -34,6 +34,9 @@ INSERT INTO arrtest (a, b[1:2][1:2], c, d, e, f, g) ...@@ -34,6 +34,9 @@ INSERT INTO arrtest (a, b[1:2][1:2], c, d, e, f, g)
INSERT INTO arrtest (a, b[1:2], c, d[1:2]) INSERT INTO arrtest (a, b[1:2], c, d[1:2])
VALUES ('{}', '{3,4}', '{foo,bar}', '{bar,foo}'); VALUES ('{}', '{3,4}', '{foo,bar}', '{bar,foo}');
INSERT INTO arrtest (b[2]) VALUES(now()); -- error, type mismatch
INSERT INTO arrtest (b[1:2]) VALUES(now()); -- error, type mismatch
SELECT * FROM arrtest; SELECT * FROM arrtest;
...@@ -122,6 +125,8 @@ UPDATE arrtest ...@@ -122,6 +125,8 @@ UPDATE arrtest
UPDATE arrtest UPDATE arrtest
SET c[1:NULL] = '{"can''t assign"}' SET c[1:NULL] = '{"can''t assign"}'
WHERE array_dims(c) is not null; WHERE array_dims(c) is not null;
-- Un-subscriptable type
SELECT (now())[1];
-- test slices with empty lower and/or upper index -- test slices with empty lower and/or upper index
CREATE TEMP TABLE arrtest_s ( CREATE TEMP TABLE arrtest_s (
......
...@@ -63,6 +63,10 @@ select * from people; ...@@ -63,6 +63,10 @@ select * from people;
insert into quadtable (f1, q.c1.r, q.c2.i) values(44,55,66); insert into quadtable (f1, q.c1.r, q.c2.i) values(44,55,66);
update quadtable set q.c1.r = 12 where f1 = 2;
update quadtable set q.c1 = 12; -- error, type mismatch
select * from quadtable; select * from quadtable;
-- The object here is to ensure that toasted references inside -- The object here is to ensure that toasted references inside
......
...@@ -50,18 +50,18 @@ FROM pg_type as p1 ...@@ -50,18 +50,18 @@ FROM pg_type as p1
WHERE (p1.typtype = 'c' AND p1.typrelid = 0) OR WHERE (p1.typtype = 'c' AND p1.typrelid = 0) OR
(p1.typtype != 'c' AND p1.typrelid != 0); (p1.typtype != 'c' AND p1.typrelid != 0);
-- Look for types that should have an array type according to their typtype, -- Look for types that should have an array type but don't.
-- but don't. We exclude composites here because we have not bothered to -- Generally anything that's not a pseudotype should have an array type.
-- make array types corresponding to the system catalogs' rowtypes. -- However, we do have a small number of exceptions.
-- NOTE: as of v10, this check finds pg_node_tree, pg_ndistinct, smgr.
SELECT p1.oid, p1.typname SELECT p1.oid, p1.typname
FROM pg_type as p1 FROM pg_type as p1
WHERE p1.typtype not in ('c','d','p') AND p1.typname NOT LIKE E'\\_%' WHERE p1.typtype not in ('p') AND p1.typname NOT LIKE E'\\_%'
AND NOT EXISTS AND NOT EXISTS
(SELECT 1 FROM pg_type as p2 (SELECT 1 FROM pg_type as p2
WHERE p2.typname = ('_' || p1.typname)::name AND WHERE p2.typname = ('_' || p1.typname)::name AND
p2.typelem = p1.oid and p1.typarray = p2.oid); p2.typelem = p1.oid and p1.typarray = p2.oid)
ORDER BY p1.oid;
-- Make sure typarray points to a varlena array type of our own base -- Make sure typarray points to a varlena array type of our own base
SELECT p1.oid, p1.typname as basetype, p2.typname as arraytype, SELECT p1.oid, p1.typname as basetype, p2.typname as arraytype,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment