github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/builtin_other_vec.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  	"strings"
    18  
    19  	"github.com/whtcorpsinc/errors"
    20  	"github.com/whtcorpsinc/milevadb/types"
    21  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    22  	"github.com/whtcorpsinc/milevadb/soliton/defCauslate"
    23  	"github.com/whtcorpsinc/milevadb/soliton/stringutil"
    24  )
    25  
    26  func (b *builtinValuesIntSig) vectorized() bool {
    27  	return false
    28  }
    29  
    30  func (b *builtinValuesIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error {
    31  	return errors.Errorf("not implemented")
    32  }
    33  
    34  func (b *builtinValuesDurationSig) vectorized() bool {
    35  	return false
    36  }
    37  
    38  func (b *builtinValuesDurationSig) vecEvalDuration(input *chunk.Chunk, result *chunk.DeferredCauset) error {
    39  	return errors.Errorf("not implemented")
    40  }
    41  
    42  func (b *builtinEventSig) vectorized() bool {
    43  	return true
    44  }
    45  
    46  func (b *builtinEventSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error {
    47  	panic("builtinEventSig.vecEvalString() should never be called.")
    48  }
    49  
    50  func (b *builtinValuesRealSig) vectorized() bool {
    51  	return false
    52  }
    53  
    54  func (b *builtinValuesRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error {
    55  	return errors.Errorf("not implemented")
    56  }
    57  
    58  func (b *builtinValuesStringSig) vectorized() bool {
    59  	return false
    60  }
    61  
    62  func (b *builtinValuesStringSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error {
    63  	return errors.Errorf("not implemented")
    64  }
    65  
    66  func (b *builtinValuesTimeSig) vectorized() bool {
    67  	return false
    68  }
    69  
    70  func (b *builtinValuesTimeSig) vecEvalTime(input *chunk.Chunk, result *chunk.DeferredCauset) error {
    71  	return errors.Errorf("not implemented")
    72  }
    73  
    74  func (b *builtinValuesJSONSig) vectorized() bool {
    75  	return false
    76  }
    77  
    78  func (b *builtinValuesJSONSig) vecEvalJSON(input *chunk.Chunk, result *chunk.DeferredCauset) error {
    79  	return errors.Errorf("not implemented")
    80  }
    81  
    82  // bitCount returns the number of bits that are set in the argument 'value'.
    83  func bitCount(value int64) int64 {
    84  	value = value - ((value >> 1) & 0x5555555555555555)
    85  	value = (value & 0x3333333333333333) + ((value >> 2) & 0x3333333333333333)
    86  	value = (value & 0x0f0f0f0f0f0f0f0f) + ((value >> 4) & 0x0f0f0f0f0f0f0f0f)
    87  	value = value + (value >> 8)
    88  	value = value + (value >> 16)
    89  	value = value + (value >> 32)
    90  	value = value & 0x7f
    91  	return value
    92  }
    93  func (b *builtinBitCountSig) vectorized() bool {
    94  	return true
    95  }
    96  func (b *builtinBitCountSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error {
    97  	n := input.NumEvents()
    98  	if err := b.args[0].VecEvalInt(b.ctx, input, result); err != nil {
    99  		if types.ErrOverflow.Equal(err) {
   100  			result.ResizeInt64(n, false)
   101  			i64s := result.Int64s()
   102  			for i := 0; i < n; i++ {
   103  				res, isNull, err := b.evalInt(input.GetEvent(i))
   104  				if err != nil {
   105  					return err
   106  				}
   107  				result.SetNull(i, isNull)
   108  				i64s[i] = res
   109  			}
   110  			return nil
   111  		}
   112  		return err
   113  	}
   114  	i64s := result.Int64s()
   115  	for i := 0; i < n; i++ {
   116  		if result.IsNull(i) {
   117  			continue
   118  		}
   119  		i64s[i] = bitCount(i64s[i])
   120  	}
   121  	return nil
   122  }
   123  
   124  func (b *builtinGetParamStringSig) vectorized() bool {
   125  	return true
   126  }
   127  
   128  func (b *builtinGetParamStringSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   129  	stochastikVars := b.ctx.GetStochastikVars()
   130  	n := input.NumEvents()
   131  	idx, err := b.bufSlabPredictor.get(types.ETInt, n)
   132  	if err != nil {
   133  		return err
   134  	}
   135  	defer b.bufSlabPredictor.put(idx)
   136  	if err := b.args[0].VecEvalInt(b.ctx, input, idx); err != nil {
   137  		return err
   138  	}
   139  	idxIs := idx.Int64s()
   140  	result.ReserveString(n)
   141  	for i := 0; i < n; i++ {
   142  		if idx.IsNull(i) {
   143  			result.AppendNull()
   144  			continue
   145  		}
   146  		idxI := idxIs[i]
   147  		v := stochastikVars.PreparedParams[idxI]
   148  		str, err := v.ToString()
   149  		if err != nil {
   150  			result.AppendNull()
   151  			continue
   152  		}
   153  		result.AppendString(str)
   154  	}
   155  	return nil
   156  }
   157  
   158  func (b *builtinSetVarSig) vectorized() bool {
   159  	return true
   160  }
   161  
   162  func (b *builtinSetVarSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   163  	n := input.NumEvents()
   164  	buf0, err := b.bufSlabPredictor.get(types.ETString, n)
   165  	if err != nil {
   166  		return err
   167  	}
   168  	defer b.bufSlabPredictor.put(buf0)
   169  	if err := b.args[0].VecEvalString(b.ctx, input, buf0); err != nil {
   170  		return err
   171  	}
   172  	buf1, err := b.bufSlabPredictor.get(types.ETString, n)
   173  	if err != nil {
   174  		return err
   175  	}
   176  	defer b.bufSlabPredictor.put(buf1)
   177  	if err := b.args[1].VecEvalString(b.ctx, input, buf1); err != nil {
   178  		return err
   179  	}
   180  	result.ReserveString(n)
   181  	stochastikVars := b.ctx.GetStochastikVars()
   182  	stochastikVars.UsersLock.Lock()
   183  	defer stochastikVars.UsersLock.Unlock()
   184  	_, defCauslation := stochastikVars.GetCharsetInfo()
   185  	for i := 0; i < n; i++ {
   186  		if buf0.IsNull(i) || buf1.IsNull(i) {
   187  			result.AppendNull()
   188  			continue
   189  		}
   190  		varName := strings.ToLower(buf0.GetString(i))
   191  		res := buf1.GetString(i)
   192  		stochastikVars.Users[varName] = types.NewDefCauslationStringCauset(stringutil.Copy(res), defCauslation, defCauslate.DefaultLen)
   193  		result.AppendString(res)
   194  	}
   195  	return nil
   196  }
   197  
   198  func (b *builtinValuesDecimalSig) vectorized() bool {
   199  	return false
   200  }
   201  
   202  func (b *builtinValuesDecimalSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   203  	return errors.Errorf("not implemented")
   204  }
   205  
   206  func (b *builtinGetVarSig) vectorized() bool {
   207  	return true
   208  }
   209  
   210  func (b *builtinGetVarSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   211  	n := input.NumEvents()
   212  	buf0, err := b.bufSlabPredictor.get(types.ETString, n)
   213  	if err != nil {
   214  		return err
   215  	}
   216  	defer b.bufSlabPredictor.put(buf0)
   217  	if err := b.args[0].VecEvalString(b.ctx, input, buf0); err != nil {
   218  		return err
   219  	}
   220  	result.ReserveString(n)
   221  	stochastikVars := b.ctx.GetStochastikVars()
   222  	stochastikVars.UsersLock.Lock()
   223  	defer stochastikVars.UsersLock.Unlock()
   224  	for i := 0; i < n; i++ {
   225  		if buf0.IsNull(i) {
   226  			result.AppendNull()
   227  			continue
   228  		}
   229  		varName := strings.ToLower(buf0.GetString(i))
   230  		if v, ok := stochastikVars.Users[varName]; ok {
   231  			result.AppendString(v.GetString())
   232  			continue
   233  		}
   234  		result.AppendNull()
   235  	}
   236  	return nil
   237  }