github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/vectorized.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package memex
    15  
    16  import (
    17  	"github.com/whtcorpsinc/errors"
    18  	"github.com/whtcorpsinc/milevadb/stochastikctx"
    19  	"github.com/whtcorpsinc/milevadb/types"
    20  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    21  )
    22  
    23  func genVecFromConstExpr(ctx stochastikctx.Context, expr Expression, targetType types.EvalType, input *chunk.Chunk, result *chunk.DeferredCauset) error {
    24  	n := 1
    25  	if input != nil {
    26  		n = input.NumEvents()
    27  		if n == 0 {
    28  			result.Reset(targetType)
    29  			return nil
    30  		}
    31  	}
    32  	switch targetType {
    33  	case types.ETInt:
    34  		v, isNull, err := expr.EvalInt(ctx, chunk.Event{})
    35  		if err != nil {
    36  			return err
    37  		}
    38  		if isNull {
    39  			result.ResizeInt64(n, true)
    40  			return nil
    41  		}
    42  		result.ResizeInt64(n, false)
    43  		i64s := result.Int64s()
    44  		for i := range i64s {
    45  			i64s[i] = v
    46  		}
    47  	case types.ETReal:
    48  		v, isNull, err := expr.EvalReal(ctx, chunk.Event{})
    49  		if err != nil {
    50  			return err
    51  		}
    52  		if isNull {
    53  			result.ResizeFloat64(n, true)
    54  			return nil
    55  		}
    56  		result.ResizeFloat64(n, false)
    57  		f64s := result.Float64s()
    58  		for i := range f64s {
    59  			f64s[i] = v
    60  		}
    61  	case types.ETDecimal:
    62  		v, isNull, err := expr.EvalDecimal(ctx, chunk.Event{})
    63  		if err != nil {
    64  			return err
    65  		}
    66  		if isNull {
    67  			result.ResizeDecimal(n, true)
    68  			return nil
    69  		}
    70  		result.ResizeDecimal(n, false)
    71  		ds := result.Decimals()
    72  		for i := range ds {
    73  			ds[i] = *v
    74  		}
    75  	case types.ETDatetime, types.ETTimestamp:
    76  		v, isNull, err := expr.EvalTime(ctx, chunk.Event{})
    77  		if err != nil {
    78  			return err
    79  		}
    80  		if isNull {
    81  			result.ResizeTime(n, true)
    82  			return nil
    83  		}
    84  		result.ResizeTime(n, false)
    85  		ts := result.Times()
    86  		for i := range ts {
    87  			ts[i] = v
    88  		}
    89  	case types.ETDuration:
    90  		v, isNull, err := expr.EvalDuration(ctx, chunk.Event{})
    91  		if err != nil {
    92  			return err
    93  		}
    94  		if isNull {
    95  			result.ResizeGoDuration(n, true)
    96  			return nil
    97  		}
    98  		result.ResizeGoDuration(n, false)
    99  		ds := result.GoDurations()
   100  		for i := range ds {
   101  			ds[i] = v.Duration
   102  		}
   103  	case types.ETJson:
   104  		result.ReserveJSON(n)
   105  		v, isNull, err := expr.EvalJSON(ctx, chunk.Event{})
   106  		if err != nil {
   107  			return err
   108  		}
   109  		if isNull {
   110  			for i := 0; i < n; i++ {
   111  				result.AppendNull()
   112  			}
   113  		} else {
   114  			for i := 0; i < n; i++ {
   115  				result.AppendJSON(v)
   116  			}
   117  		}
   118  	case types.ETString:
   119  		result.ReserveString(n)
   120  		v, isNull, err := expr.EvalString(ctx, chunk.Event{})
   121  		if err != nil {
   122  			return err
   123  		}
   124  		if isNull {
   125  			for i := 0; i < n; i++ {
   126  				result.AppendNull()
   127  			}
   128  		} else {
   129  			for i := 0; i < n; i++ {
   130  				result.AppendString(v)
   131  			}
   132  		}
   133  	default:
   134  		return errors.Errorf("unsupported Constant type for vectorized evaluation")
   135  	}
   136  	return nil
   137  }