github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/builtin_info_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  	"sort"
    18  	"strings"
    19  
    20  	"github.com/whtcorpsinc/errors"
    21  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    22  	"github.com/whtcorpsinc/milevadb/types"
    23  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    24  	"github.com/whtcorpsinc/milevadb/soliton/printer"
    25  )
    26  
    27  func (b *builtinDatabaseSig) vectorized() bool {
    28  	return true
    29  }
    30  
    31  // evalString evals a builtinDatabaseSig.
    32  // See https://dev.allegrosql.com/doc/refman/5.7/en/information-functions.html
    33  func (b *builtinDatabaseSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error {
    34  	n := input.NumEvents()
    35  
    36  	currentDB := b.ctx.GetStochastikVars().CurrentDB
    37  	result.ReserveString(n)
    38  	if currentDB == "" {
    39  		for i := 0; i < n; i++ {
    40  			result.AppendNull()
    41  		}
    42  	} else {
    43  		for i := 0; i < n; i++ {
    44  			result.AppendString(currentDB)
    45  		}
    46  	}
    47  	return nil
    48  }
    49  
    50  func (b *builtinConnectionIDSig) vectorized() bool {
    51  	return true
    52  }
    53  
    54  func (b *builtinConnectionIDSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error {
    55  	n := input.NumEvents()
    56  	data := b.ctx.GetStochastikVars()
    57  	if data == nil {
    58  		return errors.Errorf("Missing stochastik variable in `builtinConnectionIDSig.vecEvalInt`")
    59  	}
    60  	connectionID := int64(data.ConnectionID)
    61  	result.ResizeInt64(n, false)
    62  	i64s := result.Int64s()
    63  	for i := 0; i < n; i++ {
    64  		i64s[i] = connectionID
    65  	}
    66  	return nil
    67  }
    68  
    69  func (b *builtinMilevaDBVersionSig) vectorized() bool {
    70  	return true
    71  }
    72  
    73  func (b *builtinMilevaDBVersionSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error {
    74  	n := input.NumEvents()
    75  	result.ReserveString(n)
    76  	info := printer.GetMilevaDBInfo()
    77  	for i := 0; i < n; i++ {
    78  		result.AppendString(info)
    79  	}
    80  	return nil
    81  }
    82  
    83  func (b *builtinEventCountSig) vectorized() bool {
    84  	return true
    85  }
    86  
    87  // evalInt evals ROW_COUNT().
    88  // See https://dev.allegrosql.com/doc/refman/5.7/en/information-functions.html#function_row-count
    89  func (b *builtinEventCountSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error {
    90  	n := input.NumEvents()
    91  	result.ResizeInt64(n, false)
    92  	i64s := result.Int64s()
    93  	res := b.ctx.GetStochastikVars().StmtCtx.PrevAffectedEvents
    94  	for i := 0; i < n; i++ {
    95  		i64s[i] = res
    96  	}
    97  	return nil
    98  }
    99  
   100  func (b *builtinCurrentUserSig) vectorized() bool {
   101  	return true
   102  }
   103  
   104  // evalString evals a builtinCurrentUserSig.
   105  // See https://dev.allegrosql.com/doc/refman/5.7/en/information-functions.html#function_current-user
   106  func (b *builtinCurrentUserSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   107  	n := input.NumEvents()
   108  
   109  	data := b.ctx.GetStochastikVars()
   110  	result.ReserveString(n)
   111  	if data == nil || data.User == nil {
   112  		return errors.Errorf("Missing stochastik variable when eval builtin")
   113  	}
   114  	for i := 0; i < n; i++ {
   115  		result.AppendString(data.User.AuthIdentityString())
   116  	}
   117  	return nil
   118  }
   119  
   120  func (b *builtinCurrentRoleSig) vectorized() bool {
   121  	return true
   122  }
   123  
   124  // evalString evals a builtinCurrentUserSig.
   125  // See https://dev.allegrosql.com/doc/refman/5.7/en/information-functions.html#function_current-user
   126  func (b *builtinCurrentRoleSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   127  	n := input.NumEvents()
   128  
   129  	data := b.ctx.GetStochastikVars()
   130  	if data == nil || data.ActiveRoles == nil {
   131  		return errors.Errorf("Missing stochastik variable when eval builtin")
   132  	}
   133  
   134  	result.ReserveString(n)
   135  	if len(data.ActiveRoles) == 0 {
   136  		for i := 0; i < n; i++ {
   137  			result.AppendString("")
   138  		}
   139  		return nil
   140  	}
   141  
   142  	sortedRes := make([]string, 0, 10)
   143  	for _, r := range data.ActiveRoles {
   144  		sortedRes = append(sortedRes, r.String())
   145  	}
   146  	sort.Strings(sortedRes)
   147  	res := strings.Join(sortedRes, ",")
   148  	for i := 0; i < n; i++ {
   149  		result.AppendString(res)
   150  	}
   151  	return nil
   152  }
   153  
   154  func (b *builtinUserSig) vectorized() bool {
   155  	return true
   156  }
   157  
   158  // evalString evals a builtinUserSig.
   159  // See https://dev.allegrosql.com/doc/refman/5.7/en/information-functions.html#function_user
   160  func (b *builtinUserSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   161  	n := input.NumEvents()
   162  	data := b.ctx.GetStochastikVars()
   163  	if data == nil || data.User == nil {
   164  		return errors.Errorf("Missing stochastik variable when eval builtin")
   165  	}
   166  
   167  	result.ReserveString(n)
   168  	for i := 0; i < n; i++ {
   169  		result.AppendString(data.User.String())
   170  	}
   171  	return nil
   172  }
   173  
   174  func (b *builtinMilevaDBIsDBSTenantSig) vectorized() bool {
   175  	return true
   176  }
   177  
   178  func (b *builtinMilevaDBIsDBSTenantSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   179  	n := input.NumEvents()
   180  	dbsTenantChecker := b.ctx.DBSTenantChecker()
   181  	var res int64
   182  	if dbsTenantChecker.IsTenant() {
   183  		res = 1
   184  	}
   185  	result.ResizeInt64(n, false)
   186  	i64s := result.Int64s()
   187  	for i := 0; i < n; i++ {
   188  		i64s[i] = res
   189  	}
   190  	return nil
   191  }
   192  
   193  func (b *builtinFoundEventsSig) vectorized() bool {
   194  	return true
   195  }
   196  
   197  func (b *builtinFoundEventsSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   198  	data := b.ctx.GetStochastikVars()
   199  	if data == nil {
   200  		return errors.Errorf("Missing stochastik variable when eval builtin")
   201  	}
   202  	lastFoundEvents := int64(data.LastFoundEvents)
   203  	n := input.NumEvents()
   204  	result.ResizeInt64(n, false)
   205  	i64s := result.Int64s()
   206  	for i := range i64s {
   207  		i64s[i] = lastFoundEvents
   208  	}
   209  	return nil
   210  }
   211  
   212  func (b *builtinBenchmarkSig) vectorized() bool {
   213  	return b.constLoopCount > 0
   214  }
   215  
   216  func (b *builtinBenchmarkSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   217  	n := input.NumEvents()
   218  	loopCount := b.constLoopCount
   219  	arg, ctx := b.args[1], b.ctx
   220  	evalType := arg.GetType().EvalType()
   221  	buf, err := b.bufSlabPredictor.get(evalType, n)
   222  	if err != nil {
   223  		return err
   224  	}
   225  	defer b.bufSlabPredictor.put(buf)
   226  
   227  	var k int64
   228  	switch evalType {
   229  	case types.ETInt:
   230  		for ; k < loopCount; k++ {
   231  			if err = arg.VecEvalInt(ctx, input, buf); err != nil {
   232  				return err
   233  			}
   234  		}
   235  	case types.ETReal:
   236  		for ; k < loopCount; k++ {
   237  			if err = arg.VecEvalReal(ctx, input, buf); err != nil {
   238  				return err
   239  			}
   240  		}
   241  	case types.ETDecimal:
   242  		for ; k < loopCount; k++ {
   243  			if err = arg.VecEvalDecimal(ctx, input, buf); err != nil {
   244  				return err
   245  			}
   246  		}
   247  	case types.ETString:
   248  		for ; k < loopCount; k++ {
   249  			if err = arg.VecEvalString(ctx, input, buf); err != nil {
   250  				return err
   251  			}
   252  		}
   253  	case types.ETDatetime, types.ETTimestamp:
   254  		for ; k < loopCount; k++ {
   255  			if err = arg.VecEvalTime(ctx, input, buf); err != nil {
   256  				return err
   257  			}
   258  		}
   259  	case types.ETDuration:
   260  		for ; k < loopCount; k++ {
   261  			if err = arg.VecEvalDuration(ctx, input, buf); err != nil {
   262  				return err
   263  			}
   264  		}
   265  	case types.ETJson:
   266  		for ; k < loopCount; k++ {
   267  			if err = arg.VecEvalJSON(ctx, input, buf); err != nil {
   268  				return err
   269  			}
   270  		}
   271  	default: // Should never go into here.
   272  		return errors.Errorf("EvalType %v not implemented for builtin BENCHMARK()", evalType)
   273  	}
   274  
   275  	// Return value of BENCHMARK() is always 0.
   276  	// even if args[1].IsNull(i)
   277  	result.ResizeInt64(n, false)
   278  
   279  	return nil
   280  }
   281  
   282  func (b *builtinLastInsertIDSig) vectorized() bool {
   283  	return true
   284  }
   285  
   286  func (b *builtinLastInsertIDSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   287  	n := input.NumEvents()
   288  	result.ResizeInt64(n, false)
   289  	i64s := result.Int64s()
   290  	res := int64(b.ctx.GetStochastikVars().StmtCtx.PrevLastInsertID)
   291  	for i := 0; i < n; i++ {
   292  		i64s[i] = res
   293  	}
   294  	return nil
   295  }
   296  
   297  func (b *builtinLastInsertIDWithIDSig) vectorized() bool {
   298  	return true
   299  }
   300  
   301  func (b *builtinLastInsertIDWithIDSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   302  	if err := b.args[0].VecEvalInt(b.ctx, input, result); err != nil {
   303  		return err
   304  	}
   305  	i64s := result.Int64s()
   306  	for i := len(i64s) - 1; i >= 0; i-- {
   307  		if !result.IsNull(i) {
   308  			b.ctx.GetStochastikVars().SetLastInsertID(uint64(i64s[i]))
   309  			break
   310  		}
   311  	}
   312  	return nil
   313  }
   314  
   315  func (b *builtinVersionSig) vectorized() bool {
   316  	return true
   317  }
   318  
   319  func (b *builtinVersionSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   320  	n := input.NumEvents()
   321  	result.ReserveString(n)
   322  	for i := 0; i < n; i++ {
   323  		result.AppendString(allegrosql.ServerVersion)
   324  	}
   325  	return nil
   326  }
   327  
   328  func (b *builtinMilevaDBDecodeKeySig) vectorized() bool {
   329  	return true
   330  }
   331  
   332  func (b *builtinMilevaDBDecodeKeySig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   333  	n := input.NumEvents()
   334  	buf, err := b.bufSlabPredictor.get(types.ETString, n)
   335  	if err != nil {
   336  		return err
   337  	}
   338  	defer b.bufSlabPredictor.put(buf)
   339  	if err := b.args[0].VecEvalString(b.ctx, input, buf); err != nil {
   340  		return err
   341  	}
   342  	result.ReserveString(n)
   343  	for i := 0; i < n; i++ {
   344  		if buf.IsNull(i) {
   345  			result.AppendNull()
   346  			continue
   347  		}
   348  		result.AppendString(decodeKey(b.ctx, buf.GetString(i)))
   349  	}
   350  	return nil
   351  }