-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathbuiltin_convert_charset.go
137 lines (123 loc) · 3.91 KB
/
builtin_convert_charset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package expression
import (
"fmt"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/charset"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tipb/go-tipb"
)
// InternalFuncToBinary accepts a string and returns another string encoded in a given charset.
const InternalFuncToBinary = "to_binary"
type tidbConvertCharsetFunctionClass struct {
baseFunctionClass
}
func (c *tidbConvertCharsetFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (builtinFunc, error) {
if err := c.verifyArgs(args); err != nil {
return nil, c.verifyArgs(args)
}
argTp := args[0].GetType().EvalType()
var sig builtinFunc
switch argTp {
case types.ETString:
bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETString, types.ETString)
if err != nil {
return nil, err
}
sig = &builtinInternalToBinarySig{bf}
sig.setPbCode(tipb.ScalarFuncSig_ToBinary)
default:
return nil, fmt.Errorf("unexpected argTp: %d", argTp)
}
return sig, nil
}
var _ builtinFunc = &builtinInternalToBinarySig{}
type builtinInternalToBinarySig struct {
baseBuiltinFunc
}
func (b *builtinInternalToBinarySig) Clone() builtinFunc {
newSig := &builtinInternalToBinarySig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
func (b *builtinInternalToBinarySig) evalString(row chunk.Row) (res string, isNull bool, err error) {
val, isNull, err := b.args[0].EvalString(b.ctx, row)
if isNull || err != nil {
return res, isNull, err
}
tp := b.args[0].GetType()
enc := charset.NewEncoding(tp.Charset)
res, err = enc.EncodeString(val)
return res, false, err
}
func (b *builtinInternalToBinarySig) vectorized() bool {
return true
}
func (b *builtinInternalToBinarySig) vecEvalString(input *chunk.Chunk, result *chunk.Column) error {
n := input.NumRows()
buf, err := b.bufAllocator.get()
if err != nil {
return err
}
defer b.bufAllocator.put(buf)
if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil {
return err
}
enc := charset.NewEncoding(b.args[0].GetType().Charset)
result.ReserveString(n)
var encodedBuf []byte
for i := 0; i < n; i++ {
if buf.IsNull(i) {
result.AppendNull()
continue
}
strBytes, err := enc.Encode(encodedBuf, buf.GetBytes(i))
if err != nil {
return err
}
result.AppendBytes(strBytes)
}
return nil
}
// toBinaryMap contains the builtin functions which arguments need to be converted to the correct charset.
var toBinaryMap = map[string]struct{}{
ast.Hex: {}, ast.Length: {}, ast.OctetLength: {}, ast.ASCII: {},
ast.ToBase64: {}, ast.AesDecrypt: {}, ast.Decode: {}, ast.Encode: {},
ast.PasswordFunc: {}, ast.MD5: {}, ast.SHA: {}, ast.SHA1: {},
ast.SHA2: {}, ast.Compress: {},
}
// WrapWithToBinary wraps `expr` with to_binary sig.
func WrapWithToBinary(ctx sessionctx.Context, expr Expression, funcName string) Expression {
exprTp := expr.GetType()
if _, err := charset.GetDefaultCollationLegacy(exprTp.Charset); err != nil {
if _, ok := toBinaryMap[funcName]; ok {
fc := funcs[InternalFuncToBinary]
sig, err := fc.getFunction(ctx, []Expression{expr})
if err != nil {
return expr
}
sf := &ScalarFunction{
FuncName: model.NewCIStr(InternalFuncToBinary),
RetType: exprTp,
Function: sig,
}
return FoldConstant(sf)
}
}
return expr
}