Skip to content

Commit

Permalink
planner: refine collation handling for between (#30793)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjhuang2016 authored Dec 16, 2021
1 parent fa10cd1 commit ae5638c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
11 changes: 11 additions & 0 deletions expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,17 @@ func BuildCastFunction4Union(ctx sessionctx.Context, expr Expression, tp *types.
return BuildCastFunction(ctx, expr, tp)
}

// BuildCastCollationFunction builds a ScalarFunction which casts the collation.
func BuildCastCollationFunction(ctx sessionctx.Context, expr Expression, ec *ExprCollation) Expression {
if expr.GetType().Collate == ec.Collation {
return expr
}
tp := expr.GetType().Clone()
tp.Charset, tp.Collate = ec.Charset, ec.Collation
newExpr := BuildCastFunction(ctx, expr, tp)
return newExpr
}

// BuildCastFunction builds a CAST ScalarFunction from the Expression.
func BuildCastFunction(ctx sessionctx.Context, expr Expression, tp *types.FieldType) (res Expression) {
expr = TryPushCastIntoControlFunctionForHybridType(ctx, expr, tp)
Expand Down
12 changes: 6 additions & 6 deletions planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1686,19 +1686,19 @@ func (er *expressionRewriter) betweenToExpression(v *ast.BetweenExpr) {
return
}

expr = expression.BuildCastCollationFunction(er.sctx, expr, coll)
lexp = expression.BuildCastCollationFunction(er.sctx, lexp, coll)
rexp = expression.BuildCastCollationFunction(er.sctx, rexp, coll)

var l, r expression.Expression
l, er.err = expression.NewFunctionBase(er.sctx, ast.GE, &v.Type, expr, lexp)
l, er.err = expression.NewFunction(er.sctx, ast.GE, &v.Type, expr, lexp)
if er.err != nil {
return
}
r, er.err = expression.NewFunctionBase(er.sctx, ast.LE, &v.Type, expr, rexp)
r, er.err = expression.NewFunction(er.sctx, ast.LE, &v.Type, expr, rexp)
if er.err != nil {
return
}
l.SetCharsetAndCollation(coll.Charset, coll.Collation)
r.SetCharsetAndCollation(coll.Charset, coll.Collation)
l = expression.FoldConstant(l)
r = expression.FoldConstant(r)
function, err := er.newFunction(ast.LogicAnd, &v.Type, l, r)
if err != nil {
er.err = err
Expand Down

0 comments on commit ae5638c

Please sign in to comment.