Skip to content

Commit

Permalink
for test
Browse files Browse the repository at this point in the history
  • Loading branch information
Defined2014 committed Aug 29, 2024
1 parent d041cd0 commit dd6900b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
29 changes: 24 additions & 5 deletions pkg/expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/pingcap/tidb/pkg/sessionctx/stmtctx"
"github.com/pingcap/tidb/pkg/types"
"github.com/pingcap/tidb/pkg/util/chunk"
"github.com/pingcap/tidb/pkg/util/collate"
"github.com/pingcap/tipb/go-tipb"
)

Expand Down Expand Up @@ -288,7 +289,8 @@ func (c *castAsDecimalFunctionClass) getFunction(ctx BuildContext, args []Expres
type castAsStringFunctionClass struct {
baseFunctionClass

tp *types.FieldType
tp *types.FieldType
isExplicitCharSet bool
}

func (c *castAsStringFunctionClass) getFunction(ctx BuildContext, args []Expression) (sig builtinFunc, err error) {
Expand All @@ -299,6 +301,16 @@ func (c *castAsStringFunctionClass) getFunction(ctx BuildContext, args []Express
if err != nil {
return nil, err
}
if c.isExplicitCharSet {
bf.SetCharsetAndCollation(c.tp.GetCharset(), c.tp.GetCollate())
bf.setCollator(collate.GetCollator(c.tp.GetCollate()))
bf.SetCoercibility(CoercibilityExplicit)
if c.tp.GetCharset() == charset.CharsetASCII {
bf.SetRepertoire(ASCII)
} else {
bf.SetRepertoire(UNICODE)
}
}
if args[0].GetType(ctx.GetEvalCtx()).Hybrid() {
sig = &builtinCastStringAsStringSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_CastStringAsString)
Expand Down Expand Up @@ -2265,7 +2277,7 @@ func CanImplicitEvalReal(expr Expression) bool {
// BuildCastFunction4Union build a implicitly CAST ScalarFunction from the Union
// Expression.
func BuildCastFunction4Union(ctx BuildContext, expr Expression, tp *types.FieldType) (res Expression) {
res, err := BuildCastFunctionWithCheck(ctx, expr, tp, true)
res, err := BuildCastFunctionWithCheck(ctx, expr, tp, true, false)
terror.Log(err)
return
}
Expand Down Expand Up @@ -2302,13 +2314,20 @@ func BuildCastCollationFunction(ctx BuildContext, expr Expression, ec *ExprColla

// BuildCastFunction builds a CAST ScalarFunction from the Expression.
func BuildCastFunction(ctx BuildContext, expr Expression, tp *types.FieldType) (res Expression) {
res, err := BuildCastFunctionWithCheck(ctx, expr, tp, false)
res, err := BuildCastFunctionWithCheck(ctx, expr, tp, false, false)
terror.Log(err)
return
}

// BuildCastFunctionExplicitCharset builds a CAST ScalarFunction from the Expression.
func BuildCastFunctionExplicitCharset(ctx BuildContext, expr Expression, tp *types.FieldType) (res Expression) {
res, err := BuildCastFunctionWithCheck(ctx, expr, tp, false, true)
terror.Log(err)
return
}

// BuildCastFunctionWithCheck builds a CAST ScalarFunction from the Expression and return error if any.
func BuildCastFunctionWithCheck(ctx BuildContext, expr Expression, tp *types.FieldType, inUnion bool) (res Expression, err error) {
func BuildCastFunctionWithCheck(ctx BuildContext, expr Expression, tp *types.FieldType, inUnion bool, isExplicitCharSet bool) (res Expression, err error) {
argType := expr.GetType(ctx.GetEvalCtx())
// If source argument's nullable, then target type should be nullable
if !mysql.HasNotNullFlag(argType.GetFlag()) {
Expand Down Expand Up @@ -2336,7 +2355,7 @@ func BuildCastFunctionWithCheck(ctx BuildContext, expr Expression, tp *types.Fie
case types.ETVectorFloat32:
fc = &castAsVectorFloat32FunctionClass{baseFunctionClass{ast.Cast, 1, 1}, tp}
case types.ETString:
fc = &castAsStringFunctionClass{baseFunctionClass{ast.Cast, 1, 1}, tp}
fc = &castAsStringFunctionClass{baseFunctionClass{ast.Cast, 1, 1}, tp, isExplicitCharSet}
if expr.GetType(ctx.GetEvalCtx()).GetType() == mysql.TypeBit {
tp.SetFlen((expr.GetType(ctx.GetEvalCtx()).GetFlen() + 7) / 8)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/expression/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ func ColumnSubstituteImpl(ctx BuildContext, expr Expression, schema *Schema, new
flag := v.RetType.GetFlag()
var e Expression
if v.FuncName.L == ast.Cast {
e = BuildCastFunction(ctx, newArg, v.RetType)
e = BuildCastFunctionExplicitCharset(ctx, newArg, v.RetType)
} else {
// for grouping function recreation, use clone (meta included) instead of newFunction
e = v.Clone()
Expand Down
2 changes: 1 addition & 1 deletion pkg/planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1504,7 +1504,7 @@ func (er *expressionRewriter) Leave(originInNode ast.Node) (retNode ast.Node, ok
return retNode, false
}

castFunction, err := expression.BuildCastFunctionWithCheck(er.sctx, arg, v.Tp, false)
castFunction, err := expression.BuildCastFunctionWithCheck(er.sctx, arg, v.Tp, false, v.ExplicitCharSet)
if err != nil {
er.err = err
return retNode, false
Expand Down

0 comments on commit dd6900b

Please sign in to comment.