Skip to content

Commit 1546500

Browse files
authored
planner: address collation ambiguity in scalar function construction during predicate simplification. (#57049) (#57476)
close #56479
1 parent 2cb550a commit 1546500

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

pkg/planner/core/rule_predicate_simplification.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,10 @@ func unsatisfiable(ctx base.PlanContext, p1, p2 expression.Expression) bool {
221221
// Copy constant from equal predicate into other predicate.
222222
equalValue := equalPred.(*expression.ScalarFunction)
223223
otherValue := otherPred.(*expression.ScalarFunction)
224-
newPred := expression.NewFunctionInternal(ctx.GetExprCtx(), otherValue.FuncName.L, otherValue.RetType, equalValue.GetArgs()[1], otherValue.GetArgs()[1])
224+
newPred, err := expression.NewFunction(ctx.GetExprCtx(), otherValue.FuncName.L, otherValue.RetType, equalValue.GetArgs()[1], otherValue.GetArgs()[1])
225+
if err != nil {
226+
return false
227+
}
225228
newPredList := make([]expression.Expression, 0, 1)
226229
newPredList = append(newPredList, newPred)
227230
newPredList = expression.PropagateConstant(ctx.GetExprCtx(), newPredList)
@@ -273,7 +276,11 @@ func updateOrPredicate(ctx base.PlanContext, orPredicateList expression.Expressi
273276
} else if emptyFirst && emptySecond {
274277
return &expression.Constant{Value: types.NewIntDatum(0), RetType: types.NewFieldType(mysql.TypeTiny)}
275278
}
276-
return expression.NewFunctionInternal(ctx.GetExprCtx(), ast.LogicOr, v.RetType, firstCondition, secondCondition)
279+
newPred, err := expression.NewFunction(ctx.GetExprCtx(), ast.LogicOr, v.RetType, firstCondition, secondCondition)
280+
if err != nil {
281+
return orPredicateList
282+
}
283+
return newPred
277284
}
278285

279286
// pruneEmptyORBranches applies iteratively updateOrPredicate for each pair of OR predicate

tests/integrationtest/r/planner/core/issuetest/planner_issue.result

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,3 +750,12 @@ NULL NULL 2 2 4 2
750750
show warnings;
751751
Level Code Message
752752
drop table if exists t1, t2, t3, t4;
753+
drop table if exists tl6e913fb9;
754+
CREATE TABLE `tl6e913fb9` (
755+
`col_36` varchar(175) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'asMF',
756+
KEY `idx_35_5` (`col_36`(1)),
757+
PRIMARY KEY (`col_36`) /*T![clustered_index] NONCLUSTERED */,
758+
KEY `idx_65` (`col_36`(5))
759+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
760+
with cte_192 ( col_1101,col_1102,col_1103,col_1104 ) AS ( select /*+ use_index_merge( tl6e913fb9 ) */ replace( tl6e913fb9.col_36 , tl6e913fb9.col_36 , tl6e913fb9.col_36 ) as r0 , space( 0 ) as r1 , min( distinct tl6e913fb9.col_36 ) as r2 , count( distinct tl6e913fb9.col_36 ) as r3 from tl6e913fb9 where tl6e913fb9.col_36 between 'n92ok$B%W#UU%O' and '()c=KVQ=T%-vzGJ' and tl6e913fb9.col_36 in ( 'T+kf' ,'Lvluod2H' ,'3#Omx@pC^fFkeH' ,'=b$z' ) group by tl6e913fb9.col_36 having tl6e913fb9.col_36 = 'xjV@' or IsNull( tl6e913fb9.col_36 ) ) ( select 1,col_1101,col_1102,col_1103,col_1104 from cte_192 where not( IsNull( cte_192.col_1102 ) ) order by 1,2,3,4,5 limit 72850972 );
761+
1 col_1101 col_1102 col_1103 col_1104

tests/integrationtest/t/planner/core/issuetest/planner_issue.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,3 +517,13 @@ SELECT t2.a,t2.b,t3.a,t3.b,t4.a,t4.b
517517
ON t3.a=1 AND t3.b=t2.b AND t2.b=t4.b order by 1, 2, 3, 4, 5;
518518
show warnings;
519519
drop table if exists t1, t2, t3, t4;
520+
521+
# TestIssue56479
522+
drop table if exists tl6e913fb9;
523+
CREATE TABLE `tl6e913fb9` (
524+
`col_36` varchar(175) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'asMF',
525+
KEY `idx_35_5` (`col_36`(1)),
526+
PRIMARY KEY (`col_36`) /*T![clustered_index] NONCLUSTERED */,
527+
KEY `idx_65` (`col_36`(5))
528+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
529+
with cte_192 ( col_1101,col_1102,col_1103,col_1104 ) AS ( select /*+ use_index_merge( tl6e913fb9 ) */ replace( tl6e913fb9.col_36 , tl6e913fb9.col_36 , tl6e913fb9.col_36 ) as r0 , space( 0 ) as r1 , min( distinct tl6e913fb9.col_36 ) as r2 , count( distinct tl6e913fb9.col_36 ) as r3 from tl6e913fb9 where tl6e913fb9.col_36 between 'n92ok$B%W#UU%O' and '()c=KVQ=T%-vzGJ' and tl6e913fb9.col_36 in ( 'T+kf' ,'Lvluod2H' ,'3#Omx@pC^fFkeH' ,'=b$z' ) group by tl6e913fb9.col_36 having tl6e913fb9.col_36 = 'xjV@' or IsNull( tl6e913fb9.col_36 ) ) ( select 1,col_1101,col_1102,col_1103,col_1104 from cte_192 where not( IsNull( cte_192.col_1102 ) ) order by 1,2,3,4,5 limit 72850972 );

0 commit comments

Comments
 (0)