Skip to content

Commit

Permalink
plan :fix the compatibility problem of UNION statement (#6370) (#6506)
Browse files Browse the repository at this point in the history
  • Loading branch information
zz-jason authored and zhexuany committed May 10, 2018
1 parent e09213b commit 2514234
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
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 @@ -69,6 +69,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 @@ -206,6 +208,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

0 comments on commit 2514234

Please sign in to comment.