diff --git a/parser/parser.y b/parser/parser.y index 453af2a888b77..debc71a2bf482 100644 --- a/parser/parser.y +++ b/parser/parser.y @@ -464,6 +464,7 @@ import ( SubSelect "Sub Select" Symbol "Constraint Symbol" SystemVariable "System defined variable name" + TableAsOpt "table as option" TableConstraint "table constraint definition" TableElement "table definition element" TableElementList "table definition element list" @@ -1536,15 +1537,22 @@ Field1: } AsOpt: - identifier + Identifier { - // TODO: check potential bug $$ = $1 } | "AS" Identifier { $$ = $2 } +| stringLit + { + $$ = $1 + } +| "AS" stringLit + { + $$ = $2 + } FieldList: Field @@ -2911,11 +2919,11 @@ TableFactor: { $$ = &rsets.TableSource{Source: $1, Name: $2.(string)} } -| '(' SelectStmt ')' AsOpt +| '(' SelectStmt ')' TableAsOpt { $$ = &rsets.TableSource{Source: $2, Name: $4.(string)} } -| '(' UnionStmt ')' AsOpt +| '(' UnionStmt ')' TableAsOpt { $$ = &rsets.TableSource{Source: $2, Name: $4.(string)} } @@ -2928,11 +2936,20 @@ TableIdentOpt: { $$ = "" } -| AsOpt +| TableAsOpt { $$ = $1 } +TableAsOpt: + Identifier + { + $$ = $1 + } +| "AS" Identifier + { + $$ = $2 + } JoinTable: /* Use %prec to evaluate production TableRef before cross join */ diff --git a/parser/parser_test.go b/parser/parser_test.go index 4006b5dad068d..ec9bc5b8d01c7 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -496,6 +496,16 @@ func (s *testParserSuite) TestParser0(c *C) { // For check clause {"CREATE TABLE Customer (SD integer CHECK (SD > 0), First_Name varchar(30));", true}, + + // For as + {"select 1 as a, 1 as `a`, 1 as \"a\", 1 as 'a'", true}, + {`select 1 as a, 1 as "a", 1 as 'a'`, true}, + {`select 1 a, 1 "a", 1 'a'`, true}, + {`select * from t as "a"`, false}, + {`select * from t a`, true}, + {`select * from t as a`, true}, + {"select 1 full, 1 row, 1 abs", true}, + {"select * from t full, t1 row, t2 abs", true}, } for _, t := range table {