Files
tidb/expression/builtin_cast_vec.go

146 lines
3.5 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 (
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
)
func (b *builtinCastIntAsDurationSig) vecEvalDuration(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.ResizeGoDuration(n, false)
result.MergeNulls(buf)
i64s := buf.Int64s()
ds := result.GoDurations()
for i := 0; i < n; i++ {
if result.IsNull(i) {
continue
}
dur, err := types.NumberToDuration(i64s[i], int8(b.tp.Decimal))
if err != nil {
if types.ErrOverflow.Equal(err) {
err = b.ctx.GetSessionVars().StmtCtx.HandleOverflow(err, err)
}
if err != nil {
return err
}
result.SetNull(i, true)
continue
}
ds[i] = dur.Duration
}
return nil
}
func (b *builtinCastIntAsDurationSig) vectorized() bool {
return true
}
func (b *builtinCastIntAsIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.Column) error {
if err := b.args[0].VecEvalInt(b.ctx, input, result); err != nil {
return err
}
if b.inUnion && mysql.HasUnsignedFlag(b.tp.Flag) {
i64s := result.Int64s()
// the null array of result is set by its child args[0],
// so we can skip it here to make this loop simpler to improve its performance.
for i := range i64s {
if i64s[i] < 0 {
i64s[i] = 0
}
}
}
return nil
}
func (b *builtinCastIntAsIntSig) vectorized() bool {
return true
}
func (b *builtinCastIntAsRealSig) vecEvalReal(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.ResizeFloat64(n, false)
result.MergeNulls(buf)
i64s := buf.Int64s()
rs := result.Float64s()
hasUnsignedFlag0 := mysql.HasUnsignedFlag(b.tp.Flag)
hasUnsignedFlag1 := mysql.HasUnsignedFlag(b.args[0].GetType().Flag)
for i := 0; i < n; i++ {
if result.IsNull(i) {
continue
}
if !hasUnsignedFlag0 && !hasUnsignedFlag1 {
rs[i] = float64(i64s[i])
} else if b.inUnion && i64s[i] < 0 {
rs[i] = 0
} else {
// recall that, int to float is different from uint to float
rs[i] = float64(uint64(i64s[i]))
}
}
return nil
}
func (b *builtinCastIntAsRealSig) vectorized() bool {
return true
}
func (b *builtinCastRealAsRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.Column) error {
if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil {
return err
}
n := input.NumRows()
f64s := result.Float64s()
conditionUnionAndUnsigned := b.inUnion && mysql.HasUnsignedFlag(b.tp.Flag)
if !conditionUnionAndUnsigned {
return nil
}
for i := 0; i < n; i++ {
if result.IsNull(i) {
continue
}
if f64s[i] < 0 {
f64s[i] = 0
}
}
return nil
}
func (b *builtinCastRealAsRealSig) vectorized() bool {
return true
}