planner/memo: migrate test-infra to testify (#27158)

This commit is contained in:
tison
2021-08-14 14:43:58 +08:00
committed by GitHub
parent 5de5fa4afc
commit 4104fccad0
5 changed files with 395 additions and 362 deletions

View File

@ -14,141 +14,138 @@
package memo
import (
. "github.com/pingcap/check"
"testing"
"github.com/pingcap/tidb/expression"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/stretchr/testify/require"
)
func (s *testMemoSuite) TestNewExprIterFromGroupElem(c *C) {
g0 := NewGroupWithSchema(NewGroupExpr(plannercore.LogicalSelection{}.Init(s.sctx, 0)), s.schema)
g0.Insert(NewGroupExpr(plannercore.LogicalLimit{}.Init(s.sctx, 0)))
g0.Insert(NewGroupExpr(plannercore.LogicalProjection{}.Init(s.sctx, 0)))
g0.Insert(NewGroupExpr(plannercore.LogicalLimit{}.Init(s.sctx, 0)))
func TestNewExprIterFromGroupElem(t *testing.T) {
t.Parallel()
g1 := NewGroupWithSchema(NewGroupExpr(plannercore.LogicalSelection{}.Init(s.sctx, 0)), s.schema)
g1.Insert(NewGroupExpr(plannercore.LogicalLimit{}.Init(s.sctx, 0)))
g1.Insert(NewGroupExpr(plannercore.LogicalProjection{}.Init(s.sctx, 0)))
g1.Insert(NewGroupExpr(plannercore.LogicalLimit{}.Init(s.sctx, 0)))
ctx := plannercore.MockContext()
schema := expression.NewSchema()
expr := NewGroupExpr(plannercore.LogicalJoin{}.Init(s.sctx, 0))
g0 := NewGroupWithSchema(NewGroupExpr(plannercore.LogicalSelection{}.Init(ctx, 0)), schema)
g0.Insert(NewGroupExpr(plannercore.LogicalLimit{}.Init(ctx, 0)))
g0.Insert(NewGroupExpr(plannercore.LogicalProjection{}.Init(ctx, 0)))
g0.Insert(NewGroupExpr(plannercore.LogicalLimit{}.Init(ctx, 0)))
g1 := NewGroupWithSchema(NewGroupExpr(plannercore.LogicalSelection{}.Init(ctx, 0)), schema)
g1.Insert(NewGroupExpr(plannercore.LogicalLimit{}.Init(ctx, 0)))
g1.Insert(NewGroupExpr(plannercore.LogicalProjection{}.Init(ctx, 0)))
g1.Insert(NewGroupExpr(plannercore.LogicalLimit{}.Init(ctx, 0)))
expr := NewGroupExpr(plannercore.LogicalJoin{}.Init(ctx, 0))
expr.Children = append(expr.Children, g0)
expr.Children = append(expr.Children, g1)
g2 := NewGroupWithSchema(expr, s.schema)
g2 := NewGroupWithSchema(expr, schema)
pattern := BuildPattern(OperandJoin, EngineAll, BuildPattern(OperandProjection, EngineAll), BuildPattern(OperandSelection, EngineAll))
iter := NewExprIterFromGroupElem(g2.Equivalents.Front(), pattern)
c.Assert(iter, NotNil)
c.Assert(iter.Group, IsNil)
c.Assert(iter.Element, Equals, g2.Equivalents.Front())
c.Assert(iter.matched, Equals, true)
c.Assert(iter.Operand, Equals, OperandJoin)
c.Assert(len(iter.Children), Equals, 2)
require.NotNil(t, iter)
require.Nil(t, iter.Group)
require.Equal(t, g2.Equivalents.Front(), iter.Element)
require.True(t, iter.matched)
require.Equal(t, OperandJoin, iter.Operand)
require.Len(t, iter.Children, 2)
c.Assert(iter.Children[0].Group, Equals, g0)
c.Assert(iter.Children[0].Element, Equals, g0.GetFirstElem(OperandProjection))
c.Assert(iter.Children[0].matched, Equals, true)
c.Assert(iter.Children[0].Operand, Equals, OperandProjection)
c.Assert(len(iter.Children[0].Children), Equals, 0)
require.Equal(t, g0, iter.Children[0].Group)
require.Equal(t, g0.GetFirstElem(OperandProjection), iter.Children[0].Element)
require.True(t, iter.Children[0].matched)
require.Equal(t, OperandProjection, iter.Children[0].Operand)
require.Len(t, iter.Children[0].Children, 0)
c.Assert(iter.Children[1].Group, Equals, g1)
c.Assert(iter.Children[1].Element, Equals, g1.GetFirstElem(OperandSelection))
c.Assert(iter.Children[1].matched, Equals, true)
c.Assert(iter.Children[1].Operand, Equals, OperandSelection)
c.Assert(len(iter.Children[0].Children), Equals, 0)
require.Equal(t, g1, iter.Children[1].Group)
require.Equal(t, g1.GetFirstElem(OperandSelection), iter.Children[1].Element)
require.True(t, iter.Children[1].matched)
require.Equal(t, OperandSelection, iter.Children[1].Operand)
require.Len(t, iter.Children[0].Children, 0)
}
func (s *testMemoSuite) TestExprIterNext(c *C) {
g0 := NewGroupWithSchema(NewGroupExpr(
plannercore.LogicalProjection{Exprs: []expression.Expression{expression.NewZero()}}.Init(s.sctx, 0)), s.schema)
g0.Insert(NewGroupExpr(
plannercore.LogicalLimit{Count: 1}.Init(s.sctx, 0)))
g0.Insert(NewGroupExpr(
plannercore.LogicalProjection{Exprs: []expression.Expression{expression.NewOne()}}.Init(s.sctx, 0)))
g0.Insert(NewGroupExpr(
plannercore.LogicalLimit{Count: 2}.Init(s.sctx, 0)))
g0.Insert(NewGroupExpr(
plannercore.LogicalProjection{Exprs: []expression.Expression{expression.NewNull()}}.Init(s.sctx, 0)))
func TestExprIterNext(t *testing.T) {
t.Parallel()
g1 := NewGroupWithSchema(NewGroupExpr(
plannercore.LogicalSelection{Conditions: []expression.Expression{expression.NewNull()}}.Init(s.sctx, 0)), s.schema)
g1.Insert(NewGroupExpr(
plannercore.LogicalLimit{Count: 3}.Init(s.sctx, 0)))
g1.Insert(NewGroupExpr(
plannercore.LogicalSelection{Conditions: []expression.Expression{expression.NewOne()}}.Init(s.sctx, 0)))
g1.Insert(NewGroupExpr(
plannercore.LogicalLimit{Count: 4}.Init(s.sctx, 0)))
g1.Insert(NewGroupExpr(
plannercore.LogicalSelection{Conditions: []expression.Expression{expression.NewZero()}}.Init(s.sctx, 0)))
ctx := plannercore.MockContext()
schema := expression.NewSchema()
expr := NewGroupExpr(plannercore.LogicalJoin{}.Init(s.sctx, 0))
g0 := NewGroupWithSchema(NewGroupExpr(plannercore.LogicalProjection{Exprs: []expression.Expression{expression.NewZero()}}.Init(ctx, 0)), schema)
g0.Insert(NewGroupExpr(plannercore.LogicalLimit{Count: 1}.Init(ctx, 0)))
g0.Insert(NewGroupExpr(plannercore.LogicalProjection{Exprs: []expression.Expression{expression.NewOne()}}.Init(ctx, 0)))
g0.Insert(NewGroupExpr(plannercore.LogicalLimit{Count: 2}.Init(ctx, 0)))
g0.Insert(NewGroupExpr(plannercore.LogicalProjection{Exprs: []expression.Expression{expression.NewNull()}}.Init(ctx, 0)))
g1 := NewGroupWithSchema(NewGroupExpr(plannercore.LogicalSelection{Conditions: []expression.Expression{expression.NewNull()}}.Init(ctx, 0)), schema)
g1.Insert(NewGroupExpr(plannercore.LogicalLimit{Count: 3}.Init(ctx, 0)))
g1.Insert(NewGroupExpr(plannercore.LogicalSelection{Conditions: []expression.Expression{expression.NewOne()}}.Init(ctx, 0)))
g1.Insert(NewGroupExpr(plannercore.LogicalLimit{Count: 4}.Init(ctx, 0)))
g1.Insert(NewGroupExpr(plannercore.LogicalSelection{Conditions: []expression.Expression{expression.NewZero()}}.Init(ctx, 0)))
expr := NewGroupExpr(plannercore.LogicalJoin{}.Init(ctx, 0))
expr.Children = append(expr.Children, g0)
expr.Children = append(expr.Children, g1)
g2 := NewGroupWithSchema(expr, s.schema)
g2 := NewGroupWithSchema(expr, schema)
pattern := BuildPattern(OperandJoin, EngineAll, BuildPattern(OperandProjection, EngineAll), BuildPattern(OperandSelection, EngineAll))
iter := NewExprIterFromGroupElem(g2.Equivalents.Front(), pattern)
c.Assert(iter, NotNil)
require.NotNil(t, iter)
count := 0
for ; iter.Matched(); iter.Next() {
count++
c.Assert(iter.Group, IsNil)
c.Assert(iter.matched, Equals, true)
c.Assert(iter.Operand, Equals, OperandJoin)
c.Assert(len(iter.Children), Equals, 2)
require.Nil(t, iter.Group)
require.True(t, iter.matched)
require.Equal(t, OperandJoin, iter.Operand)
require.Len(t, iter.Children, 2)
c.Assert(iter.Children[0].Group, Equals, g0)
c.Assert(iter.Children[0].matched, Equals, true)
c.Assert(iter.Children[0].Operand, Equals, OperandProjection)
c.Assert(len(iter.Children[0].Children), Equals, 0)
require.Equal(t, g0, iter.Children[0].Group)
require.True(t, iter.Children[0].matched)
require.Equal(t, OperandProjection, iter.Children[0].Operand)
require.Len(t, iter.Children[0].Children, 0)
c.Assert(iter.Children[1].Group, Equals, g1)
c.Assert(iter.Children[1].matched, Equals, true)
c.Assert(iter.Children[1].Operand, Equals, OperandSelection)
c.Assert(len(iter.Children[1].Children), Equals, 0)
require.Equal(t, g1, iter.Children[1].Group)
require.True(t, iter.Children[1].matched)
require.Equal(t, OperandSelection, iter.Children[1].Operand)
require.Len(t, iter.Children[1].Children, 0)
}
c.Assert(count, Equals, 9)
require.Equal(t, 9, count)
}
func (s *testMemoSuite) TestExprIterReset(c *C) {
g0 := NewGroupWithSchema(NewGroupExpr(
plannercore.LogicalProjection{Exprs: []expression.Expression{expression.NewZero()}}.Init(s.sctx, 0)), s.schema)
g0.Insert(NewGroupExpr(
plannercore.LogicalLimit{Count: 1}.Init(s.sctx, 0)))
g0.Insert(NewGroupExpr(
plannercore.LogicalProjection{Exprs: []expression.Expression{expression.NewOne()}}.Init(s.sctx, 0)))
g0.Insert(NewGroupExpr(
plannercore.LogicalLimit{Count: 2}.Init(s.sctx, 0)))
g0.Insert(NewGroupExpr(
plannercore.LogicalProjection{Exprs: []expression.Expression{expression.NewNull()}}.Init(s.sctx, 0)))
func TestExprIterReset(t *testing.T) {
t.Parallel()
sel1 := NewGroupExpr(plannercore.LogicalSelection{Conditions: []expression.Expression{expression.NewNull()}}.Init(s.sctx, 0))
sel2 := NewGroupExpr(plannercore.LogicalSelection{Conditions: []expression.Expression{expression.NewOne()}}.Init(s.sctx, 0))
sel3 := NewGroupExpr(plannercore.LogicalSelection{Conditions: []expression.Expression{expression.NewZero()}}.Init(s.sctx, 0))
g1 := NewGroupWithSchema(sel1, s.schema)
g1.Insert(NewGroupExpr(plannercore.LogicalLimit{Count: 3}.Init(s.sctx, 0)))
ctx := plannercore.MockContext()
schema := expression.NewSchema()
g0 := NewGroupWithSchema(NewGroupExpr(plannercore.LogicalProjection{Exprs: []expression.Expression{expression.NewZero()}}.Init(ctx, 0)), schema)
g0.Insert(NewGroupExpr(plannercore.LogicalLimit{Count: 1}.Init(ctx, 0)))
g0.Insert(NewGroupExpr(plannercore.LogicalProjection{Exprs: []expression.Expression{expression.NewOne()}}.Init(ctx, 0)))
g0.Insert(NewGroupExpr(plannercore.LogicalLimit{Count: 2}.Init(ctx, 0)))
g0.Insert(NewGroupExpr(plannercore.LogicalProjection{Exprs: []expression.Expression{expression.NewNull()}}.Init(ctx, 0)))
sel1 := NewGroupExpr(plannercore.LogicalSelection{Conditions: []expression.Expression{expression.NewNull()}}.Init(ctx, 0))
sel2 := NewGroupExpr(plannercore.LogicalSelection{Conditions: []expression.Expression{expression.NewOne()}}.Init(ctx, 0))
sel3 := NewGroupExpr(plannercore.LogicalSelection{Conditions: []expression.Expression{expression.NewZero()}}.Init(ctx, 0))
g1 := NewGroupWithSchema(sel1, schema)
g1.Insert(NewGroupExpr(plannercore.LogicalLimit{Count: 3}.Init(ctx, 0)))
g1.Insert(sel2)
g1.Insert(NewGroupExpr(plannercore.LogicalLimit{Count: 4}.Init(s.sctx, 0)))
g1.Insert(NewGroupExpr(plannercore.LogicalLimit{Count: 4}.Init(ctx, 0)))
g1.Insert(sel3)
g2 := NewGroupWithSchema(NewGroupExpr(
plannercore.LogicalSelection{Conditions: []expression.Expression{expression.NewNull()}}.Init(s.sctx, 0)), s.schema)
g2.Insert(NewGroupExpr(
plannercore.LogicalLimit{Count: 3}.Init(s.sctx, 0)))
g2.Insert(NewGroupExpr(
plannercore.LogicalSelection{Conditions: []expression.Expression{expression.NewOne()}}.Init(s.sctx, 0)))
g2.Insert(NewGroupExpr(
plannercore.LogicalLimit{Count: 4}.Init(s.sctx, 0)))
g2.Insert(NewGroupExpr(
plannercore.LogicalSelection{Conditions: []expression.Expression{expression.NewZero()}}.Init(s.sctx, 0)))
g2 := NewGroupWithSchema(NewGroupExpr(plannercore.LogicalSelection{Conditions: []expression.Expression{expression.NewNull()}}.Init(ctx, 0)), schema)
g2.Insert(NewGroupExpr(plannercore.LogicalLimit{Count: 3}.Init(ctx, 0)))
g2.Insert(NewGroupExpr(plannercore.LogicalSelection{Conditions: []expression.Expression{expression.NewOne()}}.Init(ctx, 0)))
g2.Insert(NewGroupExpr(plannercore.LogicalLimit{Count: 4}.Init(ctx, 0)))
g2.Insert(NewGroupExpr(plannercore.LogicalSelection{Conditions: []expression.Expression{expression.NewZero()}}.Init(ctx, 0)))
// link join with Group 0 and 1
expr := NewGroupExpr(plannercore.LogicalJoin{}.Init(s.sctx, 0))
expr := NewGroupExpr(plannercore.LogicalJoin{}.Init(ctx, 0))
expr.Children = append(expr.Children, g0)
expr.Children = append(expr.Children, g1)
g3 := NewGroupWithSchema(expr, s.schema)
g3 := NewGroupWithSchema(expr, schema)
// link sel 1~3 with Group 2
sel1.Children = append(sel1.Children, g2)
@ -162,82 +159,62 @@ func (s *testMemoSuite) TestExprIterReset(c *C) {
// create expression iterator for the pattern on join
iter := NewExprIterFromGroupElem(g3.Equivalents.Front(), pattern)
c.Assert(iter, NotNil)
require.NotNil(t, iter)
count := 0
for ; iter.Matched(); iter.Next() {
count++
c.Assert(iter.Group, IsNil)
c.Assert(iter.matched, Equals, true)
c.Assert(iter.Operand, Equals, OperandJoin)
c.Assert(len(iter.Children), Equals, 2)
require.Nil(t, iter.Group)
require.True(t, iter.matched)
require.Equal(t, OperandJoin, iter.Operand)
require.Len(t, iter.Children, 2)
c.Assert(iter.Children[0].Group, Equals, g0)
c.Assert(iter.Children[0].matched, Equals, true)
c.Assert(iter.Children[0].Operand, Equals, OperandProjection)
c.Assert(len(iter.Children[0].Children), Equals, 0)
require.Equal(t, g0, iter.Children[0].Group)
require.True(t, iter.Children[0].matched)
require.Equal(t, OperandProjection, iter.Children[0].Operand)
require.Len(t, iter.Children[0].Children, 0)
c.Assert(iter.Children[1].Group, Equals, g1)
c.Assert(iter.Children[1].matched, Equals, true)
c.Assert(iter.Children[1].Operand, Equals, OperandSelection)
c.Assert(len(iter.Children[1].Children), Equals, 1)
require.Equal(t, g1, iter.Children[1].Group)
require.True(t, iter.Children[1].matched)
require.Equal(t, OperandSelection, iter.Children[1].Operand)
require.Len(t, iter.Children[1].Children, 1)
c.Assert(iter.Children[1].Children[0].Group, Equals, g2)
c.Assert(iter.Children[1].Children[0].matched, Equals, true)
c.Assert(iter.Children[1].Children[0].Operand, Equals, OperandLimit)
c.Assert(len(iter.Children[1].Children[0].Children), Equals, 0)
require.Equal(t, g2, iter.Children[1].Children[0].Group)
require.True(t, iter.Children[1].Children[0].matched)
require.Equal(t, OperandLimit, iter.Children[1].Children[0].Operand)
require.Len(t, iter.Children[1].Children[0].Children, 0)
}
c.Assert(count, Equals, 18)
require.Equal(t, 18, count)
}
func countMatchedIter(group *Group, pattern *Pattern) int {
count := 0
for elem := group.Equivalents.Front(); elem != nil; elem = elem.Next() {
iter := NewExprIterFromGroupElem(elem, pattern)
if iter == nil {
continue
}
for ; iter.Matched(); iter.Next() {
count++
}
}
return count
}
func TestExprIterWithEngineType(t *testing.T) {
t.Parallel()
func (s *testMemoSuite) TestExprIterWithEngineType(c *C) {
g1 := NewGroupWithSchema(NewGroupExpr(
plannercore.LogicalSelection{Conditions: []expression.Expression{expression.NewOne()}}.Init(s.sctx, 0)), s.schema).SetEngineType(EngineTiFlash)
g1.Insert(NewGroupExpr(
plannercore.LogicalLimit{Count: 1}.Init(s.sctx, 0)))
g1.Insert(NewGroupExpr(
plannercore.LogicalProjection{Exprs: []expression.Expression{expression.NewOne()}}.Init(s.sctx, 0)))
g1.Insert(NewGroupExpr(
plannercore.LogicalLimit{Count: 2}.Init(s.sctx, 0)))
ctx := plannercore.MockContext()
schema := expression.NewSchema()
g2 := NewGroupWithSchema(NewGroupExpr(
plannercore.LogicalSelection{Conditions: []expression.Expression{expression.NewOne()}}.Init(s.sctx, 0)), s.schema).SetEngineType(EngineTiKV)
g2.Insert(NewGroupExpr(
plannercore.LogicalLimit{Count: 2}.Init(s.sctx, 0)))
g2.Insert(NewGroupExpr(
plannercore.LogicalProjection{Exprs: []expression.Expression{expression.NewOne()}}.Init(s.sctx, 0)))
g2.Insert(NewGroupExpr(
plannercore.LogicalLimit{Count: 3}.Init(s.sctx, 0)))
g1 := NewGroupWithSchema(NewGroupExpr(plannercore.LogicalSelection{Conditions: []expression.Expression{expression.NewOne()}}.Init(ctx, 0)), schema).SetEngineType(EngineTiFlash)
g1.Insert(NewGroupExpr(plannercore.LogicalLimit{Count: 1}.Init(ctx, 0)))
g1.Insert(NewGroupExpr(plannercore.LogicalProjection{Exprs: []expression.Expression{expression.NewOne()}}.Init(ctx, 0)))
g1.Insert(NewGroupExpr(plannercore.LogicalLimit{Count: 2}.Init(ctx, 0)))
flashGather := NewGroupExpr(
plannercore.TiKVSingleGather{}.Init(s.sctx, 0))
g2 := NewGroupWithSchema(NewGroupExpr(plannercore.LogicalSelection{Conditions: []expression.Expression{expression.NewOne()}}.Init(ctx, 0)), schema).SetEngineType(EngineTiKV)
g2.Insert(NewGroupExpr(plannercore.LogicalLimit{Count: 2}.Init(ctx, 0)))
g2.Insert(NewGroupExpr(plannercore.LogicalProjection{Exprs: []expression.Expression{expression.NewOne()}}.Init(ctx, 0)))
g2.Insert(NewGroupExpr(plannercore.LogicalLimit{Count: 3}.Init(ctx, 0)))
flashGather := NewGroupExpr(plannercore.TiKVSingleGather{}.Init(ctx, 0))
flashGather.Children = append(flashGather.Children, g1)
g3 := NewGroupWithSchema(flashGather, s.schema).SetEngineType(EngineTiDB)
g3 := NewGroupWithSchema(flashGather, schema).SetEngineType(EngineTiDB)
tikvGather := NewGroupExpr(
plannercore.TiKVSingleGather{}.Init(s.sctx, 0))
tikvGather := NewGroupExpr(plannercore.TiKVSingleGather{}.Init(ctx, 0))
tikvGather.Children = append(tikvGather.Children, g2)
g3.Insert(tikvGather)
join := NewGroupExpr(
plannercore.LogicalJoin{}.Init(s.sctx, 0))
join := NewGroupExpr(plannercore.LogicalJoin{}.Init(ctx, 0))
join.Children = append(join.Children, g3, g3)
g4 := NewGroupWithSchema(join, s.schema).SetEngineType(EngineTiDB)
g4 := NewGroupWithSchema(join, schema).SetEngineType(EngineTiDB)
// The Groups look like this:
// Group 4
@ -257,37 +234,36 @@ func (s *testMemoSuite) TestExprIterWithEngineType(c *C) {
// Limit
p0 := BuildPattern(OperandTiKVSingleGather, EngineTiDBOnly, BuildPattern(OperandLimit, EngineTiKVOnly))
c.Assert(countMatchedIter(g3, p0), Equals, 2)
require.Equal(t, 2, countMatchedIter(g3, p0))
p1 := BuildPattern(OperandTiKVSingleGather, EngineTiDBOnly, BuildPattern(OperandLimit, EngineTiFlashOnly))
c.Assert(countMatchedIter(g3, p1), Equals, 2)
require.Equal(t, 2, countMatchedIter(g3, p1))
p2 := BuildPattern(OperandTiKVSingleGather, EngineTiDBOnly, BuildPattern(OperandLimit, EngineTiKVOrTiFlash))
c.Assert(countMatchedIter(g3, p2), Equals, 4)
require.Equal(t, 4, countMatchedIter(g3, p2))
p3 := BuildPattern(OperandTiKVSingleGather, EngineTiDBOnly, BuildPattern(OperandSelection, EngineTiFlashOnly))
c.Assert(countMatchedIter(g3, p3), Equals, 1)
require.Equal(t, 1, countMatchedIter(g3, p3))
p4 := BuildPattern(OperandTiKVSingleGather, EngineTiDBOnly, BuildPattern(OperandProjection, EngineTiKVOnly))
c.Assert(countMatchedIter(g3, p4), Equals, 1)
require.Equal(t, 1, countMatchedIter(g3, p4))
p5 := BuildPattern(
OperandJoin,
EngineTiDBOnly,
BuildPattern(OperandTiKVSingleGather, EngineTiDBOnly, BuildPattern(OperandLimit, EngineTiKVOnly)),
BuildPattern(OperandTiKVSingleGather, EngineTiDBOnly, BuildPattern(OperandLimit, EngineTiKVOnly)),
)
c.Assert(countMatchedIter(g4, p5), Equals, 4)
require.Equal(t, 4, countMatchedIter(g4, p5))
p6 := BuildPattern(
OperandJoin,
EngineTiDBOnly,
BuildPattern(OperandTiKVSingleGather, EngineTiDBOnly, BuildPattern(OperandLimit, EngineTiFlashOnly)),
BuildPattern(OperandTiKVSingleGather, EngineTiDBOnly, BuildPattern(OperandLimit, EngineTiKVOnly)),
)
c.Assert(countMatchedIter(g4, p6), Equals, 4)
require.Equal(t, 4, countMatchedIter(g4, p6))
p7 := BuildPattern(
OperandJoin,
EngineTiDBOnly,
BuildPattern(OperandTiKVSingleGather, EngineTiDBOnly, BuildPattern(OperandLimit, EngineTiKVOrTiFlash)),
BuildPattern(OperandTiKVSingleGather, EngineTiDBOnly, BuildPattern(OperandLimit, EngineTiKVOrTiFlash)),
)
c.Assert(countMatchedIter(g4, p7), Equals, 16)
require.Equal(t, 16, countMatchedIter(g4, p7))
// This is not a test case for EngineType. This case is to test
// the Pattern without a leaf AnyOperand. It is more efficient to
@ -298,5 +274,19 @@ func (s *testMemoSuite) TestExprIterWithEngineType(c *C) {
BuildPattern(OperandTiKVSingleGather, EngineTiDBOnly),
BuildPattern(OperandTiKVSingleGather, EngineTiDBOnly),
)
c.Assert(countMatchedIter(g4, p8), Equals, 4)
require.Equal(t, 4, countMatchedIter(g4, p8))
}
func countMatchedIter(group *Group, pattern *Pattern) int {
count := 0
for elem := group.Equivalents.Front(); elem != nil; elem = elem.Next() {
iter := NewExprIterFromGroupElem(elem, pattern)
if iter == nil {
continue
}
for ; iter.Matched(); iter.Next() {
count++
}
}
return count
}

View File

@ -16,21 +16,24 @@ package memo
import (
"encoding/binary"
"reflect"
"testing"
. "github.com/pingcap/check"
"github.com/pingcap/tidb/expression"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/stretchr/testify/require"
)
func (s *testMemoSuite) TestNewGroupExpr(c *C) {
func TestNewGroupExpr(t *testing.T) {
t.Parallel()
p := &plannercore.LogicalLimit{}
expr := NewGroupExpr(p)
c.Assert(expr.ExprNode, Equals, p)
c.Assert(expr.Children, IsNil)
c.Assert(expr.Explored(0), IsFalse)
require.Equal(t, p, expr.ExprNode)
require.Nil(t, expr.Children)
require.False(t, expr.Explored(0))
}
func (s *testMemoSuite) TestGroupExprFingerprint(c *C) {
func TestGroupExprFingerprint(t *testing.T) {
t.Parallel()
p := &plannercore.LogicalLimit{Count: 3}
expr := NewGroupExpr(p)
childGroup := NewGroupWithSchema(nil, expression.NewSchema())
@ -41,5 +44,5 @@ func (s *testMemoSuite) TestGroupExprFingerprint(c *C) {
binary.BigEndian.PutUint16(buffer, 1)
binary.BigEndian.PutUint64(buffer[2:], uint64(reflect.ValueOf(childGroup).Pointer()))
copy(buffer[10:], planHash)
c.Assert(expr.FingerPrint(), Equals, string(buffer))
require.Equal(t, string(buffer), expr.FingerPrint())
}

View File

@ -17,113 +17,102 @@ import (
"context"
"testing"
. "github.com/pingcap/check"
"github.com/pingcap/parser"
"github.com/pingcap/parser/model"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/infoschema"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/planner/property"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/util/testleak"
"github.com/stretchr/testify/require"
)
func TestT(t *testing.T) {
CustomVerboseFlag = true
TestingT(t)
}
func TestNewGroup(t *testing.T) {
t.Parallel()
var _ = Suite(&testMemoSuite{})
type testMemoSuite struct {
*parser.Parser
is infoschema.InfoSchema
schema *expression.Schema
sctx sessionctx.Context
}
func (s *testMemoSuite) SetUpSuite(c *C) {
testleak.BeforeTest()
s.is = infoschema.MockInfoSchema([]*model.TableInfo{plannercore.MockSignedTable()})
s.sctx = plannercore.MockContext()
s.Parser = parser.New()
s.schema = expression.NewSchema()
}
func (s *testMemoSuite) TearDownSuite(c *C) {
testleak.AfterTest(c)()
}
func (s *testMemoSuite) TestNewGroup(c *C) {
p := &plannercore.LogicalLimit{}
expr := NewGroupExpr(p)
g := NewGroupWithSchema(expr, s.schema)
g := NewGroupWithSchema(expr, expression.NewSchema())
c.Assert(g.Equivalents.Len(), Equals, 1)
c.Assert(g.Equivalents.Front().Value.(*GroupExpr), Equals, expr)
c.Assert(len(g.Fingerprints), Equals, 1)
c.Assert(g.Explored(0), IsFalse)
require.Equal(t, 1, g.Equivalents.Len())
require.Equal(t, expr, g.Equivalents.Front().Value.(*GroupExpr))
require.Len(t, g.Fingerprints, 1)
require.False(t, g.Explored(0))
}
func (s *testMemoSuite) TestGroupInsert(c *C) {
func TestGroupInsert(t *testing.T) {
t.Parallel()
p := &plannercore.LogicalLimit{}
expr := NewGroupExpr(p)
g := NewGroupWithSchema(expr, s.schema)
c.Assert(g.Insert(expr), IsFalse)
g := NewGroupWithSchema(expr, expression.NewSchema())
require.False(t, g.Insert(expr))
expr.selfFingerprint = "1"
c.Assert(g.Insert(expr), IsTrue)
require.True(t, g.Insert(expr))
}
func (s *testMemoSuite) TestGroupDelete(c *C) {
func TestGroupDelete(t *testing.T) {
t.Parallel()
p := &plannercore.LogicalLimit{}
expr := NewGroupExpr(p)
g := NewGroupWithSchema(expr, s.schema)
c.Assert(g.Equivalents.Len(), Equals, 1)
g := NewGroupWithSchema(expr, expression.NewSchema())
require.Equal(t, 1, g.Equivalents.Len())
g.Delete(expr)
c.Assert(g.Equivalents.Len(), Equals, 0)
require.Equal(t, 0, g.Equivalents.Len())
g.Delete(expr)
c.Assert(g.Equivalents.Len(), Equals, 0)
require.Equal(t, 0, g.Equivalents.Len())
}
func (s *testMemoSuite) TestGroupDeleteAll(c *C) {
expr := NewGroupExpr(plannercore.LogicalSelection{}.Init(s.sctx, 0))
g := NewGroupWithSchema(expr, s.schema)
c.Assert(g.Insert(NewGroupExpr(plannercore.LogicalLimit{}.Init(s.sctx, 0))), IsTrue)
c.Assert(g.Insert(NewGroupExpr(plannercore.LogicalProjection{}.Init(s.sctx, 0))), IsTrue)
c.Assert(g.Equivalents.Len(), Equals, 3)
c.Assert(g.GetFirstElem(OperandProjection), NotNil)
c.Assert(g.Exists(expr), IsTrue)
func TestGroupDeleteAll(t *testing.T) {
t.Parallel()
ctx := plannercore.MockContext()
expr := NewGroupExpr(plannercore.LogicalSelection{}.Init(ctx, 0))
g := NewGroupWithSchema(expr, expression.NewSchema())
require.True(t, g.Insert(NewGroupExpr(plannercore.LogicalLimit{}.Init(ctx, 0))))
require.True(t, g.Insert(NewGroupExpr(plannercore.LogicalProjection{}.Init(ctx, 0))))
require.Equal(t, 3, g.Equivalents.Len())
require.NotNil(t, g.GetFirstElem(OperandProjection))
require.True(t, g.Exists(expr))
g.DeleteAll()
c.Assert(g.Equivalents.Len(), Equals, 0)
c.Assert(g.GetFirstElem(OperandProjection), IsNil)
c.Assert(g.Exists(expr), IsFalse)
require.Equal(t, 0, g.Equivalents.Len())
require.Nil(t, g.GetFirstElem(OperandProjection))
require.False(t, g.Exists(expr))
}
func (s *testMemoSuite) TestGroupExists(c *C) {
func TestGroupExists(t *testing.T) {
t.Parallel()
p := &plannercore.LogicalLimit{}
expr := NewGroupExpr(p)
g := NewGroupWithSchema(expr, s.schema)
c.Assert(g.Exists(expr), IsTrue)
g := NewGroupWithSchema(expr, expression.NewSchema())
require.True(t, g.Exists(expr))
g.Delete(expr)
c.Assert(g.Exists(expr), IsFalse)
require.False(t, g.Exists(expr))
}
func (s *testMemoSuite) TestGroupFingerPrint(c *C) {
stmt1, err := s.ParseOneStmt("select * from t where a > 1 and a < 100", "", "")
c.Assert(err, IsNil)
p1, _, err := plannercore.BuildLogicalPlan(context.Background(), s.sctx, stmt1, s.is)
c.Assert(err, IsNil)
logic1, ok := p1.(plannercore.LogicalPlan)
c.Assert(ok, IsTrue)
func TestGroupFingerPrint(t *testing.T) {
t.Parallel()
p := parser.New()
stmt1, err := p.ParseOneStmt("select * from t where a > 1 and a < 100", "", "")
require.NoError(t, err)
is := infoschema.MockInfoSchema([]*model.TableInfo{plannercore.MockSignedTable()})
ctx := plannercore.MockContext()
plan, _, err := plannercore.BuildLogicalPlan(context.Background(), ctx, stmt1, is)
require.NoError(t, err)
logic1, ok := plan.(plannercore.LogicalPlan)
require.True(t, ok)
// Plan tree should be: DataSource -> Selection -> Projection
proj, ok := logic1.(*plannercore.LogicalProjection)
c.Assert(ok, IsTrue)
require.True(t, ok)
sel, ok := logic1.Children()[0].(*plannercore.LogicalSelection)
c.Assert(ok, IsTrue)
require.True(t, ok)
group1 := Convert2Group(logic1)
oldGroupExpr := group1.Equivalents.Front().Value.(*GroupExpr)
@ -131,24 +120,24 @@ func (s *testMemoSuite) TestGroupFingerPrint(c *C) {
newGroupExpr := NewGroupExpr(proj)
newGroupExpr.SetChildren(oldGroupExpr.Children[0])
group1.Insert(newGroupExpr)
c.Assert(group1.Equivalents.Len(), Equals, 1)
require.Equal(t, 1, group1.Equivalents.Len())
// Insert a GroupExpr with different children。
newGroupExpr2 := NewGroupExpr(proj)
newGroup := NewGroupWithSchema(oldGroupExpr, group1.Prop.Schema)
newGroupExpr2.SetChildren(newGroup)
group1.Insert(newGroupExpr2)
c.Assert(group1.Equivalents.Len(), Equals, 2)
require.Equal(t, 2, group1.Equivalents.Len())
// Insert a GroupExpr with different ExprNode.
limit := plannercore.LogicalLimit{}.Init(proj.SCtx(), 0)
newGroupExpr3 := NewGroupExpr(limit)
newGroupExpr3.SetChildren(oldGroupExpr.Children[0])
group1.Insert(newGroupExpr3)
c.Assert(group1.Equivalents.Len(), Equals, 3)
require.Equal(t, 3, group1.Equivalents.Len())
// Insert two LogicalSelections with same conditions but different order.
c.Assert(len(sel.Conditions), Equals, 2)
require.Len(t, sel.Conditions, 2)
newSelection := plannercore.LogicalSelection{
Conditions: make([]expression.Expression, 2)}.Init(sel.SCtx(), sel.SelectBlockOffset())
newSelection.Conditions[0], newSelection.Conditions[1] = sel.Conditions[1], sel.Conditions[0]
@ -157,27 +146,30 @@ func (s *testMemoSuite) TestGroupFingerPrint(c *C) {
newGroupExpr4.SetChildren(oldGroupExpr.Children[0])
newGroupExpr5.SetChildren(oldGroupExpr.Children[0])
group1.Insert(newGroupExpr4)
c.Assert(group1.Equivalents.Len(), Equals, 4)
require.Equal(t, 4, group1.Equivalents.Len())
group1.Insert(newGroupExpr5)
c.Assert(group1.Equivalents.Len(), Equals, 4)
require.Equal(t, 4, group1.Equivalents.Len())
}
func (s *testMemoSuite) TestGroupGetFirstElem(c *C) {
expr0 := NewGroupExpr(plannercore.LogicalProjection{}.Init(s.sctx, 0))
expr1 := NewGroupExpr(plannercore.LogicalLimit{}.Init(s.sctx, 0))
expr2 := NewGroupExpr(plannercore.LogicalProjection{}.Init(s.sctx, 0))
expr3 := NewGroupExpr(plannercore.LogicalLimit{}.Init(s.sctx, 0))
expr4 := NewGroupExpr(plannercore.LogicalProjection{}.Init(s.sctx, 0))
func TestGroupGetFirstElem(t *testing.T) {
t.Parallel()
g := NewGroupWithSchema(expr0, s.schema)
ctx := plannercore.MockContext()
expr0 := NewGroupExpr(plannercore.LogicalProjection{}.Init(ctx, 0))
expr1 := NewGroupExpr(plannercore.LogicalLimit{}.Init(ctx, 0))
expr2 := NewGroupExpr(plannercore.LogicalProjection{}.Init(ctx, 0))
expr3 := NewGroupExpr(plannercore.LogicalLimit{}.Init(ctx, 0))
expr4 := NewGroupExpr(plannercore.LogicalProjection{}.Init(ctx, 0))
g := NewGroupWithSchema(expr0, expression.NewSchema())
g.Insert(expr1)
g.Insert(expr2)
g.Insert(expr3)
g.Insert(expr4)
c.Assert(g.GetFirstElem(OperandProjection).Value.(*GroupExpr), Equals, expr0)
c.Assert(g.GetFirstElem(OperandLimit).Value.(*GroupExpr), Equals, expr1)
c.Assert(g.GetFirstElem(OperandAny).Value.(*GroupExpr), Equals, expr0)
require.Equal(t, expr0, g.GetFirstElem(OperandProjection).Value.(*GroupExpr))
require.Equal(t, expr1, g.GetFirstElem(OperandLimit).Value.(*GroupExpr))
require.Equal(t, expr0, g.GetFirstElem(OperandAny).Value.(*GroupExpr))
}
type fakeImpl struct {
@ -190,111 +182,124 @@ func (impl *fakeImpl) GetCost() float64 { return
func (impl *fakeImpl) GetPlan() plannercore.PhysicalPlan { return impl.plan }
func (impl *fakeImpl) AttachChildren(...Implementation) Implementation { return nil }
func (impl *fakeImpl) GetCostLimit(float64, ...Implementation) float64 { return 0 }
func (s *testMemoSuite) TestGetInsertGroupImpl(c *C) {
g := NewGroupWithSchema(NewGroupExpr(plannercore.LogicalLimit{}.Init(s.sctx, 0)), s.schema)
func TestGetInsertGroupImpl(t *testing.T) {
t.Parallel()
g := NewGroupWithSchema(NewGroupExpr(plannercore.LogicalLimit{}.Init(plannercore.MockContext(), 0)), expression.NewSchema())
emptyProp := &property.PhysicalProperty{}
orderProp := &property.PhysicalProperty{SortItems: []property.SortItem{{Col: &expression.Column{}}}}
require.Nil(t, g.GetImpl(emptyProp))
impl := g.GetImpl(emptyProp)
c.Assert(impl, IsNil)
impl = &fakeImpl{plan: &plannercore.PhysicalLimit{}}
impl := &fakeImpl{plan: &plannercore.PhysicalLimit{}}
g.InsertImpl(emptyProp, impl)
require.Equal(t, impl, g.GetImpl(emptyProp))
newImpl := g.GetImpl(emptyProp)
c.Assert(newImpl, Equals, impl)
newImpl = g.GetImpl(orderProp)
c.Assert(newImpl, IsNil)
orderProp := &property.PhysicalProperty{SortItems: []property.SortItem{{Col: &expression.Column{}}}}
require.Nil(t, g.GetImpl(orderProp))
}
func (s *testMemoSuite) TestEngineTypeSet(c *C) {
c.Assert(EngineAll.Contains(EngineTiDB), IsTrue)
c.Assert(EngineAll.Contains(EngineTiKV), IsTrue)
c.Assert(EngineAll.Contains(EngineTiFlash), IsTrue)
func TestEngineTypeSet(t *testing.T) {
t.Parallel()
c.Assert(EngineTiDBOnly.Contains(EngineTiDB), IsTrue)
c.Assert(EngineTiDBOnly.Contains(EngineTiKV), IsFalse)
c.Assert(EngineTiDBOnly.Contains(EngineTiFlash), IsFalse)
require.True(t, EngineAll.Contains(EngineTiDB))
require.True(t, EngineAll.Contains(EngineTiKV))
require.True(t, EngineAll.Contains(EngineTiFlash))
c.Assert(EngineTiKVOnly.Contains(EngineTiDB), IsFalse)
c.Assert(EngineTiKVOnly.Contains(EngineTiKV), IsTrue)
c.Assert(EngineTiKVOnly.Contains(EngineTiFlash), IsFalse)
require.True(t, EngineTiDBOnly.Contains(EngineTiDB))
require.False(t, EngineTiDBOnly.Contains(EngineTiKV))
require.False(t, EngineTiDBOnly.Contains(EngineTiFlash))
c.Assert(EngineTiFlashOnly.Contains(EngineTiDB), IsFalse)
c.Assert(EngineTiFlashOnly.Contains(EngineTiKV), IsFalse)
c.Assert(EngineTiFlashOnly.Contains(EngineTiFlash), IsTrue)
require.False(t, EngineTiKVOnly.Contains(EngineTiDB))
require.True(t, EngineTiKVOnly.Contains(EngineTiKV))
require.False(t, EngineTiKVOnly.Contains(EngineTiFlash))
c.Assert(EngineTiKVOrTiFlash.Contains(EngineTiDB), IsFalse)
c.Assert(EngineTiKVOrTiFlash.Contains(EngineTiKV), IsTrue)
c.Assert(EngineTiKVOrTiFlash.Contains(EngineTiFlash), IsTrue)
require.False(t, EngineTiFlashOnly.Contains(EngineTiDB))
require.False(t, EngineTiFlashOnly.Contains(EngineTiKV))
require.True(t, EngineTiFlashOnly.Contains(EngineTiFlash))
require.False(t, EngineTiKVOrTiFlash.Contains(EngineTiDB))
require.True(t, EngineTiKVOrTiFlash.Contains(EngineTiKV))
require.True(t, EngineTiKVOrTiFlash.Contains(EngineTiFlash))
}
func (s *testMemoSuite) TestFirstElemAfterDelete(c *C) {
oldExpr := NewGroupExpr(plannercore.LogicalLimit{Count: 10}.Init(s.sctx, 0))
g := NewGroupWithSchema(oldExpr, s.schema)
newExpr := NewGroupExpr(plannercore.LogicalLimit{Count: 20}.Init(s.sctx, 0))
func TestFirstElemAfterDelete(t *testing.T) {
t.Parallel()
ctx := plannercore.MockContext()
oldExpr := NewGroupExpr(plannercore.LogicalLimit{Count: 10}.Init(ctx, 0))
g := NewGroupWithSchema(oldExpr, expression.NewSchema())
newExpr := NewGroupExpr(plannercore.LogicalLimit{Count: 20}.Init(ctx, 0))
g.Insert(newExpr)
c.Assert(g.GetFirstElem(OperandLimit), NotNil)
c.Assert(g.GetFirstElem(OperandLimit).Value, Equals, oldExpr)
require.NotNil(t, g.GetFirstElem(OperandLimit))
require.Equal(t, oldExpr, g.GetFirstElem(OperandLimit).Value)
g.Delete(oldExpr)
c.Assert(g.GetFirstElem(OperandLimit), NotNil)
c.Assert(g.GetFirstElem(OperandLimit).Value, Equals, newExpr)
require.NotNil(t, g.GetFirstElem(OperandLimit))
require.Equal(t, newExpr, g.GetFirstElem(OperandLimit).Value)
g.Delete(newExpr)
c.Assert(g.GetFirstElem(OperandLimit), IsNil)
require.Nil(t, g.GetFirstElem(OperandLimit))
}
func (s *testMemoSuite) TestBuildKeyInfo(c *C) {
func TestBuildKeyInfo(t *testing.T) {
t.Parallel()
p := parser.New()
ctx := plannercore.MockContext()
is := infoschema.MockInfoSchema([]*model.TableInfo{plannercore.MockSignedTable()})
// case 1: primary key has constant constraint
stmt1, err := s.ParseOneStmt("select a from t where a = 10", "", "")
c.Assert(err, IsNil)
p1, _, err := plannercore.BuildLogicalPlan(context.Background(), s.sctx, stmt1, s.is)
c.Assert(err, IsNil)
stmt1, err := p.ParseOneStmt("select a from t where a = 10", "", "")
require.NoError(t, err)
p1, _, err := plannercore.BuildLogicalPlan(context.Background(), ctx, stmt1, is)
require.NoError(t, err)
logic1, ok := p1.(plannercore.LogicalPlan)
c.Assert(ok, IsTrue)
require.True(t, ok)
group1 := Convert2Group(logic1)
group1.BuildKeyInfo()
c.Assert(group1.Prop.MaxOneRow, IsTrue)
c.Assert(len(group1.Prop.Schema.Keys), Equals, 1)
require.True(t, group1.Prop.MaxOneRow)
require.Len(t, group1.Prop.Schema.Keys, 1)
// case 2: group by column is key
stmt2, err := s.ParseOneStmt("select b, sum(a) from t group by b", "", "")
c.Assert(err, IsNil)
p2, _, err := plannercore.BuildLogicalPlan(context.Background(), s.sctx, stmt2, s.is)
c.Assert(err, IsNil)
stmt2, err := p.ParseOneStmt("select b, sum(a) from t group by b", "", "")
require.NoError(t, err)
p2, _, err := plannercore.BuildLogicalPlan(context.Background(), ctx, stmt2, is)
require.NoError(t, err)
logic2, ok := p2.(plannercore.LogicalPlan)
c.Assert(ok, IsTrue)
require.True(t, ok)
group2 := Convert2Group(logic2)
group2.BuildKeyInfo()
c.Assert(group2.Prop.MaxOneRow, IsFalse)
c.Assert(len(group2.Prop.Schema.Keys), Equals, 1)
require.False(t, group2.Prop.MaxOneRow)
require.Len(t, group2.Prop.Schema.Keys, 1)
// case 3: build key info for new Group
newSel := plannercore.LogicalSelection{}.Init(s.sctx, 0)
newSel := plannercore.LogicalSelection{}.Init(ctx, 0)
newExpr1 := NewGroupExpr(newSel)
newExpr1.SetChildren(group2)
newGroup1 := NewGroupWithSchema(newExpr1, group2.Prop.Schema)
newGroup1.BuildKeyInfo()
c.Assert(len(newGroup1.Prop.Schema.Keys), Equals, 1)
require.Len(t, newGroup1.Prop.Schema.Keys, 1)
// case 4: build maxOneRow for new Group
newLimit := plannercore.LogicalLimit{Count: 1}.Init(s.sctx, 0)
newLimit := plannercore.LogicalLimit{Count: 1}.Init(ctx, 0)
newExpr2 := NewGroupExpr(newLimit)
newExpr2.SetChildren(group2)
newGroup2 := NewGroupWithSchema(newExpr2, group2.Prop.Schema)
newGroup2.BuildKeyInfo()
c.Assert(newGroup2.Prop.MaxOneRow, IsTrue)
require.True(t, newGroup2.Prop.MaxOneRow)
}
func (s *testMemoSuite) TestExploreMark(c *C) {
func TestExploreMark(t *testing.T) {
t.Parallel()
mark := ExploreMark(0)
c.Assert(mark.Explored(0), IsFalse)
c.Assert(mark.Explored(1), IsFalse)
require.False(t, mark.Explored(0))
require.False(t, mark.Explored(1))
mark.SetExplored(0)
mark.SetExplored(1)
c.Assert(mark.Explored(0), IsTrue)
c.Assert(mark.Explored(1), IsTrue)
require.True(t, mark.Explored(0))
require.True(t, mark.Explored(1))
mark.SetUnexplored(1)
c.Assert(mark.Explored(0), IsTrue)
c.Assert(mark.Explored(1), IsFalse)
require.True(t, mark.Explored(0))
require.False(t, mark.Explored(1))
}

26
planner/memo/main_test.go Normal file
View File

@ -0,0 +1,26 @@
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package memo
import (
"testing"
"github.com/pingcap/tidb/util/testbridge"
"go.uber.org/goleak"
)
func TestMain(m *testing.M) {
testbridge.WorkaroundGoCheckFlags()
goleak.VerifyTestMain(m)
}

View File

@ -14,73 +14,82 @@
package memo
import (
. "github.com/pingcap/check"
"testing"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/stretchr/testify/require"
)
func (s *testMemoSuite) TestGetOperand(c *C) {
c.Assert(GetOperand(&plannercore.LogicalJoin{}), Equals, OperandJoin)
c.Assert(GetOperand(&plannercore.LogicalAggregation{}), Equals, OperandAggregation)
c.Assert(GetOperand(&plannercore.LogicalProjection{}), Equals, OperandProjection)
c.Assert(GetOperand(&plannercore.LogicalSelection{}), Equals, OperandSelection)
c.Assert(GetOperand(&plannercore.LogicalApply{}), Equals, OperandApply)
c.Assert(GetOperand(&plannercore.LogicalMaxOneRow{}), Equals, OperandMaxOneRow)
c.Assert(GetOperand(&plannercore.LogicalTableDual{}), Equals, OperandTableDual)
c.Assert(GetOperand(&plannercore.DataSource{}), Equals, OperandDataSource)
c.Assert(GetOperand(&plannercore.LogicalUnionScan{}), Equals, OperandUnionScan)
c.Assert(GetOperand(&plannercore.LogicalUnionAll{}), Equals, OperandUnionAll)
c.Assert(GetOperand(&plannercore.LogicalSort{}), Equals, OperandSort)
c.Assert(GetOperand(&plannercore.LogicalTopN{}), Equals, OperandTopN)
c.Assert(GetOperand(&plannercore.LogicalLock{}), Equals, OperandLock)
c.Assert(GetOperand(&plannercore.LogicalLimit{}), Equals, OperandLimit)
func TestGetOperand(t *testing.T) {
t.Parallel()
require.Equal(t, OperandJoin, GetOperand(&plannercore.LogicalJoin{}))
require.Equal(t, OperandAggregation, GetOperand(&plannercore.LogicalAggregation{}))
require.Equal(t, OperandProjection, GetOperand(&plannercore.LogicalProjection{}))
require.Equal(t, OperandSelection, GetOperand(&plannercore.LogicalSelection{}))
require.Equal(t, OperandApply, GetOperand(&plannercore.LogicalApply{}))
require.Equal(t, OperandMaxOneRow, GetOperand(&plannercore.LogicalMaxOneRow{}))
require.Equal(t, OperandTableDual, GetOperand(&plannercore.LogicalTableDual{}))
require.Equal(t, OperandDataSource, GetOperand(&plannercore.DataSource{}))
require.Equal(t, OperandUnionScan, GetOperand(&plannercore.LogicalUnionScan{}))
require.Equal(t, OperandUnionAll, GetOperand(&plannercore.LogicalUnionAll{}))
require.Equal(t, OperandSort, GetOperand(&plannercore.LogicalSort{}))
require.Equal(t, OperandTopN, GetOperand(&plannercore.LogicalTopN{}))
require.Equal(t, OperandLock, GetOperand(&plannercore.LogicalLock{}))
require.Equal(t, OperandLimit, GetOperand(&plannercore.LogicalLimit{}))
}
func (s *testMemoSuite) TestOperandMatch(c *C) {
c.Assert(OperandAny.Match(OperandLimit), IsTrue)
c.Assert(OperandAny.Match(OperandSelection), IsTrue)
c.Assert(OperandAny.Match(OperandJoin), IsTrue)
c.Assert(OperandAny.Match(OperandMaxOneRow), IsTrue)
c.Assert(OperandAny.Match(OperandAny), IsTrue)
func TestOperandMatch(t *testing.T) {
t.Parallel()
c.Assert(OperandLimit.Match(OperandAny), IsTrue)
c.Assert(OperandSelection.Match(OperandAny), IsTrue)
c.Assert(OperandJoin.Match(OperandAny), IsTrue)
c.Assert(OperandMaxOneRow.Match(OperandAny), IsTrue)
c.Assert(OperandAny.Match(OperandAny), IsTrue)
require.True(t, OperandAny.Match(OperandLimit))
require.True(t, OperandAny.Match(OperandSelection))
require.True(t, OperandAny.Match(OperandJoin))
require.True(t, OperandAny.Match(OperandMaxOneRow))
require.True(t, OperandAny.Match(OperandAny))
c.Assert(OperandLimit.Match(OperandLimit), IsTrue)
c.Assert(OperandSelection.Match(OperandSelection), IsTrue)
c.Assert(OperandJoin.Match(OperandJoin), IsTrue)
c.Assert(OperandMaxOneRow.Match(OperandMaxOneRow), IsTrue)
c.Assert(OperandAny.Match(OperandAny), IsTrue)
require.True(t, OperandLimit.Match(OperandAny))
require.True(t, OperandSelection.Match(OperandAny))
require.True(t, OperandJoin.Match(OperandAny))
require.True(t, OperandMaxOneRow.Match(OperandAny))
require.True(t, OperandAny.Match(OperandAny))
c.Assert(OperandLimit.Match(OperandSelection), IsFalse)
c.Assert(OperandLimit.Match(OperandJoin), IsFalse)
c.Assert(OperandLimit.Match(OperandMaxOneRow), IsFalse)
require.True(t, OperandLimit.Match(OperandLimit))
require.True(t, OperandSelection.Match(OperandSelection))
require.True(t, OperandJoin.Match(OperandJoin))
require.True(t, OperandMaxOneRow.Match(OperandMaxOneRow))
require.True(t, OperandAny.Match(OperandAny))
require.False(t, OperandLimit.Match(OperandSelection))
require.False(t, OperandLimit.Match(OperandJoin))
require.False(t, OperandLimit.Match(OperandMaxOneRow))
}
func (s *testMemoSuite) TestNewPattern(c *C) {
func TestNewPattern(t *testing.T) {
t.Parallel()
p := NewPattern(OperandAny, EngineAll)
c.Assert(p.Operand, Equals, OperandAny)
c.Assert(p.Children, IsNil)
require.Equal(t, OperandAny, p.Operand)
require.Nil(t, p.Children)
p = NewPattern(OperandJoin, EngineAll)
c.Assert(p.Operand, Equals, OperandJoin)
c.Assert(p.Children, IsNil)
require.Equal(t, OperandJoin, p.Operand)
require.Nil(t, p.Children)
}
func (s *testMemoSuite) TestPatternSetChildren(c *C) {
func TestPatternSetChildren(t *testing.T) {
t.Parallel()
p := NewPattern(OperandAny, EngineAll)
p.SetChildren(NewPattern(OperandLimit, EngineAll))
c.Assert(len(p.Children), Equals, 1)
c.Assert(p.Children[0].Operand, Equals, OperandLimit)
c.Assert(p.Children[0].Children, IsNil)
require.Len(t, p.Children, 1)
require.Equal(t, OperandLimit, p.Children[0].Operand)
require.Nil(t, p.Children[0].Children)
p = NewPattern(OperandJoin, EngineAll)
p.SetChildren(NewPattern(OperandProjection, EngineAll), NewPattern(OperandSelection, EngineAll))
c.Assert(len(p.Children), Equals, 2)
c.Assert(p.Children[0].Operand, Equals, OperandProjection)
c.Assert(p.Children[0].Children, IsNil)
c.Assert(p.Children[1].Operand, Equals, OperandSelection)
c.Assert(p.Children[1].Children, IsNil)
require.Len(t, p.Children, 2)
require.Equal(t, OperandProjection, p.Children[0].Operand)
require.Nil(t, p.Children[0].Children)
require.Equal(t, OperandSelection, p.Children[1].Operand)
require.Nil(t, p.Children[1].Children)
}