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 1g4Cm6-0002qs-P3 for pgsql-hackers@arkaria.postgresql.org; Sun, 23 Sep 2018 22:22:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g4Cm2-0003pF-6s for pgsql-hackers@arkaria.postgresql.org; Sun, 23 Sep 2018 22:22:42 +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 1g4Cm1-0003o9-Vy for pgsql-hackers@lists.postgresql.org; Sun, 23 Sep 2018 22:22:42 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g4Clz-0004vs-8R for pgsql-hackers@postgresql.org; Sun, 23 Sep 2018 22:22:41 +0000 Received: from [192.168.127.1] (port=44175 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 1g4Clr-000CfD-Su; Sun, 23 Sep 2018 22:22:31 +0000 Received: from [127.0.0.1] (port=51086 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1g4Clq-000Ilu-Vq; Sun, 23 Sep 2018 22:22:31 +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: <2897.1537737232@sss.pgh.pa.us> (Tom Lane's message of "Sun, 23 Sep 2018 17:13:52 -0400") Message-ID: <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> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Sun, 23 Sep 2018 23:22:30 +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> 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. 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. (Neither "first" nor "ignore" are reserved, so "select foo(x) from first ignore;" is a valid and complete query, and without reserving the function name we'd need at least four tokens of lookahead to decide otherwise.) This is why I think the col_name_keyword option needs to be given serious consideration - it still doesn't reserve the names as strongly as the spec does, but enough to make the standard syntax work without needing any dubious hacks. -- Andrew (irc:RhodiumToad)