-
Notifications
You must be signed in to change notification settings - Fork 524
/
Copy pathbuiltin_string.go
128 lines (111 loc) · 3.53 KB
/
builtin_string.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
// Copyright 2013 The ql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSES/QL-LICENSE file.
// Copyright 2015 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package expression
import (
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tipb/go-tipb"
)
var (
_ functionClass = &lengthFunctionClass{}
_ functionClass = &strcmpFunctionClass{}
)
var (
_ builtinFunc = &builtinLengthSig{}
_ builtinFunc = &builtinStrcmpSig{}
)
// SetBinFlagOrBinStr sets resTp to binary string if argTp is a binary string,
// if not, sets the binary flag of resTp to true if argTp has binary flag.
func SetBinFlagOrBinStr(argTp *types.FieldType, resTp *types.FieldType) {
if types.IsBinaryStr(argTp) {
types.SetBinChsClnFlag(resTp)
} else if mysql.HasBinaryFlag(argTp.Flag) || !types.IsNonBinaryStr(argTp) {
resTp.Flag |= mysql.BinaryFlag
}
}
type lengthFunctionClass struct {
baseFunctionClass
}
func (c *lengthFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (builtinFunc, error) {
if err := c.verifyArgs(args); err != nil {
return nil, err
}
bf := newBaseBuiltinFuncWithTp(ctx, args, types.ETInt, types.ETString)
bf.tp.Flen = 10
sig := &builtinLengthSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_Length)
return sig, nil
}
type builtinLengthSig struct {
baseBuiltinFunc
}
func (b *builtinLengthSig) Clone() builtinFunc {
newSig := &builtinLengthSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
// evalInt evaluates a builtinLengthSig.
// See https://dev.mysql.com/doc/refman/5.7/en/string-functions.html
func (b *builtinLengthSig) evalInt(row chunk.Row) (int64, bool, error) {
val, isNull, err := b.args[0].EvalString(b.ctx, row)
if isNull || err != nil {
return 0, isNull, err
}
return int64(len([]byte(val))), false, nil
}
type strcmpFunctionClass struct {
baseFunctionClass
}
func (c *strcmpFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (builtinFunc, error) {
if err := c.verifyArgs(args); err != nil {
return nil, err
}
bf := newBaseBuiltinFuncWithTp(ctx, args, types.ETInt, types.ETString, types.ETString)
bf.tp.Flen = 2
types.SetBinChsClnFlag(bf.tp)
sig := &builtinStrcmpSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_Strcmp)
return sig, nil
}
type builtinStrcmpSig struct {
baseBuiltinFunc
}
func (b *builtinStrcmpSig) Clone() builtinFunc {
newSig := &builtinStrcmpSig{}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}
// evalInt evals a builtinStrcmpSig.
// See https://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html
func (b *builtinStrcmpSig) evalInt(row chunk.Row) (int64, bool, error) {
var (
left, right string
isNull bool
err error
)
left, isNull, err = b.args[0].EvalString(b.ctx, row)
if isNull || err != nil {
return 0, isNull, err
}
right, isNull, err = b.args[1].EvalString(b.ctx, row)
if isNull || err != nil {
return 0, isNull, err
}
res := types.CompareString(left, right)
return int64(res), false, nil
}