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

Commit ce38949

Browse files
committed
Improve expression evaluation test coverage.
Upcoming patches are revamping expression evaluation significantly. It therefore seems prudent to try to ensure that the coverage of the existing evaluation code is high. This commit adds coverage for the cases that can reasonably be tested. There's still a bunch of unreachable error messages and such, but otherwise this achieves nearly full regression test coverage (with the exception of the unused GetAttributeByNum/GetAttributeByName). Author: Andres Freund Discussion: https://postgr.es/m/20170310194021.ek4bs4bl2khxkmll@alap3.anarazel.de
1 parent cd1e23e commit ce38949

17 files changed

+406
-1
lines changed

src/backend/executor/execQual.c

+1
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ ExecEvalArrayRef(ArrayRefExprState *astate,
392392
}
393393
else
394394
{
395+
/* this is currently unreachable */
395396
econtext->caseValue_datum =
396397
array_get_slice(array_source, i,
397398
upper.indx, lower.indx,

src/test/regress/expected/arrays.out

+81
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,44 @@ select ('[0:2][0:2]={{1,2,3},{4,5,6},{7,8,9}}'::int[])[1:2][2];
187187
{{5,6},{8,9}}
188188
(1 row)
189189

190+
--
191+
-- check subscription corner cases
192+
--
193+
-- More subscripts than MAXDIMS(6)
194+
SELECT ('{}'::int[])[1][2][3][4][5][6][7];
195+
ERROR: number of array dimensions (7) exceeds the maximum allowed (6)
196+
-- NULL index yields NULL when selecting
197+
SELECT ('{{{1},{2},{3}},{{4},{5},{6}}}'::int[])[1][NULL][1];
198+
int4
199+
------
200+
201+
(1 row)
202+
203+
SELECT ('{{{1},{2},{3}},{{4},{5},{6}}}'::int[])[1][NULL:1][1];
204+
int4
205+
------
206+
207+
(1 row)
208+
209+
SELECT ('{{{1},{2},{3}},{{4},{5},{6}}}'::int[])[1][1:NULL][1];
210+
int4
211+
------
212+
213+
(1 row)
214+
215+
-- NULL index in assignment is an error
216+
UPDATE arrtest
217+
SET c[NULL] = '{"can''t assign"}'
218+
WHERE array_dims(c) is not null;
219+
ERROR: array subscript in assignment must not be null
220+
UPDATE arrtest
221+
SET c[NULL:1] = '{"can''t assign"}'
222+
WHERE array_dims(c) is not null;
223+
ERROR: array subscript in assignment must not be null
224+
UPDATE arrtest
225+
SET c[1:NULL] = '{"can''t assign"}'
226+
WHERE array_dims(c) is not null;
227+
ERROR: array subscript in assignment must not be null
190228
-- test slices with empty lower and/or upper index
191229
CREATE TEMP TABLE arrtest_s (
192230
a int2[],
@@ -263,6 +301,36 @@ SELECT f1[:1] FROM POINT_TBL;
263301
ERROR: slices of fixed-length arrays not implemented
264302
SELECT f1[:] FROM POINT_TBL;
265303
ERROR: slices of fixed-length arrays not implemented
304+
-- subscript assignments to fixed-width result in NULL if previous value is NULL
305+
UPDATE point_tbl SET f1[0] = 10 WHERE f1 IS NULL RETURNING *;
306+
f1
307+
----
308+
309+
(1 row)
310+
311+
INSERT INTO point_tbl(f1[0]) VALUES(0) RETURNING *;
312+
f1
313+
----
314+
315+
(1 row)
316+
317+
-- NULL assignments get ignored
318+
UPDATE point_tbl SET f1[0] = NULL WHERE f1::text = '(10,10)'::point::text RETURNING *;
319+
f1
320+
---------
321+
(10,10)
322+
(1 row)
323+
324+
-- but non-NULL subscript assignments work
325+
UPDATE point_tbl SET f1[0] = -10, f1[1] = -10 WHERE f1::text = '(10,10)'::point::text RETURNING *;
326+
f1
327+
-----------
328+
(-10,-10)
329+
(1 row)
330+
331+
-- but not to expand the range
332+
UPDATE point_tbl SET f1[3] = 10 WHERE f1::text = '(-10,-10)'::point::text RETURNING *;
333+
ERROR: array subscript out of range
266334
--
267335
-- test array extension
268336
--
@@ -1099,6 +1167,12 @@ SELECT CAST(ARRAY[[[[[['a','bb','ccc']]]]]] as text[]) as "{{{{{{a,bb,ccc}}}}}}"
10991167
{{{{{{a,bb,ccc}}}}}}
11001168
(1 row)
11011169

1170+
SELECT NULL::text[]::int[] AS "NULL";
1171+
NULL
1172+
------
1173+
1174+
(1 row)
1175+
11021176
-- scalar op any/all (array)
11031177
select 33 = any ('{1,2,3}');
11041178
?column?
@@ -1214,6 +1288,13 @@ select 33 = all ('{33,null,33}');
12141288

12151289
(1 row)
12161290

1291+
-- nulls later in the bitmap
1292+
SELECT -1 != ALL(ARRAY(SELECT NULLIF(g.i, 900) FROM generate_series(1,1000) g(i)));
1293+
?column?
1294+
----------
1295+
1296+
(1 row)
1297+
12171298
-- test indexes on arrays
12181299
create temp table arr_tbl (f1 int[] unique);
12191300
insert into arr_tbl values ('{1,2,3}');

src/test/regress/expected/boolean.out

+24
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,29 @@ SELECT '' AS "Not True", f1
442442
| f
443443
(4 rows)
444444

445+
--
446+
-- Tests for BooleanTest
447+
--
448+
CREATE TABLE BOOLTBL3 (d text, b bool, o int);
449+
INSERT INTO BOOLTBL3 (d, b, o) VALUES ('true', true, 1);
450+
INSERT INTO BOOLTBL3 (d, b, o) VALUES ('false', false, 2);
451+
INSERT INTO BOOLTBL3 (d, b, o) VALUES ('null', null, 3);
452+
SELECT
453+
d,
454+
b IS TRUE AS istrue,
455+
b IS NOT TRUE AS isnottrue,
456+
b IS FALSE AS isfalse,
457+
b IS NOT FALSE AS isnotfalse,
458+
b IS UNKNOWN AS isunknown,
459+
b IS NOT UNKNOWN AS isnotunknown
460+
FROM booltbl3 ORDER BY o;
461+
d | istrue | isnottrue | isfalse | isnotfalse | isunknown | isnotunknown
462+
-------+--------+-----------+---------+------------+-----------+--------------
463+
true | t | f | f | t | f | t
464+
false | f | t | t | f | f | t
465+
null | f | t | f | t | t | f
466+
(3 rows)
467+
445468
--
446469
-- Clean up
447470
-- Many tables are retained by the regression test, but these do not seem
@@ -450,3 +473,4 @@ SELECT '' AS "Not True", f1
450473
--
451474
DROP TABLE BOOLTBL1;
452475
DROP TABLE BOOLTBL2;
476+
DROP TABLE BOOLTBL3;

src/test/regress/expected/case.out

+8
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ SELECT '6' AS "One",
7272
6 | 6
7373
(1 row)
7474

75+
SELECT '7' AS "None",
76+
CASE WHEN random() < 0 THEN 1
77+
END AS "NULL on no matches";
78+
None | NULL on no matches
79+
------+--------------------
80+
7 |
81+
(1 row)
82+
7583
-- Constant-expression folding shouldn't evaluate unreachable subexpressions
7684
SELECT CASE WHEN 1=0 THEN 1/0 WHEN 1=1 THEN 1 ELSE 2/0 END;
7785
case
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
--
2+
-- expression evaluated tests that don't fit into a more specific file
3+
--
4+
--
5+
-- Tests for SQLVAlueFunction
6+
--
7+
-- current_date (always matches because of transactional behaviour)
8+
SELECT date(now())::text = current_date::text;
9+
?column?
10+
----------
11+
t
12+
(1 row)
13+
14+
-- current_time / localtime
15+
SELECT now()::timetz::text = current_time::text;
16+
?column?
17+
----------
18+
t
19+
(1 row)
20+
21+
SELECT now()::time::text = localtime::text;
22+
?column?
23+
----------
24+
t
25+
(1 row)
26+
27+
-- current_timestamp / localtimestamp (always matches because of transactional behaviour)
28+
SELECT current_timestamp = NOW();
29+
?column?
30+
----------
31+
t
32+
(1 row)
33+
34+
-- precision
35+
SELECT length(current_timestamp::text) >= length(current_timestamp(0)::text);
36+
?column?
37+
----------
38+
t
39+
(1 row)
40+
41+
-- localtimestamp
42+
SELECT now()::timestamp::text = localtimestamp::text;
43+
?column?
44+
----------
45+
t
46+
(1 row)
47+
48+
-- current_role/user/user is tested in rolnames.sql
49+
-- current database / catalog
50+
SELECT current_catalog = current_database();
51+
?column?
52+
----------
53+
t
54+
(1 row)
55+
56+
-- current_schema
57+
SELECT current_schema;
58+
current_schema
59+
----------------
60+
public
61+
(1 row)
62+
63+
SET search_path = 'notme';
64+
SELECT current_schema;
65+
current_schema
66+
----------------
67+
68+
(1 row)
69+
70+
SET search_path = 'pg_catalog';
71+
SELECT current_schema;
72+
current_schema
73+
----------------
74+
pg_catalog
75+
(1 row)
76+
77+
RESET search_path;

src/test/regress/expected/inherit.out

+6
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,12 @@ select derived::base from derived;
710710
(0)
711711
(1 row)
712712

713+
select NULL::derived::base;
714+
base
715+
------
716+
717+
(1 row)
718+
713719
drop table derived;
714720
drop table base;
715721
create table p1(ff1 int);

src/test/regress/expected/privileges.out

+37
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,43 @@ ERROR: must be owner of function testfunc1
586586
DROP FUNCTION testfunc1(int); -- ok
587587
-- restore to sanity
588588
GRANT ALL PRIVILEGES ON LANGUAGE sql TO PUBLIC;
589+
-- verify privilege checks on coercions
590+
BEGIN;
591+
SELECT NULL::int4[]::int8[];
592+
int8
593+
------
594+
595+
(1 row)
596+
597+
SELECT '{1}'::int4[]::int8[];
598+
int8
599+
------
600+
{1}
601+
(1 row)
602+
603+
REVOKE ALL ON FUNCTION int8(integer) FROM PUBLIC;
604+
SELECT NULL::int4[]::int8[];
605+
int8
606+
------
607+
608+
(1 row)
609+
610+
SELECT '{1}'::int4[]::int8[]; --superuser, suceed
611+
int8
612+
------
613+
{1}
614+
(1 row)
615+
616+
SET SESSION AUTHORIZATION regress_user4;
617+
SELECT NULL::int4[]::int8[]; --other user, no elements to convert
618+
int8
619+
------
620+
621+
(1 row)
622+
623+
SELECT '{1}'::int4[]::int8[]; --other user, fail
624+
ERROR: permission denied for function int8
625+
ROLLBACK;
589626
-- privileges on types
590627
-- switch to superuser
591628
\c -

src/test/regress/expected/rowtypes.out

+40
Original file line numberDiff line numberDiff line change
@@ -711,3 +711,43 @@ select r, r is null as isnull, r is not null as isnotnull from r;
711711
(,) | t | f
712712
(6 rows)
713713

714+
--
715+
-- Tests for component access / FieldSelect
716+
--
717+
CREATE TABLE compositetable(a text, b text) WITH OIDS;
718+
INSERT INTO compositetable(a, b) VALUES('fa', 'fb');
719+
-- composite type columns can't directly be accessed (error)
720+
SELECT d.a FROM (SELECT compositetable AS d FROM compositetable) s;
721+
ERROR: missing FROM-clause entry for table "d"
722+
LINE 1: SELECT d.a FROM (SELECT compositetable AS d FROM compositeta...
723+
^
724+
-- but can be accessed with proper parens
725+
SELECT (d).a, (d).b FROM (SELECT compositetable AS d FROM compositetable) s;
726+
a | b
727+
----+----
728+
fa | fb
729+
(1 row)
730+
731+
-- oids can't be accessed in composite types (error)
732+
SELECT (d).oid FROM (SELECT compositetable AS d FROM compositetable) s;
733+
ERROR: column "oid" not found in data type compositetable
734+
LINE 1: SELECT (d).oid FROM (SELECT compositetable AS d FROM composi...
735+
^
736+
-- accessing non-existing column in NULL datum errors out
737+
SELECT (NULL::compositetable).nonexistant;
738+
ERROR: column "nonexistant" not found in data type compositetable
739+
LINE 1: SELECT (NULL::compositetable).nonexistant;
740+
^
741+
-- existing column in a NULL composite yield NULL
742+
SELECT (NULL::compositetable).a;
743+
a
744+
---
745+
746+
(1 row)
747+
748+
-- oids can't be accessed in composite types (error)
749+
SELECT (NULL::compositetable).oid;
750+
ERROR: column "oid" not found in data type compositetable
751+
LINE 1: SELECT (NULL::compositetable).oid;
752+
^
753+
DROP TABLE compositetable;

src/test/regress/parallel_schedule

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ test: point lseg line box path polygon circle date time timetz timestamp timesta
3030
# geometry depends on point, lseg, box, path, polygon and circle
3131
# horology depends on interval, timetz, timestamp, timestamptz, reltime and abstime
3232
# ----------
33-
test: geometry horology regex oidjoins type_sanity opr_sanity
33+
test: geometry horology regex oidjoins type_sanity opr_sanity expressions
3434

3535
# ----------
3636
# These four each depend on the previous one

src/test/regress/serial_schedule

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ test: regex
4949
test: oidjoins
5050
test: type_sanity
5151
test: opr_sanity
52+
test: expressions
5253
test: insert
5354
test: insert_conflict
5455
test: create_function_1

0 commit comments

Comments
 (0)