Re: "SELECT ... FROM DUAL" is not quite as silly as it appears

From: Ashutosh Bapat <ashutosh(dot)bapat(at)enterprisedb(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: "SELECT ... FROM DUAL" is not quite as silly as it appears
Date: 2018-03-16 08:39:28
Message-ID: CAFjFpRdHJbM2EgDng2NgMLkXWD2bYrKhZPchsFj8r4PcVW2E5g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, Mar 15, 2018 at 8:57 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> We've long made fun of Oracle(TM) for the fact that if you just want
> to evaluate some expressions, you have to write "select ... from dual"
> rather than just "select ...". But I've realized recently that there's
> a bit of method in that madness after all. Specifically, having to
> cope with FromExprs that contain no base relation is fairly problematic
> in the planner. prepjointree.c is basically unable to cope with
> flattening a subquery that looks that way, although we've inserted a
> lot of overly-baroque logic to handle some subsets of the case (cf
> is_simple_subquery(), around line 1500). If memory serves, there are
> other places that are complicated by the case.
>
> Suppose that, either in the rewriter or early in the planner, we were
> to replace such cases with nonempty FromExprs, by adding a dummy RTE
> representing a table with no columns and one row. This would in turn
> give rise to an ordinary Path that converts to a Result plan, so that
> the case is handled without any special contortions later. Then there
> is no case where we don't have a nonempty relids set identifying a
> subquery, so that all that special-case hackery in prepjointree.c
> goes away, and we can simplify whatever else is having a hard time
> with it.
>

The idea looks neat.

Since table in the dummy FROM clause returns one row without any
column, I guess, there will be at least one row in the output. I am
curious how would we handle cases which do not return any row
like

create function set_ret_func() returns setof record as $$select * from
pg_class where oid = 0;$$ language sql;
select set_ret_func();
set_ret_func
--------------
(0 rows)

--
Best Wishes,
Ashutosh Bapat
EnterpriseDB Corporation
The Postgres Database Company

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Pavel Stehule 2018-03-16 08:40:18 Re: pg_get_functiondef forgets about most GUC_LIST_INPUT GUCs
Previous Message Kyotaro HORIGUCHI 2018-03-16 08:34:33 Re: pg_get_functiondef forgets about most GUC_LIST_INPUT GUCs