示例#1
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)
}
示例#2
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,
	}
	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)
}
示例#3
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)
	}
}
示例#4
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)
}
示例#5
0
func (p *testShowSuit) TestShowCollation(c *C) {
	pln := &plans.ShowPlan{}

	pln.Target = stmt.ShowCollation
	fls := pln.GetFields()
	c.Assert(fls, HasLen, 6)
	c.Assert(fls[2].Col.Tp, Equals, mysql.TypeLonglong)

	pln.Pattern = &expression.PatternLike{
		Pattern: &expression.Value{
			Val: "utf8%",
		},
	}

	rset := rsets.Recordset{
		Ctx:  p.ctx,
		Plan: pln,
	}

	rows, err := rset.Rows(-1, 0)
	c.Assert(err, IsNil)
	c.Assert(len(rows), Greater, 0)
	pln.Close()

	pln.Pattern = nil
	tblWhere := []struct {
		Key   string
		Value interface{}
	}{
		{"Collation", "utf8_bin"},
		{"Charset", "utf8"},
		{"Id", 83},
		{"Default", "Yes"},
		{"Compiled", "Yes"},
		{"Sortlen", 1},
	}

	for _, w := range tblWhere {
		pln.Where = &expression.BinaryOperation{
			L:  &expression.Ident{CIStr: model.NewCIStr(w.Key)},
			R:  expression.Value{Val: w.Value},
			Op: opcode.EQ,
		}

		row, err := rset.FirstRow()
		c.Assert(err, IsNil)
		c.Assert(row, HasLen, 6)
		pln.Close()
	}
}
示例#6
0
func (t *testUnionSuit) TestUnion(c *C) {
	tblPlan := &testTablePlan{t.data, []string{"id", "name"}, 0}
	tblPlan2 := &testTablePlan{t.data2, []string{"id", "name"}, 0}
	cols := []*column.Col{
		{
			ColumnInfo: model.ColumnInfo{
				ID:           0,
				Name:         model.NewCIStr("id"),
				Offset:       0,
				DefaultValue: 0,
				FieldType:    *types.NewFieldType(mysql.TypeLonglong),
			},
		},
		{
			ColumnInfo: model.ColumnInfo{
				ID:           1,
				Name:         model.NewCIStr("name"),
				Offset:       1,
				DefaultValue: nil,
				FieldType:    *types.NewFieldType(mysql.TypeVarchar),
			},
		},
	}

	// Set Flen explicitly here, because following ColToResultField will change Flen to zero.
	// TODO: remove this if ColToResultField update.
	cols[1].FieldType.Flen = 100

	pln := &plans.UnionPlan{
		Srcs: []plan.Plan{
			tblPlan,
			tblPlan2,
		},
		Distincts: []bool{true},
		RFields: []*field.ResultField{
			field.ColToResultField(cols[0], "t"),
			field.ColToResultField(cols[1], "t"),
		},
	}
	rset := rsets.Recordset{
		Plan: pln,
		Ctx:  mock.NewContext()}
	cnt := 0
	rset.Do(func(data []interface{}) (bool, error) {
		cnt++
		return true, nil
	})
	c.Assert(cnt, Equals, 6)
}
示例#7
0
func (p *testIndexSuit) TestTableNilPlan(c *C) {
	nilPlan := &plans.TableNilPlan{
		T: p.tbl,
	}
	var ids []int
	id := 0
	rset := rsets.Recordset{Plan: nilPlan, Ctx: p.ctx}
	err := rset.Do(func(data []interface{}) (bool, error) {
		id++
		ids = append(ids, id)
		return true, nil
	})
	c.Assert(err, IsNil)
	c.Assert(ids, DeepEquals, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
}
示例#8
0
func (s *testFieldsSuit) TestSelectExprPlan(c *C) {
	pln := &plans.SelectEmptyFieldListPlan{
		Fields: []*field.Field{
			{
				Expr: &expressions.Value{
					Val: "data",
				},
			},
		},
	}
	fields := pln.GetFields()
	c.Assert(fields, HasLen, 1)
	rset := rsets.Recordset{Plan: pln}
	rset.Do(func(data []interface{}) (bool, error) {
		return true, nil
	})
}
示例#9
0
func (t *testLimitSuit) TestLimit(c *C) {
	tblPlan := &testTablePlan{t.data, []string{"id", "name"}, 0}

	pln := &plans.LimitDefaultPlan{
		Count:  2,
		Src:    tblPlan,
		Fields: []*field.ResultField{},
	}
	rset := rsets.Recordset{
		Ctx:  t.sess.(context.Context),
		Plan: pln,
	}
	rset.Do(func(data []interface{}) (bool, error) {
		// TODO check result
		return true, nil
	})
}
示例#10
0
func (p *testFromSuit) TestTableNilPlan(c *C) {
	nilPlan := &plans.TableNilPlan{
		T: p.tbl,
	}
	var ids []int64
	rset := rsets.Recordset{
		Plan: nilPlan,
		Ctx:  p,
	}
	var id int64
	rset.Do(func(data []interface{}) (bool, error) {
		id++
		ids = append(ids, id)
		return true, nil
	})
	c.Assert(reflect.DeepEqual(ids, []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}), Equals, true)
}
示例#11
0
func (t *testUnionSuit) TestUnion(c *C) {
	tblPlan := &testTablePlan{t.data, []string{"id", "name"}, 0}
	tblPlan2 := &testTablePlan{t.data2, []string{"id", "name"}, 0}
	cols := []*column.Col{
		{
			ColumnInfo: model.ColumnInfo{
				ID:           0,
				Name:         model.NewCIStr("id"),
				Offset:       0,
				DefaultValue: 0,
				FieldType:    *types.NewFieldType(mysql.TypeLonglong),
			},
		},
		{
			ColumnInfo: model.ColumnInfo{
				ID:           1,
				Name:         model.NewCIStr("name"),
				Offset:       1,
				DefaultValue: nil,
				FieldType:    *types.NewFieldType(mysql.TypeVarchar),
			},
		},
	}

	pln := &plans.UnionPlan{
		Srcs: []plan.Plan{
			tblPlan,
			tblPlan2,
		},
		Distincts: []bool{true},
		RFields: []*field.ResultField{
			field.ColToResultField(cols[0], "t"),
			field.ColToResultField(cols[1], "t"),
		},
	}
	rset := rsets.Recordset{Plan: pln}
	cnt := 0
	rset.Do(func(data []interface{}) (bool, error) {
		cnt++
		return true, nil
	})
	c.Assert(cnt, Equals, 6)
}
示例#12
0
func (s *testOuterQuerySuite) TestRowStackFromPlan(c *C) {
	var data = []*testRowData{
		{1, []interface{}{10, "hello"}},
	}

	pln := &testTablePlan{data, []string{"id", "name"}, 0}

	p := &plans.RowStackFromPlan{
		Src: pln,
	}

	fields := p.GetFields()
	c.Assert(fields, HasLen, 2)

	rset := rsets.Recordset{
		Plan: p,
		Ctx:  mock.NewContext()}
	_, err := rset.Rows(-1, 0)
	c.Assert(err, IsNil)
}
示例#13
0
func (p *testShowSuit) TestShowVariables(c *C) {
	pln := &plans.ShowPlan{
		Target:      stmt.ShowVariables,
		GlobalScope: true,
		Pattern: &expressions.PatternLike{
			Pattern: &expressions.Value{
				Val: "character_set_results",
			},
		},
	}
	fls := pln.GetFields()
	c.Assert(fls, HasLen, 2)
	c.Assert(fls[0].Name, Equals, "Variable_name")
	c.Assert(fls[1].Name, Equals, "Value")

	sessionVars := variable.GetSessionVars(p)
	ret := map[string]string{}
	rset := rsets.Recordset{
		Ctx:  p,
		Plan: pln,
	}
	rset.Do(func(data []interface{}) (bool, error) {
		ret[data[0].(string)] = data[1].(string)
		return true, nil
	})
	c.Assert(ret, HasLen, 1)
	v, ok := ret["character_set_results"]
	c.Assert(ok, IsTrue)
	c.Assert(v, Equals, "latin1")
	// Set session variable to utf8
	sessionVars.Systems["character_set_results"] = "utf8"
	pln.Close()
	rset.Do(func(data []interface{}) (bool, error) {
		ret[data[0].(string)] = data[1].(string)
		return true, nil
	})
	c.Assert(ret, HasLen, 1)
	v, ok = ret["character_set_results"]
	c.Assert(ok, IsTrue)
	// Show global varibale get latin1
	c.Assert(v, Equals, "latin1")

	pln.GlobalScope = false
	pln.Close()
	rset.Do(func(data []interface{}) (bool, error) {
		ret[data[0].(string)] = data[1].(string)
		return true, nil
	})
	c.Assert(ret, HasLen, 1)
	v, ok = ret["character_set_results"]
	c.Assert(ok, IsTrue)
	// Show session varibale get utf8
	c.Assert(v, Equals, "utf8")
}
示例#14
0
文件: show_test.go 项目: rorovic/tidb
func (p *testShowSuit) TestShowStatusVariables(c *C) {
	pln := &plans.ShowPlan{
		Target:      stmt.ShowStatus,
		GlobalScope: true,
		Pattern: &expression.PatternLike{
			Pattern: &expression.Value{
				Val: "tc_log_page_size",
			},
		},
	}
	fls := pln.GetFields()
	c.Assert(fls, HasLen, 2)
	c.Assert(fls[0].Name, Equals, "Variable_name")
	c.Assert(fls[1].Name, Equals, "Value")
	c.Assert(fls[0].Col.Tp, Equals, mysql.TypeVarchar)
	c.Assert(fls[1].Col.Tp, Equals, mysql.TypeVarchar)

	sessionVars := variable.GetSessionVars(p.ctx)
	ret := map[string]string{}
	rset := rsets.Recordset{
		Ctx:  p.ctx,
		Plan: pln,
	}
	rset.Do(func(data []interface{}) (bool, error) {
		ret[data[0].(string)] = data[1].(string)
		return true, nil
	})
	c.Assert(ret, HasLen, 1)
	v, ok := ret["tc_log_page_size"]
	c.Assert(ok, IsTrue)
	c.Assert(v, Equals, "0")
	pln.Close()

	sessionVars.StatusVars["tc_log_page_size"] = "1024"
	pln.GlobalScope = false
	rset.Do(func(data []interface{}) (bool, error) {
		ret[data[0].(string)] = data[1].(string)
		return true, nil
	})
	c.Assert(ret, HasLen, 1)
	v, ok = ret["tc_log_page_size"]
	c.Assert(ok, IsTrue)
	c.Assert(v, Equals, "1024")
	pln.Close()

	pln.Pattern = &expression.PatternLike{
		Pattern: &expression.Value{
			Val: "compression",
		},
	}
	sessionVars.StatusVars["compression"] = "on"
	pln.GlobalScope = true
	ret = map[string]string{}
	rset.Do(func(data []interface{}) (bool, error) {
		ret[data[0].(string)] = data[1].(string)
		return true, nil
	})
	c.Assert(ret, HasLen, 0)

	pln.GlobalScope = false
	rset.Do(func(data []interface{}) (bool, error) {
		ret[data[0].(string)] = data[1].(string)
		return true, nil
	})
	c.Assert(ret, HasLen, 1)
	v, ok = ret["compression"]
	c.Assert(ok, IsTrue)
	c.Assert(v, Equals, "on")
	pln.Close()

	pln.Pattern = nil
	pln.Where = &expression.BinaryOperation{
		L:  &expression.Ident{CIStr: model.NewCIStr("Variable_name")},
		R:  expression.Value{Val: "aborted_clients"},
		Op: opcode.EQ,
	}
	ret = map[string]string{}
	sessionVars.StatusVars["aborted_clients"] = "0"
	rset.Do(func(data []interface{}) (bool, error) {
		ret[data[0].(string)] = data[1].(string)
		return true, nil
	})
	c.Assert(ret, HasLen, 1)
	v, ok = ret["aborted_clients"]
	c.Assert(ok, IsTrue)
	c.Assert(v, Equals, "0")
}
示例#15
0
func (p *testIndexSuit) TestIndexPlan(c *C) {
	pln := &plans.TableDefaultPlan{
		T: p.tbl,
		Fields: []*field.ResultField{
			field.ColToResultField(p.tbl.Cols()[0], "t"),
			field.ColToResultField(p.tbl.Cols()[1], "t"),
		},
	}

	// expr: id > 0
	expr := &expression.BinaryOperation{
		Op: opcode.GE,
		L: &expression.Ident{
			CIStr: model.NewCIStr("id"),
		},
		R: expression.Value{
			Val: 50,
		},
	}

	expr2 := &expression.BinaryOperation{
		Op: opcode.LT,
		L: &expression.Ident{
			CIStr: model.NewCIStr("id"),
		},
		R: expression.Value{
			Val: 100,
		},
	}

	expr3 := &expression.BinaryOperation{
		Op: opcode.LE,
		L: &expression.Ident{
			CIStr: model.NewCIStr("id"),
		},
		R: expression.Value{
			Val: 100,
		},
	}

	expr4 := &expression.BinaryOperation{
		Op: opcode.GE,
		L: &expression.Ident{
			CIStr: model.NewCIStr("id"),
		},
		R: expression.Value{
			Val: 60,
		},
	}

	expr5 := &expression.Ident{
		CIStr: model.NewCIStr("id"),
	}

	np, _, err := pln.Filter(p.ctx, expr)
	c.Assert(err, IsNil)
	c.Assert(np, NotNil)
	np, _, err = np.Filter(p.ctx, expr2)
	c.Assert(err, IsNil)
	c.Assert(np, NotNil)
	np, _, err = np.Filter(p.ctx, expr3)
	c.Assert(err, IsNil)
	c.Assert(np, NotNil)
	np, _, err = np.Filter(p.ctx, expr4)
	c.Assert(err, IsNil)
	c.Assert(np, NotNil)
	np, _, err = np.Filter(p.ctx, expr5)
	c.Assert(err, IsNil)
	c.Assert(np, NotNil)

	ret := map[int64][]byte{}
	rset := rsets.Recordset{
		Plan: np,
		Ctx:  p.ctx,
	}
	rset.Do(func(data []interface{}) (bool, error) {
		ret[data[0].(int64)] = data[1].([]byte)
		return true, nil
	})
	excepted := map[int64][]byte{}
	for i := 6; i < 10; i++ {
		excepted[int64(i*10)] = []byte("hello")
	}

	expr6 := &expression.UnaryOperation{
		Op: '!',
		V: &expression.Ident{
			CIStr: model.NewCIStr("id"),
		},
	}
	np, _, err = np.Filter(p.ctx, expr6)
	c.Assert(err, IsNil)
	c.Assert(np, NotNil)
	c.Assert(ret, DeepEquals, excepted)
}
示例#16
0
func (t *testFinalPlan) TestFinalPlan(c *C) {
	col1 := &column.Col{
		ColumnInfo: model.ColumnInfo{
			ID:           0,
			Name:         model.NewCIStr("id"),
			Offset:       0,
			DefaultValue: 0,
		},
	}

	col2 := &column.Col{
		ColumnInfo: model.ColumnInfo{
			ID:           1,
			Name:         model.NewCIStr("name"),
			Offset:       1,
			DefaultValue: nil,
		},
	}

	col3 := &column.Col{
		ColumnInfo: model.ColumnInfo{
			ID:           2,
			Name:         model.NewCIStr("ok"),
			Offset:       2,
			DefaultValue: false,
		},
	}

	tblPlan := &testTablePlan{finalTestData, []string{"id", "name", "ok"}, 0}

	p := &plans.SelectFinalPlan{
		SelectList: &plans.SelectList{
			HiddenFieldOffset: len(tblPlan.GetFields()),
			ResultFields: []*field.ResultField{
				field.ColToResultField(col1, "t"),
				field.ColToResultField(col2, "t"),
				field.ColToResultField(col3, "t"),
			},
		},
		Src: tblPlan,
	}

	for _, rf := range p.ResultFields {
		c.Assert(rf.Col.Flag, Equals, uint(0))
		c.Assert(rf.Col.Tp, Equals, byte(0))
		c.Assert(rf.Col.Charset, Equals, "")
	}

	rset := rsets.Recordset{
		Plan: p,
		Ctx:  mock.NewContext()}
	rset.Do(func(in []interface{}) (bool, error) {
		return true, nil
	})

	for _, rf := range p.ResultFields {
		if rf.Name == "id" {
			c.Assert(rf.Col.Tp, Equals, mysql.TypeLonglong)
			c.Assert(rf.Col.Charset, Equals, charset.CharsetBin)
		}
		if rf.Name == "name" {
			c.Assert(rf.Col.Tp, Equals, mysql.TypeVarchar)
		}
		// TODO add more type tests
	}
}
示例#17
0
func (p *testShowSuit) TestShowStatusVariables(c *C) {
	pln := &plans.ShowPlan{
		Target:      stmt.ShowStatus,
		GlobalScope: true,
		Pattern: &expression.PatternLike{
			Pattern: &expression.Value{
				Val: testStatusBothScopes,
			},
		},
	}
	fls := pln.GetFields()
	c.Assert(fls, HasLen, 2)
	c.Assert(fls[0].Name, Equals, "Variable_name")
	c.Assert(fls[1].Name, Equals, "Value")
	c.Assert(fls[0].Col.Tp, Equals, mysql.TypeVarchar)
	c.Assert(fls[1].Col.Tp, Equals, mysql.TypeVarchar)

	ret := map[string]string{}
	rset := rsets.Recordset{
		Ctx:  p.ctx,
		Plan: pln,
	}
	rset.Do(func(data []interface{}) (bool, error) {
		ret[data[0].(string)] = data[1].(string)
		return true, nil
	})
	c.Assert(ret, HasLen, 1)
	v, ok := ret[testStatusBothScopes]
	c.Assert(ok, IsTrue)
	c.Assert(v, DeepEquals, testStatusValBothScope)
	pln.Close()

	pln.GlobalScope = true
	pln.Pattern = &expression.PatternLike{
		Pattern: &expression.Value{
			Val: testStatusSessionScope,
		},
	}
	ret = map[string]string{}
	rset.Do(func(data []interface{}) (bool, error) {
		ret[data[0].(string)] = data[1].(string)
		return true, nil
	})
	c.Assert(ret, HasLen, 0)
	pln.Close()

	pln.Pattern = nil
	pln.GlobalScope = false
	pln.Where = &expression.BinaryOperation{
		L:  &expression.Ident{CIStr: model.NewCIStr("Variable_name")},
		R:  expression.Value{Val: testStatusBothScopes},
		Op: opcode.EQ,
	}
	ret = map[string]string{}
	rset.Do(func(data []interface{}) (bool, error) {
		ret[data[0].(string)] = data[1].(string)
		return true, nil
	})
	c.Assert(ret, HasLen, 1)
	v, ok = ret[testStatusBothScopes]
	c.Assert(ok, IsTrue)
	c.Assert(v, Equals, testStatusValBothScope)
}
示例#18
0
func (p *testShowSuit) TestShowSysVariables(c *C) {
	pln := &plans.ShowPlan{
		Target:      stmt.ShowVariables,
		GlobalScope: true,
		Pattern: &expression.PatternLike{
			Pattern: &expression.Value{
				Val: "character_set_results",
			},
		},
	}
	fls := pln.GetFields()
	c.Assert(fls, HasLen, 2)
	c.Assert(fls[0].Name, Equals, "Variable_name")
	c.Assert(fls[1].Name, Equals, "Value")
	c.Assert(fls[0].Col.Tp, Equals, mysql.TypeVarchar)
	c.Assert(fls[1].Col.Tp, Equals, mysql.TypeVarchar)

	sessionVars := variable.GetSessionVars(p.ctx)
	ret := map[string]string{}
	rset := rsets.Recordset{
		Ctx:  p.ctx,
		Plan: pln,
	}
	rset.Do(func(data []interface{}) (bool, error) {
		ret[data[0].(string)] = data[1].(string)
		return true, nil
	})
	c.Assert(ret, HasLen, 1)
	v, ok := ret["character_set_results"]
	c.Assert(ok, IsTrue)
	c.Assert(v, Equals, "latin1")
	// Set session variable to utf8
	sessionVars.Systems["character_set_results"] = "utf8"
	pln.Close()
	rset.Do(func(data []interface{}) (bool, error) {
		ret[data[0].(string)] = data[1].(string)
		return true, nil
	})
	c.Assert(ret, HasLen, 1)
	v, ok = ret["character_set_results"]
	c.Assert(ok, IsTrue)
	// Show global variable get latin1
	c.Assert(v, Equals, "latin1")

	pln.GlobalScope = false
	pln.Close()
	rset.Do(func(data []interface{}) (bool, error) {
		ret[data[0].(string)] = data[1].(string)
		return true, nil
	})
	c.Assert(ret, HasLen, 1)
	v, ok = ret["character_set_results"]
	c.Assert(ok, IsTrue)
	// Show session variable get utf8
	c.Assert(v, Equals, "utf8")
	pln.Close()
	pln.Pattern = nil
	pln.Where = &expression.BinaryOperation{
		L:  &expression.Ident{CIStr: model.NewCIStr("Variable_name")},
		R:  expression.Value{Val: "autocommit"},
		Op: opcode.EQ,
	}

	ret = map[string]string{}
	sessionVars.Systems["autocommit"] = "on"
	rset.Do(func(data []interface{}) (bool, error) {
		ret[data[0].(string)] = data[1].(string)
		return true, nil
	})

	c.Assert(ret, HasLen, 1)
	v, ok = ret["autocommit"]
	c.Assert(ok, IsTrue)
	c.Assert(v, Equals, "on")
}
示例#19
0
func (s *testFieldsSuit) TestDefaultFieldsPlan(c *C) {
	tblPlan := &testTablePlan{selectFieldsTestData, []string{"id", "name"}, 0}

	sl1 := &plans.SelectList{
		Fields: []*field.Field{
			{
				Expr: &expression.Ident{
					CIStr: model.NewCIStr("name"),
				},
			},
		},
	}

	selFieldsPlan := &plans.SelectFieldsDefaultPlan{
		SelectList: sl1,
		Src:        tblPlan,
	}
	rset := rsets.Recordset{
		Plan: selFieldsPlan,
		Ctx:  mock.NewContext(),
	}
	rset.Do(func(data []interface{}) (bool, error) {
		c.Assert(len(data), Equals, 1)
		c.Assert(data[0].(string), Equals, "hello")
		return true, nil
	})

	// test multiple fields
	sl2 := &plans.SelectList{
		Fields: []*field.Field{
			{
				Expr: &expression.Ident{
					CIStr: model.NewCIStr("name"),
				},
			},
			{
				Expr: &expression.Ident{
					CIStr: model.NewCIStr("id"),
				},
			},
		},
	}
	tblPlan.Close()
	rset.Plan = &plans.SelectFieldsDefaultPlan{
		SelectList: sl2,
		Src:        tblPlan,
	}

	rset.Do(func(data []interface{}) (bool, error) {
		c.Assert(len(data), Equals, 2)
		c.Assert(data[0].(string), Equals, "hello")
		c.Assert(data[1].(int), GreaterEqual, 10)
		return true, nil
	})

	// test field doesn't exists
	sl3 := &plans.SelectList{
		Fields: []*field.Field{
			{
				Expr: &expression.Ident{
					CIStr: model.NewCIStr("nosuchfield"),
				},
			},
		},
	}
	tblPlan.Close()
	rset.Plan = &plans.SelectFieldsDefaultPlan{
		SelectList: sl3,
		Src:        tblPlan,
	}
	err := rset.Do(func(data []interface{}) (bool, error) {
		return true, nil
	})
	c.Assert(err.Error() == "unknown field nosuchfield", Equals, true)
}
示例#20
0
func (p *testFromSuit) TestTableDefaultPlan(c *C) {
	pln := &plans.TableDefaultPlan{
		T: p.tbl,
		Fields: []*field.ResultField{
			field.ColToResultField(p.cols[0], "t"),
			field.ColToResultField(p.cols[1], "t"),
		},
	}

	ret := map[int64]string{}
	rset := rsets.Recordset{Ctx: p, Plan: pln}
	rset.Do(func(data []interface{}) (bool, error) {
		ret[data[0].(int64)] = data[1].(string)
		return true, nil
	})
	excepted := map[int64]string{}
	for i := 0; i < 10; i++ {
		excepted[int64(i*10)] = "hello"
	}
	c.Assert(reflect.DeepEqual(ret, excepted), Equals, true)

	// expr: id > 0
	expr := &expression.BinaryOperation{
		Op: opcode.GE,
		L: &expression.Ident{
			CIStr: model.NewCIStr("id"),
		},
		R: &expression.Value{
			Val: 5,
		},
	}
	_, filtered, err := pln.FilterForUpdateAndDelete(p, expr)
	c.Assert(err, IsNil)
	c.Assert(filtered, IsFalse)
	// with no index

	idxCol := &column.IndexedCol{
		IndexInfo: model.IndexInfo{
			Name:  model.NewCIStr("id"),
			Table: model.NewCIStr("t"),
			Columns: []*model.IndexColumn{
				{
					Name:   model.NewCIStr("id"),
					Offset: 0,
					Length: 0,
				},
			},
			Unique:  false,
			Primary: false,
		},
		X: kv.NewKVIndex("i", "id", false),
	}
	p.tbl.AddIndex(idxCol)

	expr4 := &expression.Ident{
		CIStr: model.NewCIStr("id"),
	}
	_, filtered, err = pln.FilterForUpdateAndDelete(p, expr4)
	c.Assert(err, IsNil)
	// with no index
	c.Assert(filtered, IsTrue)

	expr5 := &expression.IsNull{
		Expr: &expression.Ident{
			CIStr: model.NewCIStr("id"),
		},
		Not: true,
	}
	_, filtered, err = pln.FilterForUpdateAndDelete(p, expr5)
	c.Assert(err, IsNil)
	// with no index
	c.Assert(filtered, IsTrue)
}
示例#21
0
func (s *testJoinSuit) TestJoin(c *C) {
	s.cols = []*column.Col{
		{
			ColumnInfo: model.ColumnInfo{
				ID:           0,
				Name:         model.NewCIStr("id"),
				Offset:       0,
				DefaultValue: 0,
				FieldType:    *types.NewFieldType(mysql.TypeLonglong),
			},
		},
		{
			ColumnInfo: model.ColumnInfo{
				ID:           1,
				Name:         model.NewCIStr("name"),
				Offset:       1,
				DefaultValue: nil,
				FieldType:    *types.NewFieldType(mysql.TypeVarchar),
			},
		},
	}

	var testData1 = []*testRowData{
		{1, []interface{}{10, "10"}},
		{2, []interface{}{10, "20"}},
		{3, []interface{}{10, "30"}},
		{4, []interface{}{40, "40"}},
		{6, []interface{}{60, "60"}},
	}

	var testData2 = []*testRowData{
		{1, []interface{}{10, "10"}},
		{2, []interface{}{10, "20"}},
		{3, []interface{}{10, "30"}},
		{4, []interface{}{40, "40"}},
		{6, []interface{}{60, "60"}},
	}

	tblPlan1 := &testTablePlan{testData1, []string{"id", "name"}, 0}
	tblPlan2 := &testTablePlan{testData2, []string{"id", "name"}, 0}

	joinPlan := &plans.JoinPlan{
		Left:   tblPlan1,
		Right:  tblPlan2,
		Type:   "CROSS",
		Fields: []*field.ResultField{},
		On:     expression.Value{Val: true},
	}
	rset := rsets.Recordset{
		Plan: joinPlan,
		Ctx:  mock.NewContext()}

	rset.Do(func(data []interface{}) (bool, error) {
		return true, nil
	})
	tblPlan1.Close()
	tblPlan2.Close()
	rset.Plan = &plans.JoinPlan{
		Left:   tblPlan1,
		Right:  tblPlan2,
		Type:   "LEFT",
		Fields: []*field.ResultField{},
		On:     expression.Value{Val: true},
	}
	rset.Do(func(data []interface{}) (bool, error) {
		return true, nil
	})
	tblPlan1.Close()
	tblPlan2.Close()
	joinPlan = &plans.JoinPlan{
		Left:   tblPlan1,
		Right:  tblPlan2,
		Type:   "RIGHT",
		Fields: []*field.ResultField{},
		On:     expression.Value{Val: true},
	}

	expr := &expression.BinaryOperation{
		Op: opcode.LT,
		L: &expression.Ident{
			CIStr: model.NewCIStr("id"),
		},
		R: expression.Value{
			Val: 100,
		},
	}
	np, _, err := joinPlan.Filter(nil, expr)
	c.Assert(np, NotNil)
	c.Assert(err, IsNil)
	rset.Plan = joinPlan
	rset.Do(func(data []interface{}) (bool, error) {
		return true, nil
	})
	tblPlan1.Close()
	tblPlan2.Close()
	rset.Plan = &plans.JoinPlan{
		Left:   tblPlan1,
		Right:  tblPlan2,
		Type:   "FULL",
		Fields: []*field.ResultField{},
		On:     expression.Value{Val: true},
	}

	rset.Do(func(data []interface{}) (bool, error) {
		return true, nil
	})
}
示例#22
0
func (t *testGroupBySuite) TestGroupBy(c *C) {
	tblPlan := &testTablePlan{groupByTestData, []string{"id", "name"}, 0}
	// test multiple fields
	sl := &plans.SelectList{
		Fields: []*field.Field{
			{
				Expr: &expressions.Ident{
					CIStr: model.NewCIStr("id"),
				},
			},
			{
				Expr: &expressions.Ident{
					CIStr: model.NewCIStr("name"),
				},
			},
			{
				Expr: &expressions.Call{
					F: "sum",
					Args: []expression.Expression{
						&expressions.Ident{
							CIStr: model.NewCIStr("id"),
						},
					},
				},
			},
		},
		AggFields: map[int]struct{}{2: {}},
	}

	groupbyPlan := &plans.GroupByDefaultPlan{
		SelectList: sl,
		Src:        tblPlan,
		By: []expression.Expression{
			&expressions.Ident{
				CIStr: model.NewCIStr("id"),
			},
		},
	}

	ret := map[int]string{}
	rset := rsets.Recordset{
		Plan: groupbyPlan,
	}
	rset.Do(func(data []interface{}) (bool, error) {
		ret[data[0].(int)] = data[1].(string)
		return true, nil
	})
	c.Assert(ret, HasLen, 3)
	excepted := map[int]string{
		10: "10",
		40: "40",
		60: "60",
	}
	c.Assert(ret, DeepEquals, excepted)

	// test empty
	tblPlan.rows = []*testRowData{}
	tblPlan.Close()
	groupbyPlan.Src = tblPlan
	groupbyPlan.By = nil
	groupbyPlan.Close()
	rset.Do(func(data []interface{}) (bool, error) {
		return true, nil
	})
}