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: label predicate_simplification as over-optimized for plan cache (#43408) #43415

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
38 changes: 38 additions & 0 deletions planner/core/plan_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,44 @@ func TestPlanCacheUnsafeRange(t *testing.T) {
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
}

func TestIssue43405(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)

tk.MustExec(`create table t (a int)`)
tk.MustExec(`insert into t values (1), (2), (3), (4)`)
tk.MustExec(`prepare st from 'select * from t where a!=? and a in (?, ?, ?)'`)
tk.MustExec(`set @a=1, @b=2, @c=3, @d=4`)
tk.MustQuery(`execute st using @a, @a, @a, @a`).Sort().Check(testkit.Rows())
tk.MustQuery(`show warnings`).Check(testkit.Rows("Warning 1105 skip prepared plan-cache: NE/INList simplification is triggered"))
tk.MustQuery(`execute st using @a, @a, @b, @c`).Sort().Check(testkit.Rows("2", "3"))
tk.MustQuery(`show warnings`).Check(testkit.Rows("Warning 1105 skip prepared plan-cache: NE/INList simplification is triggered"))
tk.MustQuery(`execute st using @a, @b, @c, @d`).Sort().Check(testkit.Rows("2", "3", "4"))
tk.MustQuery(`show warnings`).Check(testkit.Rows("Warning 1105 skip prepared plan-cache: NE/INList simplification is triggered"))

tk.MustExec(`CREATE TABLE UK_SIGNED_19384 (
COL1 decimal(37,4) unsigned DEFAULT NULL COMMENT 'WITH DEFAULT',
COL2 varchar(20) COLLATE utf8mb4_bin DEFAULT NULL,
COL4 datetime DEFAULT NULL,
COL3 bigint DEFAULT NULL,
COL5 float DEFAULT NULL,
UNIQUE KEY UK_COL1 (COL1)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin`)
tk.MustExec(`INSERT INTO UK_SIGNED_19384 VALUES
(729024465529090.5423,'劗驻胭毤橰亀讁陶ĉ突錌ͳ河碡祁聓兕锻觰俆','4075-07-11 12:02:57',6021562653572886552,1.93349e38),
(492790234219503.0846,'硴皡箒嫹璞玚囑蚂身囈軔獰髴囥慍廂頚禌浖蕐','1193-09-27 12:13:40',1836453747944153034,-2.67982e38),
(471841432147994.4981,'豻貐裝濂婝蒙蘦镢県蟎髓蓼窘搴熾臐哥递泒執','1618-01-24 05:06:44',6669616052974883820,9.38232e37)`)
tk.MustExec(`prepare stmt from 'select/*+ tidb_inlj(t1) */ t1.col1 from UK_SIGNED_19384 t1 join UK_SIGNED_19384 t2 on t1.col1 = t2.col1 where t1. col1 != ? and t2. col1 in (?, ?, ?)'`)
tk.MustExec(`set @a=999999999999999999999999999999999.9999, @b=999999999999999999999999999999999.9999, @c=999999999999999999999999999999999.9999, @d=999999999999999999999999999999999.9999`)
tk.MustQuery(`execute stmt using @a,@b,@c,@d`).Check(testkit.Rows()) // empty result
tk.MustExec(`set @a=895769331208356.9029, @b=471841432147994.4981, @c=729024465529090.5423, @d=492790234219503.0846`)
tk.MustQuery(`execute stmt using @a,@b,@c,@d`).Sort().Check(testkit.Rows(
"471841432147994.4981",
"492790234219503.0846",
"729024465529090.5423"))
}

func TestIssue40296(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
Expand Down
14 changes: 9 additions & 5 deletions planner/core/rule_predicate_simplification.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ package core

import (
"context"
"errors"

"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/sessionctx"
"golang.org/x/exp/slices"
)

Expand Down Expand Up @@ -90,8 +92,8 @@ func updateInPredicate(inPredicate expression.Expression, notEQPredicate express
var lastValue *expression.Constant
for _, element := range v.GetArgs() {
value, valueOK := element.(*expression.Constant)
redudantValue := valueOK && value.Equal(v.GetCtx(), notEQValue)
if !redudantValue {
redundantValue := valueOK && value.Equal(v.GetCtx(), notEQValue)
if !redundantValue {
newValues = append(newValues, element)
}
if valueOK {
Expand All @@ -110,7 +112,7 @@ func updateInPredicate(inPredicate expression.Expression, notEQPredicate express
return newPred, specialCase
}

func applyPredicateSimplification(predicates []expression.Expression) []expression.Expression {
func applyPredicateSimplification(sctx sessionctx.Context, predicates []expression.Expression) []expression.Expression {
if len(predicates) <= 1 {
return predicates
}
Expand All @@ -125,11 +127,13 @@ func applyPredicateSimplification(predicates []expression.Expression) []expressi
if iCol == jCol {
if iType == notEqualPredicate && jType == inListPredicate {
predicates[j], specialCase = updateInPredicate(jthPredicate, ithPredicate)
sctx.GetSessionVars().StmtCtx.SetSkipPlanCache(errors.New("NE/INList simplification is triggered"))
if !specialCase {
removeValues = append(removeValues, i)
}
} else if iType == inListPredicate && jType == notEqualPredicate {
predicates[i], specialCase = updateInPredicate(ithPredicate, jthPredicate)
sctx.GetSessionVars().StmtCtx.SetSkipPlanCache(errors.New("NE/INList simplification is triggered"))
if !specialCase {
removeValues = append(removeValues, j)
}
Expand All @@ -148,8 +152,8 @@ func applyPredicateSimplification(predicates []expression.Expression) []expressi

func (s *DataSource) predicateSimplification(opt *logicalOptimizeOp) LogicalPlan {
p := s.self.(*DataSource)
p.pushedDownConds = applyPredicateSimplification(p.pushedDownConds)
p.allConds = applyPredicateSimplification(p.allConds)
p.pushedDownConds = applyPredicateSimplification(p.ctx, p.pushedDownConds)
p.allConds = applyPredicateSimplification(p.ctx, p.allConds)
return p
}

Expand Down