Andrew Gierth <and...@tao11.riddles.org.uk> writes: > Normally I'd push hard to try and get some solution that's sufficiently > generic to allow user-defined functions to make use of the feature. But > I think the SQL spec people have managed to make that literally > impossible in this case, what with the FROM keyword appearing in the > middle of a production and not followed by anything sufficiently > distinctive to even use for extra token lookahead.
Yeah. Is there any appetite for a "Just Say No" approach? That is, refuse to implement the spec's syntax on the grounds that it's too brain-dead to even consider, and instead provide some less random, more extensibility-friendly way to accomplish the same thing? The FROM FIRST/LAST bit seems particularly badly thought through, because AFAICS it is flat out ambiguous with a normal FROM clause immediately following the window function call. The only way to make it not so would be to make FIRST and LAST be fully reserved, which is neither a good idea nor spec-compliant. In short, there's a really good case to be made here that the SQL committee is completely clueless about syntax design, and so we shouldn't follow this particular pied piper. > ... This patch would make lead / lag / > first_value / last_value / nth_value syntactically "special" while not > actually reserving them (beyond having them in unreserved_keywords); I > think serious consideration should be given to whether they should > instead become col_name_keywords (which would, I believe, make it > unnecessary to mess with precedence). I agree that messing with the precedence rules is likely to have unforeseen and undesirable side-effects. Generally, if you need to create a precedence rule, that's because your grammar is ambiguous. Precedence fixes that in a well-behaved way only for cases that actually are very much like operator precedence rules. Otherwise, you may just be papering over something that isn't working very well. See e.g. commits 670a6c7a2 and 12b716457 for past cases where we learned that the hard way. (The latter also points out that if you must have a precedence hack, it's safer to hack individual rules than to stick precedences onto terminal symbols.) In the case at hand, since the proposed patch doesn't make FIRST and LAST be fully reserved, it seems just about certain that it can be made to misbehave, including failing on queries that were and should remain legal. regards, tom lane