postgresql - SQL multiple UNNEST in single select list -
i implementing query system. implemented unnest function. user asking using multiple unnest in single select statement. using postgresql kind of guideline since users using before our query system.
postgresql has such strange behavior:
postgres=# select unnest(array[1,2]), unnest(array[1,2]); unnest | unnest --------+-------- 1 | 1 2 | 2 (2 rows) postgres=# select unnest(array[1,2]), unnest(array[1,2,3]); unnest | unnest --------+-------- 1 | 1 2 | 2 1 | 3 2 | 1 1 | 2 2 | 3 (6 rows)
my implementation generate cartesian product. i'm wondering, what's correct logic behind this? postgresql doing right thing or bug? didn't find clear description in ansi document or postgresql document.
this isn't unnest such, postgresql's weird handling of multiple set-returning functions in select
list. set-returning functions in select
aren't part of ansi sql standard.
you find behaviour saner lateral
queries, should preferred on using a set-returning function in from
as possible:
select a, b unnest(array[1,2]) a, lateral unnest(array[1,2,3]) b;
e.g.
regress=> select a, b unnest(array[1,2]) a, lateral unnest(array[1,2,3]) b; | b ---+--- 1 | 1 1 | 2 1 | 3 2 | 1 2 | 2 2 | 3 (6 rows)
the time still use multiple set-returning functions in select
when want pair values functions both return same number of rows. need go away in 9.4, multi-argument unnest
, support with ordinality
.
Comments
Post a Comment