pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedSet-Returning functions in a select list
2+ messages / 2 participants
[nested] [flat]
* Set-Returning functions in a select list
@ 2021-07-24 15:52 J Lumby <johnlumby@hotmail.com>
2021-07-24 16:03 ` Re: Set-Returning functions in a select list Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 2+ messages in thread
From: J Lumby @ 2021-07-24 15:52 UTC (permalink / raw)
To: pgsql-general
I am unclear about the rules which postgresql uses in building a result
set when the select list ( see note ) contains several different
Set-Returning functions, possibly with sets of different sizes.
At a very high level, what I see happening is that each Set-Returning
expression causes the result set to be expanded with additional rows,
one per member of the set.
When there is only one such expression, this is clear - the size
of the final result set is N times the size of the output of the final
operator where N is the size of the set (i.e. N times the size if that
expression had been a scalar).
But it becomes unclear where there is more than one such expression.
I can't find much on this point in the doc (I am looking in PostgreSQL
13.2 Documentation). There is one small example in
"9.25. Set Returning Functions"
but in this example there is only one Set-Returning expression in the
select list.
Also the description of the SELECT command mentions something called a
"row group" :
"The actual output rows are computed using the SELECT output expressions
for each selected row or row group. (See SELECT List below.)"
but no mention of Set-Returning expressions in the referenced section.
I tried an experiment :
<code>
echo -e "DROP SCHEMA testing cascade;
CREATE SCHEMA testing;
CREATE TABLE testing.set_returning_in_select_list (id int4 , bitmask_a
int , bitmask_b int );
insert into testing.set_returning_in_select_list values ( 1 ,
x'1010'::int , x'0101'::int ) , ( 2 , x'2020'::int , x'0202'::int ) , (
3 , x'3030'::int , x'0303'::int );
select id , to_hex(bitmask_a) as hex_a , unnest( array [ ( bitmask_a &
16 ) , ( bitmask_a & 32 ) , ( bitmask_a & 48 ) ] ) as bits_of_a ,
to_hex(bitmask_b) as hex_b from
testing.set_returning_in_select_list order by bits_of_a;
select id , to_hex(bitmask_a) as hex_a , unnest( array [ ( bitmask_a &
16 ) , ( bitmask_a & 32 ) , ( bitmask_a & 48 ) ] ) as bits_of_a ,
to_hex(bitmask_b) as hex_b , unnest( array [ ( bitmask_b
& 1 ) , ( bitmask_b & 2 ) , ( bitmask_b & 3 ) ] ) as bits_of_b
from testing.set_returning_in_select_list order by bits_of_a;
select id , to_hex(bitmask_a) as hex_a , unnest( array [ ( bitmask_a &
16 ) , ( bitmask_a & 32 ) , ( bitmask_a & 48 ) ] ) as bits_of_a ,
to_hex(bitmask_b) as hex_b , unnest( array [ ( bitmask_b
& 3 ) , ( bitmask_b & 2 ) , ( bitmask_b & 1 ) ] ) as bits_of_b ,
unnest( array [ ( bitmask_b & 3 ) , ( bitmask_b & 2 ) ,
( bitmask_b & 1 ) , ( bitmask_b & 17 ) ] ) as extraneous_bits_of_b
from testing.set_returning_in_select_list order by bits_of_a;" | psql -e
</code>
The output of the last query is, firstly, different on postgresql-9.6
than on 13.3, and secondly, hard to explain in either case.
my outputs appended below.
I *think* :
. postgresql-9.6 applies a kind of triple cartesian join on the
elements of each set, joining one set with the other set and then again
with itself, ending up with many duplicates
. postgresql-13.3 applies a kind of null-padding extension of the
elements of the smaller set and then an arbitrary linear pairing of the
two sets, ending up with one duplicate.
Which one, if any, is correct? And why?
(note : I am not asking about the case of Set-Returning functions in the
FROM clause, where things seem clear(er).)
Cheers, John Lumby
My outputs of the final query :
. postgresql-9.6
<code>
id | hex_a | bits_of_a | hex_b | bits_of_b | extraneous_bits_of_b
----+-------+-----------+-------+-----------+----------------------
1 | 1010 | 0 | 101 | 0 | 0
1 | 1010 | 0 | 101 | 0 | 1
1 | 1010 | 0 | 101 | 0 | 1
1 | 1010 | 0 | 101 | 0 | 1
2 | 2020 | 0 | 202 | 2 | 2
2 | 2020 | 0 | 202 | 2 | 2
2 | 2020 | 0 | 202 | 2 | 0
2 | 2020 | 0 | 202 | 2 | 0
1 | 1010 | 16 | 101 | 1 | 1
1 | 1010 | 16 | 101 | 1 | 1
1 | 1010 | 16 | 101 | 1 | 1
1 | 1010 | 16 | 101 | 1 | 0
1 | 1010 | 16 | 101 | 1 | 1
1 | 1010 | 16 | 101 | 1 | 1
1 | 1010 | 16 | 101 | 1 | 0
1 | 1010 | 16 | 101 | 1 | 1
3 | 3030 | 16 | 303 | 3 | 3
3 | 3030 | 16 | 303 | 3 | 1
3 | 3030 | 16 | 303 | 3 | 1
3 | 3030 | 16 | 303 | 3 | 2
2 | 2020 | 32 | 202 | 2 | 0
2 | 2020 | 32 | 202 | 2 | 2
2 | 2020 | 32 | 202 | 2 | 0
2 | 2020 | 32 | 202 | 0 | 0
2 | 2020 | 32 | 202 | 2 | 2
2 | 2020 | 32 | 202 | 0 | 2
2 | 2020 | 32 | 202 | 0 | 0
2 | 2020 | 32 | 202 | 0 | 2
3 | 3030 | 32 | 303 | 2 | 2
3 | 3030 | 32 | 303 | 2 | 3
3 | 3030 | 32 | 303 | 2 | 1
3 | 3030 | 32 | 303 | 2 | 1
3 | 3030 | 48 | 303 | 1 | 3
3 | 3030 | 48 | 303 | 1 | 2
3 | 3030 | 48 | 303 | 1 | 1
3 | 3030 | 48 | 303 | 1 | 1
(36 rows)
</code>
. postgresql-13.3
<code>
id | hex_a | bits_of_a | hex_b | bits_of_b | extraneous_bits_of_b
----+-------+-----------+-------+-----------+----------------------
1 | 1010 | 0 | 101 | 0 | 0
2 | 2020 | 0 | 202 | 2 | 2
1 | 1010 | 16 | 101 | 1 | 1
1 | 1010 | 16 | 101 | 1 | 1
3 | 3030 | 16 | 303 | 3 | 3
2 | 2020 | 32 | 202 | 0 | 0
2 | 2020 | 32 | 202 | 2 | 2
3 | 3030 | 32 | 303 | 2 | 2
3 | 3030 | 48 | 303 | 1 | 1
1 | 1010 | | 101 | | 1
3 | 3030 | | 303 | | 1
2 | 2020 | | 202 | | 0
(12 rows)
</code>
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: Set-Returning functions in a select list
2021-07-24 15:52 Set-Returning functions in a select list J Lumby <johnlumby@hotmail.com>
@ 2021-07-24 16:03 ` Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 2+ messages in thread
From: Tom Lane @ 2021-07-24 16:03 UTC (permalink / raw)
To: J Lumby <johnlumby@hotmail.com>; +Cc: pgsql-general
J Lumby <johnlumby@hotmail.com> <DM5PR06MB3082AEC288F546F7AF5393E3A3E69@DM5PR06MB3082.namprd06.prod.outlook.com> writes:
> I am unclear about the rules which postgresql uses in building a result
> set when the select list ( see note ) contains several different
> Set-Returning functions, possibly with sets of different sizes.
The docs you want exist, but they're a bit buried ... see
https://www.postgresql.org/docs/current/xfunc-sql.html#XFUNC-SQL-FUNCTIONS-RETURNING-SET
In particular, the NOTE at the bottom of that section explains that
the behavior changed in v10.
regards, tom lane
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2021-07-24 16:03 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-07-24 15:52 Set-Returning functions in a select list J Lumby <johnlumby@hotmail.com>
2021-07-24 16:03 ` Tom Lane <tgl@sss.pgh.pa.us>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox