Skip to content

Commit 830d1c7

Browse files
committed
Improve test coverage of ruleutils.c.
While fooling around with the EXPLAIN improvements I've been working on, I noticed that there were some large gaps in our test coverage of ruleutils.c, according to the code coverage report. This commit just adds a few test cases to improve coverage of: get_name_for_var_field() get_update_query_targetlist_def() isSimpleNode() get_sublink_expr()
1 parent 30d4772 commit 830d1c7

File tree

4 files changed

+172
-1
lines changed

4 files changed

+172
-1
lines changed

src/test/regress/expected/create_view.out

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1775,6 +1775,104 @@ select pg_get_ruledef(oid, true) from pg_rewrite
17751775
43 AS col_b;
17761776
(1 row)
17771777

1778+
-- test extraction of FieldSelect field names (get_name_for_var_field)
1779+
create view tt24v as
1780+
with cte as materialized (select r from (values(1,2),(3,4)) r)
1781+
select (r).column2 as col_a, (rr).column2 as col_b from
1782+
cte join (select rr from (values(1,7),(3,8)) rr limit 2) ss
1783+
on (r).column1 = (rr).column1;
1784+
select pg_get_viewdef('tt24v', true);
1785+
pg_get_viewdef
1786+
------------------------------------------------------------
1787+
WITH cte AS MATERIALIZED ( +
1788+
SELECT r.*::record AS r +
1789+
FROM ( VALUES (1,2), (3,4)) r +
1790+
) +
1791+
SELECT (cte.r).column2 AS col_a, +
1792+
(ss.rr).column2 AS col_b +
1793+
FROM cte +
1794+
JOIN ( SELECT rr.*::record AS rr +
1795+
FROM ( VALUES (1,7), (3,8)) rr +
1796+
LIMIT 2) ss ON (cte.r).column1 = (ss.rr).column1;
1797+
(1 row)
1798+
1799+
create view tt25v as
1800+
with cte as materialized (select pg_get_keywords() k)
1801+
select (k).word from cte;
1802+
select pg_get_viewdef('tt25v', true);
1803+
pg_get_viewdef
1804+
----------------------------------------
1805+
WITH cte AS MATERIALIZED ( +
1806+
SELECT pg_get_keywords() AS k+
1807+
) +
1808+
SELECT (cte.k).word AS word +
1809+
FROM cte;
1810+
(1 row)
1811+
1812+
-- also check cases seen only in EXPLAIN
1813+
explain (verbose, costs off)
1814+
select * from tt24v;
1815+
QUERY PLAN
1816+
------------------------------------------------------------------------------------------
1817+
Hash Join
1818+
Output: (cte.r).column2, ((ROW("*VALUES*".column1, "*VALUES*".column2))).column2
1819+
Hash Cond: (((ROW("*VALUES*".column1, "*VALUES*".column2))).column1 = (cte.r).column1)
1820+
CTE cte
1821+
-> Values Scan on "*VALUES*_1"
1822+
Output: ROW("*VALUES*_1".column1, "*VALUES*_1".column2)
1823+
-> Limit
1824+
Output: (ROW("*VALUES*".column1, "*VALUES*".column2))
1825+
-> Values Scan on "*VALUES*"
1826+
Output: ROW("*VALUES*".column1, "*VALUES*".column2)
1827+
-> Hash
1828+
Output: cte.r
1829+
-> CTE Scan on cte
1830+
Output: cte.r
1831+
(14 rows)
1832+
1833+
explain (verbose, costs off)
1834+
select (r).column2 from (select r from (values(1,2),(3,4)) r limit 1) ss;
1835+
QUERY PLAN
1836+
-------------------------------------------------------------------
1837+
Subquery Scan on ss
1838+
Output: (ss.r).column2
1839+
-> Limit
1840+
Output: (ROW("*VALUES*".column1, "*VALUES*".column2))
1841+
-> Values Scan on "*VALUES*"
1842+
Output: ROW("*VALUES*".column1, "*VALUES*".column2)
1843+
(6 rows)
1844+
1845+
-- test pretty-print parenthesization rules, and SubLink deparsing
1846+
create view tt26v as
1847+
select x + y + z as c1,
1848+
(x * y) + z as c2,
1849+
x + (y * z) as c3,
1850+
(x + y) * z as c4,
1851+
x * (y + z) as c5,
1852+
x + (y + z) as c6,
1853+
x + (y # z) as c7,
1854+
(x > y) AND (y > z OR x > z) as c8,
1855+
(x > y) OR (y > z AND NOT (x > z)) as c9,
1856+
(x,y) <> ALL (values(1,2),(3,4)) as c10,
1857+
(x,y) <= ANY (values(1,2),(3,4)) as c11
1858+
from (values(1,2,3)) v(x,y,z);
1859+
select pg_get_viewdef('tt26v', true);
1860+
pg_get_viewdef
1861+
--------------------------------------------------------
1862+
SELECT v.x + v.y + v.z AS c1, +
1863+
v.x * v.y + v.z AS c2, +
1864+
v.x + v.y * v.z AS c3, +
1865+
(v.x + v.y) * v.z AS c4, +
1866+
v.x * (v.y + v.z) AS c5, +
1867+
v.x + (v.y + v.z) AS c6, +
1868+
v.x + (v.y # v.z) AS c7, +
1869+
v.x > v.y AND (v.y > v.z OR v.x > v.z) AS c8, +
1870+
v.x > v.y OR v.y > v.z AND NOT v.x > v.z AS c9, +
1871+
((v.x, v.y) <> ALL ( VALUES (1,2), (3,4))) AS c10,+
1872+
((v.x, v.y) <= ANY ( VALUES (1,2), (3,4))) AS c11 +
1873+
FROM ( VALUES (1,2,3)) v(x, y, z);
1874+
(1 row)
1875+
17781876
-- clean up all the random objects we made above
17791877
DROP SCHEMA temp_view_test CASCADE;
17801878
NOTICE: drop cascades to 27 other objects
@@ -1806,7 +1904,7 @@ drop cascades to view aliased_view_2
18061904
drop cascades to view aliased_view_3
18071905
drop cascades to view aliased_view_4
18081906
DROP SCHEMA testviewschm2 CASCADE;
1809-
NOTICE: drop cascades to 64 other objects
1907+
NOTICE: drop cascades to 67 other objects
18101908
DETAIL: drop cascades to table t1
18111909
drop cascades to view temporal1
18121910
drop cascades to view temporal2
@@ -1871,3 +1969,6 @@ drop cascades to view tt20v
18711969
drop cascades to view tt21v
18721970
drop cascades to view tt22v
18731971
drop cascades to view tt23v
1972+
drop cascades to view tt24v
1973+
drop cascades to view tt25v
1974+
drop cascades to view tt26v

src/test/regress/expected/rules.out

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2974,6 +2974,30 @@ Rules:
29742974
ON UPDATE TO rules_src DO INSTEAD UPDATE rules_log trgt SET tag = 'updated'::text
29752975
WHERE trgt.f1 = new.f1
29762976

2977+
--
2978+
-- Also check multiassignment deparsing.
2979+
--
2980+
create table rule_t1(f1 int, f2 int);
2981+
create table rule_dest(f1 int, f2 int[], tag text);
2982+
create rule rr as on update to rule_t1 do instead UPDATE rule_dest trgt
2983+
SET (f2[1], f1, tag) = (SELECT new.f2, new.f1, 'updated'::varchar)
2984+
WHERE trgt.f1 = new.f1 RETURNING new.*;
2985+
\d+ rule_t1
2986+
Table "public.rule_t1"
2987+
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
2988+
--------+---------+-----------+----------+---------+---------+--------------+-------------
2989+
f1 | integer | | | | plain | |
2990+
f2 | integer | | | | plain | |
2991+
Rules:
2992+
rr AS
2993+
ON UPDATE TO rule_t1 DO INSTEAD UPDATE rule_dest trgt SET (f2[1], f1, tag) = ( SELECT new.f2,
2994+
new.f1,
2995+
'updated'::character varying AS "varchar")
2996+
WHERE trgt.f1 = new.f1
2997+
RETURNING new.f1,
2998+
new.f2
2999+
3000+
drop table rule_t1, rule_dest;
29773001
--
29783002
-- check alter rename rule
29793003
--

src/test/regress/sql/create_view.sql

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,41 @@ select pg_get_viewdef('tt23v', true);
607607
select pg_get_ruledef(oid, true) from pg_rewrite
608608
where ev_class = 'tt23v'::regclass and ev_type = '1';
609609

610+
-- test extraction of FieldSelect field names (get_name_for_var_field)
611+
612+
create view tt24v as
613+
with cte as materialized (select r from (values(1,2),(3,4)) r)
614+
select (r).column2 as col_a, (rr).column2 as col_b from
615+
cte join (select rr from (values(1,7),(3,8)) rr limit 2) ss
616+
on (r).column1 = (rr).column1;
617+
select pg_get_viewdef('tt24v', true);
618+
create view tt25v as
619+
with cte as materialized (select pg_get_keywords() k)
620+
select (k).word from cte;
621+
select pg_get_viewdef('tt25v', true);
622+
-- also check cases seen only in EXPLAIN
623+
explain (verbose, costs off)
624+
select * from tt24v;
625+
explain (verbose, costs off)
626+
select (r).column2 from (select r from (values(1,2),(3,4)) r limit 1) ss;
627+
628+
-- test pretty-print parenthesization rules, and SubLink deparsing
629+
630+
create view tt26v as
631+
select x + y + z as c1,
632+
(x * y) + z as c2,
633+
x + (y * z) as c3,
634+
(x + y) * z as c4,
635+
x * (y + z) as c5,
636+
x + (y + z) as c6,
637+
x + (y # z) as c7,
638+
(x > y) AND (y > z OR x > z) as c8,
639+
(x > y) OR (y > z AND NOT (x > z)) as c9,
640+
(x,y) <> ALL (values(1,2),(3,4)) as c10,
641+
(x,y) <= ANY (values(1,2),(3,4)) as c11
642+
from (values(1,2,3)) v(x,y,z);
643+
select pg_get_viewdef('tt26v', true);
644+
610645
-- clean up all the random objects we made above
611646
DROP SCHEMA temp_view_test CASCADE;
612647
DROP SCHEMA testviewschm2 CASCADE;

src/test/regress/sql/rules.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,17 @@ create rule r4 as on insert to rules_src do instead insert into rules_log AS trg
10141014
create rule r5 as on update to rules_src do instead UPDATE rules_log AS trgt SET tag = 'updated' WHERE trgt.f1 = new.f1;
10151015
\d+ rules_src
10161016

1017+
--
1018+
-- Also check multiassignment deparsing.
1019+
--
1020+
create table rule_t1(f1 int, f2 int);
1021+
create table rule_dest(f1 int, f2 int[], tag text);
1022+
create rule rr as on update to rule_t1 do instead UPDATE rule_dest trgt
1023+
SET (f2[1], f1, tag) = (SELECT new.f2, new.f1, 'updated'::varchar)
1024+
WHERE trgt.f1 = new.f1 RETURNING new.*;
1025+
\d+ rule_t1
1026+
drop table rule_t1, rule_dest;
1027+
10171028
--
10181029
-- check alter rename rule
10191030
--

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy