323 lines
7.3 KiB
Go
323 lines
7.3 KiB
Go
// Copyright 2019 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 (
|
|
"math"
|
|
"strings"
|
|
"unicode/utf8"
|
|
|
|
"github.com/pingcap/parser/mysql"
|
|
"github.com/pingcap/tidb/types"
|
|
"github.com/pingcap/tidb/util/chunk"
|
|
)
|
|
|
|
func (b *builtinLowerSig) vecEvalString(input *chunk.Chunk, result *chunk.Column) error {
|
|
if err := b.args[0].VecEvalString(b.ctx, input, result); err != nil {
|
|
return err
|
|
}
|
|
if types.IsBinaryStr(b.args[0].GetType()) {
|
|
return nil
|
|
}
|
|
|
|
Loop:
|
|
for i := 0; i < input.NumRows(); i++ {
|
|
str := result.GetBytes(i)
|
|
for _, c := range str {
|
|
if c >= utf8.RuneSelf {
|
|
continue Loop
|
|
}
|
|
}
|
|
for i := range str {
|
|
if str[i] >= 'A' && str[i] <= 'Z' {
|
|
str[i] += 'a' - 'A'
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (b *builtinLowerSig) vectorized() bool {
|
|
return true
|
|
}
|
|
|
|
func (b *builtinRepeatSig) vecEvalString(input *chunk.Chunk, result *chunk.Column) error {
|
|
n := input.NumRows()
|
|
buf, err := b.bufAllocator.get(types.ETString, n)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer b.bufAllocator.put(buf)
|
|
if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil {
|
|
return err
|
|
}
|
|
|
|
buf2, err := b.bufAllocator.get(types.ETInt, n)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer b.bufAllocator.put(buf2)
|
|
if err := b.args[1].VecEvalInt(b.ctx, input, buf2); err != nil {
|
|
return err
|
|
}
|
|
|
|
result.ReserveString(n)
|
|
nums := buf2.Int64s()
|
|
for i := 0; i < n; i++ {
|
|
// TODO: introduce vectorized null-bitmap to speed it up.
|
|
if buf.IsNull(i) || buf2.IsNull(i) {
|
|
result.AppendNull()
|
|
continue
|
|
}
|
|
num := nums[i]
|
|
if num < 1 {
|
|
result.AppendString("")
|
|
continue
|
|
}
|
|
if num > math.MaxInt32 {
|
|
// to avoid overflow when calculating uint64(byteLength)*uint64(num) later
|
|
num = math.MaxInt32
|
|
}
|
|
|
|
str := buf.GetString(i)
|
|
byteLength := len(str)
|
|
if uint64(byteLength)*uint64(num) > b.maxAllowedPacket {
|
|
b.ctx.GetSessionVars().StmtCtx.AppendWarning(errWarnAllowedPacketOverflowed.GenWithStackByArgs("repeat", b.maxAllowedPacket))
|
|
result.AppendNull()
|
|
continue
|
|
}
|
|
if int64(byteLength) > int64(b.tp.Flen)/num {
|
|
result.AppendNull()
|
|
continue
|
|
}
|
|
result.AppendString(strings.Repeat(str, int(num)))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (b *builtinRepeatSig) vectorized() bool {
|
|
return true
|
|
}
|
|
|
|
func (b *builtinStringIsNullSig) vecEvalInt(input *chunk.Chunk, result *chunk.Column) error {
|
|
n := input.NumRows()
|
|
buf, err := b.bufAllocator.get(types.ETString, n)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer b.bufAllocator.put(buf)
|
|
if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil {
|
|
return err
|
|
}
|
|
|
|
result.ResizeInt64(n, false)
|
|
i64s := result.Int64s()
|
|
for i := 0; i < n; i++ {
|
|
if buf.IsNull(i) {
|
|
i64s[i] = 1
|
|
} else {
|
|
i64s[i] = 0
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (b *builtinStringIsNullSig) vectorized() bool {
|
|
return true
|
|
}
|
|
|
|
func (b *builtinUpperSig) vecEvalString(input *chunk.Chunk, result *chunk.Column) error {
|
|
if err := b.args[0].VecEvalString(b.ctx, input, result); err != nil {
|
|
return err
|
|
}
|
|
if types.IsBinaryStr(b.args[0].GetType()) {
|
|
return nil
|
|
}
|
|
|
|
Loop:
|
|
for i := 0; i < input.NumRows(); i++ {
|
|
str := result.GetBytes(i)
|
|
for _, c := range str {
|
|
if c >= utf8.RuneSelf {
|
|
continue Loop
|
|
}
|
|
}
|
|
for i := range str {
|
|
if str[i] >= 'a' && str[i] <= 'z' {
|
|
str[i] -= 'a' - 'A'
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (b *builtinUpperSig) vectorized() bool {
|
|
return true
|
|
}
|
|
|
|
func (b *builtinLeftSig) vecEvalString(input *chunk.Chunk, result *chunk.Column) error {
|
|
n := input.NumRows()
|
|
buf, err := b.bufAllocator.get(types.ETString, n)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer b.bufAllocator.put(buf)
|
|
if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil {
|
|
return err
|
|
}
|
|
|
|
buf2, err := b.bufAllocator.get(types.ETInt, n)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer b.bufAllocator.put(buf2)
|
|
if err := b.args[1].VecEvalInt(b.ctx, input, buf2); err != nil {
|
|
return err
|
|
}
|
|
|
|
result.ReserveString(n)
|
|
nums := buf2.Int64s()
|
|
for i := 0; i < n; i++ {
|
|
if buf.IsNull(i) || buf2.IsNull(i) {
|
|
result.AppendNull()
|
|
continue
|
|
}
|
|
|
|
str := buf.GetString(i)
|
|
runes, leftLength := []rune(str), int(nums[i])
|
|
if runeLength := len(runes); leftLength > runeLength {
|
|
leftLength = runeLength
|
|
} else if leftLength < 0 {
|
|
leftLength = 0
|
|
}
|
|
|
|
result.AppendString(string(runes[:leftLength]))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (b *builtinLeftSig) vectorized() bool {
|
|
return true
|
|
}
|
|
|
|
func (b *builtinRightSig) vecEvalString(input *chunk.Chunk, result *chunk.Column) error {
|
|
n := input.NumRows()
|
|
buf, err := b.bufAllocator.get(types.ETString, n)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer b.bufAllocator.put(buf)
|
|
if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil {
|
|
return err
|
|
}
|
|
|
|
buf2, err := b.bufAllocator.get(types.ETInt, n)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer b.bufAllocator.put(buf2)
|
|
if err := b.args[1].VecEvalInt(b.ctx, input, buf2); err != nil {
|
|
return err
|
|
}
|
|
|
|
result.ReserveString(n)
|
|
nums := buf2.Int64s()
|
|
for i := 0; i < n; i++ {
|
|
if buf.IsNull(i) || buf2.IsNull(i) {
|
|
result.AppendNull()
|
|
continue
|
|
}
|
|
|
|
str := buf.GetString(i)
|
|
runes := []rune(str)
|
|
strLength, rightLength := len(runes), int(nums[i])
|
|
if rightLength > strLength {
|
|
rightLength = strLength
|
|
} else if rightLength < 0 {
|
|
rightLength = 0
|
|
}
|
|
|
|
result.AppendString(string(runes[strLength-rightLength:]))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (b *builtinRightSig) vectorized() bool {
|
|
return true
|
|
}
|
|
|
|
// vecEvalString evals a builtinSpaceSig.
|
|
// See https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_space
|
|
func (b *builtinSpaceSig) vecEvalString(input *chunk.Chunk, result *chunk.Column) error {
|
|
n := input.NumRows()
|
|
buf, err := b.bufAllocator.get(types.ETInt, n)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer b.bufAllocator.put(buf)
|
|
if err := b.args[0].VecEvalInt(b.ctx, input, buf); err != nil {
|
|
return err
|
|
}
|
|
|
|
result.ReserveString(n)
|
|
nums := buf.Int64s()
|
|
for i := 0; i < n; i++ {
|
|
if buf.IsNull(i) {
|
|
result.AppendNull()
|
|
continue
|
|
}
|
|
num := nums[i]
|
|
if num < 0 {
|
|
num = 0
|
|
}
|
|
if uint64(num) > b.maxAllowedPacket {
|
|
b.ctx.GetSessionVars().StmtCtx.AppendWarning(errWarnAllowedPacketOverflowed.GenWithStackByArgs("space", b.maxAllowedPacket))
|
|
result.AppendNull()
|
|
continue
|
|
}
|
|
if num > mysql.MaxBlobWidth {
|
|
result.AppendNull()
|
|
continue
|
|
}
|
|
result.AppendString(strings.Repeat(" ", int(num)))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (b *builtinSpaceSig) vectorized() bool {
|
|
return true
|
|
}
|
|
|
|
// vecEvalString evals a REVERSE(str).
|
|
// See https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_reverse
|
|
func (b *builtinReverseSig) vecEvalString(input *chunk.Chunk, result *chunk.Column) error {
|
|
if err := b.args[0].VecEvalString(b.ctx, input, result); err != nil {
|
|
return err
|
|
}
|
|
|
|
for i := 0; i < input.NumRows(); i++ {
|
|
if result.IsNull(i) {
|
|
continue
|
|
}
|
|
str := result.GetString(i)
|
|
reversed := reverseRunes([]rune(str))
|
|
result.SetRaw(i, []byte(string(reversed)))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (b *builtinReverseSig) vectorized() bool {
|
|
return true
|
|
}
|