Exemple #1
0
func (s *testStatSuite) TestStat(c *C) {
	defer testleak.AfterTest(c)()
	store := testCreateStore(c, "test_stat")
	defer store.Close()

	lease := 50 * time.Millisecond

	d := newDDL(store, nil, nil, lease)
	defer d.close()

	time.Sleep(lease)

	dbInfo := testSchemaInfo(c, d, "test")
	testCreateSchema(c, mock.NewContext(), d, dbInfo)

	m, err := d.Stats()
	c.Assert(err, IsNil)
	c.Assert(m[ddlOwnerID], Equals, d.uuid)

	job := &model.Job{
		SchemaID: dbInfo.ID,
		Type:     model.ActionDropSchema,
		Args:     []interface{}{dbInfo.Name},
	}

	ctx := mock.NewContext()
	done := make(chan error, 1)
	go func() {
		done <- d.doDDLJob(ctx, job)
	}()

	ticker := time.NewTicker(d.lease * 1)
	defer ticker.Stop()

	ver := s.getDDLSchemaVer(c, d)
LOOP:
	for {
		select {
		case <-ticker.C:
			d.close()
			c.Assert(s.getDDLSchemaVer(c, d), GreaterEqual, ver)
			d.start()
		case err := <-done:
			c.Assert(err, IsNil)
			m, err := d.Stats()
			c.Assert(err, IsNil)
			c.Assert(m[bgOwnerID], Equals, d.uuid)
			break LOOP
		}
	}
}
Exemple #2
0
func (s *testEvaluatorSuite) TestVersion(c *C) {
	defer testleak.AfterTest(c)()
	ctx := mock.NewContext()
	v, err := builtinVersion(nil, ctx)
	c.Assert(err, IsNil)
	c.Assert(v.GetString(), Equals, mysql.ServerVersion)
}
Exemple #3
0
func (*testSuite) TestT(c *C) {
	driver := localstore.Driver{Driver: goleveldb.MemoryDriver{}}
	store, err := driver.Open("memory")
	c.Assert(err, IsNil)
	defer store.Close()

	ctx := mock.NewContext()

	dom, err := NewDomain(store, 0)
	c.Assert(err, IsNil)
	store = dom.Store()
	dd := dom.DDL()
	c.Assert(dd, NotNil)
	err = dd.CreateSchema(ctx, model.NewCIStr("aaa"))
	c.Assert(err, IsNil)
	is := dom.InfoSchema()
	c.Assert(is, NotNil)
	dom, err = NewDomain(store, 0)
	c.Assert(err, IsNil)

	dom.SetLease(10 * time.Second)

	m, err := dom.Stats()
	c.Assert(err, IsNil)
	c.Assert(m[ddlLastReloadSchemaTS], GreaterEqual, int64(0))

	dom.SetLease(50 * time.Millisecond)
	store.Close()
	time.Sleep(1 * time.Second)
}
Exemple #4
0
func (s *testEvaluatorSuite) TestSleep(c *C) {
	defer testleak.AfterTest(c)()
	ctx := mock.NewContext()
	sessVars := ctx.GetSessionVars()

	// non-strict model
	sessVars.StrictSQLMode = false
	d := make([]types.Datum, 1)
	ret, err := builtinSleep(d, ctx)
	c.Assert(err, IsNil)
	c.Assert(ret, DeepEquals, types.NewIntDatum(0))
	d[0].SetInt64(-1)
	ret, err = builtinSleep(d, ctx)
	c.Assert(err, IsNil)
	c.Assert(ret, DeepEquals, types.NewIntDatum(0))

	// for error case under the strict model
	sessVars.StrictSQLMode = true
	d[0].SetNull()
	_, err = builtinSleep(d, ctx)
	c.Assert(err, NotNil)
	d[0].SetFloat64(-2.5)
	_, err = builtinSleep(d, ctx)
	c.Assert(err, NotNil)

	// strict model
	d[0].SetFloat64(0.5)
	start := time.Now()
	ret, err = builtinSleep(d, ctx)
	c.Assert(err, IsNil)
	c.Assert(ret, DeepEquals, types.NewIntDatum(0))
	sub := time.Since(start)
	c.Assert(sub.Nanoseconds(), GreaterEqual, int64(0.5*1e9))
}
Exemple #5
0
func (t *testDistinctSuit) TestDistinct(c *C) {
	tblPlan := &testTablePlan{distinctTestData, []string{"id", "name"}, 0}

	p := plans.DistinctDefaultPlan{
		SelectList: &plans.SelectList{
			HiddenFieldOffset: len(tblPlan.GetFields()),
		},
		Src: tblPlan,
	}
	rset := rsets.Recordset{
		Plan: &p,
		Ctx:  mock.NewContext(),
	}
	r := map[int][]interface{}{}
	err := rset.Do(func(data []interface{}) (bool, error) {
		r[data[0].(int)] = data
		return true, nil
	})
	c.Assert(err, IsNil)

	expected := map[int][]interface{}{
		10: {10, "hello"},
		40: {40, "hello"},
		60: {60, "hello"},
	}

	c.Assert(reflect.DeepEqual(r, expected), Equals, true)
}
Exemple #6
0
func (s *testAggFuncSuite) TestCount(c *C) {
	// Compose aggregate exec for "select c1, count(c2) from t";
	// c1  c2
	// 1	1
	// 2	1
	// 3    nil
	c1 := ast.NewValueExpr(0)
	rf1 := &ast.ResultField{Expr: c1}
	col1 := &ast.ColumnNameExpr{Refer: rf1}
	fc1 := &ast.AggregateFuncExpr{
		F:    ast.AggFuncFirstRow,
		Args: []ast.ExprNode{col1},
	}
	c2 := ast.NewValueExpr(0)
	rf2 := &ast.ResultField{Expr: c2}
	col2 := &ast.ColumnNameExpr{Refer: rf2}
	fc2 := &ast.AggregateFuncExpr{
		F:    ast.AggFuncCount,
		Args: []ast.ExprNode{col2},
	}
	row1 := []interface{}{1, 1}
	row2 := []interface{}{2, 1}
	row3 := []interface{}{3, nil}
	data := []([]interface{}){row1, row2, row3}

	rows := make([]*Row, 0, 3)
	for _, d := range data {
		rows = append(rows, &Row{Data: d})
	}
	src := &mockExec{
		rows:   rows,
		fields: []*ast.ResultField{rf1, rf2},
	}
	agg := &AggregateExec{
		AggFuncs: []*ast.AggregateFuncExpr{fc1, fc2},
		Src:      src,
	}
	var (
		row *Row
		cnt int
	)
	for {
		r, err := agg.Next()
		c.Assert(err, IsNil)
		if r == nil {
			break
		}
		row = r
		cnt++
	}
	c.Assert(cnt, Equals, 1)
	c.Assert(row, NotNil)
	ctx := mock.NewContext()
	val, err := evaluator.Eval(ctx, fc1)
	c.Assert(err, IsNil)
	c.Assert(val, Equals, 1)
	val, err = evaluator.Eval(ctx, fc2)
	c.Assert(err, IsNil)
	c.Assert(val, Equals, int64(2))
}
Exemple #7
0
func (s *testEvaluatorSuite) TestRegexp(c *C) {
	tbl := []struct {
		pattern string
		input   string
		match   int64
	}{
		{"^$", "a", 0},
		{"a", "a", 1},
		{"a", "b", 0},
		{"aA", "aA", 1},
		{".", "a", 1},
		{"^.$", "ab", 0},
		{"..", "b", 0},
		{".ab", "aab", 1},
		{".*", "abcd", 1},
	}
	ctx := mock.NewContext()
	for _, v := range tbl {
		pattern := &ast.PatternRegexpExpr{
			Pattern: ast.NewValueExpr(v.pattern),
			Expr:    ast.NewValueExpr(v.input),
		}
		match, err := Eval(ctx, pattern)
		c.Assert(err, IsNil)
		c.Assert(match, Equals, v.match, Commentf("%v", v))
	}
}
Exemple #8
0
func (s *testSchemaSuite) TestSchema(c *C) {
	store := testCreateStore(c, "test_schema")
	defer store.Close()

	lease := 100 * time.Millisecond

	d1 := newDDL(store, nil, nil, lease)
	defer d1.close()

	ctx := mock.NewContext()

	dbInfo := testSchemaInfo(c, d1, "test")
	job := testCreateSchema(c, ctx, d1, dbInfo)

	testCheckSchemaState(c, d1, dbInfo, model.StatePublic)
	testCheckJobDone(c, d1, job, true)

	job = testDropSchema(c, ctx, d1, dbInfo)
	testCheckSchemaState(c, d1, dbInfo, model.StateNone)
	testCheckJobDone(c, d1, job, false)

	job = &model.Job{
		SchemaID: dbInfo.ID,
		Type:     model.ActionDropSchema,
	}

	err := d1.startDDLJob(ctx, job)
	c.Assert(terror.ErrorEqual(err, infoschema.DatabaseNotExists), IsTrue)
}
Exemple #9
0
func (p *testShowSuit) SetUpSuite(c *C) {
	nc := mock.NewContext()
	p.ctx = nc
	variable.BindSessionVars(p.ctx)
	variable.BindGlobalVarAccessor(p.ctx, nc)
	variable.RegisterStatistics(p.ms)

	p.dbName = "testshowplan"
	p.store = newStore(c, p.dbName)
	p.txn, _ = p.store.Begin()
	se := newSession(c, p.store, p.dbName)
	p.createDBSQL = fmt.Sprintf("create database if not exists %s;", p.dbName)
	p.dropDBSQL = fmt.Sprintf("drop database if exists %s;", p.dbName)
	p.useDBSQL = fmt.Sprintf("use %s;", p.dbName)
	p.createTableSQL = `CREATE TABLE test(id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));`

	mustExecSQL(c, se, p.createDBSQL)
	mustExecSQL(c, se, p.useDBSQL)
	mustExecSQL(c, se, p.createTableSQL)

	p.createSystemDBSQL = fmt.Sprintf("create database if not exists %s;", mysql.SystemDB)
	p.createUserTableSQL = tidb.CreateUserTable
	p.createDBPrivTableSQL = tidb.CreateDBPrivTable
	p.createTablePrivTableSQL = tidb.CreateTablePrivTable
	p.createColumnPrivTableSQL = tidb.CreateColumnPrivTable

	mustExecSQL(c, se, p.createSystemDBSQL)
	mustExecSQL(c, se, p.createUserTableSQL)
	mustExecSQL(c, se, p.createDBPrivTableSQL)
	mustExecSQL(c, se, p.createTablePrivTableSQL)
	mustExecSQL(c, se, p.createColumnPrivTableSQL)

}
Exemple #10
0
func (s *testPlanSuite) TestTableScanWithOrder(c *C) {
	defer testleak.AfterTest(c)()
	// Sort result by scanning PKHandle column.
	sql := "select * from t order by a limit 1;"
	stmt, err := s.ParseOneStmt(sql, "", "")
	c.Assert(err, IsNil)
	ast.SetFlag(stmt)

	err = newMockResolve(stmt)
	c.Assert(err, IsNil)

	builder := &planBuilder{
		allocator: new(idAllocator),
		ctx:       mock.NewContext(),
		colMapper: make(map[*ast.ColumnNameExpr]int),
	}
	p := builder.build(stmt)
	c.Assert(builder.err, IsNil)
	logic, ok := p.(LogicalPlan)
	c.Assert(ok, IsTrue)
	// Get physical plan.
	_, pp, _, err := logic.convert2PhysicalPlan(nil)
	c.Assert(err, IsNil)
	// Limit->Projection->PhysicalTableScan
	// Get PhysicalTableScan plan.
	cpp, ok := pp.p.GetChildByIndex(0).GetChildByIndex(0).(*PhysicalTableScan)
	c.Assert(cpp, NotNil)
	c.Assert(ok, IsTrue)
	// Make sure KeepOrder is true.
	c.Assert(cpp.KeepOrder, IsTrue)
}
Exemple #11
0
func (t *testWhereSuit) TestWhere(c *C) {
	tblPlan := &testTablePlan{t.data, []string{"id", "name"}, 0}
	pln := &plans.FilterDefaultPlan{
		Plan: tblPlan,
		Expr: &expression.BinaryOperation{
			Op: opcode.GE,
			L: &expression.Ident{
				CIStr:      model.NewCIStr("id"),
				ReferScope: expression.IdentReferFromTable,
				ReferIndex: 0,
			},
			R: expression.Value{
				Val: 30,
			},
		},
	}

	cnt := 0
	rset := rsets.Recordset{Plan: pln,
		Ctx: mock.NewContext()}
	rset.Do(func(data []interface{}) (bool, error) {
		cnt++
		return true, nil
	})
	c.Assert(cnt, Equals, 2)
}
Exemple #12
0
func (t *testOrderBySuit) TestOrderBy(c *C) {
	tblPlan := &testTablePlan{t.data, []string{"id", "name"}, 0}

	pln := &plans.OrderByDefaultPlan{
		SelectList: &plans.SelectList{
			HiddenFieldOffset: len(tblPlan.GetFields()),
			ResultFields:      tblPlan.GetFields(),
		},
		Src: tblPlan,
		By: []expression.Expression{
			&expression.Ident{
				CIStr: model.NewCIStr("id"),
			},
		},
		Ascs: []bool{false},
	}

	prev := 10000
	rset := rsets.Recordset{
		Plan: pln,
		Ctx:  mock.NewContext(),
	}
	err := rset.Do(func(data []interface{}) (bool, error) {
		// DESC
		if data[0].(int) > prev {
			c.Error("should no be here", data[0], prev)
		}
		prev = data[0].(int)
		return true, nil
	})
	if err != nil {
		log.Error(err)
	}
}
Exemple #13
0
func (s *testStmtSuite) SetUpTest(c *C) {
	log.SetLevelByString("error")
	s.dbName = "teststmts"
	var err error
	s.testDB, err = sql.Open(tidb.DriverName, tidb.EngineGoLevelDBMemory+"/"+s.dbName+"/"+s.dbName)
	c.Assert(err, IsNil)
	// create db
	s.createDBSql = fmt.Sprintf("create database if not exists %s;", s.dbName)
	s.dropDBSql = fmt.Sprintf("drop database if exists %s;", s.dbName)
	s.useDBSql = fmt.Sprintf("use %s;", s.dbName)
	s.createTableSql = `
    CREATE TABLE test(id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));
    CREATE TABLE test1(id INT NOT NULL DEFAULT 2, name varchar(255), PRIMARY KEY(id), INDEX name(name));
    CREATE TABLE test2(id INT NOT NULL DEFAULT 3, name varchar(255), PRIMARY KEY(id));`

	s.selectSql = `SELECT * from test limit 2;`
	mustExec(c, s.testDB, s.createDBSql)
	mustExec(c, s.testDB, s.useDBSql)

	s.createSystemDBSQL = fmt.Sprintf("create database if not exists %s;", mysql.SystemDB)
	s.createUserTableSQL = tidb.CreateUserTable
	s.createDBPrivTableSQL = tidb.CreateDBPrivTable
	s.createTablePrivTableSQL = tidb.CreateTablePrivTable
	s.createColumnPrivTableSQL = tidb.CreateColumnPrivTable

	mustExec(c, s.testDB, s.createSystemDBSQL)
	mustExec(c, s.testDB, s.createUserTableSQL)
	mustExec(c, s.testDB, s.createDBPrivTableSQL)
	mustExec(c, s.testDB, s.createTablePrivTableSQL)
	mustExec(c, s.testDB, s.createColumnPrivTableSQL)

	s.ctx = mock.NewContext()
	variable.BindSessionVars(s.ctx)
}
Exemple #14
0
func (s *testStmtSuite) TestSetCharsetStmt(c *C) {
	testSQL := `SET NAMES utf8;`

	stmtList, err := tidb.Compile(testSQL)
	c.Assert(err, IsNil)
	c.Assert(stmtList, HasLen, 1)

	testStmt, ok := stmtList[0].(*stmts.SetCharsetStmt)
	c.Assert(ok, IsTrue)

	c.Assert(testStmt.IsDDL(), IsFalse)
	c.Assert(len(testStmt.OriginText()), Greater, 0)

	ctx := mock.NewContext()
	variable.BindSessionVars(ctx)
	sessionVars := variable.GetSessionVars(ctx)
	for _, v := range variable.SetNamesVariables {
		c.Assert(sessionVars.Systems[v] != "utf8", IsTrue)
	}
	_, err = testStmt.Exec(ctx)
	c.Assert(err, IsNil)
	for _, v := range variable.SetNamesVariables {
		c.Assert(sessionVars.Systems[v], Equals, "utf8")
	}
	c.Assert(sessionVars.Systems[variable.CollationConnection], Equals, "utf8_general_ci")

	mf := newMockFormatter()
	testStmt.Explain(nil, mf)
	c.Assert(mf.Len(), Greater, 0)
}
Exemple #15
0
func (s *testEvaluatorSuite) TestCast(c *C) {
	f := types.NewFieldType(mysql.TypeLonglong)

	expr := &ast.FuncCastExpr{
		Expr: ast.NewValueExpr(1),
		Tp:   f,
	}
	ctx := mock.NewContext()
	v, err := Eval(ctx, expr)
	c.Assert(err, IsNil)
	c.Assert(v, Equals, int64(1))

	f.Flag |= mysql.UnsignedFlag
	v, err = Eval(ctx, expr)
	c.Assert(err, IsNil)
	c.Assert(v, Equals, uint64(1))

	f.Tp = mysql.TypeString
	f.Charset = charset.CharsetBin
	v, err = Eval(ctx, expr)
	c.Assert(err, IsNil)
	c.Assert(v, DeepEquals, []byte("1"))

	f.Tp = mysql.TypeString
	f.Charset = "utf8"
	v, err = Eval(ctx, expr)
	c.Assert(err, IsNil)
	c.Assert(v, DeepEquals, "1")

	expr.Expr = ast.NewValueExpr(nil)
	v, err = Eval(ctx, expr)
	c.Assert(err, IsNil)
	c.Assert(v, IsNil)
}
Exemple #16
0
func (s *testColumnSuite) TearDownSuite(c *C) {
	testDropSchema(c, mock.NewContext(), s.d, s.dbInfo)
	s.d.close()

	err := s.store.Close()
	c.Assert(err, IsNil)
}
Exemple #17
0
func (t *testHavingPlan) TestHaving(c *C) {
	tblPlan := &testTablePlan{groupByTestData, []string{"id", "name"}, 0}
	havingPlan := &plans.HavingPlan{
		Src: tblPlan,
		Expr: &expression.BinaryOperation{
			Op: opcode.GE,
			L: &expression.Ident{
				CIStr: model.NewCIStr("id"),
			},
			R: &expression.Value{
				Val: 20,
			},
		},
	}

	// having's behavior just like where
	cnt := 0
	rset := rsets.Recordset{
		Plan: havingPlan,
		Ctx:  mock.NewContext(),
	}
	rset.Do(func(data []interface{}) (bool, error) {
		cnt++
		return true, nil
	})
	c.Assert(cnt, Equals, 2)
}
Exemple #18
0
func (s *testColumnSuite) SetUpSuite(c *C) {
	s.store = testCreateStore(c, "test_column")
	s.d = newDDL(s.store, nil, nil, testLease)

	s.dbInfo = testSchemaInfo(c, s.d, "test_column")
	testCreateSchema(c, mock.NewContext(), s.d, s.dbInfo)
}
Exemple #19
0
func mockContext() context.Context {
	ctx := mock.NewContext()
	ctx.Store = &mockStore{
		client: &mockClient{},
	}
	return ctx
}
Exemple #20
0
func (s *testEvaluatorSuite) TestTrim(c *C) {
	tbl := []struct {
		str    interface{}
		remstr interface{}
		dir    ast.TrimDirectionType
		result interface{}
	}{
		{"  bar   ", nil, ast.TrimBothDefault, "bar"},
		{"xxxbarxxx", "x", ast.TrimLeading, "barxxx"},
		{"xxxbarxxx", "x", ast.TrimBoth, "bar"},
		{"barxxyz", "xyz", ast.TrimTrailing, "barx"},
		{nil, "xyz", ast.TrimBoth, nil},
		{1, 2, ast.TrimBoth, "1"},
		{"  \t\rbar\n   ", nil, ast.TrimBothDefault, "bar"},
	}
	ctx := mock.NewContext()
	for _, v := range tbl {
		f := &ast.FuncTrimExpr{
			Str:       ast.NewValueExpr(v.str),
			Direction: v.dir,
		}
		if v.remstr != nil {
			f.RemStr = ast.NewValueExpr(v.remstr)
		}

		r, err := Eval(ctx, f)
		c.Assert(err, IsNil)
		c.Assert(r, Equals, v.result)
	}
}
Exemple #21
0
func (s *testDDLSuite) TestDropTableError(c *C) {
	defer testleak.AfterTest(c)()
	store := testCreateStore(c, "test_drop_table")
	defer store.Close()

	d := newDDL(store, nil, nil, testLease)
	defer d.close()

	dbInfo := testSchemaInfo(c, d, "test")
	testCreateSchema(c, mock.NewContext(), d, dbInfo)

	job := &model.Job{
		SchemaID: dbInfo.ID,
		Type:     model.ActionDropTable,
		Args: []interface{}{&model.TableInfo{
			ID:   1,
			Name: model.CIStr{O: "t"},
		}},
	}
	err := kv.RunInNewTxn(store, false, func(txn kv.Transaction) error {
		t := meta.NewMeta(txn)
		return d.prepareBgJob(t, job)
	})
	c.Check(err, IsNil)
	d.startBgJob(job.Type)

	time.Sleep(testLease * 3)
	verifyBgJobState(c, d, job, model.JobDone)
}
Exemple #22
0
func (s *testEvaluatorSuite) TestSubstring(c *C) {
	tbl := []struct {
		str    string
		pos    int64
		slen   int64
		result string
	}{
		{"Quadratically", 5, -1, "ratically"},
		{"foobarbar", 4, -1, "barbar"},
		{"Quadratically", 5, 6, "ratica"},
		{"Sakila", -3, -1, "ila"},
		{"Sakila", -5, 3, "aki"},
		{"Sakila", -4, 2, "ki"},
		{"Sakila", 1000, 2, ""},
		{"", 2, 3, ""},
	}
	ctx := mock.NewContext()
	for _, v := range tbl {
		f := &ast.FuncSubstringExpr{
			StrExpr: ast.NewValueExpr(v.str),
			Pos:     ast.NewValueExpr(v.pos),
		}
		if v.slen != -1 {
			f.Len = ast.NewValueExpr(v.slen)
		}

		r, err := Eval(ctx, f)
		c.Assert(err, IsNil)
		s, ok := r.(string)
		c.Assert(ok, IsTrue)
		c.Assert(s, Equals, v.result)

		r1, err := Eval(ctx, f)
		c.Assert(err, IsNil)
		s1, ok := r1.(string)
		c.Assert(ok, IsTrue)
		c.Assert(s, Equals, s1)
	}
	errTbl := []struct {
		str    interface{}
		pos    interface{}
		len    interface{}
		result string
	}{
		{1, 5, -1, "ratically"},
		{"foobarbar", "4", -1, "barbar"},
		{"Quadratically", 5, "6", "ratica"},
	}
	for _, v := range errTbl {
		f := &ast.FuncSubstringExpr{
			StrExpr: ast.NewValueExpr(v.str),
			Pos:     ast.NewValueExpr(v.pos),
		}
		if v.len != -1 {
			f.Len = ast.NewValueExpr(v.len)
		}
		_, err := Eval(ctx, f)
		c.Assert(err, NotNil)
	}
}
Exemple #23
0
func (s *testEvaluatorSuite) TestBinopBitop(c *C) {
	ctx := mock.NewContext()
	tbl := []struct {
		lhs interface{}
		op  opcode.Op
		rhs interface{}
		ret interface{}
	}{
		{1, opcode.And, 1, 1},
		{1, opcode.Or, 1, 1},
		{1, opcode.Xor, 1, 0},
		{1, opcode.LeftShift, 1, 2},
		{2, opcode.RightShift, 1, 1},
		{nil, opcode.And, 1, nil},
		{1, opcode.And, nil, nil},
		{nil, opcode.Or, 1, nil},
		{nil, opcode.Xor, 1, nil},
		{nil, opcode.LeftShift, 1, nil},
		{nil, opcode.RightShift, 1, nil},
	}

	for _, t := range tbl {
		expr := &ast.BinaryOperationExpr{Op: t.op, L: ast.NewValueExpr(t.lhs), R: ast.NewValueExpr(t.rhs)}
		v, err := Eval(ctx, expr)
		c.Assert(err, IsNil)

		switch x := t.ret.(type) {
		case nil:
			c.Assert(v, IsNil)
		case int:
			c.Assert(v, DeepEquals, uint64(x))
		}
	}
}
Exemple #24
0
func (s *testDDLSuite) TestDropTableError(c *C) {
	defer testleak.AfterTest(c)()
	store := testCreateStore(c, "test_drop_table")
	defer store.Close()

	lease := 50 * time.Millisecond
	d := newDDL(store, nil, nil, lease)
	defer d.close()

	dbInfo := testSchemaInfo(c, d, "test")
	testCreateSchema(c, mock.NewContext(), d, dbInfo)

	job := &model.Job{
		SchemaID: dbInfo.ID,
		Type:     model.ActionDropTable,
		Args: []interface{}{&model.TableInfo{
			ID:   1,
			Name: model.CIStr{O: "t"},
		}},
	}
	d.prepareBgJob(job)
	d.startBgJob(job.Type)

	time.Sleep(lease)
	verifyBgJobState(c, d, job, model.JobDone)
}
Exemple #25
0
func (s *testSchemaSuite) TestSchema(c *C) {
	defer testleak.AfterTest(c)()
	store := testCreateStore(c, "test_schema")
	defer store.Close()

	d1 := newDDL(store, nil, nil, testLease)
	defer d1.close()

	ctx := mock.NewContext()

	dbInfo := testSchemaInfo(c, d1, "test")
	job := testCreateSchema(c, ctx, d1, dbInfo)

	testCheckSchemaState(c, d1, dbInfo, model.StatePublic)
	testCheckJobDone(c, d1, job, true)

	job = testDropSchema(c, ctx, d1, dbInfo)
	testCheckSchemaState(c, d1, dbInfo, model.StateNone)
	testCheckJobDone(c, d1, job, false)

	job = &model.Job{
		SchemaID: dbInfo.ID,
		Type:     model.ActionDropSchema,
	}

	err := d1.doDDLJob(ctx, job)
	c.Assert(terror.ErrorEqual(err, infoschema.ErrDatabaseDropExists), IsTrue)
}
Exemple #26
0
func (s *testPlanSuite) TestJoinReOrder(c *C) {
	defer testleak.AfterTest(c)()
	cases := []struct {
		sql  string
		best string
	}{
		{
			sql:  "select * from t t1, t t2, t t3, t t4, t t5, t t6 where t1.a = t2.b and t2.a = t3.b and t3.c = t4.a and t4.d = t2.c and t5.d = t6.d",
			best: "LeftHashJoin{LeftHashJoin{LeftHashJoin{LeftHashJoin{Table(t)->Table(t)}(t1.a,t2.b)->Table(t)}(t2.a,t3.b)->Table(t)}(t3.c,t4.a)(t2.c,t4.d)->LeftHashJoin{Table(t)->Table(t)}(t5.d,t6.d)}->Projection",
		},
		{
			sql:  "select * from t t1, t t2, t t3, t t4, t t5, t t6, t t7, t t8 where t1.a = t8.a",
			best: "LeftHashJoin{LeftHashJoin{LeftHashJoin{LeftHashJoin{Table(t)->Table(t)}(t1.a,t8.a)->Table(t)}->LeftHashJoin{Table(t)->Table(t)}}->LeftHashJoin{LeftHashJoin{Table(t)->Table(t)}->Table(t)}}->Projection",
		},
		{
			sql:  "select * from t t1, t t2, t t3, t t4, t t5 where t1.a = t5.a and t5.a = t4.a and t4.a = t3.a and t3.a = t2.a and t2.a = t1.a and t1.a = t3.a and t2.a = t4.a and t5.b < 8",
			best: "LeftHashJoin{LeftHashJoin{LeftHashJoin{RightHashJoin{Table(t)->Selection->Table(t)}(t5.a,t1.a)->Table(t)}(t1.a,t2.a)->Table(t)}(t2.a,t3.a)(t1.a,t3.a)->Table(t)}(t5.a,t4.a)(t3.a,t4.a)(t2.a,t4.a)->Projection",
		},
		{
			sql:  "select * from t t1, t t2, t t3, t t4, t t5 where t1.a = t5.a and t5.a = t4.a and t4.a = t3.a and t3.a = t2.a and t2.a = t1.a and t1.a = t3.a and t2.a = t4.a and t3.b = 1 and t4.a = 1",
			best: "LeftHashJoin{LeftHashJoin{LeftHashJoin{Table(t)->Selection->Table(t)}->LeftHashJoin{Table(t)->Table(t)}}->Table(t)}->Projection",
		},
		{
			sql:  "select * from t o where o.b in (select t3.c from t t1, t t2, t t3 where t1.a = t3.a and t2.a = t3.a and t2.a = o.a)",
			best: "Table(t)->Apply(LeftHashJoin{RightHashJoin{Table(t)->Selection->Table(t)}(t2.a,t3.a)->Table(t)}(t3.a,t1.a)->Projection)->Selection->Projection",
		},
		{
			sql:  "select * from t o where o.b in (select t3.c from t t1, t t2, t t3 where t1.a = t3.a and t2.a = t3.a and t2.a = o.a and t1.a = 1)",
			best: "Table(t)->Apply(LeftHashJoin{LeftHashJoin{Table(t)->Table(t)}->Table(t)->Selection}->Projection)->Selection->Projection",
		},
	}
	for _, ca := range cases {
		comment := Commentf("for %s", ca.sql)
		stmt, err := s.ParseOneStmt(ca.sql, "", "")
		c.Assert(err, IsNil, comment)
		ast.SetFlag(stmt)

		err = mockResolve(stmt)
		c.Assert(err, IsNil)

		builder := &planBuilder{
			allocator: new(idAllocator),
			ctx:       mock.NewContext(),
			colMapper: make(map[*ast.ColumnNameExpr]int),
		}
		p := builder.build(stmt)
		c.Assert(builder.err, IsNil)
		lp := p.(LogicalPlan)

		_, lp, err = lp.PredicatePushDown(nil)
		c.Assert(err, IsNil)
		_, err = lp.PruneColumnsAndResolveIndices(lp.GetSchema())
		c.Assert(err, IsNil)
		_, res, _, err := lp.convert2PhysicalPlan(nil)
		c.Assert(err, IsNil)
		p = res.p.PushLimit(nil)
		c.Assert(ToString(p), Equals, ca.best, Commentf("for %s", ca.sql))
	}
}
Exemple #27
0
func (*testSuite) TestT(c *C) {
	driver := localstore.Driver{Driver: goleveldb.MemoryDriver{}}
	store, err := driver.Open("memory")
	c.Assert(err, IsNil)
	defer testleak.AfterTest(c)()

	ctx := mock.NewContext()

	dom, err := NewDomain(store, 0)
	c.Assert(err, IsNil)
	store = dom.Store()
	dd := dom.DDL()
	c.Assert(dd, NotNil)
	cs := &ast.CharsetOpt{
		Chs: "utf8",
		Col: "utf8_bin",
	}
	err = dd.CreateSchema(ctx, model.NewCIStr("aaa"), cs)
	c.Assert(err, IsNil)
	is := dom.InfoSchema()
	c.Assert(is, NotNil)
	dom, err = NewDomain(store, 0)
	c.Assert(err, IsNil)

	dom.SetLease(10 * time.Second)

	m, err := dom.Stats()
	c.Assert(err, IsNil)
	c.Assert(m[ddlLastReloadSchemaTS], GreaterEqual, int64(0))

	c.Assert(dom.GetScope("dummy_status"), Equals, variable.DefaultScopeFlag)

	dom.SetLease(10 * time.Millisecond)
	time.Sleep(20 * time.Millisecond)
	atomic.StoreInt64(&dom.lastLeaseTS, 0)
	dom.tryReload()
	time.Sleep(1 * time.Second)

	// for schemaValidity
	err = dom.SchemaValidity.Check(0)
	c.Assert(err, IsNil)
	dom.SchemaValidity.MockReloadFailed = true
	err = dom.MustReload()
	c.Assert(err, NotNil)
	err = dom.SchemaValidity.Check(0)
	c.Assert(err, NotNil)
	dom.SchemaValidity.MockReloadFailed = false
	err = dom.MustReload()
	c.Assert(err, IsNil)
	err = dom.SchemaValidity.Check(0)
	c.Assert(err, IsNil)

	// for goroutine exit in Reload
	defaultMinReloadTimeout = 1 * time.Second
	err = store.Close()
	c.Assert(err, IsNil)
	err = dom.Reload()
	c.Assert(err, NotNil)
}
Exemple #28
0
func (s *testTableSuite) SetUpSuite(c *C) {
	s.store = testCreateStore(c, "test_table")
	lease := 50 * time.Millisecond
	s.d = newDDL(s.store, nil, nil, lease)

	s.dbInfo = testSchemaInfo(c, s.d, "test")
	testCreateSchema(c, mock.NewContext(), s.d, s.dbInfo)
}
Exemple #29
0
func (p *testShowSuit) SetUpSuite(c *C) {
	var err error
	store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory)
	c.Assert(err, IsNil)
	p.ctx = mock.NewContext()
	p.txn, _ = store.Begin()
	variable.BindSessionVars(p.ctx)
}
Exemple #30
0
func (s *testPlanSuite) TestAllocID(c *C) {
	pA := &DataSource{baseLogicalPlan: newBaseLogicalPlan(Tbl, new(idAllocator))}
	pB := &DataSource{baseLogicalPlan: newBaseLogicalPlan(Tbl, new(idAllocator))}
	ctx := mock.NewContext()
	pA.initIDAndContext(ctx)
	pB.initIDAndContext(ctx)
	c.Assert(pA.id, Equals, pB.id)
}