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

plan :fix the compatibility problem of UNION statement #6370

Merged
merged 5 commits into from
Apr 25, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 10 additions & 1 deletion executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -947,8 +947,17 @@ func (s *testSuite) TestUnion(c *C) {
tk.MustExec("create table t(a int)")
tk.MustExec("insert into t value(0),(0)")
tk.MustQuery("select 1 from (select a from t union all select a from t) tmp").Check(testkit.Rows("1", "1", "1", "1"))
tk.MustQuery("select 1 from (select a from t limit 1 union all select a from t limit 1) tmp").Check(testkit.Rows("1", "1"))
tk.MustQuery("select count(1) from (select a from t union all select a from t) tmp").Check(testkit.Rows("4"))

_, err := tk.Exec("select 1 from (select a from t limit 1 union all select a from t limit 1) tmp")
c.Assert(err, NotNil)
terr := errors.Trace(err).(*errors.Err).Cause().(*terror.Error)
c.Assert(terr.Code(), Equals, terror.ErrCode(mysql.ErrWrongUsage))

_, err = tk.Exec("select 1 from (select a from t order by a union all select a from t limit 1) tmp")
c.Assert(err, NotNil)
terr = errors.Trace(err).(*errors.Err).Cause().(*terror.Error)
c.Assert(terr.Code(), Equals, terror.ErrCode(mysql.ErrWrongUsage))
}

func (s *testSuite) TestIn(c *C) {
Expand Down
35 changes: 35 additions & 0 deletions plan/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2018 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package plan

import (
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/terror"
)

var (
// ErrWrongUsage is returned when SQL operators are not properly used.
ErrWrongUsage = terror.ClassParser.New(codeWrongUsage, mysql.MySQLErrName[mysql.ErrWrongUsage])
)

const (
codeWrongUsage = terror.ErrCode(mysql.ErrWrongUsage)
)

func init() {
typesMySQLErrCodes := map[terror.ErrCode]uint16{
codeWrongUsage: mysql.ErrWrongUsage,
}
terror.ErrClassToMySQLCodes[terror.ClassParser] = typesMySQLErrCodes
}
14 changes: 14 additions & 0 deletions plan/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func (p *preprocessor) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
p.checkDropDatabaseGrammar(node)
case *ast.ShowStmt:
p.resolveShowStmt(node)
case *ast.UnionSelectList:
p.checkUnionSelectList(node)
case *ast.DeleteTableList:
return in, true
}
Expand Down Expand Up @@ -205,6 +207,18 @@ func (p *preprocessor) checkAutoIncrement(stmt *ast.CreateTableStmt) {
}
}

func (p *preprocessor) checkUnionSelectList(stmt *ast.UnionSelectList) {
for idx, sel := range stmt.Selects {
if idx != len(stmt.Selects)-1 {
if sel.OrderBy != nil {
p.err = ErrWrongUsage.GenByArgs("UNION", "ORDER BY")
} else if sel.Limit != nil {
p.err = ErrWrongUsage.GenByArgs("UNION", "LIMIT")
}
}
}
}

func (p *preprocessor) checkCreateDatabaseGrammar(stmt *ast.CreateDatabaseStmt) {
if isIncorrectName(stmt.Name) {
p.err = ddl.ErrWrongDBName.GenByArgs(stmt.Name)
Expand Down