コード例 #1
0
ファイル: visitor_test.go プロジェクト: nengwang/tidb
func (s *testVisitorSuite) TestBase(c *C) {
	val := expression.Value{Val: 1}
	visitor := expression.NewIdentEvalVisitor()
	var exp expression.Expression
	exp = &expression.Between{Expr: val, Left: val, Right: val}
	exp.Accept(visitor)
	exp = expression.NewBinaryOperation(opcode.And, val, val)
	exp.Accept(visitor)
	exp, _ = expression.NewCall("avg", []expression.Expression{val}, true)
	exp.Accept(visitor)
	rows := [][]interface{}{{1}}
	sq := newMockSubQuery(rows, []string{"a"})
	exp = expression.NewCompareSubQuery(opcode.EQ, val, sq, true)
	exp.Accept(visitor)
	exp = &expression.Default{Name: "a"}
	exp.Accept(visitor)
	exp = expression.NewExistsSubQuery(sq)
	exp.Accept(visitor)

	when := &expression.WhenClause{Expr: val, Result: val}
	exp = &expression.FunctionCase{
		WhenClauses: []*expression.WhenClause{when},
		Value:       val,
		ElseClause:  val,
	}
	exp.Accept(visitor)
	exp = &expression.FunctionCast{Expr: val, Tp: types.NewFieldType(mysql.TypeLong), FunctionType: expression.ConvertFunction}
	exp.Accept(visitor)
	exp = &expression.FunctionConvert{Expr: val, Charset: "utf8"}
	exp.Accept(visitor)
	exp = &expression.FunctionSubstring{StrExpr: expression.Value{Val: "string"}, Pos: expression.Value{Val: 0}, Len: val}
	exp.Accept(visitor)
	exp = &expression.IsNull{Expr: val}
	exp.Accept(visitor)
	exp = &expression.IsTruth{Expr: val}
	exp.Accept(visitor)
	exp = &expression.ParamMarker{Expr: val}
	exp.Accept(visitor)
	exp = &expression.PatternIn{Expr: val, List: []expression.Expression{val}}
	exp.Accept(visitor)
	exp = &expression.PatternLike{Expr: val, Pattern: val}
	exp.Accept(visitor)
	exp = &expression.PatternRegexp{Expr: val, Pattern: val}
	exp.Accept(visitor)
	exp = &expression.PExpr{Expr: val}
	exp.Accept(visitor)
	exp = &expression.Position{Name: "a"}
	exp.Accept(visitor)
	exp = &expression.Row{Values: []expression.Expression{val}}
	exp.Accept(visitor)
	exp = &expression.UnaryOperation{V: val}
	exp.Accept(visitor)
	exp = &expression.Values{CIStr: model.NewCIStr("a")}
	exp.Accept(visitor)
	exp = &expression.Variable{Name: "a"}
	exp.Accept(visitor)
}
コード例 #2
0
ファイル: exists_subquery_test.go プロジェクト: lovedboy/tidb
func (s *testExistsSubQuerySuite) TestExistsSubQuery(c *C) {
	// Test exists subquery.
	tbl := []struct {
		in     []interface{}
		result int64 // 0 for false, 1 for true.
	}{
		{[]interface{}{1}, 1},
		{[]interface{}{nil}, 1},
		{[]interface{}{}, 0},
	}

	ctx := mock.NewContext()
	for _, t := range tbl {
		in := make([][]interface{}, 0, len(t.in))
		for _, v := range t.in {
			in = append(in, []interface{}{convert(v)})
		}

		sq := newMockSubQuery(in, []string{"c"})
		expr := expression.NewExistsSubQuery(sq)

		c.Assert(expr.IsStatic(), IsFalse)

		exprc := expr.Clone()
		c.Assert(exprc, NotNil)

		str := exprc.String()
		c.Assert(len(str), Greater, 0)

		v, err := exprc.Eval(ctx, nil)
		c.Assert(err, IsNil)

		val, err := types.ToBool(v)
		c.Assert(err, IsNil)
		c.Assert(val, Equals, t.result)

		// test cache
		v, err = exprc.Eval(ctx, nil)
		c.Assert(err, IsNil)

		val, err = types.ToBool(v)
		c.Assert(err, IsNil)
		c.Assert(val, Equals, t.result)
	}

	// Test not exists subquery.
	tbl = []struct {
		in     []interface{}
		result int64 // 0 for false, 1 for true.
	}{
		{[]interface{}{1}, 0},
		{[]interface{}{nil}, 0},
		{[]interface{}{}, 1},
	}
	for _, t := range tbl {
		in := make([][]interface{}, 0, len(t.in))
		for _, v := range t.in {
			in = append(in, []interface{}{convert(v)})
		}

		sq := newMockSubQuery(in, []string{"c"})
		es := expression.NewExistsSubQuery(sq)

		c.Assert(es.IsStatic(), IsFalse)

		str := es.String()
		c.Assert(len(str), Greater, 0)

		expr := expression.NewUnaryOperation(opcode.Not, es)

		exprc := expr.Clone()
		c.Assert(exprc, NotNil)

		str = exprc.String()
		c.Assert(len(str), Greater, 0)

		v, err := exprc.Eval(ctx, nil)
		c.Assert(err, IsNil)

		val, err := types.ToBool(v)
		c.Assert(err, IsNil)
		c.Assert(val, Equals, t.result)
	}
}