diff --git a/parser/parser.y b/parser/parser.y index 5ade6f6ee0d53..7a0d237cb0494 100644 --- a/parser/parser.y +++ b/parser/parser.y @@ -3481,6 +3481,12 @@ Statement: | UnionStmt | UpdateStmt | UseStmt +| SubSelect + { + // `(select 1)`; is a valid select statement + // TODO: This is used to fix issue #320. There may be a better solution. + $$ = $1.(*subquery.SubQuery).Stmt + } ExplainableStmt: SelectStmt diff --git a/parser/parser_test.go b/parser/parser_test.go index 61b48a4e60ae0..1c1c5191d77e8 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -282,6 +282,9 @@ func (s *testParserSuite) TestDMLStmt(c *C) { // For show create table {"show create table test.t", true}, {"show create table t", true}, + + // For https://github.com/pingcap/tidb/issues/320 + {`(select 1);`, true}, } s.RunTest(c, table) } diff --git a/tidb.go b/tidb.go index b9e8f7555baaf..f78a8bbcd0048 100644 --- a/tidb.go +++ b/tidb.go @@ -257,7 +257,8 @@ func trimSQL(sql string) string { } break } - return sql + // Trim leading '('. For `(select 1);` is also a query. + return strings.TrimLeft(sql, "( ") } // IsQuery checks if a sql statement is a query statement. diff --git a/tidb_test.go b/tidb_test.go index 19a469f16b7a3..b14b5fdbad525 100644 --- a/tidb_test.go +++ b/tidb_test.go @@ -300,6 +300,7 @@ func (s *testMainSuite) TestIsQuery(c *C) { {"/*comment*/ select 1;", true}, {"/*comment*/ /*comment*/ select 1;", true}, {"select /*comment*/ 1 /*comment*/;", true}, + {"(select /*comment*/ 1 /*comment*/);", true}, } for _, t := range tbl { c.Assert(IsQuery(t.sql), Equals, t.ok, Commentf(t.sql))