Skip to content

Commit 0e39d0d

Browse files
authored
expression: let cast function supports explicit set charset (#55724) (#58154)
close #55677
1 parent 1e2bb37 commit 0e39d0d

File tree

13 files changed

+137
-16
lines changed

13 files changed

+137
-16
lines changed

pkg/expression/bench_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1449,7 +1449,7 @@ func genVecBuiltinFuncBenchCase(ctx BuildContext, funcName string, testCase vecE
14491449
case types.ETJson:
14501450
fc = &castAsJSONFunctionClass{baseFunctionClass{ast.Cast, 1, 1}, tp}
14511451
case types.ETString:
1452-
fc = &castAsStringFunctionClass{baseFunctionClass{ast.Cast, 1, 1}, tp}
1452+
fc = &castAsStringFunctionClass{baseFunctionClass{ast.Cast, 1, 1}, tp, false}
14531453
}
14541454
baseFunc, err = fc.getFunction(ctx, cols)
14551455
} else if funcName == ast.GetVar {

pkg/expression/builtin.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,35 @@ func newBaseBuiltinCastFunc(builtinFunc baseBuiltinFunc, inUnion bool) baseBuilt
448448
}
449449
}
450450

451+
func newBaseBuiltinCastFunc4String(ctx BuildContext, funcName string, args []Expression, tp *types.FieldType, isExplicitCharset bool) (baseBuiltinFunc, error) {
452+
var bf baseBuiltinFunc
453+
var err error
454+
if isExplicitCharset {
455+
bf = baseBuiltinFunc{
456+
bufAllocator: newLocalColumnPool(),
457+
childrenVectorizedOnce: new(sync.Once),
458+
459+
args: args,
460+
tp: tp,
461+
}
462+
bf.SetCharsetAndCollation(tp.GetCharset(), tp.GetCollate())
463+
bf.setCollator(collate.GetCollator(tp.GetCollate()))
464+
bf.SetCoercibility(CoercibilityExplicit)
465+
bf.SetExplicitCharset(true)
466+
if tp.GetCharset() == charset.CharsetASCII {
467+
bf.SetRepertoire(ASCII)
468+
} else {
469+
bf.SetRepertoire(UNICODE)
470+
}
471+
} else {
472+
bf, err = newBaseBuiltinFunc(ctx, funcName, args, tp)
473+
if err != nil {
474+
return baseBuiltinFunc{}, err
475+
}
476+
}
477+
return bf, nil
478+
}
479+
451480
// vecBuiltinFunc contains all vectorized methods for a builtin function.
452481
type vecBuiltinFunc interface {
453482
// vectorized returns if this builtin function itself supports vectorized evaluation.

pkg/expression/builtin_cast.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,15 @@ func (c *castAsDecimalFunctionClass) getFunction(ctx BuildContext, args []Expres
270270
type castAsStringFunctionClass struct {
271271
baseFunctionClass
272272

273-
tp *types.FieldType
273+
tp *types.FieldType
274+
isExplicitCharset bool
274275
}
275276

276277
func (c *castAsStringFunctionClass) getFunction(ctx BuildContext, args []Expression) (sig builtinFunc, err error) {
277278
if err := c.verifyArgs(args); err != nil {
278279
return nil, err
279280
}
280-
bf, err := newBaseBuiltinFunc(ctx, c.funcName, args, c.tp)
281+
bf, err := newBaseBuiltinCastFunc4String(ctx, c.funcName, args, c.tp, c.isExplicitCharset)
281282
if err != nil {
282283
return nil, err
283284
}
@@ -2057,7 +2058,9 @@ func BuildCastFunction4Union(ctx BuildContext, expr Expression, tp *types.FieldT
20572058
defer func() {
20582059
ctx.SetValue(inUnionCastContext, nil)
20592060
}()
2060-
return BuildCastFunction(ctx, expr, tp)
2061+
res, err := BuildCastFunctionWithCheck(ctx, expr, tp, false)
2062+
terror.Log(err)
2063+
return
20612064
}
20622065

20632066
// BuildCastCollationFunction builds a ScalarFunction which casts the collation.
@@ -2092,13 +2095,13 @@ func BuildCastCollationFunction(ctx BuildContext, expr Expression, ec *ExprColla
20922095

20932096
// BuildCastFunction builds a CAST ScalarFunction from the Expression.
20942097
func BuildCastFunction(ctx BuildContext, expr Expression, tp *types.FieldType) (res Expression) {
2095-
res, err := BuildCastFunctionWithCheck(ctx, expr, tp)
2098+
res, err := BuildCastFunctionWithCheck(ctx, expr, tp, false)
20962099
terror.Log(err)
20972100
return
20982101
}
20992102

21002103
// BuildCastFunctionWithCheck builds a CAST ScalarFunction from the Expression and return error if any.
2101-
func BuildCastFunctionWithCheck(ctx BuildContext, expr Expression, tp *types.FieldType) (res Expression, err error) {
2104+
func BuildCastFunctionWithCheck(ctx BuildContext, expr Expression, tp *types.FieldType, isExplicitCharset bool) (res Expression, err error) {
21022105
argType := expr.GetType()
21032106
// If source argument's nullable, then target type should be nullable
21042107
if !mysql.HasNotNullFlag(argType.GetFlag()) {
@@ -2124,7 +2127,7 @@ func BuildCastFunctionWithCheck(ctx BuildContext, expr Expression, tp *types.Fie
21242127
fc = &castAsJSONFunctionClass{baseFunctionClass{ast.Cast, 1, 1}, tp}
21252128
}
21262129
case types.ETString:
2127-
fc = &castAsStringFunctionClass{baseFunctionClass{ast.Cast, 1, 1}, tp}
2130+
fc = &castAsStringFunctionClass{baseFunctionClass{ast.Cast, 1, 1}, tp, isExplicitCharset}
21282131
if expr.GetType().GetType() == mysql.TypeBit {
21292132
tp.SetFlen((expr.GetType().GetFlen() + 7) / 8)
21302133
}

pkg/expression/builtin_cast_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ func TestCastFuncSig(t *testing.T) {
655655
tp := types.NewFieldType(mysql.TypeVarString)
656656
tp.SetCharset(charset.CharsetBin)
657657
args := []Expression{c.before}
658-
stringFunc, err := newBaseBuiltinFunc(ctx, "", args, tp)
658+
stringFunc, err := newBaseBuiltinCastFunc4String(ctx, "", args, tp, false)
659659
require.NoError(t, err)
660660
switch i {
661661
case 0:
@@ -742,7 +742,7 @@ func TestCastFuncSig(t *testing.T) {
742742
tp := types.NewFieldType(mysql.TypeVarString)
743743
tp.SetFlen(c.flen)
744744
tp.SetCharset(charset.CharsetBin)
745-
stringFunc, err := newBaseBuiltinFunc(ctx, "", args, tp)
745+
stringFunc, err := newBaseBuiltinCastFunc4String(ctx, "", args, tp, false)
746746
require.NoError(t, err)
747747
switch i {
748748
case 0:
@@ -1099,7 +1099,7 @@ func TestCastFuncSig(t *testing.T) {
10991099
// null case
11001100
args := []Expression{&Column{RetType: types.NewFieldType(mysql.TypeDouble), Index: 0}}
11011101
row := chunk.MutRowFromDatums([]types.Datum{types.NewDatum(nil)})
1102-
bf, err := newBaseBuiltinFunc(ctx, "", args, types.NewFieldType(mysql.TypeVarString))
1102+
bf, err := newBaseBuiltinCastFunc4String(ctx, "", args, types.NewFieldType(mysql.TypeVarString), false)
11031103
require.NoError(t, err)
11041104
sig = &builtinCastRealAsStringSig{bf}
11051105
sRes, err := evalBuiltinFunc(sig, ctx, row.ToRow())
@@ -1694,7 +1694,7 @@ func TestCastArrayFunc(t *testing.T) {
16941694
},
16951695
}
16961696
for _, tt := range tbl {
1697-
f, err := BuildCastFunctionWithCheck(ctx, datumsToConstants(types.MakeDatums(types.CreateBinaryJSON(tt.input)))[0], tt.tp)
1697+
f, err := BuildCastFunctionWithCheck(ctx, datumsToConstants(types.MakeDatums(types.CreateBinaryJSON(tt.input)))[0], tt.tp, false)
16981698
if !tt.buildFuncSuccess {
16991699
require.Error(t, err, tt.input)
17001700
continue

pkg/expression/collation.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ type collationInfo struct {
4343

4444
charset string
4545
collation string
46+
47+
isExplicitCharset bool
4648
}
4749

4850
func (c *collationInfo) HasCoercibility() bool {
@@ -75,6 +77,14 @@ func (c *collationInfo) CharsetAndCollation() (string, string) {
7577
return c.charset, c.collation
7678
}
7779

80+
func (c *collationInfo) IsExplicitCharset() bool {
81+
return c.isExplicitCharset
82+
}
83+
84+
func (c *collationInfo) SetExplicitCharset(explicit bool) {
85+
c.isExplicitCharset = explicit
86+
}
87+
7888
// CollationInfo contains all interfaces about dealing with collation.
7989
type CollationInfo interface {
8090
// HasCoercibility returns if the Coercibility value is initialized.
@@ -97,6 +107,12 @@ type CollationInfo interface {
97107

98108
// SetCharsetAndCollation sets charset and collation.
99109
SetCharsetAndCollation(chs, coll string)
110+
111+
// IsExplicitCharset return the charset is explicit set or not.
112+
IsExplicitCharset() bool
113+
114+
// SetExplicitCharset set the charset is explicit or not.
115+
SetExplicitCharset(bool)
100116
}
101117

102118
// Coercibility values are used to check whether the collation of one item can be coerced to
@@ -245,9 +261,8 @@ func deriveCollation(ctx BuildContext, funcName string, args []Expression, retTy
245261
case ast.Cast:
246262
// We assume all the cast are implicit.
247263
ec = &ExprCollation{args[0].Coercibility(), args[0].Repertoire(), args[0].GetType().GetCharset(), args[0].GetType().GetCollate()}
248-
// Non-string type cast to string type should use @@character_set_connection and @@collation_connection.
249-
// String type cast to string type should keep its original charset and collation. It should not happen.
250-
if retType == types.ETString && argTps[0] != types.ETString {
264+
// Cast to string type should use @@character_set_connection and @@collation_connection.
265+
if retType == types.ETString {
251266
ec.Charset, ec.Collation = ctx.GetCharsetInfo()
252267
}
253268
return ec, nil

pkg/expression/scalar_function.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,16 @@ func (sf *ScalarFunction) SetRepertoire(r Repertoire) {
824824
sf.Function.SetRepertoire(r)
825825
}
826826

827+
// IsExplicitCharset return the charset is explicit set or not.
828+
func (sf *ScalarFunction) IsExplicitCharset() bool {
829+
return sf.Function.IsExplicitCharset()
830+
}
831+
832+
// SetExplicitCharset set the charset is explicit or not.
833+
func (sf *ScalarFunction) SetExplicitCharset(explicit bool) {
834+
sf.Function.SetExplicitCharset(explicit)
835+
}
836+
827837
const emptyScalarFunctionSize = int64(unsafe.Sizeof(ScalarFunction{}))
828838

829839
// MemoryUsage return the memory usage of ScalarFunction

pkg/expression/util.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,10 @@ func ColumnSubstituteImpl(ctx BuildContext, expr Expression, schema *Schema, new
455455
if substituted {
456456
flag := v.RetType.GetFlag()
457457
var e Expression
458+
var err error
458459
if v.FuncName.L == ast.Cast {
459-
e = BuildCastFunction(ctx, newArg, v.RetType)
460+
e, err = BuildCastFunctionWithCheck(ctx, newArg, v.RetType, v.Function.IsExplicitCharset())
461+
terror.Log(err)
460462
} else {
461463
// for grouping function recreation, use clone (meta included) instead of newFunction
462464
e = v.Clone()

pkg/expression/util_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,8 @@ func (m *MockExpr) Coercibility() Coercibility { return
593593
func (m *MockExpr) SetCoercibility(Coercibility) {}
594594
func (m *MockExpr) Repertoire() Repertoire { return UNICODE }
595595
func (m *MockExpr) SetRepertoire(Repertoire) {}
596+
func (m *MockExpr) IsExplicitCharset() bool { return false }
597+
func (m *MockExpr) SetExplicitCharset(bool) {}
596598

597599
func (m *MockExpr) CharsetAndCollation() (string, string) {
598600
return "", ""

pkg/planner/core/expression_rewriter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1485,7 +1485,7 @@ func (er *expressionRewriter) Leave(originInNode ast.Node) (retNode ast.Node, ok
14851485
return retNode, false
14861486
}
14871487

1488-
castFunction, err := expression.BuildCastFunctionWithCheck(er.sctx, arg, v.Tp)
1488+
castFunction, err := expression.BuildCastFunctionWithCheck(er.sctx, arg, v.Tp, v.ExplicitCharSet)
14891489
if err != nil {
14901490
er.err = err
14911491
return retNode, false

tests/integrationtest/r/executor/executor.result

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4578,4 +4578,5 @@ LOCK TABLE executor__executor.t WRITE, test2.t2 WRITE;
45784578
LOCK TABLE executor__executor.t WRITE, test2.t2 WRITE;
45794579
Error 8020 (HY000): Table 't' was locked in WRITE by server: <server> session: <session>
45804580
unlock tables;
4581+
unlock tables;
45814582
drop user 'testuser'@'localhost';

0 commit comments

Comments
 (0)