Skip to content

Commit

Permalink
parser: sub select support union
Browse files Browse the repository at this point in the history
  • Loading branch information
siddontang committed Oct 7, 2015
1 parent 67864a4 commit 4cc7351
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
20 changes: 14 additions & 6 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -2687,10 +2687,6 @@ QuickOptional:
$$ = true
}

semiOpt:
/* EMPTY */
| ';'


/***************************Prepared Statement Start******************************
* See: https://dev.mysql.com/doc/refman/5.7/en/prepare.html
Expand Down Expand Up @@ -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 ')'
{
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4cc7351

Please sign in to comment.