Re: "AS" by the syntax of table reference.(8.4 proposal) - Mailing list pgsql-hackers

From Tom Lane
Subject Re: "AS" by the syntax of table reference.(8.4 proposal)
Date
Msg-id 29049.1202587650@sss.pgh.pa.us
Whole thread Raw
In response to Re: "AS" by the syntax of table reference.(8.4 proposal)  (Gregory Stark <stark@enterprisedb.com>)
Responses Re: "AS" by the syntax of table reference.(8.4 proposal)  (Gregory Stark <stark@enterprisedb.com>)
List pgsql-hackers
Gregory Stark <stark@enterprisedb.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-characterops 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);


pgsql-hackers by date:

Previous
From: Stefan Kaltenbrunner
Date:
Subject: Re: Patch review
Next
From: Gregory Stark
Date:
Subject: Re: "AS" by the syntax of table reference.(8.4 proposal)