1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-28 18:48:04 +03:00

Allow most keywords to be used as column labels without requiring AS.

Up to now, if you tried to omit "AS" before a column label in a SELECT
list, it would only work if the column label was an IDENT, that is not
any known keyword.  This is rather unfriendly considering that we have
so many keywords and are constantly growing more.  In the wake of commit
1ed6b8956 it's possible to improve matters quite a bit.

We'd originally tried to make this work by having some of the existing
keyword categories be allowed without AS, but that didn't work too well,
because each category contains a few special cases that don't work
without AS.  Instead, invent an entirely orthogonal keyword property
"can be bare column label", and mark all keywords that way for which
we don't get shift/reduce errors by doing so.

It turns out that of our 450 current keywords, all but 39 can be made
bare column labels, improving the situation by over 90%.  This number
might move around a little depending on future grammar work, but it's
a pretty nice improvement.

Mark Dilger, based on work by myself and Robert Haas;
review by John Naylor

Discussion: https://postgr.es/m/38ca86db-42ab-9b48-2902-337a0d6b8311@2ndquadrant.com
This commit is contained in:
Tom Lane
2020-09-18 16:46:26 -04:00
parent 0811f766fd
commit 06a7c3154f
14 changed files with 1055 additions and 520 deletions

View File

@@ -1496,21 +1496,25 @@ SELECT a AS value, b + c AS sum FROM ...
</para>
<para>
The <literal>AS</literal> keyword is optional, but only if the new column
name does not match any
<productname>PostgreSQL</productname> keyword (see <xref
linkend="sql-keywords-appendix"/>). To avoid an accidental match to
a keyword, you can double-quote the column name. For example,
<literal>VALUE</literal> is a keyword, so this does not work:
The <literal>AS</literal> key word is usually optional, but in some
cases where the desired column name matches a
<productname>PostgreSQL</productname> key word, you must write
<literal>AS</literal> or double-quote the column name in order to
avoid ambiguity.
(<xref linkend="sql-keywords-appendix"/> shows which key words
require <literal>AS</literal> to be used as a column label.)
For example, <literal>FROM</literal> is one such key word, so this
does not work:
<programlisting>
SELECT a value, b + c AS sum FROM ...
SELECT a from, b + c AS sum FROM ...
</programlisting>
but this does:
but either of these do:
<programlisting>
SELECT a "value", b + c AS sum FROM ...
SELECT a AS from, b + c AS sum FROM ...
SELECT a "from", b + c AS sum FROM ...
</programlisting>
For protection against possible
future keyword additions, it is recommended that you always either
For greatest safety against possible
future key word additions, it is recommended that you always either
write <literal>AS</literal> or double-quote the output column name.
</para>