Re: Alias of VALUES RTE in explain plan

2025-01-06 Thread Tom Lane
BTW, it suddenly strikes me that if anyone actually is using this syntax in the field, they're most likely doing it like this: regression=# create view v as regression-# select * from (values (1),(2),(3) order by 1) v(x); CREATE VIEW which nicely sidesteps the question of what column aliases appl

Re: Alias of VALUES RTE in explain plan

2025-01-06 Thread Tom Lane
Robert Haas writes: > To be honest, that pushdown feels really uncomfortable to me. To me, > the natural syntax for associating an alias with a VALUES clause would > be something like "VALUES (...) myalias" or, if you also wanted column > aliasing, "VALUES (...) myalias(a,b,c)". That would feel ju

Re: Alias of VALUES RTE in explain plan

2025-01-06 Thread Robert Haas
On Mon, Jan 6, 2025 at 3:45 PM Tom Lane wrote: > SELECT ... FROM (VALUES (...) ORDER BY a) v(a,b,c) > > If it'd been like that all along, nobody would blink at it I think, > even though you could argue that it's action-at-a-distance to let > an outer alias affect what happens inside the im

Re: Alias of VALUES RTE in explain plan

2025-01-06 Thread Tom Lane
Robert Haas writes: > On Thu, Jan 2, 2025 at 4:41 PM Tom Lane wrote: >> (Or we could decide to simplify >> things at the cost of breaking such SQL code, since there probably >> is none in the field. It's still not clear to me which choice is >> better.) > This part I don't understand. Sorry, n

Re: Alias of VALUES RTE in explain plan

2025-01-06 Thread Robert Haas
On Thu, Jan 2, 2025 at 4:41 PM Tom Lane wrote: > I'm not seeing where there's a correctness issue here? The parser > is charged with assigning aliases to RTEs that lack one, and with > this patch that's still true. It's just assigning a different alias > than it did before. There is no question

Re: Alias of VALUES RTE in explain plan

2025-01-02 Thread Tom Lane
Robert Haas writes: > On Sat, Nov 2, 2024 at 1:09 PM Tom Lane wrote: >> It seems sufficient to avoid alias pushdown when there's an ORDER BY >> inside the VALUES subquery. We disallow a locking clause, and >> while there can be LIMIT/OFFSET, those aren't allowed to reference the >> VALUES output

Re: Alias of VALUES RTE in explain plan

2025-01-02 Thread Robert Haas
On Sat, Nov 2, 2024 at 1:09 PM Tom Lane wrote: > regression=# create view vv as SELECT * FROM (VALUES (4),(2),(3),(1) ORDER BY > t1.x LIMIT 2) AS t1(x); > CREATE VIEW > regression=# \d+ vv > View "public.vv" > Column | Type | Collation | Nullable | Default | Stora

Re: Alias of VALUES RTE in explain plan

2024-11-07 Thread Tom Lane
Just to follow up here --- I put this patch on hold for a few days because I had to work on release notes. Now I'm glad I did, because Robert Haas is pushing a proposal that would change the basis of discussion: https://www.postgresql.org/message-id/flat/CA%2BTgmoYSYmDA2GvanzPMci084n%2BmVucv0bJ0H

Re: Alias of VALUES RTE in explain plan

2024-11-04 Thread Andrei Lepikhov
On 11/3/24 00:09, Tom Lane wrote: Ashutosh Bapat writes: On Tue, Oct 29, 2024 at 10:49 PM Tom Lane wrote: regression=# SELECT x regression-#FROM ( VALUES (4), (2), (3), (1) regression(# ORDER BY t1_1.x regression(# LIMIT 2) t1(x); ERROR: missing FROM-clause entry for t

Re: Alias of VALUES RTE in explain plan

2024-11-02 Thread Tom Lane
Ashutosh Bapat writes: > On Tue, Oct 29, 2024 at 10:49 PM Tom Lane wrote: >> So what I now think >> is that we ought to tweak the patch so that the parent alias is >> pushed down only when the subquery contains just VALUES, no other >> clauses. Per a look at the grammar, ORDER BY, LIMIT, and FOR

Re: Alias of VALUES RTE in explain plan

2024-10-30 Thread Ashutosh Bapat
On Tue, Oct 29, 2024 at 10:49 PM Tom Lane wrote: > > Andrei Lepikhov writes: > > -- New behavior > > EXPLAIN (COSTS OFF, VERBOSE) > > SELECT * FROM (VALUES (4),(2),(3),(1) ORDER BY t1.x LIMIT 2) AS t1(x); > > SELECT * FROM (VALUES (4),(2),(3),(1) ORDER BY t1.x LIMIT 2) AS t1(x); > > After taking

Re: Alias of VALUES RTE in explain plan

2024-10-29 Thread Andrei Lepikhov
On 10/30/24 00:19, Tom Lane wrote: Andrei Lepikhov writes: -- New behavior EXPLAIN (COSTS OFF, VERBOSE) SELECT * FROM (VALUES (4),(2),(3),(1) ORDER BY t1.x LIMIT 2) AS t1(x); SELECT * FROM (VALUES (4),(2),(3),(1) ORDER BY t1.x LIMIT 2) AS t1(x); After taking a closer look at that, yeah it's n

Re: Alias of VALUES RTE in explain plan

2024-10-29 Thread Tom Lane
Andrei Lepikhov writes: > -- New behavior > EXPLAIN (COSTS OFF, VERBOSE) > SELECT * FROM (VALUES (4),(2),(3),(1) ORDER BY t1.x LIMIT 2) AS t1(x); > SELECT * FROM (VALUES (4),(2),(3),(1) ORDER BY t1.x LIMIT 2) AS t1(x); After taking a closer look at that, yeah it's new behavior, and I'm not sure w

Re: Alias of VALUES RTE in explain plan

2024-10-29 Thread Tom Lane
Andrei Lepikhov writes: > Thanks for the detailed explanation. I agree it make sense. Cool, I think we're all agreed then. > Also, after skimming the code, I propose some extra tests: Most of these are covered well enough by existing tests, aren't they? regards, tom lan

Re: Alias of VALUES RTE in explain plan

2024-10-29 Thread Ashutosh Bapat
On Mon, Oct 28, 2024 at 8:46 PM Tom Lane wrote: > > Ashutosh Bapat writes: > > The patch looks good to me, except the name of the new member. > > > CommonTableExpr *p_parent_cte; /* this query's containing CTE */ > > + Alias*p_parent_alias; /* parent's alias for this query */ > > > the two

Re: Alias of VALUES RTE in explain plan

2024-10-29 Thread Andrei Lepikhov
On 10/28/24 22:05, Tom Lane wrote: Andrei Lepikhov writes: My goal is to understand why the implementation follows this pattern. As I see, previously, we had consistent behaviour, according to which we removed the pulling-up subquery's alias as well. And I want to know, is it really the only wa

Re: Alias of VALUES RTE in explain plan

2024-10-28 Thread Yasir
On Mon, Oct 28, 2024 at 8:16 PM Tom Lane wrote: > Ashutosh Bapat writes: > > The patch looks good to me, except the name of the new member. > > > CommonTableExpr *p_parent_cte; /* this query's containing CTE */ > > + Alias*p_parent_alias; /* parent's alias for this query */ > > > the two "

Re: Alias of VALUES RTE in explain plan

2024-10-28 Thread Tom Lane
Ashutosh Bapat writes: > The patch looks good to me, except the name of the new member. > CommonTableExpr *p_parent_cte; /* this query's containing CTE */ > + Alias*p_parent_alias; /* parent's alias for this query */ > the two "parent"s here mean different things and that might lead one >

Re: Alias of VALUES RTE in explain plan

2024-10-28 Thread Tom Lane
Andrei Lepikhov writes: > My goal is to understand why the implementation follows this pattern. As > I see, previously, we had consistent behaviour, according to which we > removed the pulling-up subquery's alias as well. And I want to know, is > it really the only way to break this behavior? M

Re: Alias of VALUES RTE in explain plan

2024-10-28 Thread Andrei Lepikhov
On 28/10/2024 20:19, Ashutosh Bapat wrote: On Mon, Oct 28, 2024 at 10:17 AM Andrei Lepikhov wrote: On 10/28/24 03:15, Yasir wrote: By design of my solution, I was not taking it as a bug. But now, I agree with your opinion. I think the case provided by Ashutosh was initially correct, and noth

Re: Alias of VALUES RTE in explain plan

2024-10-28 Thread Ashutosh Bapat
On Mon, Oct 28, 2024 at 10:17 AM Andrei Lepikhov wrote: > > On 10/28/24 03:15, Yasir wrote: > > By design of my solution, I was not taking it as a bug. But now, I agree > > with your opinion. > I think the case provided by Ashutosh was initially correct, and nothing > needs to change. Look at the

Re: Alias of VALUES RTE in explain plan

2024-10-27 Thread Andrei Lepikhov
On 10/28/24 03:15, Yasir wrote: By design of my solution, I was not taking it as a bug. But now, I agree with your opinion. I think the case provided by Ashutosh was initially correct, and nothing needs to change. Look at the similar case: EXPLAIN SELECT x,y FROM ( SELECT oid,relname FROM pg

Re: Alias of VALUES RTE in explain plan

2024-10-27 Thread Yasir
On Mon, Oct 28, 2024 at 1:07 AM Tom Lane wrote: > Yasir writes: > > On Sat, Oct 26, 2024 at 12:21 AM Tom Lane wrote: > >> I forgot to mention a third problem, which is that reassigning the > >> alias during subquery pullup means it doesn't happen if subquery > >> pullup doesn't happen. > > > Ye

Re: Alias of VALUES RTE in explain plan

2024-10-27 Thread Tom Lane
Yasir writes: > On Sat, Oct 26, 2024 at 12:21 AM Tom Lane wrote: >> I forgot to mention a third problem, which is that reassigning the >> alias during subquery pullup means it doesn't happen if subquery >> pullup doesn't happen. > Yes, that is by design. By design? How can you claim that's not

Re: Alias of VALUES RTE in explain plan

2024-10-27 Thread Yasir
On Sat, Oct 26, 2024 at 12:21 AM Tom Lane wrote: > I wrote: > > However ... I don't like this implementation, not even a little > > bit. > > I forgot to mention a third problem, which is that reassigning the > alias during subquery pullup means it doesn't happen if subquery > pullup doesn't happe

Re: Alias of VALUES RTE in explain plan

2024-10-27 Thread Yasir
On Fri, Oct 25, 2024 at 10:35 PM Tom Lane wrote: > Yasir writes: > > I have fixed the code to produce desired output by adding a few lines in > > pull_up_simple_subquery(). > > Attached patch is divided in 2 files: > > - 001-Fix-Alias-VALUES-RTE.patch contains the actual fix. > > - 002-Fix-Ali

Re: Alias of VALUES RTE in explain plan

2024-10-25 Thread Tom Lane
I wrote: > However ... I don't like this implementation, not even a little > bit. I forgot to mention a third problem, which is that reassigning the alias during subquery pullup means it doesn't happen if subquery pullup doesn't happen. As an example, with your patch: regression=# explain verbos

Re: Alias of VALUES RTE in explain plan

2024-10-25 Thread Tom Lane
Yasir writes: > I have fixed the code to produce desired output by adding a few lines in > pull_up_simple_subquery(). > Attached patch is divided in 2 files: > - 001-Fix-Alias-VALUES-RTE.patch contains the actual fix. > - 002-Fix-Alias-VALUES-RTE.patch contains the expected output changes > agai

Re: Alias of VALUES RTE in explain plan

2024-10-25 Thread Yasir
Hi Ashutosh & PG Hackers, I have fixed the code to produce desired output by adding a few lines in pull_up_simple_subquery(). Attached patch is divided in 2 files: - 001-Fix-Alias-VALUES-RTE.patch contains the actual fix. - 002-Fix-Alias-VALUES-RTE.patch contains the expected output changes agai

Re: Alias of VALUES RTE in explain plan

2024-08-15 Thread Yasir
On Mon, Jul 1, 2024 at 3:17 PM Ashutosh Bapat wrote: > Hi All, > While reviewing Richard's patch for grouping sets, I stumbled upon > following explain output > > explain (costs off) > select distinct on (a, b) a, b > from (values (1, 1), (2, 2)) as t (a, b) where a = b > group by grouping sets((