diff --git a/parser/parser.y b/parser/parser.y index 99283183964b9..d45e1e00aff7e 100644 --- a/parser/parser.y +++ b/parser/parser.y @@ -2687,10 +2687,6 @@ QuickOptional: $$ = true } -semiOpt: - /* EMPTY */ -| ';' - /***************************Prepared Statement Start****************************** * See: https://dev.mysql.com/doc/refman/5.7/en/prepare.html @@ -2879,9 +2875,13 @@ TableFactor: { $$ = &rsets.TableSource{Source: $1, Name: $2.(string)} } -| '(' SelectStmt semiOpt ')' AsOpt +| '(' SelectStmt ')' AsOpt { - $$ = &rsets.TableSource{Source: $2, Name: $5.(string)} + $$ = &rsets.TableSource{Source: $2, Name: $4.(string)} + } +| '(' UnionStmt ')' AsOpt + { + $$ = &rsets.TableSource{Source: $2, Name: $4.(string)} } | '(' TableRefs ')' { @@ -3035,6 +3035,14 @@ SubSelect: s.SetText(src[yyS[yypt-1].offset-1:yyS[yypt].offset-1]) $$ = &subquery.SubQuery{Stmt: s} } +| '(' UnionStmt ')' + { + s := $2.(*stmts.UnionStmt) + src := yylex.(*lexer).src + // See the implemention of yyParse function + s.SetText(src[yyS[yypt-1].offset-1:yyS[yypt].offset-1]) + $$ = &subquery.SubQuery{Stmt: s} + } // See: https://dev.mysql.com/doc/refman/5.7/en/innodb-locking-reads.html SelectLockOpt: diff --git a/parser/parser_test.go b/parser/parser_test.go index 7ed6a384094b4..560d0b95a4332 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -483,6 +483,8 @@ func (s *testParserSuite) TestParser0(c *C) { {"(select c1 from t1) union (select c2 from t2) order by c1 union select c3 from t3", false}, {"(select c1 from t1) union (select c2 from t2) limit 1 union select c3 from t3", false}, {"(select c1 from t1) union select c2 from t2 union (select c3 from t3) order by c1 limit 1", true}, + {"select (select 1 union select 1) as a", true}, + {"select * from (select 1 union select 2) as a", true}, } for _, t := range table {