Skip to content

Commit

Permalink
expression: implement vectorized evaluation for 'builtinCastRealAsStr…
Browse files Browse the repository at this point in the history
…ingSig' (#12533)
  • Loading branch information
tsthght authored and sre-bot committed Oct 9, 2019
1 parent af37398 commit 0127047
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
46 changes: 44 additions & 2 deletions expression/builtin_cast_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,53 @@ func (b *builtinCastTimeAsJSONSig) vecEvalJSON(input *chunk.Chunk, result *chunk
}

func (b *builtinCastRealAsStringSig) vectorized() bool {
return false
return true
}

func (b *builtinCastRealAsStringSig) vecEvalString(input *chunk.Chunk, result *chunk.Column) error {
return errors.Errorf("not implemented")
n := input.NumRows()
buf, err := b.bufAllocator.get(types.ETReal, n)
if err != nil {
return err
}
defer b.bufAllocator.put(buf)
if err := b.args[0].VecEvalReal(b.ctx, input, buf); err != nil {
return err
}

bits := 64
if b.args[0].GetType().Tp == mysql.TypeFloat {
// b.args[0].EvalReal() casts the value from float32 to float64, for example:
// float32(208.867) is cast to float64(208.86700439)
// If we strconv.FormatFloat the value with 64bits, the result is incorrect!
bits = 32
}

var isNull bool
var res string
f64s := buf.Float64s()
result.ReserveString(n)
sc := b.ctx.GetSessionVars().StmtCtx
for i, v := range f64s {
if buf.IsNull(i) {
result.AppendNull()
continue
}
res, err = types.ProduceStrWithSpecifiedTp(strconv.FormatFloat(v, 'f', -1, bits), b.tp, sc, false)
if err != nil {
return err
}
res, isNull, err = padZeroForBinaryType(res, b.tp, b.ctx)
if err != nil {
return err
}
if isNull {
result.AppendNull()
continue
}
result.AppendString(res)
}
return nil
}

func (b *builtinCastDecimalAsStringSig) vectorized() bool {
Expand Down
1 change: 1 addition & 0 deletions expression/builtin_cast_vec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var vecBuiltinCastCases = map[string][]vecExprBenchCase{
},
{retEvalType: types.ETString, childrenTypes: []types.EvalType{types.ETInt}},
{retEvalType: types.ETDecimal, childrenTypes: []types.EvalType{types.ETDatetime}},
{retEvalType: types.ETString, childrenTypes: []types.EvalType{types.ETReal}},
{retEvalType: types.ETString, childrenTypes: []types.EvalType{types.ETJson}},
{retEvalType: types.ETString, childrenTypes: []types.EvalType{types.ETDecimal}},
},
Expand Down

0 comments on commit 0127047

Please sign in to comment.