Skip to content

Commit

Permalink
expression/expressions: add not exists subquery test.
Browse files Browse the repository at this point in the history
  • Loading branch information
qiuyesuifeng committed Sep 15, 2015
1 parent d8246b7 commit c29d726
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
49 changes: 46 additions & 3 deletions expression/expressions/exists_subquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package expressions

import (
. "github.com/pingcap/check"
"github.com/pingcap/tidb/parser/opcode"
"github.com/pingcap/tidb/util/types"
)

Expand All @@ -24,14 +25,14 @@ type testExistsSubQuerySuite struct {
}

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},
{nil, 0},
}

for _, t := range tbl {
Expand All @@ -45,10 +46,52 @@ func (s *testExistsSubQuerySuite) TestExistsSubQuery(c *C) {

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

str := expr.String()
exprc, err := expr.Clone()
c.Assert(err, IsNil)

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

v, err := exprc.Eval(nil, 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 := NewExistsSubQuery(sq)

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

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

expr := NewUnaryOperation(opcode.Not, es)

exprc, err := expr.Clone()
c.Assert(err, IsNil)

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

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

val, err := types.ToBool(v)
Expand Down
11 changes: 10 additions & 1 deletion expression/expressions/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ func (s *mockStatement) String() string {
}

type mockPlan struct {
rset *mockRecordset
rset *mockRecordset
cursor int
}

func newMockPlan(rset *mockRecordset) *mockPlan {
Expand Down Expand Up @@ -205,9 +206,17 @@ func (p *mockPlan) Filter(ctx context.Context, expr expression.Expression) (plan
}

func (p *mockPlan) Next(ctx context.Context) (row *plan.Row, err error) {
if p.cursor == len(p.rset.rows) {
return
}
row = &plan.Row{
Data: p.rset.rows[p.cursor],
}
p.cursor++
return
}

func (p *mockPlan) Close() error {
p.cursor = 0
return nil
}

0 comments on commit c29d726

Please sign in to comment.