2 -- Tests for common table expressions (WITH query, ... SELECT ...)
5 WITH q1(x,y) AS (SELECT 1,2)
6 SELECT * FROM q1, q1 AS q2;
12 -- Multiple uses are evaluated only once
13 SELECT count(*) FROM (
14 WITH q1(x) AS (SELECT random() FROM generate_series(1, 5))
26 WITH RECURSIVE t(n) AS (
29 SELECT n+1 FROM t WHERE n < 100
37 WITH RECURSIVE t(n) AS (
40 SELECT n+1 FROM t WHERE n < 5
52 -- This is an infinite loop with UNION ALL, but not with UNION
53 WITH RECURSIVE t(n) AS (
64 -- This'd be an infinite loop, but outside query reads only as much as needed
65 WITH RECURSIVE t(n) AS (
69 SELECT * FROM t LIMIT 10;
84 -- UNION case should have same property
85 WITH RECURSIVE t(n) AS (
89 SELECT * FROM t LIMIT 10;
104 -- Test behavior with an unknown-type literal in the WITH
105 WITH q AS (SELECT 'foo' AS x)
106 SELECT x, x IS OF (unknown) as is_unknown FROM q;
112 WITH RECURSIVE t(n) AS (
115 SELECT n || ' bar' FROM t WHERE length(n) < 20
117 SELECT n, n IS OF (text) as is_text FROM t;
119 -------------------------+---------
124 foo bar bar bar bar | t
125 foo bar bar bar bar bar | t
129 -- Some examples with a tree
131 -- department structure represented here is as follows:
133 -- ROOT-+->A-+->B-+->C
137 CREATE TEMP TABLE department (
138 id INTEGER PRIMARY KEY, -- department ID
139 parent_department INTEGER REFERENCES department, -- upper department ID
140 name TEXT -- department name
142 NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "department_pkey" for table "department"
143 INSERT INTO department VALUES (0, NULL, 'ROOT');
144 INSERT INTO department VALUES (1, 0, 'A');
145 INSERT INTO department VALUES (2, 1, 'B');
146 INSERT INTO department VALUES (3, 2, 'C');
147 INSERT INTO department VALUES (4, 2, 'D');
148 INSERT INTO department VALUES (5, 0, 'E');
149 INSERT INTO department VALUES (6, 4, 'F');
150 INSERT INTO department VALUES (7, 5, 'G');
151 -- extract all departments under 'A'. Result should be A, B, C, D and F
152 WITH RECURSIVE subdepartment AS
154 -- non recursive term
155 SELECT name as root_name, * FROM department WHERE name = 'A'
158 SELECT sd.root_name, d.* FROM department AS d, subdepartment AS sd
159 WHERE d.parent_department = sd.id
161 SELECT * FROM subdepartment ORDER BY name;
162 root_name | id | parent_department | name
163 -----------+----+-------------------+------
171 -- extract all departments under 'A' with "level" number
172 WITH RECURSIVE subdepartment(level, id, parent_department, name) AS
174 -- non recursive term
175 SELECT 1, * FROM department WHERE name = 'A'
178 SELECT sd.level + 1, d.* FROM department AS d, subdepartment AS sd
179 WHERE d.parent_department = sd.id
181 SELECT * FROM subdepartment ORDER BY name;
182 level | id | parent_department | name
183 -------+----+-------------------+------
191 -- extract all departments under 'A' with "level" number.
192 -- Only shows level 2 or more
193 WITH RECURSIVE subdepartment(level, id, parent_department, name) AS
195 -- non recursive term
196 SELECT 1, * FROM department WHERE name = 'A'
199 SELECT sd.level + 1, d.* FROM department AS d, subdepartment AS sd
200 WHERE d.parent_department = sd.id
202 SELECT * FROM subdepartment WHERE level >= 2 ORDER BY name;
203 level | id | parent_department | name
204 -------+----+-------------------+------
211 -- "RECURSIVE" is ignored if the query has no self-reference
212 WITH RECURSIVE subdepartment AS
214 -- note lack of recursive UNION structure
215 SELECT * FROM department WHERE name = 'A'
217 SELECT * FROM subdepartment ORDER BY name;
218 id | parent_department | name
219 ----+-------------------+------
224 SELECT count(*) FROM (
225 WITH RECURSIVE t(n) AS (
226 SELECT 1 UNION ALL SELECT n + 1 FROM t WHERE n < 500
228 SELECT * FROM t) AS t WHERE n < (
229 SELECT count(*) FROM (
230 WITH RECURSIVE t(n) AS (
231 SELECT 1 UNION ALL SELECT n + 1 FROM t WHERE n < 100
233 SELECT * FROM t WHERE n < 50000
234 ) AS t WHERE n < 100);
240 -- use same CTE twice at different subquery levels
242 SELECT hundred, sum(ten) FROM tenk1 GROUP BY hundred
244 SELECT count(*) FROM q1 WHERE y > (SELECT sum(y)/100 FROM q1 qsub);
251 CREATE TEMPORARY VIEW vsubdepartment AS
252 WITH RECURSIVE subdepartment AS
254 -- non recursive term
255 SELECT * FROM department WHERE name = 'A'
258 SELECT d.* FROM department AS d, subdepartment AS sd
259 WHERE d.parent_department = sd.id
261 SELECT * FROM subdepartment;
262 SELECT * FROM vsubdepartment ORDER BY name;
263 id | parent_department | name
264 ----+-------------------+------
272 -- Check reverse listing
273 SELECT pg_get_viewdef('vsubdepartment'::regclass);
275 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
276 WITH RECURSIVE subdepartment AS (SELECT department.id, department.parent_department, department.name FROM department WHERE (department.name = 'A'::text) UNION ALL SELECT d.id, d.parent_department, d.name FROM department d, subdepartment sd WHERE (d.parent_department = sd.id)) SELECT subdepartment.id, subdepartment.parent_department, subdepartment.name FROM subdepartment;
279 SELECT pg_get_viewdef('vsubdepartment'::regclass, true);
281 --------------------------------------------------------------------------------------
282 WITH RECURSIVE subdepartment AS (
283 SELECT department.id, department.parent_department, department.name
285 WHERE department.name = 'A'::text
287 SELECT d.id, d.parent_department, d.name
288 FROM department d, subdepartment sd
289 WHERE d.parent_department = sd.id
291 SELECT subdepartment.id, subdepartment.parent_department, subdepartment.name
295 -- corner case in which sub-WITH gets initialized first
296 with recursive q as (
297 select * from department
299 (with x as (select * from q)
302 select * from q limit 24;
303 id | parent_department | name
304 ----+-------------------+------
331 with recursive q as (
332 select * from department
334 (with recursive x as (
335 select * from department
337 (select * from q union all select * from x)
341 select * from q limit 32;
342 id | parent_department | name
343 ----+-------------------+------
378 -- recursive term has sub-UNION
379 WITH RECURSIVE t(i,j) AS (
382 SELECT t2.i, t.j+1 FROM
383 (SELECT 2 AS i UNION ALL SELECT 3 AS i) AS t2
384 JOIN t ON (t2.i = t.i+1))
394 -- different tree example
396 CREATE TEMPORARY TABLE tree(
397 id INTEGER PRIMARY KEY,
398 parent_id INTEGER REFERENCES tree(id)
400 NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "tree_pkey" for table "tree"
402 VALUES (1, NULL), (2, 1), (3,1), (4,2), (5,2), (6,2), (7,3), (8,3),
403 (9,4), (10,4), (11,7), (12,7), (13,7), (14, 9), (15,11), (16,11);
405 -- get all paths from "second level" nodes to leaf nodes
407 WITH RECURSIVE t(id, path) AS (
408 VALUES(1,ARRAY[]::integer[])
410 SELECT tree.id, t.path || tree.id
411 FROM tree JOIN t ON (tree.parent_id = t.id)
413 SELECT t1.*, t2.* FROM t AS t1 JOIN t AS t2 ON
414 (t1.path[1] = t2.path[1] AND
415 array_upper(t1.path,1) = 1 AND
416 array_upper(t2.path,1) > 1)
417 ORDER BY t1.id, t2.id;
418 id | path | id | path
419 ----+------+----+-------------
423 2 | {2} | 9 | {2,4,9}
424 2 | {2} | 10 | {2,4,10}
425 2 | {2} | 14 | {2,4,9,14}
428 3 | {3} | 11 | {3,7,11}
429 3 | {3} | 12 | {3,7,12}
430 3 | {3} | 13 | {3,7,13}
431 3 | {3} | 15 | {3,7,11,15}
432 3 | {3} | 16 | {3,7,11,16}
436 WITH RECURSIVE t(id, path) AS (
437 VALUES(1,ARRAY[]::integer[])
439 SELECT tree.id, t.path || tree.id
440 FROM tree JOIN t ON (tree.parent_id = t.id)
442 SELECT t1.id, count(t2.*) FROM t AS t1 JOIN t AS t2 ON
443 (t1.path[1] = t2.path[1] AND
444 array_upper(t1.path,1) = 1 AND
445 array_upper(t2.path,1) > 1)
455 -- test cycle detection
457 create temp table graph( f int, t int, label text );
458 insert into graph values
459 (1, 2, 'arc 1 -> 2'),
460 (1, 3, 'arc 1 -> 3'),
461 (2, 3, 'arc 2 -> 3'),
462 (1, 4, 'arc 1 -> 4'),
463 (4, 5, 'arc 4 -> 5'),
464 (5, 1, 'arc 5 -> 1');
465 with recursive search_graph(f, t, label, path, cycle) as (
466 select *, array[row(g.f, g.t)], false from graph g
468 select g.*, path || row(g.f, g.t), row(g.f, g.t) = any(path)
469 from graph g, search_graph sg
470 where g.f = sg.t and not cycle
472 select * from search_graph;
473 f | t | label | path | cycle
474 ---+---+------------+-------------------------------------------+-------
475 1 | 2 | arc 1 -> 2 | {"(1,2)"} | f
476 1 | 3 | arc 1 -> 3 | {"(1,3)"} | f
477 2 | 3 | arc 2 -> 3 | {"(2,3)"} | f
478 1 | 4 | arc 1 -> 4 | {"(1,4)"} | f
479 4 | 5 | arc 4 -> 5 | {"(4,5)"} | f
480 5 | 1 | arc 5 -> 1 | {"(5,1)"} | f
481 1 | 2 | arc 1 -> 2 | {"(5,1)","(1,2)"} | f
482 1 | 3 | arc 1 -> 3 | {"(5,1)","(1,3)"} | f
483 1 | 4 | arc 1 -> 4 | {"(5,1)","(1,4)"} | f
484 2 | 3 | arc 2 -> 3 | {"(1,2)","(2,3)"} | f
485 4 | 5 | arc 4 -> 5 | {"(1,4)","(4,5)"} | f
486 5 | 1 | arc 5 -> 1 | {"(4,5)","(5,1)"} | f
487 1 | 2 | arc 1 -> 2 | {"(4,5)","(5,1)","(1,2)"} | f
488 1 | 3 | arc 1 -> 3 | {"(4,5)","(5,1)","(1,3)"} | f
489 1 | 4 | arc 1 -> 4 | {"(4,5)","(5,1)","(1,4)"} | f
490 2 | 3 | arc 2 -> 3 | {"(5,1)","(1,2)","(2,3)"} | f
491 4 | 5 | arc 4 -> 5 | {"(5,1)","(1,4)","(4,5)"} | f
492 5 | 1 | arc 5 -> 1 | {"(1,4)","(4,5)","(5,1)"} | f
493 1 | 2 | arc 1 -> 2 | {"(1,4)","(4,5)","(5,1)","(1,2)"} | f
494 1 | 3 | arc 1 -> 3 | {"(1,4)","(4,5)","(5,1)","(1,3)"} | f
495 1 | 4 | arc 1 -> 4 | {"(1,4)","(4,5)","(5,1)","(1,4)"} | t
496 2 | 3 | arc 2 -> 3 | {"(4,5)","(5,1)","(1,2)","(2,3)"} | f
497 4 | 5 | arc 4 -> 5 | {"(4,5)","(5,1)","(1,4)","(4,5)"} | t
498 5 | 1 | arc 5 -> 1 | {"(5,1)","(1,4)","(4,5)","(5,1)"} | t
499 2 | 3 | arc 2 -> 3 | {"(1,4)","(4,5)","(5,1)","(1,2)","(2,3)"} | f
502 -- ordering by the path column has same effect as SEARCH DEPTH FIRST
503 with recursive search_graph(f, t, label, path, cycle) as (
504 select *, array[row(g.f, g.t)], false from graph g
506 select g.*, path || row(g.f, g.t), row(g.f, g.t) = any(path)
507 from graph g, search_graph sg
508 where g.f = sg.t and not cycle
510 select * from search_graph order by path;
511 f | t | label | path | cycle
512 ---+---+------------+-------------------------------------------+-------
513 1 | 2 | arc 1 -> 2 | {"(1,2)"} | f
514 2 | 3 | arc 2 -> 3 | {"(1,2)","(2,3)"} | f
515 1 | 3 | arc 1 -> 3 | {"(1,3)"} | f
516 1 | 4 | arc 1 -> 4 | {"(1,4)"} | f
517 4 | 5 | arc 4 -> 5 | {"(1,4)","(4,5)"} | f
518 5 | 1 | arc 5 -> 1 | {"(1,4)","(4,5)","(5,1)"} | f
519 1 | 2 | arc 1 -> 2 | {"(1,4)","(4,5)","(5,1)","(1,2)"} | f
520 2 | 3 | arc 2 -> 3 | {"(1,4)","(4,5)","(5,1)","(1,2)","(2,3)"} | f
521 1 | 3 | arc 1 -> 3 | {"(1,4)","(4,5)","(5,1)","(1,3)"} | f
522 1 | 4 | arc 1 -> 4 | {"(1,4)","(4,5)","(5,1)","(1,4)"} | t
523 2 | 3 | arc 2 -> 3 | {"(2,3)"} | f
524 4 | 5 | arc 4 -> 5 | {"(4,5)"} | f
525 5 | 1 | arc 5 -> 1 | {"(4,5)","(5,1)"} | f
526 1 | 2 | arc 1 -> 2 | {"(4,5)","(5,1)","(1,2)"} | f
527 2 | 3 | arc 2 -> 3 | {"(4,5)","(5,1)","(1,2)","(2,3)"} | f
528 1 | 3 | arc 1 -> 3 | {"(4,5)","(5,1)","(1,3)"} | f
529 1 | 4 | arc 1 -> 4 | {"(4,5)","(5,1)","(1,4)"} | f
530 4 | 5 | arc 4 -> 5 | {"(4,5)","(5,1)","(1,4)","(4,5)"} | t
531 5 | 1 | arc 5 -> 1 | {"(5,1)"} | f
532 1 | 2 | arc 1 -> 2 | {"(5,1)","(1,2)"} | f
533 2 | 3 | arc 2 -> 3 | {"(5,1)","(1,2)","(2,3)"} | f
534 1 | 3 | arc 1 -> 3 | {"(5,1)","(1,3)"} | f
535 1 | 4 | arc 1 -> 4 | {"(5,1)","(1,4)"} | f
536 4 | 5 | arc 4 -> 5 | {"(5,1)","(1,4)","(4,5)"} | f
537 5 | 1 | arc 5 -> 1 | {"(5,1)","(1,4)","(4,5)","(5,1)"} | t
541 -- test multiple WITH queries
544 y (id) AS (VALUES (1)),
545 x (id) AS (SELECT * FROM y UNION ALL SELECT id+1 FROM x WHERE id < 5)
556 -- forward reference OK
558 x(id) AS (SELECT * FROM y UNION ALL SELECT id+1 FROM x WHERE id < 5),
559 y(id) AS (values (1))
572 (VALUES (1) UNION ALL SELECT id+1 FROM x WHERE id < 5),
574 (VALUES (1) UNION ALL SELECT id+1 FROM y WHERE id < 10)
575 SELECT y.*, x.* FROM y LEFT JOIN x USING (id);
592 (VALUES (1) UNION ALL SELECT id+1 FROM x WHERE id < 5),
594 (VALUES (1) UNION ALL SELECT id+1 FROM x WHERE id < 10)
595 SELECT y.*, x.* FROM y LEFT JOIN x USING (id);
608 (SELECT 1 UNION ALL SELECT id+1 FROM x WHERE id < 3 ),
610 (SELECT * FROM x UNION ALL SELECT * FROM x),
612 (SELECT * FROM x UNION ALL SELECT id+1 FROM z WHERE id < 10)
647 (SELECT 1 UNION ALL SELECT id+1 FROM x WHERE id < 3 ),
649 (SELECT * FROM x UNION ALL SELECT * FROM x),
651 (SELECT * FROM y UNION ALL SELECT id+1 FROM z WHERE id < 10)
715 WITH RECURSIVE x(n) AS (SELECT 1 INTERSECT SELECT n+1 FROM x)
717 ERROR: recursive query "x" does not have the form non-recursive-term UNION [ALL] recursive-term
718 LINE 1: WITH RECURSIVE x(n) AS (SELECT 1 INTERSECT SELECT n+1 FROM x...
720 WITH RECURSIVE x(n) AS (SELECT 1 INTERSECT ALL SELECT n+1 FROM x)
722 ERROR: recursive query "x" does not have the form non-recursive-term UNION [ALL] recursive-term
723 LINE 1: WITH RECURSIVE x(n) AS (SELECT 1 INTERSECT ALL SELECT n+1 FR...
726 WITH RECURSIVE x(n) AS (SELECT 1 EXCEPT SELECT n+1 FROM x)
728 ERROR: recursive query "x" does not have the form non-recursive-term UNION [ALL] recursive-term
729 LINE 1: WITH RECURSIVE x(n) AS (SELECT 1 EXCEPT SELECT n+1 FROM x)
731 WITH RECURSIVE x(n) AS (SELECT 1 EXCEPT ALL SELECT n+1 FROM x)
733 ERROR: recursive query "x" does not have the form non-recursive-term UNION [ALL] recursive-term
734 LINE 1: WITH RECURSIVE x(n) AS (SELECT 1 EXCEPT ALL SELECT n+1 FROM ...
736 -- no non-recursive term
737 WITH RECURSIVE x(n) AS (SELECT n FROM x)
739 ERROR: recursive query "x" does not have the form non-recursive-term UNION [ALL] recursive-term
740 LINE 1: WITH RECURSIVE x(n) AS (SELECT n FROM x)
742 -- recursive term in the left hand side (strictly speaking, should allow this)
743 WITH RECURSIVE x(n) AS (SELECT n FROM x UNION ALL SELECT 1)
745 ERROR: recursive reference to query "x" must not appear within its non-recursive term
746 LINE 1: WITH RECURSIVE x(n) AS (SELECT n FROM x UNION ALL SELECT 1)
748 CREATE TEMPORARY TABLE y (a INTEGER);
749 INSERT INTO y SELECT generate_series(1, 10);
751 WITH RECURSIVE x(n) AS (SELECT a FROM y WHERE a = 1
753 SELECT x.n+1 FROM y LEFT JOIN x ON x.n = y.a WHERE n < 10)
755 ERROR: recursive reference to query "x" must not appear within an outer join
756 LINE 3: SELECT x.n+1 FROM y LEFT JOIN x ON x.n = y.a WHERE n < 10)
759 WITH RECURSIVE x(n) AS (SELECT a FROM y WHERE a = 1
761 SELECT x.n+1 FROM x RIGHT JOIN y ON x.n = y.a WHERE n < 10)
763 ERROR: recursive reference to query "x" must not appear within an outer join
764 LINE 3: SELECT x.n+1 FROM x RIGHT JOIN y ON x.n = y.a WHERE n < 10)
767 WITH RECURSIVE x(n) AS (SELECT a FROM y WHERE a = 1
769 SELECT x.n+1 FROM x FULL JOIN y ON x.n = y.a WHERE n < 10)
771 ERROR: recursive reference to query "x" must not appear within an outer join
772 LINE 3: SELECT x.n+1 FROM x FULL JOIN y ON x.n = y.a WHERE n < 10)
775 WITH RECURSIVE x(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM x
776 WHERE n IN (SELECT * FROM x))
778 ERROR: recursive reference to query "x" must not appear within a subquery
779 LINE 2: WHERE n IN (SELECT * FROM x))
781 -- aggregate functions
782 WITH RECURSIVE x(n) AS (SELECT 1 UNION ALL SELECT count(*) FROM x)
784 ERROR: aggregates not allowed in a recursive query's recursive term
785 LINE 1: WITH RECURSIVE x(n) AS (SELECT 1 UNION ALL SELECT count(*) F...
787 WITH RECURSIVE x(n) AS (SELECT 1 UNION ALL SELECT sum(n) FROM x)
789 ERROR: aggregates not allowed in a recursive query's recursive term
790 LINE 1: WITH RECURSIVE x(n) AS (SELECT 1 UNION ALL SELECT sum(n) FRO...
793 WITH RECURSIVE x(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM x ORDER BY 1)
795 ERROR: ORDER BY in a recursive query is not implemented
796 LINE 1: ...VE x(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM x ORDER BY 1)
799 WITH RECURSIVE x(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM x LIMIT 10 OFFSET 1)
801 ERROR: OFFSET in a recursive query is not implemented
802 LINE 1: ... AS (SELECT 1 UNION ALL SELECT n+1 FROM x LIMIT 10 OFFSET 1)
805 WITH RECURSIVE x(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM x FOR UPDATE)
807 ERROR: FOR UPDATE/SHARE in a recursive query is not implemented
808 -- target list has a recursive query name
809 WITH RECURSIVE x(id) AS (values (1)
811 SELECT (SELECT * FROM x) FROM x WHERE id < 5
813 ERROR: recursive reference to query "x" must not appear within a subquery
814 LINE 3: SELECT (SELECT * FROM x) FROM x WHERE id < 5
816 -- mutual recursive query (not implemented)
818 x (id) AS (SELECT 1 UNION ALL SELECT id+1 FROM y WHERE id < 5),
819 y (id) AS (SELECT 1 UNION ALL SELECT id+1 FROM x WHERE id < 5)
821 ERROR: mutual recursion between WITH items is not implemented
822 LINE 2: x (id) AS (SELECT 1 UNION ALL SELECT id+1 FROM y WHERE id ...
824 -- non-linear recursion is not allowed
825 WITH RECURSIVE foo(i) AS
828 (SELECT i+1 FROM foo WHERE i < 10
830 SELECT i+1 FROM foo WHERE i < 5)
832 ERROR: recursive reference to query "foo" must not appear more than once
833 LINE 6: SELECT i+1 FROM foo WHERE i < 5)
835 WITH RECURSIVE foo(i) AS
839 (SELECT i+1 FROM foo WHERE i < 10
841 SELECT i+1 FROM foo WHERE i < 5) AS t
843 ERROR: recursive reference to query "foo" must not appear more than once
844 LINE 7: SELECT i+1 FROM foo WHERE i < 5) AS t
846 WITH RECURSIVE foo(i) AS
849 (SELECT i+1 FROM foo WHERE i < 10
851 SELECT i+1 FROM foo WHERE i < 5)
853 ERROR: recursive reference to query "foo" must not appear within EXCEPT
854 LINE 6: SELECT i+1 FROM foo WHERE i < 5)
856 WITH RECURSIVE foo(i) AS
859 (SELECT i+1 FROM foo WHERE i < 10
861 SELECT i+1 FROM foo WHERE i < 5)
863 ERROR: recursive reference to query "foo" must not appear more than once
864 LINE 6: SELECT i+1 FROM foo WHERE i < 5)
866 -- Wrong type induced from non-recursive term
867 WITH RECURSIVE foo(i) AS
868 (SELECT i FROM (VALUES(1),(2)) t(i)
870 SELECT (i+1)::numeric(10,0) FROM foo WHERE i < 10)
872 ERROR: recursive query "foo" column 1 has type integer in non-recursive term but type numeric overall
873 LINE 2: (SELECT i FROM (VALUES(1),(2)) t(i)
875 HINT: Cast the output of the non-recursive term to the correct type.
876 -- rejects different typmod, too (should we allow this?)
877 WITH RECURSIVE foo(i) AS
878 (SELECT i::numeric(3,0) FROM (VALUES(1),(2)) t(i)
880 SELECT (i+1)::numeric(10,0) FROM foo WHERE i < 10)
882 ERROR: recursive query "foo" column 1 has type numeric(3,0) in non-recursive term but type numeric overall
883 LINE 2: (SELECT i::numeric(3,0) FROM (VALUES(1),(2)) t(i)
885 HINT: Cast the output of the non-recursive term to the correct type.