Selecting fields from a RowExpr

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Selecting fields from a RowExpr
Date: 2019-10-27 18:46:46
Message-ID: 10872.1572202006@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

At pgconf.eu, someone whose name I've forgotten pointed out to me
that this doesn't work:

regression=# select (row(1, 2.0)).f1;
ERROR: could not identify column "f1" in record data type
LINE 1: select (row(1, 2.0)).f1;
^

The fields of an anonymous rowtype are certainly named f1, f2, etc,
so it seems like this *should* work. A related case is

regression=# select (row(1, 2.0)).*;
ERROR: record type has not been registered

Admittedly, these probably aren't terribly useful cases in practice,
but it's unfortunate that they don't work as one would expect.
So I propose the attached patch to make them work.

The underlying reason for both of these failures is that RowExpr
doesn't carry a typmod, so if it's of type RECORD then
get_expr_result_type doesn't know how to find a tupdesc for it.
The minimum-code solution is to teach get_expr_result_type to build
a tupdesc directly from the RowExpr, and that seems to be necessary
for complicated cases like

select (r).f1 from (select row(1, 2.0) as r) ss;

In an earlier version of the patch I chose to add in some fast-path
logic in ParseComplexProjection and ExpandRowReference, so as to
make the really simple cases shown above a bit less inefficient.
But on second thought, these are such corner cases that it doesn't
seem worth carrying extra code for them. The cases that are more
likely to arise in practice are like that last example, and we
can't optimize that in the parser. (The planner will optimize
FieldSelect-from-RowExpr after flattening subqueries, which is
probably as much as we really need to do here.)

I don't feel a need to back-patch this, but I would like to push
it into HEAD.

Thoughts?

regards, tom lane

Attachment Content-Type Size
allow-selecting-from-RowExprs-in-more-cases.patch text/x-diff 3.3 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Pavel Stehule 2019-10-27 18:59:07 Re: Selecting fields from a RowExpr
Previous Message David Fetter 2019-10-27 18:07:20 Re: [Proposal] Arbitrary queries in postgres_fdw