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: refine collation handling for between #30793

Merged
merged 5 commits into from
Dec 16, 2021
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
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to align the Coercibility?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No

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