Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g4DWS-0006Bu-49 for pgsql-hackers@arkaria.postgresql.org; Sun, 23 Sep 2018 23:10:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g4DWP-0006q5-RC for pgsql-hackers@arkaria.postgresql.org; Sun, 23 Sep 2018 23:10:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g4DWP-0006py-JY for pgsql-hackers@lists.postgresql.org; Sun, 23 Sep 2018 23:10:37 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g4DWJ-00077U-7C for pgsql-hackers@postgresql.org; Sun, 23 Sep 2018 23:10:37 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w8NNAPx8009365; Sun, 23 Sep 2018 19:10:25 -0400 From: Tom Lane To: Andrew Gierth cc: pgsql-hackers@postgresql.org, david@fetter.org, Oliver Ford , Krasiyan Andreev Subject: Re: Add RESPECT/IGNORE NULLS and FROM FIRST/LAST options In-reply-to: <87va6vhoey.fsf@news-spur.riddles.org.uk> References: <20180728185958.GP17411@fetter.org> <878t3vj279.fsf@news-spur.riddles.org.uk> <2897.1537737232@sss.pgh.pa.us> <87va6vhoey.fsf@news-spur.riddles.org.uk> Comments: In-reply-to Andrew Gierth message dated "Sun, 23 Sep 2018 23:22:30 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <9360.1537744225.1@sss.pgh.pa.us> Date: Sun, 23 Sep 2018 19:10:25 -0400 Message-ID: <9361.1537744225@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Andrew Gierth writes: > "Tom" == Tom Lane writes: > Tom> The FROM FIRST/LAST bit seems particularly badly thought through, > Tom> because AFAICS it is flat out ambiguous with a normal FROM clause > Tom> immediately following the window function call. The only way to > Tom> make it not so would be to make FIRST and LAST be fully reserved, > Tom> which is neither a good idea nor spec-compliant. > In the actual spec syntax it's not ambiguous at all because NTH_VALUE is > a reserved word (as are LEAD, LAG, FIRST_VALUE and LAST_VALUE), and OVER > is a mandatory clause in its syntax, so a FROM appearing before the OVER > must be part of a FROM FIRST/LAST and not introducing a FROM-clause. Hmm ... > In our syntax, if we made NTH_VALUE etc. a col_name_keyword (and thus > not legal as a function name outside its own special syntax) it would > also become unambiguous. > i.e. given this token sequence (with . marking the current posision): > select nth_value(x) . from first ignore > if we know up front that "nth_value" is a window function and not any > other kind of function, we know that we have to shift the "from" rather > than reducing the select-list because we haven't seen an "over" yet. I don't really find that to be a desirable solution, because quite aside from the extensibility problem, it would mean that a lot of errors become "syntax error" where we formerly gave a more useful message. This does open up a thought about how to proceed, though. I'd been trying to think of a way to solve this using base_yylex's ability to do some internal lookahead and change token types based on that. If you just think of recognizing FROM FIRST/LAST, you get nowhere because that's still legal in other contexts. But if you were to look for FROM followed by FIRST/LAST followed by IGNORE/RESPECT/OVER, I think that could only validly happen in this syntax. It'd take some work to extend base_yylex to look ahead 2 tokens not one, but I'm sure that could be done. (You'd also need a lookahead rule to match "IGNORE/RESPECT NULLS OVER", but that seems just as doable.) Then the relevant productions use FROM_LA, IGNORE_LA, RESPECT_LA instead of the corresponding bare tokens, and the grammar no longer has an ambiguity problem. regards, tom lane