Skip to content

Commit

Permalink
planner: keep unfoldable exprs when pruning columns for ORDER BY items (
Browse files Browse the repository at this point in the history
  • Loading branch information
lysu authored and winoros committed Apr 15, 2019
1 parent 9d25a85 commit 0972a6f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
46 changes: 46 additions & 0 deletions executor/sort_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2019 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 executor_test

import (
. "github.com/pingcap/check"
"github.com/pingcap/tidb/util/testkit"
)

func (s *testSuite2) TestSortRand(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b int);")

tk.MustQuery("explain select a from t order by rand()").Check(testkit.Rows(
"Projection_8 10000.00 root test.t.a",
"└─Sort_4 10000.00 root col_1:asc",
" └─Projection_9 10000.00 root test.t.a, rand()",
" └─TableReader_7 10000.00 root data:TableScan_6",
" └─TableScan_6 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo",
))

tk.MustQuery("explain select a, b from t order by abs(2)").Check(testkit.Rows(
"TableReader_8 10000.00 root data:TableScan_7",
"└─TableScan_7 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo"))

tk.MustQuery("explain select a from t order by abs(rand())+1").Check(testkit.Rows(
"Projection_8 10000.00 root test.t.a",
"└─Sort_4 10000.00 root col_1:asc",
" └─Projection_9 10000.00 root test.t.a, plus(abs(rand()), 1)",
" └─TableReader_7 10000.00 root data:TableScan_6",
" └─TableScan_6 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo",
))
}
2 changes: 1 addition & 1 deletion expression/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (col *CorrelatedColumn) IsCorrelated() bool {

// ConstItem implements Expression interface.
func (col *CorrelatedColumn) ConstItem() bool {
return false
return true
}

// Decorrelate implements Expression interface.
Expand Down
2 changes: 1 addition & 1 deletion expression/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (s *testEvaluatorSuite) TestColumn(c *C) {
c.Assert(corCol.Equal(nil, corCol), IsTrue)
c.Assert(corCol.Equal(nil, invalidCorCol), IsFalse)
c.Assert(corCol.IsCorrelated(), IsTrue)
c.Assert(corCol.ConstItem(), IsFalse)
c.Assert(corCol.ConstItem(), IsTrue)
c.Assert(corCol.Decorrelate(schema).Equal(nil, col), IsTrue)
c.Assert(invalidCorCol.Decorrelate(schema).Equal(nil, invalidCorCol), IsTrue)

Expand Down
3 changes: 3 additions & 0 deletions planner/core/rule_column_pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ func (ls *LogicalSort) PruneColumns(parentUsedCols []*expression.Column) error {
for i := len(ls.ByItems) - 1; i >= 0; i-- {
cols := expression.ExtractColumns(ls.ByItems[i].Expr)
if len(cols) == 0 {
if !ls.ByItems[i].Expr.ConstItem() {
continue
}
ls.ByItems = append(ls.ByItems[:i], ls.ByItems[i+1:]...)
} else {
parentUsedCols = append(parentUsedCols, cols...)
Expand Down

0 comments on commit 0972a6f

Please sign in to comment.