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 1g4E5a-0000R5-Jv for pgsql-hackers@arkaria.postgresql.org; Sun, 23 Sep 2018 23:46:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g4E5X-0003na-Pn for pgsql-hackers@arkaria.postgresql.org; Sun, 23 Sep 2018 23:46:55 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g4E5X-0003nS-FF for pgsql-hackers@lists.postgresql.org; Sun, 23 Sep 2018 23:46:55 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g4E5Q-0006It-1v for pgsql-hackers@postgresql.org; Sun, 23 Sep 2018 23:46:54 +0000 Received: from [192.168.127.1] (port=58961 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1g4E5J-000Cki-IA; Sun, 23 Sep 2018 23:46:41 +0000 Received: from [127.0.0.1] (port=34257 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1g4E5I-000JIX-Ss; Sun, 23 Sep 2018 23:46:41 +0000 From: Andrew Gierth To: Tom Lane 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: <9361.1537744225@sss.pgh.pa.us> (Tom Lane's message of "Sun, 23 Sep 2018 19:10:25 -0400") Message-ID: <87o9cnhjwh.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> <9361.1537744225@sss.pgh.pa.us> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Mon, 24 Sep 2018 00:46:40 +0100 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Tom" == Tom Lane writes: Tom> If you just think of recognizing FROM FIRST/LAST, you get nowhere Tom> because that's still legal in other contexts. But if you were to Tom> look for FROM followed by FIRST/LAST followed by Tom> IGNORE/RESPECT/OVER, I think that could only validly happen in Tom> this syntax. No; you need to go four tokens ahead in total, not three. Assuming nth_value is unreserved, then select nth_value(x) from first ignore; is a valid query that has nth_value(x) as an expression, "first" as a table name and "ignore" as its alias. Only when you see NULLS after IGNORE, or OVER after FIRST/LAST, do you know that you're looking at a window function and not a from clause. So FROM_LA would have to mean "FROM" followed by any of: FIRST IGNORE NULLS LAST IGNORE NULLS FIRST RESPECT NULLS LAST RESPECT NULLS FIRST OVER LAST OVER Remember that while OVER is reserved, all of FIRST, LAST, RESPECT and IGNORE are unreserved. Tom> It'd take some work to extend base_yylex to look ahead 2 tokens Tom> not one, but I'm sure that could be done. (You'd also need a Tom> lookahead rule to match "IGNORE/RESPECT NULLS OVER", but that Tom> seems just as doable.) Then the relevant productions use FROM_LA, Tom> IGNORE_LA, RESPECT_LA instead of the corresponding bare tokens, Tom> and the grammar no longer has an ambiguity problem. Yeah, but at the cost of having to extend base_yylex to go 3 tokens ahead (not 2) rather than the current single lookahead slot. Doable, certainly (probably not much harder to do 3 than 2 actually) -- Andrew (irc:RhodiumToad)