Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: SubSelect can be used as select statement #350

Merged
merged 4 commits into from
Oct 13, 2015
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -3437,6 +3437,12 @@ Statement:
| UnionStmt
| UpdateStmt
| UseStmt
| SubSelect
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here may be `'(' SelectStmt ')', not SubSelect, in MySQL, SubSelect is not the same as select.
And you can see the MySQL parser that it uses select too, not sub select.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to add '(' SelectStmt ')' into SelectStmt rule, but I got unresolveable confliction.

{
// `(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
Expand Down
3 changes: 3 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 8 additions & 0 deletions tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,14 @@ func trimSQL(sql string) string {
}
break
}
// Trim leading '('. For `(select 1);` is also a query.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If trim '(', any need to trim ')' suffix?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, We judge if the statement is a query only by the prefix

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then sql will be select 1);
Seems like an invalid sql?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only used to check if sql is query in interpreter. And the trimed sql text will not be used outside this function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for {
s := strings.TrimPrefix(sql, "(")
if len(s) == len(sql) {
break
}
sql = s
}
return sql
}

Expand Down
1 change: 1 addition & 0 deletions tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down