Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: keep unfoldable exprs when pruning columns for ORDER BY items #10064

Merged
merged 5 commits into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions cmd/explaintest/r/topn_push_down.result
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,16 @@ create table t1(a bigint, b bigint);
create table t2(a bigint, b bigint);
desc select * from t1 where t1.a in (select t2.a as a from t2 where t2.b > t1.b order by t1.b limit 1);
id count task operator info
Apply_15 9990.00 root semi join, inner:Selection_19, equal:[eq(test.t1.a, test.t2.a)]
├─TableReader_18 9990.00 root data:Selection_17
│ └─Selection_17 9990.00 cop not(isnull(test.t1.a))
│ └─TableScan_16 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo
└─Selection_19 0.80 root not(isnull(test.t2.a))
└─Limit_20 1.00 root offset:0, count:1
└─TableReader_26 1.00 root data:Limit_25
└─Limit_25 1.00 cop offset:0, count:1
└─Selection_24 1.00 cop gt(test.t2.b, test.t1.b)
└─TableScan_23 1.25 cop table:t2, range:[-inf,+inf], keep order:false, stats:pseudo
Apply_14 9990.00 root semi join, inner:Selection_18, equal:[eq(test.t1.a, test.t2.a)]
├─TableReader_17 9990.00 root data:Selection_16
│ └─Selection_16 9990.00 cop not(isnull(test.t1.a))
│ └─TableScan_15 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo
└─Selection_18 0.80 root not(isnull(test.t2.a))
└─TopN_19 1.00 root test.t1.b:asc, offset:0, count:1
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
└─TableReader_25 1.00 root data:TopN_24
└─TopN_24 1.00 cop test.t1.b:asc, offset:0, count:1
└─Selection_23 8000.00 cop gt(test.t2.b, test.t1.b)
└─TableScan_22 10000.00 cop table:t2, range:[-inf,+inf], keep order:false, stats:pseudo
desc select * from t1 where t1.a in (select a from (select t2.a as a, t1.b as b from t2 where t2.b > t1.b) x order by b limit 1);
id count task operator info
Apply_17 9990.00 root semi join, inner:Selection_21, equal:[eq(test.t1.a, x.a)]
Expand Down
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",
))
}
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