Skip to content

Commit 0a665bb

Browse files
committed
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 (f7f70d5), nor when we added arrays over domain types (c12d570). 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.
1 parent 6ba581c commit 0a665bb

File tree

6 files changed

+41
-13
lines changed

6 files changed

+41
-13
lines changed

src/test/regress/expected/arrays.out

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ INSERT INTO arrtest (a, b[1:2][1:2], c, d, e, f, g)
2626
'{"abc","abcde"}', '{"abc","abcde"}');
2727
INSERT INTO arrtest (a, b[1:2], c, d[1:2])
2828
VALUES ('{}', '{3,4}', '{foo,bar}', '{bar,foo}');
29+
INSERT INTO arrtest (b[2]) VALUES(now()); -- error, type mismatch
30+
ERROR: array assignment to "b" requires type integer but expression is of type timestamp with time zone
31+
LINE 1: INSERT INTO arrtest (b[2]) VALUES(now());
32+
^
33+
HINT: You will need to rewrite or cast the expression.
34+
INSERT INTO arrtest (b[1:2]) VALUES(now()); -- error, type mismatch
35+
ERROR: array assignment to "b" requires type integer[] but expression is of type timestamp with time zone
36+
LINE 1: INSERT INTO arrtest (b[1:2]) VALUES(now());
37+
^
38+
HINT: You will need to rewrite or cast the expression.
2939
SELECT * FROM arrtest;
3040
a | b | c | d | e | f | g
3141
-------------+-----------------+-----------+---------------+-----------------+-----------------+-------------
@@ -225,6 +235,9 @@ UPDATE arrtest
225235
SET c[1:NULL] = '{"can''t assign"}'
226236
WHERE array_dims(c) is not null;
227237
ERROR: array subscript in assignment must not be null
238+
-- Un-subscriptable type
239+
SELECT (now())[1];
240+
ERROR: cannot subscript type timestamp with time zone because it is not an array
228241
-- test slices with empty lower and/or upper index
229242
CREATE TEMP TABLE arrtest_s (
230243
a int2[],

src/test/regress/expected/rowtypes.out

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,18 @@ select * from people;
118118
(1 row)
119119

120120
insert into quadtable (f1, q.c1.r, q.c2.i) values(44,55,66);
121+
update quadtable set q.c1.r = 12 where f1 = 2;
122+
update quadtable set q.c1 = 12; -- error, type mismatch
123+
ERROR: subfield "c1" is of type complex but expression is of type integer
124+
LINE 1: update quadtable set q.c1 = 12;
125+
^
126+
HINT: You will need to rewrite or cast the expression.
121127
select * from quadtable;
122128
f1 | q
123129
----+---------------------------
124130
1 | ("(3.3,4.4)","(5.5,6.6)")
125-
2 | ("(,4.4)","(5.5,6.6)")
126131
44 | ("(55,)","(,66)")
132+
2 | ("(12,4.4)","(5.5,6.6)")
127133
(3 rows)
128134

129135
-- The object here is to ensure that toasted references inside

src/test/regress/expected/type_sanity.out

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ WHERE (p1.typtype = 'c' AND p1.typrelid = 0) OR
5656
-----+---------
5757
(0 rows)
5858

59-
-- Look for types that should have an array type according to their typtype,
60-
-- but don't. We exclude composites here because we have not bothered to
61-
-- make array types corresponding to the system catalogs' rowtypes.
62-
-- NOTE: as of v10, this check finds pg_node_tree, pg_ndistinct, smgr.
59+
-- Look for types that should have an array type but don't.
60+
-- Generally anything that's not a pseudotype should have an array type.
61+
-- However, we do have a small number of exceptions.
6362
SELECT p1.oid, p1.typname
6463
FROM pg_type as p1
65-
WHERE p1.typtype not in ('c','d','p') AND p1.typname NOT LIKE E'\\_%'
64+
WHERE p1.typtype not in ('p') AND p1.typname NOT LIKE E'\\_%'
6665
AND NOT EXISTS
6766
(SELECT 1 FROM pg_type as p2
6867
WHERE p2.typname = ('_' || p1.typname)::name AND
69-
p2.typelem = p1.oid and p1.typarray = p2.oid);
68+
p2.typelem = p1.oid and p1.typarray = p2.oid)
69+
ORDER BY p1.oid;
7070
oid | typname
7171
------+-----------------
7272
194 | pg_node_tree

src/test/regress/sql/arrays.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ INSERT INTO arrtest (a, b[1:2][1:2], c, d, e, f, g)
3434
INSERT INTO arrtest (a, b[1:2], c, d[1:2])
3535
VALUES ('{}', '{3,4}', '{foo,bar}', '{bar,foo}');
3636

37+
INSERT INTO arrtest (b[2]) VALUES(now()); -- error, type mismatch
38+
39+
INSERT INTO arrtest (b[1:2]) VALUES(now()); -- error, type mismatch
3740

3841
SELECT * FROM arrtest;
3942

@@ -122,6 +125,8 @@ UPDATE arrtest
122125
UPDATE arrtest
123126
SET c[1:NULL] = '{"can''t assign"}'
124127
WHERE array_dims(c) is not null;
128+
-- Un-subscriptable type
129+
SELECT (now())[1];
125130

126131
-- test slices with empty lower and/or upper index
127132
CREATE TEMP TABLE arrtest_s (

src/test/regress/sql/rowtypes.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ select * from people;
6363

6464
insert into quadtable (f1, q.c1.r, q.c2.i) values(44,55,66);
6565

66+
update quadtable set q.c1.r = 12 where f1 = 2;
67+
68+
update quadtable set q.c1 = 12; -- error, type mismatch
69+
6670
select * from quadtable;
6771

6872
-- The object here is to ensure that toasted references inside

src/test/regress/sql/type_sanity.sql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,18 @@ FROM pg_type as p1
5050
WHERE (p1.typtype = 'c' AND p1.typrelid = 0) OR
5151
(p1.typtype != 'c' AND p1.typrelid != 0);
5252

53-
-- Look for types that should have an array type according to their typtype,
54-
-- but don't. We exclude composites here because we have not bothered to
55-
-- make array types corresponding to the system catalogs' rowtypes.
56-
-- NOTE: as of v10, this check finds pg_node_tree, pg_ndistinct, smgr.
53+
-- Look for types that should have an array type but don't.
54+
-- Generally anything that's not a pseudotype should have an array type.
55+
-- However, we do have a small number of exceptions.
5756

5857
SELECT p1.oid, p1.typname
5958
FROM pg_type as p1
60-
WHERE p1.typtype not in ('c','d','p') AND p1.typname NOT LIKE E'\\_%'
59+
WHERE p1.typtype not in ('p') AND p1.typname NOT LIKE E'\\_%'
6160
AND NOT EXISTS
6261
(SELECT 1 FROM pg_type as p2
6362
WHERE p2.typname = ('_' || p1.typname)::name AND
64-
p2.typelem = p1.oid and p1.typarray = p2.oid);
63+
p2.typelem = p1.oid and p1.typarray = p2.oid)
64+
ORDER BY p1.oid;
6565

6666
-- Make sure typarray points to a varlena array type of our own base
6767
SELECT p1.oid, p1.typname as basetype, p2.typname as arraytype,

0 commit comments

Comments
 (0)