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) } }
func (s *testWhereRsetSuite) TestShowRsetPlan(c *C) { // `select id, name from t where id + 1` p, err := s.r.Plan(nil) c.Assert(err, IsNil) _, ok := p.(*plans.FilterDefaultPlan) c.Assert(ok, IsTrue) // `select id, name from t where 1` s.r.Expr = expression.Value{Val: int64(1)} _, err = s.r.Plan(nil) c.Assert(err, IsNil) // `select id, name from t where 0` s.r.Expr = expression.Value{Val: int64(0)} _, err = s.r.Plan(nil) c.Assert(err, IsNil) // `select id, name from t where null` s.r.Expr = expression.Value{} _, err = s.r.Plan(nil) c.Assert(err, IsNil) // `select id, name from t where id < 10` expr := expression.NewBinaryOperation(opcode.LT, &expression.Ident{CIStr: model.NewCIStr("id")}, expression.Value{Val: 10}) s.r.Expr = expr _, err = s.r.Plan(nil) c.Assert(err, IsNil) src := s.r.Src.(*testTablePlan) src.SetFilter(true) _, err = s.r.Plan(nil) c.Assert(err, IsNil) // `select id, name from t where null && 1` src.SetFilter(false) expr = expression.NewBinaryOperation(opcode.AndAnd, expression.Value{}, expression.Value{Val: 1}) s.r.Expr = expr _, err = s.r.Plan(nil) c.Assert(err, IsNil) // `select id, name from t where id && 1` expr = expression.NewBinaryOperation(opcode.AndAnd, &expression.Ident{CIStr: model.NewCIStr("id")}, expression.Value{Val: 1}) s.r.Expr = expr _, err = s.r.Plan(nil) c.Assert(err, IsNil) // `select id, name from t where id && (id < 10)` exprx := expression.NewBinaryOperation(opcode.LT, &expression.Ident{CIStr: model.NewCIStr("id")}, expression.Value{Val: 10}) expr = expression.NewBinaryOperation(opcode.AndAnd, &expression.Ident{CIStr: model.NewCIStr("id")}, exprx) s.r.Expr = expr _, err = s.r.Plan(nil) c.Assert(err, IsNil) src.SetFilter(true) _, err = s.r.Plan(nil) c.Assert(err, IsNil) // `select id, name from t where abc` src.SetFilter(false) expr = &expression.Ident{CIStr: model.NewCIStr("abc")} s.r.Expr = expr _, err = s.r.Plan(nil) c.Assert(err, IsNil) src.SetFilter(true) _, err = s.r.Plan(nil) c.Assert(err, IsNil) // `select id, name from t where 1 is null` src.SetFilter(false) exprx = expression.Value{Val: 1} expr = &expression.IsNull{Expr: exprx} s.r.Expr = expr _, err = s.r.Plan(nil) c.Assert(err, IsNil) src.SetFilter(true) _, err = s.r.Plan(nil) c.Assert(err, IsNil) // `select id, name from t where id is null` src.SetFilter(false) exprx = &expression.Ident{CIStr: model.NewCIStr("id")} expr = &expression.IsNull{Expr: exprx} s.r.Expr = expr _, err = s.r.Plan(nil) c.Assert(err, IsNil) src.SetFilter(true) _, err = s.r.Plan(nil) c.Assert(err, IsNil) // `select id, name from t where +id` src.SetFilter(false) exprx = &expression.Ident{CIStr: model.NewCIStr("id")} expr = expression.NewUnaryOperation(opcode.Plus, exprx) s.r.Expr = expr _, err = s.r.Plan(nil) c.Assert(err, IsNil) src.SetFilter(true) _, err = s.r.Plan(nil) c.Assert(err, IsNil) // `select id, name from t where id in (id)` expr = &expression.PatternIn{Expr: exprx, List: []expression.Expression{exprx}} s.r.Expr = expr _, err = s.r.Plan(nil) c.Assert(err, IsNil) // `select id, name from t where id like '%s'` expry := expression.Value{Val: "%s"} expr = &expression.PatternLike{Expr: exprx, Pattern: expry} s.r.Expr = expr _, err = s.r.Plan(nil) c.Assert(err, IsNil) // `select id, name from t where id is true` expr = &expression.IsTruth{Expr: exprx} s.r.Expr = expr _, err = s.r.Plan(nil) c.Assert(err, IsNil) }