Skip to content

Commit

Permalink
planner: prune duplicate expr in sort (#20325) (#20333)
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
ti-srebot authored Oct 13, 2020
1 parent 08c63bf commit 8c6879c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
46 changes: 46 additions & 0 deletions planner/core/logical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,31 @@ func (s *testPlanSuite) TestColumnPruning(c *C) {
}
}

func (s *testPlanSuite) TestSortByItemsPruning(c *C) {
defer testleak.AfterTest(c)()
var (
input []string
output [][]string
)
s.testData.GetTestCases(c, &input, &output)
s.testData.OnRecord(func() {
output = make([][]string, len(input))
})

ctx := context.Background()
for i, tt := range input {
comment := Commentf("for %s", tt)
stmt, err := s.ParseOneStmt(tt, "", "")
c.Assert(err, IsNil, comment)

p, _, err := BuildLogicalPlan(ctx, s.ctx, stmt, s.is)
c.Assert(err, IsNil)
lp, err := logicalOptimize(ctx, flagEliminateProjection|flagPredicatePushDown|flagPrunColumns|flagPrunColumnsAgain, p.(LogicalPlan))
c.Assert(err, IsNil)
s.checkOrderByItems(lp, c, &output[i], comment)
}
}

func (s *testPlanSuite) TestProjectionEliminator(c *C) {
defer testleak.AfterTest(c)()
tests := []struct {
Expand Down Expand Up @@ -620,6 +645,27 @@ func (s *testPlanSuite) checkDataSourceCols(p LogicalPlan, c *C, ans map[int][]s
}
}

func (s *testPlanSuite) checkOrderByItems(p LogicalPlan, c *C, colList *[]string, comment CommentInterface) {
switch p := p.(type) {
case *LogicalSort:
s.testData.OnRecord(func() {
*colList = make([]string, len(p.ByItems))
})
for i, col := range p.ByItems {
s.testData.OnRecord(func() {
(*colList)[i] = col.String()
})
s := col.String()
c.Assert(s, Equals, (*colList)[i], comment)
}
}
children := p.Children()
c.Assert(len(children), LessEqual, 1, Commentf("For %v Expected <= 1 Child", comment))
for _, child := range children {
s.checkOrderByItems(child, c, colList, comment)
}
}

func (s *testPlanSuite) TestValidate(c *C) {
defer testleak.AfterTest(c)()
tests := []struct {
Expand Down
8 changes: 7 additions & 1 deletion planner/core/rule_column_pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,15 @@ func (la *LogicalAggregation) PruneColumns(parentUsedCols []*expression.Column)

func pruneByItems(old []*util.ByItems) (new []*util.ByItems, parentUsedCols []*expression.Column) {
new = make([]*util.ByItems, 0, len(old))
seen := make(map[string]struct{}, len(old))
for _, byItem := range old {
hash := string(byItem.Expr.HashCode(nil))
_, hashMatch := seen[hash]
seen[hash] = struct{}{}
cols := expression.ExtractColumns(byItem.Expr)
if len(cols) == 0 {
if hashMatch {
// do nothing, should be filtered
} else if len(cols) == 0 {
if !expression.IsRuntimeConstExpr(byItem.Expr) {
new = append(new, byItem)
}
Expand Down
8 changes: 8 additions & 0 deletions planner/core/testdata/plan_suite_unexported_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,14 @@
"select count(1) from (select count(b) as cnt from t group by c) t1"
]
},
{
"name": "TestSortByItemsPruning",
"cases": [
"select * from t where a > 1 order by a asc, a asc limit 10",
"select * from t where a > 1 order by a asc, b asc, a asc, c asc limit 10",
"select * from t where a > 1 order by pow(a, 2) asc, b asc, pow(a, 2) asc, c asc limit 10"
]
},
{
"name": "TestDeriveNotNullConds",
"cases": [
Expand Down
18 changes: 18 additions & 0 deletions planner/core/testdata/plan_suite_unexported_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,24 @@
}
]
},
{
"Name": "TestSortByItemsPruning",
"Cases": [
[
"test.t.a"
],
[
"test.t.a",
"test.t.b",
"test.t.c"
],
[
"pow(cast(test.t.a, double BINARY), 2)",
"test.t.b",
"test.t.c"
]
]
},
{
"Name": "TestDeriveNotNullConds",
"Cases": [
Expand Down

0 comments on commit 8c6879c

Please sign in to comment.