Re: "AS" by the syntax of table reference.(8.4 proposal)

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Gregory Stark <stark(at)enterprisedb(dot)com>
Cc: "Hiroshi Saito" <z-saito(at)guitar(dot)ocn(dot)ne(dot)jp>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: "AS" by the syntax of table reference.(8.4 proposal)
Date: 2008-02-09 20:07:30
Message-ID: 29049.1202587650@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Gregory Stark <stark(at)enterprisedb(dot)com> writes:
> Sure, just like a + + b is ambiguous. We define an arbitrary choice and tell
> people to put parentheses if they want the other. It's not too hard to write
> SELECT (a +) b, ...
> if you want an alias. Besides, nobody uses postfix expressions anyways. It
> would be a pain if it worked the other way and you had to write (a + b) all
> the time.

Hm, well, now that you mention it we already have provisions to
discriminate against the postfix-op case when things are ambiguous.
So really this is a precedence problem, which leads to the attached
proposal for a patch. This still has the problem of only allowing
IDENT for AS-less column labels, but at least it avoids restricting
the expression.

regards, tom lane

Index: gram.y
===================================================================
RCS file: /cvsroot/pgsql/src/backend/parser/gram.y,v
retrieving revision 2.606
diff -c -r2.606 gram.y
*** gram.y 7 Feb 2008 21:07:55 -0000 2.606
--- gram.y 9 Feb 2008 20:01:36 -0000
***************
*** 477,482 ****
--- 477,483 ----
%nonassoc BETWEEN
%nonassoc IN_P
%left POSTFIXOP /* dummy for postfix Op rules */
+ %nonassoc IDENT /* to support target_el without AS */
%left Op OPERATOR /* multi-character ops and user-defined operators */
%nonassoc NOTNULL
%nonassoc ISNULL
***************
*** 8705,8711 ****
| target_list ',' target_el { $$ = lappend($1, $3); }
;

- /* AS is not optional because shift/red conflict with unary ops */
target_el: a_expr AS ColLabel
{
$$ = makeNode(ResTarget);
--- 8706,8711 ----
***************
*** 8714,8719 ****
--- 8714,8735 ----
$$->val = (Node *)$1;
$$->location = @1;
}
+ /*
+ * We support omitting AS only for column labels that aren't
+ * any known keyword. There is an ambiguity against postfix
+ * operators: is "a ! b" an infix expression, or a postfix
+ * expression and a column label? We prefer to resolve this
+ * as an infix expression, which we accomplish by assigning
+ * IDENT a precedence higher than POSTFIXOP.
+ */
+ | a_expr IDENT
+ {
+ $$ = makeNode(ResTarget);
+ $$->name = $2;
+ $$->indirection = NIL;
+ $$->val = (Node *)$1;
+ $$->location = @1;
+ }
| a_expr
{
$$ = makeNode(ResTarget);

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Gregory Stark 2008-02-09 20:50:09 Re: "AS" by the syntax of table reference.(8.4 proposal)
Previous Message Stefan Kaltenbrunner 2008-02-09 19:41:39 Re: Patch review