LATERAL, UNNEST and spec compliance
LATERAL, UNNEST and spec compliance
От:
David Fetter <david@fetter.org>
Дата:
Folks, Andrew Gierth asked me to send this out as his email is in a parlous state at the moment. My comments will follow in replies. Without further ado: SQL2008 says, for 7.6
Re: LATERAL, UNNEST and spec compliance
On Thu, Jan 24, 2013 at 09:51:46AM -0800, David Fetter wrote:
> Folks,
>
> Andrew Gierth asked me to send this out as his email is in a parlous
> state at the moment. My comments will follow in replies. Without
> further ado:
> [snip]
>
> As I see it, the current options are:
>
> 1. Do nothing, and insist on non-standard use of the LATERAL keyword.
>
> 2. Add UNNEST to the grammar (or parse analysis) as a special case, making
> it implicitly LATERAL.
>
> (This would make implementing S301 easier, but special cases are ugly.)
>
> 3. Make all cases of SRFs in the FROM-clause implicitly LATERAL.
>
> (As far as I can tell, those cases whose behaviour would be changed by
> this actually produce errors in versions prior to 9.3, so no working
> code should be affected.)
>
> Since LATERAL is new in 9.3, I think the pros and cons of these choices
> should be considered now, rather than being allowed to slide by unexamined.
Please find attached a patch which implements approach 3. The vast
majority of it is changes to the regression tests. The removed
regression tests in join.{sql,out} are no longer errors, although some
of them are pretty standard DoS attacks, hence they're all removed.
Cheers,
David.
--
David Fetter http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david.fetter@gmail.com
iCal: webcal://www.tripit.com/feed/ical/people/david74/tripit.ics
Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate
Re: LATERAL, UNNEST and spec compliance
On Thu, Jan 24, 2013 at 09:12:41PM -0800, David Fetter wrote:
> On Thu, Jan 24, 2013 at 09:51:46AM -0800, David Fetter wrote:
> > Folks,
> >
> > Andrew Gierth asked me to send this out as his email is in a parlous
> > state at the moment. My comments will follow in replies. Without
> > further ado:
> > [snip]
> >
> > As I see it, the current options are:
> >
> > 1. Do nothing, and insist on non-standard use of the LATERAL keyword.
> >
> > 2. Add UNNEST to the grammar (or parse analysis) as a special case, making
> > it implicitly LATERAL.
> >
> > (This would make implementing S301 easier, but special cases are ugly.)
> >
> > 3. Make all cases of SRFs in the FROM-clause implicitly LATERAL.
> >
> > (As far as I can tell, those cases whose behaviour would be changed by
> > this actually produce errors in versions prior to 9.3, so no working
> > code should be affected.)
> >
> > Since LATERAL is new in 9.3, I think the pros and cons of these choices
> > should be considered now, rather than being allowed to slide by unexamined.
>
> Please find attached a patch which implements approach 3. The vast
> majority of it is changes to the regression tests. The removed
> regression tests in join.{sql,out} are no longer errors, although some
> of them are pretty standard DoS attacks, hence they're all removed.
>
> Cheers,
> David.
Oops. Misspelled rtekind in the previous patch. Here's a corrected
one, much shorter.
Cheers,
David.
--
David Fetter http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david.fetter@gmail.com
iCal: webcal://www.tripit.com/feed/ical/people/david74/tripit.ics
Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate
Re: LATERAL, UNNEST and spec compliance
От:
Stephen Frost <sfrost@snowman.net>
Дата:
* David Fetter (david@fetter.org) wrote: > As I see it, the current options are: > > 1. Do nothing, and insist on non-standard use of the LATERAL keyword. I'm not a big fan of this. Providing a good error message saying "you need to use LATERAL for this query to work" makes it slightly better, but I don't feel like there's really any ambiguity here. > 2. Add UNNEST to the grammar (or parse analysis) as a special case, making > it implicitly LATERAL. > > (This would make implementing S301 easier, but special cases are ugly.) This I really don't like. > 3. Make all cases of SRFs in the FROM-clause implicitly LATERAL. > > (As far as I can tell, those cases whose behaviour would be changed by > this actually produce errors in versions prior to 9.3, so no working > code should be affected.) +1 for me on this idea. If you're calling an SRF, passing in a lateral value, 'LATERAL' seems like it's just a noise word, and apparently the SQL authors felt the same, as they don't require it for unnest(). > Since LATERAL is new in 9.3, I think the pros and cons of these choices > should be considered now, rather than being allowed to slide by unexamined. I agree that we should really hammer this down before 9.3 is out the door. Thanks, Stephen
Re: LATERAL, UNNEST and spec compliance
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Stephen Frost writes: > * David Fetter (david@fetter.org) wrote: >> 3. Make all cases of SRFs in the FROM-clause implicitly LATERAL. >> >> (As far as I can tell, those cases whose behaviour would be changed by >> this actually produce errors in versions prior to 9.3, so no working >> code should be affected.) > +1 for me on this idea. If you're calling an SRF, passing in a lateral > value, 'LATERAL' seems like it's just a noise word, and apparently the > SQL authors felt the same, as they don't require it for unnest(). At first I didn't like this idea, but it's growing on me. However ... David is wrong to claim that it's zero-risk. It's true that an SRF can't contain any side-references today, but it can contain an outer reference. Consider a case like SELECT ... FROM a WHERE a.x IN (SELECT ... FROM b, srf(y) WHERE ...) In existing releases the "y" could be a valid outer reference to a.y. If b also has a column y, David's proposal would cause us to prefer that interpretation, since b.y would be more closely nested than a.y. If you're lucky, you'd get a type-mismatch error, but if the two y's are of similar datatypes the query would just silently do something different than it used to. This is a little bit far-fetched, but it could happen. As against that, we make incompatible changes in every release, and it does seem like assuming LATERAL for functions in FROM would be a usability gain most of the time. And special-casing UNNEST to satisfy the standard seems *really* ugly. > I agree that we should really hammer this down before 9.3 is out the > door. Yeah, if we're going to do this it'd make the most sense to do it in the same release that introduces LATERAL. regards, tom lane
Re: LATERAL, UNNEST and spec compliance
От:
Stephen Frost <sfrost@snowman.net>
Дата:
* Tom Lane (tgl@sss.pgh.pa.us) wrote: > However ... David is wrong to claim that it's zero-risk. It's true that > an SRF can't contain any side-references today, but it can contain an > outer reference. Consider a case like > > SELECT ... FROM a WHERE a.x IN (SELECT ... FROM b, srf(y) WHERE ...) I see what you mean, but on the other hand, that looks like something we might actually want to complain about as 'y' is pretty clearly ambiguous here. I'm a bit surprised that doesn't already throw an error. > This is a little bit far-fetched, but it could happen. As against that, > we make incompatible changes in every release, and it does seem like > assuming LATERAL for functions in FROM would be a usability gain most > of the time. And special-casing UNNEST to satisfy the standard seems > *really* ugly. It's definitely far-fetched, imv. If it's possible, within reason, to explicitly throw a "please disambiguate 'y'" type of error in those specific cases, that'd be nice, but I don't think it'd be required. A mention in the release notes would be sufficient. Thanks, Stephen
Re: LATERAL, UNNEST and spec compliance
От:
Stephen Frost <sfrost@snowman.net>
Дата:
* Tom Lane (tgl@sss.pgh.pa.us) wrote: > SELECT ... FROM a WHERE a.x IN (SELECT ... FROM b, srf(y) WHERE ...) Actually, this appears to fail already, at least in 9.2.2: => select * from (values (1)) v(a) where v.a in (select x from (values (2)) v2(a), -> generate_series(1,a) x); ERROR: function expression in FROM cannot refer to other relations of same query level LINE 2: generate_series(1,a) x); ^ Unless it's something else that you were referring to...? Thanks, Stephen
Re: LATERAL, UNNEST and spec compliance
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Stephen Frost writes: > * Tom Lane (tgl@sss.pgh.pa.us) wrote: >> SELECT ... FROM a WHERE a.x IN (SELECT ... FROM b, srf(y) WHERE ...) > Actually, this appears to fail already, at least in 9.2.2: > => select * from (values (1)) v(a) where v.a in (select x from (values (2)) v2(a), > -> generate_series(1,a) x); > ERROR: function expression in FROM cannot refer to other relations of same query level > LINE 2: generate_series(1,a) x); > ^ Huh ... you're right, I'd forgotten about that. That's an ancient bug that got fixed in passing in the LATERAL work. So, as long as we're not going to fix that bug in the back branches (which would be difficult anyway IIRC), we don't have a compatibility problem ... regards, tom lane
Re: LATERAL, UNNEST and spec compliance
David Fetter writes:
> Please find attached a patch which implements approach 3. The vast
> majority of it is changes to the regression tests. The removed
> regression tests in join.{sql,out} are no longer errors, although some
> of them are pretty standard DoS attacks, hence they're all removed.
Here's a less quick-hack-y approach to that.
regards, tom lane