Skip to content

Commit

Permalink
optimizer/plan: fix join panic (pingcap#1246)
Browse files Browse the repository at this point in the history
* optimizer/plan: fix join panic
  • Loading branch information
coocood committed May 23, 2016
1 parent e22d8d0 commit a98ecf8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
9 changes: 9 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1304,3 +1304,12 @@ func (s *testSuite) TestDatumXAPI(c *C) {
result = tk.MustQuery("select * from t where b > '11:11:11.5'")
result.Check(testkit.Rows("11:11:12 11:11:12", "11:11:13 11:11:13"))
}

func (s *testSuite) TestJoinPanic(c *C) {
defer testleak.AfterTest(c)()
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists events")
tk.MustExec("create table events (clock int, source int)")
tk.MustQuery("SELECT * FROM events e JOIN (SELECT MAX(clock) AS clock FROM events e2 GROUP BY e2.source) e3 ON e3.clock=e.clock")
}
4 changes: 3 additions & 1 deletion optimizer/plan/planbuilder_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ func (p *joinPath) attachCondition(condition ast.ExprNode, availablePaths []*joi
}

func (p *joinPath) containsTable(table *ast.TableName) bool {
if table == nil {
return false
}
if p.table != nil {
return p.table == table
}
Expand All @@ -234,7 +237,6 @@ func (p *joinPath) containsTable(table *ast.TableName) bool {
}
return false
}

return p.outer.containsTable(table) || p.inner.containsTable(table)
}

Expand Down
10 changes: 8 additions & 2 deletions optimizer/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package optimizer

import (
"math"
"strings"

"github.com/juju/errors"
"github.com/pingcap/tidb/ast"
Expand Down Expand Up @@ -183,11 +184,16 @@ func (v *validator) checkAutoIncrement(stmt *ast.CreateTableStmt) {
if count < 1 {
return
}

if !isKey {
isKey = isConstraintKeyTp(stmt.Constraints, autoIncrementCol)
}
if !isKey || count > 1 {
autoIncrementMustBeKey := true
for _, opt := range stmt.Options {
if opt.Tp == ast.TableOptionEngine && strings.EqualFold(opt.StrValue, "MyISAM") {
autoIncrementMustBeKey = false
}
}
if (autoIncrementMustBeKey && !isKey) || count > 1 {
v.err = errors.New("Incorrect table definition; there can be only one auto column and it must be defined as a key")
}

Expand Down
1 change: 1 addition & 0 deletions optimizer/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (s *testValidatorSuite) TestValidator(c *C) {
{"create table t(id decimal auto_increment, key (id))", true,
errors.New("Incorrect column specifier for column 'id'")},
{"create table t(id float auto_increment, key (id))", true, nil},
{"create table t(id int auto_increment) ENGINE=MYISAM", true, nil},
}
store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory)
c.Assert(err, IsNil)
Expand Down
5 changes: 5 additions & 0 deletions server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ func (cc *clientConn) dispatch(data []byte) error {
}()

switch cmd {
case mysql.ComSleep:
// TODO: According to mysql document, this command is supposed to be used only internally.
// So it's just a temp fix, not sure if it's done right.
// Investigate this command and write test case later.
return nil
case mysql.ComQuit:
return io.EOF
case mysql.ComQuery:
Expand Down

0 comments on commit a98ecf8

Please sign in to comment.