github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/builtin_compare_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  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    18  	"github.com/whtcorpsinc/milevadb/types"
    19  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    20  )
    21  
    22  // vecEvalDecimal evals a builtinGreatestDecimalSig.
    23  // See http://dev.allegrosql.com/doc/refman/5.7/en/comparison-operators.html#function_greatest
    24  func (b *builtinGreatestDecimalSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error {
    25  	n := input.NumEvents()
    26  	buf, err := b.bufSlabPredictor.get(types.ETDecimal, n)
    27  	if err != nil {
    28  		return err
    29  	}
    30  	defer b.bufSlabPredictor.put(buf)
    31  	if err := b.args[0].VecEvalDecimal(b.ctx, input, result); err != nil {
    32  		return err
    33  	}
    34  
    35  	d64s := result.Decimals()
    36  	for j := 1; j < len(b.args); j++ {
    37  		if err := b.args[j].VecEvalDecimal(b.ctx, input, buf); err != nil {
    38  			return err
    39  		}
    40  		for i := 0; i < n; i++ {
    41  			if result.IsNull(i) {
    42  				continue
    43  			} else if buf.IsNull(i) {
    44  				result.SetNull(i, true)
    45  				continue
    46  			}
    47  			v := buf.GetDecimal(i)
    48  			if v.Compare(&d64s[i]) > 0 {
    49  				d64s[i] = *v
    50  			}
    51  		}
    52  	}
    53  	return nil
    54  }
    55  
    56  func (b *builtinGreatestDecimalSig) vectorized() bool {
    57  	return true
    58  }
    59  
    60  func (b *builtinLeastDecimalSig) vecEvalDecimal(input *chunk.Chunk, result *chunk.DeferredCauset) error {
    61  	n := input.NumEvents()
    62  	buf, err := b.bufSlabPredictor.get(types.ETDecimal, n)
    63  	if err != nil {
    64  		return err
    65  	}
    66  	defer b.bufSlabPredictor.put(buf)
    67  	if err := b.args[0].VecEvalDecimal(b.ctx, input, result); err != nil {
    68  		return err
    69  	}
    70  
    71  	d64s := result.Decimals()
    72  	for j := 1; j < len(b.args); j++ {
    73  		if err := b.args[j].VecEvalDecimal(b.ctx, input, buf); err != nil {
    74  			return err
    75  		}
    76  
    77  		result.MergeNulls(buf)
    78  		for i := 0; i < n; i++ {
    79  			if result.IsNull(i) {
    80  				continue
    81  			}
    82  			v := buf.GetDecimal(i)
    83  			if v.Compare(&d64s[i]) < 0 {
    84  				d64s[i] = *v
    85  			}
    86  		}
    87  	}
    88  	return nil
    89  }
    90  
    91  func (b *builtinLeastDecimalSig) vectorized() bool {
    92  	return true
    93  }
    94  
    95  func (b *builtinLeastIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error {
    96  	n := input.NumEvents()
    97  	buf, err := b.bufSlabPredictor.get(types.ETInt, n)
    98  	if err != nil {
    99  		return err
   100  	}
   101  	defer b.bufSlabPredictor.put(buf)
   102  	if err := b.args[0].VecEvalInt(b.ctx, input, result); err != nil {
   103  		return err
   104  	}
   105  
   106  	i64s := result.Int64s()
   107  	for j := 1; j < len(b.args); j++ {
   108  		if err := b.args[j].VecEvalInt(b.ctx, input, buf); err != nil {
   109  			return err
   110  		}
   111  
   112  		result.MergeNulls(buf)
   113  		for i := 0; i < n; i++ {
   114  			if result.IsNull(i) {
   115  				continue
   116  			}
   117  			v := buf.GetInt64(i)
   118  			if v < i64s[i] {
   119  				i64s[i] = v
   120  			}
   121  		}
   122  	}
   123  	return nil
   124  }
   125  
   126  func (b *builtinLeastIntSig) vectorized() bool {
   127  	return true
   128  }
   129  
   130  func (b *builtinGreatestIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   131  	n := input.NumEvents()
   132  	buf, err := b.bufSlabPredictor.get(types.ETInt, n)
   133  	if err != nil {
   134  		return err
   135  	}
   136  	defer b.bufSlabPredictor.put(buf)
   137  	if err := b.args[0].VecEvalInt(b.ctx, input, result); err != nil {
   138  		return err
   139  	}
   140  
   141  	i64s := result.Int64s()
   142  	for j := 1; j < len(b.args); j++ {
   143  		if err := b.args[j].VecEvalInt(b.ctx, input, buf); err != nil {
   144  			return err
   145  		}
   146  
   147  		result.MergeNulls(buf)
   148  		v := buf.Int64s()
   149  		for i := 0; i < n; i++ {
   150  			if result.IsNull(i) {
   151  				continue
   152  			}
   153  			if v[i] > i64s[i] {
   154  				i64s[i] = v[i]
   155  			}
   156  		}
   157  	}
   158  	return nil
   159  }
   160  
   161  func (b *builtinGreatestIntSig) vectorized() bool {
   162  	return true
   163  }
   164  
   165  func (b *builtinGEIntSig) vectorized() bool {
   166  	return true
   167  }
   168  
   169  func (b *builtinGEIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   170  	n := input.NumEvents()
   171  	var err error
   172  	var buf0, buf1 *chunk.DeferredCauset
   173  	buf0, err = b.bufSlabPredictor.get(types.ETInt, n)
   174  	if err != nil {
   175  		return err
   176  	}
   177  	defer b.bufSlabPredictor.put(buf0)
   178  	if err = b.args[0].VecEvalInt(b.ctx, input, buf0); err != nil {
   179  		return err
   180  	}
   181  	buf1, err = b.bufSlabPredictor.get(types.ETInt, n)
   182  	if err != nil {
   183  		return err
   184  	}
   185  	defer b.bufSlabPredictor.put(buf1)
   186  	if err = b.args[1].VecEvalInt(b.ctx, input, buf1); err != nil {
   187  		return err
   188  	}
   189  
   190  	result.ResizeInt64(n, false)
   191  	vecCompareInt(allegrosql.HasUnsignedFlag(b.args[0].GetType().Flag), allegrosql.HasUnsignedFlag(b.args[1].GetType().Flag), buf0, buf1, result)
   192  	result.MergeNulls(buf0, buf1)
   193  	vecResOfGE(result.Int64s())
   194  	return nil
   195  }
   196  
   197  func (b *builtinLeastRealSig) vectorized() bool {
   198  	return true
   199  }
   200  
   201  func (b *builtinLeastRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   202  	n := input.NumEvents()
   203  	buf, err := b.bufSlabPredictor.get(types.ETReal, n)
   204  	if err != nil {
   205  		return err
   206  	}
   207  	defer b.bufSlabPredictor.put(buf)
   208  	if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil {
   209  		return err
   210  	}
   211  
   212  	f64s := result.Float64s()
   213  	for j := 1; j < len(b.args); j++ {
   214  		if err := b.args[j].VecEvalReal(b.ctx, input, buf); err != nil {
   215  			return err
   216  		}
   217  
   218  		result.MergeNulls(buf)
   219  		v := buf.Float64s()
   220  		for i := 0; i < n; i++ {
   221  			if result.IsNull(i) {
   222  				continue
   223  			}
   224  			if v[i] < f64s[i] {
   225  				f64s[i] = v[i]
   226  			}
   227  		}
   228  	}
   229  	return nil
   230  }
   231  
   232  func (b *builtinLeastStringSig) vectorized() bool {
   233  	return true
   234  }
   235  
   236  func (b *builtinLeastStringSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   237  	if err := b.args[0].VecEvalString(b.ctx, input, result); err != nil {
   238  		return err
   239  	}
   240  
   241  	n := input.NumEvents()
   242  	buf1, err := b.bufSlabPredictor.get(types.ETString, n)
   243  	if err != nil {
   244  		return err
   245  	}
   246  	defer b.bufSlabPredictor.put(buf1)
   247  
   248  	buf2, err := b.bufSlabPredictor.get(types.ETString, n)
   249  	if err != nil {
   250  		return err
   251  	}
   252  	defer b.bufSlabPredictor.put(buf2)
   253  
   254  	src := result
   255  	arg := buf1
   256  	dst := buf2
   257  	dst.ReserveString(n)
   258  	for j := 1; j < len(b.args); j++ {
   259  		if err := b.args[j].VecEvalString(b.ctx, input, arg); err != nil {
   260  			return err
   261  		}
   262  		for i := 0; i < n; i++ {
   263  			if src.IsNull(i) || arg.IsNull(i) {
   264  				dst.AppendNull()
   265  				continue
   266  			}
   267  			srcStr := src.GetString(i)
   268  			argStr := arg.GetString(i)
   269  			if types.CompareString(srcStr, argStr, b.defCauslation) < 0 {
   270  				dst.AppendString(srcStr)
   271  			} else {
   272  				dst.AppendString(argStr)
   273  			}
   274  		}
   275  		src, dst = dst, src
   276  		arg.ReserveString(n)
   277  		dst.ReserveString(n)
   278  	}
   279  	if len(b.args)%2 == 0 {
   280  		src.CopyConstruct(result)
   281  	}
   282  	return nil
   283  }
   284  
   285  func (b *builtinEQIntSig) vectorized() bool {
   286  	return true
   287  }
   288  
   289  func (b *builtinEQIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   290  	n := input.NumEvents()
   291  	var err error
   292  	var buf0, buf1 *chunk.DeferredCauset
   293  	buf0, err = b.bufSlabPredictor.get(types.ETInt, n)
   294  	if err != nil {
   295  		return err
   296  	}
   297  	defer b.bufSlabPredictor.put(buf0)
   298  	if err := b.args[0].VecEvalInt(b.ctx, input, buf0); err != nil {
   299  		return err
   300  	}
   301  	buf1, err = b.bufSlabPredictor.get(types.ETInt, n)
   302  	if err != nil {
   303  		return err
   304  	}
   305  	defer b.bufSlabPredictor.put(buf1)
   306  	if err := b.args[1].VecEvalInt(b.ctx, input, buf1); err != nil {
   307  		return err
   308  	}
   309  
   310  	result.ResizeInt64(n, false)
   311  	vecCompareInt(allegrosql.HasUnsignedFlag(b.args[0].GetType().Flag), allegrosql.HasUnsignedFlag(b.args[1].GetType().Flag), buf0, buf1, result)
   312  	result.MergeNulls(buf0, buf1)
   313  	vecResOfEQ(result.Int64s())
   314  	return nil
   315  }
   316  
   317  func (b *builtinNEIntSig) vectorized() bool {
   318  	return true
   319  }
   320  
   321  func (b *builtinNEIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   322  	n := input.NumEvents()
   323  	var err error
   324  	var buf0, buf1 *chunk.DeferredCauset
   325  	buf0, err = b.bufSlabPredictor.get(types.ETInt, n)
   326  	if err != nil {
   327  		return err
   328  	}
   329  	defer b.bufSlabPredictor.put(buf0)
   330  	if err := b.args[0].VecEvalInt(b.ctx, input, buf0); err != nil {
   331  		return err
   332  	}
   333  	buf1, err = b.bufSlabPredictor.get(types.ETInt, n)
   334  	if err != nil {
   335  		return err
   336  	}
   337  	defer b.bufSlabPredictor.put(buf1)
   338  	if err := b.args[1].VecEvalInt(b.ctx, input, buf1); err != nil {
   339  		return err
   340  	}
   341  
   342  	result.ResizeInt64(n, false)
   343  	vecCompareInt(allegrosql.HasUnsignedFlag(b.args[0].GetType().Flag), allegrosql.HasUnsignedFlag(b.args[1].GetType().Flag), buf0, buf1, result)
   344  	result.MergeNulls(buf0, buf1)
   345  	vecResOfNE(result.Int64s())
   346  	return nil
   347  }
   348  
   349  func (b *builtinGTIntSig) vectorized() bool {
   350  	return true
   351  }
   352  
   353  func (b *builtinGTIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   354  	n := input.NumEvents()
   355  	var err error
   356  	var buf0, buf1 *chunk.DeferredCauset
   357  	buf0, err = b.bufSlabPredictor.get(types.ETInt, n)
   358  	if err != nil {
   359  		return err
   360  	}
   361  	defer b.bufSlabPredictor.put(buf0)
   362  	if err := b.args[0].VecEvalInt(b.ctx, input, buf0); err != nil {
   363  		return err
   364  	}
   365  	buf1, err = b.bufSlabPredictor.get(types.ETInt, n)
   366  	if err != nil {
   367  		return err
   368  	}
   369  	defer b.bufSlabPredictor.put(buf1)
   370  	if err := b.args[1].VecEvalInt(b.ctx, input, buf1); err != nil {
   371  		return err
   372  	}
   373  
   374  	result.ResizeInt64(n, false)
   375  	vecCompareInt(allegrosql.HasUnsignedFlag(b.args[0].GetType().Flag), allegrosql.HasUnsignedFlag(b.args[1].GetType().Flag), buf0, buf1, result)
   376  	result.MergeNulls(buf0, buf1)
   377  	vecResOfGT(result.Int64s())
   378  	return nil
   379  }
   380  
   381  func (b *builtinNullEQIntSig) vectorized() bool {
   382  	return true
   383  }
   384  
   385  func (b *builtinNullEQIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   386  	n := input.NumEvents()
   387  	buf0, err := b.bufSlabPredictor.get(types.ETInt, n)
   388  	if err != nil {
   389  		return err
   390  	}
   391  	defer b.bufSlabPredictor.put(buf0)
   392  	if err := b.args[0].VecEvalInt(b.ctx, input, buf0); err != nil {
   393  		return err
   394  	}
   395  	buf1, err := b.bufSlabPredictor.get(types.ETInt, n)
   396  	if err != nil {
   397  		return err
   398  	}
   399  	defer b.bufSlabPredictor.put(buf1)
   400  	result.ResizeInt64(n, false)
   401  	if err := b.args[1].VecEvalInt(b.ctx, input, buf1); err != nil {
   402  		return err
   403  	}
   404  	vecCompareInt(allegrosql.HasUnsignedFlag(b.args[0].GetType().Flag), allegrosql.HasUnsignedFlag(b.args[1].GetType().Flag), buf0, buf1, result)
   405  	i64s := result.Int64s()
   406  	for i := 0; i < n; i++ {
   407  		isNull0 := buf0.IsNull(i)
   408  		isNull1 := buf1.IsNull(i)
   409  		if isNull0 && isNull1 {
   410  			i64s[i] = 1
   411  		} else if isNull0 || isNull1 || i64s[i] != 0 {
   412  			i64s[i] = 0
   413  		} else {
   414  			i64s[i] = 1
   415  		}
   416  	}
   417  	return nil
   418  }
   419  
   420  func (b *builtinIntervalIntSig) vectorized() bool {
   421  	return true
   422  }
   423  
   424  func (b *builtinIntervalIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   425  	var err error
   426  	if err = b.args[0].VecEvalInt(b.ctx, input, result); err != nil {
   427  		return err
   428  	}
   429  	i64s := result.Int64s()
   430  	var idx int
   431  	for i, v := range i64s {
   432  		if result.IsNull(i) {
   433  			result.SetNull(i, false)
   434  			i64s[i] = -1
   435  			continue
   436  		}
   437  		if b.hasNullable {
   438  			idx, err = b.linearSearch(v, allegrosql.HasUnsignedFlag(b.args[0].GetType().Flag), b.args[1:], input.GetEvent(i))
   439  		} else {
   440  			idx, err = b.binSearch(v, allegrosql.HasUnsignedFlag(b.args[0].GetType().Flag), b.args[1:], input.GetEvent(i))
   441  		}
   442  		if err != nil {
   443  			return err
   444  		}
   445  		i64s[i] = int64(idx)
   446  	}
   447  	return nil
   448  }
   449  
   450  func (b *builtinIntervalRealSig) vectorized() bool {
   451  	return true
   452  }
   453  
   454  func (b *builtinIntervalRealSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   455  	n := input.NumEvents()
   456  	buf, err := b.bufSlabPredictor.get(types.ETReal, n)
   457  	if err != nil {
   458  		return err
   459  	}
   460  	defer b.bufSlabPredictor.put(buf)
   461  	if err = b.args[0].VecEvalReal(b.ctx, input, buf); err != nil {
   462  		return err
   463  	}
   464  
   465  	f64s := buf.Float64s()
   466  	result.ResizeInt64(n, false)
   467  	res := result.Int64s()
   468  	var idx int
   469  	for i := 0; i < n; i++ {
   470  		if buf.IsNull(i) {
   471  			res[i] = -1
   472  			continue
   473  		}
   474  		if b.hasNullable {
   475  			idx, err = b.linearSearch(f64s[i], b.args[1:], input.GetEvent(i))
   476  		} else {
   477  			idx, err = b.binSearch(f64s[i], b.args[1:], input.GetEvent(i))
   478  		}
   479  		if err != nil {
   480  			return err
   481  		}
   482  		res[i] = int64(idx)
   483  	}
   484  	return nil
   485  }
   486  
   487  func (b *builtinLEIntSig) vectorized() bool {
   488  	return true
   489  }
   490  
   491  func (b *builtinLEIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   492  	n := input.NumEvents()
   493  	var err error
   494  	var buf0, buf1 *chunk.DeferredCauset
   495  	buf0, err = b.bufSlabPredictor.get(types.ETInt, n)
   496  	if err != nil {
   497  		return err
   498  	}
   499  	defer b.bufSlabPredictor.put(buf0)
   500  	if err := b.args[0].VecEvalInt(b.ctx, input, buf0); err != nil {
   501  		return err
   502  	}
   503  	buf1, err = b.bufSlabPredictor.get(types.ETInt, n)
   504  	if err != nil {
   505  		return err
   506  	}
   507  	defer b.bufSlabPredictor.put(buf1)
   508  	if err := b.args[1].VecEvalInt(b.ctx, input, buf1); err != nil {
   509  		return err
   510  	}
   511  
   512  	result.ResizeInt64(n, false)
   513  	vecCompareInt(allegrosql.HasUnsignedFlag(b.args[0].GetType().Flag), allegrosql.HasUnsignedFlag(b.args[1].GetType().Flag), buf0, buf1, result)
   514  	result.MergeNulls(buf0, buf1)
   515  	vecResOfLE(result.Int64s())
   516  	return nil
   517  }
   518  
   519  func (b *builtinLTIntSig) vectorized() bool {
   520  	return true
   521  }
   522  
   523  func (b *builtinLTIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   524  	n := input.NumEvents()
   525  	var err error
   526  	var buf0, buf1 *chunk.DeferredCauset
   527  	buf0, err = b.bufSlabPredictor.get(types.ETInt, n)
   528  	if err != nil {
   529  		return err
   530  	}
   531  	defer b.bufSlabPredictor.put(buf0)
   532  	if err := b.args[0].VecEvalInt(b.ctx, input, buf0); err != nil {
   533  		return err
   534  	}
   535  	buf1, err = b.bufSlabPredictor.get(types.ETInt, n)
   536  	if err != nil {
   537  		return err
   538  	}
   539  	defer b.bufSlabPredictor.put(buf1)
   540  	if err := b.args[1].VecEvalInt(b.ctx, input, buf1); err != nil {
   541  		return err
   542  	}
   543  
   544  	result.ResizeInt64(n, false)
   545  	vecCompareInt(allegrosql.HasUnsignedFlag(b.args[0].GetType().Flag), allegrosql.HasUnsignedFlag(b.args[1].GetType().Flag), buf0, buf1, result)
   546  	result.MergeNulls(buf0, buf1)
   547  	vecResOfLT(result.Int64s())
   548  	return nil
   549  }
   550  
   551  func vecResOfLT(res []int64) {
   552  	n := len(res)
   553  	for i := 0; i < n; i++ {
   554  		if res[i] < 0 {
   555  			res[i] = 1
   556  		} else {
   557  			res[i] = 0
   558  		}
   559  	}
   560  }
   561  
   562  func vecResOfNE(res []int64) {
   563  	n := len(res)
   564  	for i := 0; i < n; i++ {
   565  		if res[i] != 0 {
   566  			res[i] = 1
   567  		} else {
   568  			res[i] = 0
   569  		}
   570  	}
   571  }
   572  
   573  func vecResOfEQ(res []int64) {
   574  	n := len(res)
   575  	for i := 0; i < n; i++ {
   576  		if res[i] == 0 {
   577  			res[i] = 1
   578  		} else {
   579  			res[i] = 0
   580  		}
   581  	}
   582  }
   583  
   584  func vecResOfLE(res []int64) {
   585  	n := len(res)
   586  	for i := 0; i < n; i++ {
   587  		if res[i] <= 0 {
   588  			res[i] = 1
   589  		} else {
   590  			res[i] = 0
   591  		}
   592  	}
   593  }
   594  
   595  func vecResOfGT(res []int64) {
   596  	n := len(res)
   597  	for i := 0; i < n; i++ {
   598  		if res[i] > 0 {
   599  			res[i] = 1
   600  		} else {
   601  			res[i] = 0
   602  		}
   603  	}
   604  }
   605  
   606  func vecResOfGE(res []int64) {
   607  	n := len(res)
   608  	for i := 0; i < n; i++ {
   609  		if res[i] >= 0 {
   610  			res[i] = 1
   611  		} else {
   612  			res[i] = 0
   613  		}
   614  	}
   615  }
   616  
   617  //vecCompareInt is vectorized CompareInt()
   618  func vecCompareInt(isUnsigned0, isUnsigned1 bool, largs, rargs, result *chunk.DeferredCauset) {
   619  	switch {
   620  	case isUnsigned0 && isUnsigned1:
   621  		types.VecCompareUU(largs.Uint64s(), rargs.Uint64s(), result.Int64s())
   622  	case isUnsigned0 && !isUnsigned1:
   623  		types.VecCompareUI(largs.Uint64s(), rargs.Int64s(), result.Int64s())
   624  	case !isUnsigned0 && isUnsigned1:
   625  		types.VecCompareIU(largs.Int64s(), rargs.Uint64s(), result.Int64s())
   626  	case !isUnsigned0 && !isUnsigned1:
   627  		types.VecCompareII(largs.Int64s(), rargs.Int64s(), result.Int64s())
   628  	}
   629  }
   630  
   631  func (b *builtinGreatestTimeSig) vectorized() bool {
   632  	return true
   633  }
   634  
   635  func (b *builtinGreatestTimeSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   636  	n := input.NumEvents()
   637  	dst, err := b.bufSlabPredictor.get(types.ETTimestamp, n)
   638  	if err != nil {
   639  		return err
   640  	}
   641  	defer b.bufSlabPredictor.put(dst)
   642  
   643  	sc := b.ctx.GetStochastikVars().StmtCtx
   644  	dst.ResizeTime(n, false)
   645  	dstTimes := dst.Times()
   646  	for i := 0; i < n; i++ {
   647  		dstTimes[i] = types.ZeroDatetime
   648  	}
   649  	var argTime types.Time
   650  	for j := 0; j < len(b.args); j++ {
   651  		if err := b.args[j].VecEvalString(b.ctx, input, result); err != nil {
   652  			return err
   653  		}
   654  		for i := 0; i < n; i++ {
   655  			if result.IsNull(i) || dst.IsNull(i) {
   656  				dst.SetNull(i, true)
   657  				continue
   658  			}
   659  			argTime, err = types.ParseDatetime(sc, result.GetString(i))
   660  			if err != nil {
   661  				if err = handleInvalidTimeError(b.ctx, err); err != nil {
   662  					return err
   663  				}
   664  				continue
   665  			}
   666  			if argTime.Compare(dstTimes[i]) > 0 {
   667  				dstTimes[i] = argTime
   668  			}
   669  		}
   670  	}
   671  	result.ReserveString(n)
   672  	for i := 0; i < n; i++ {
   673  		if dst.IsNull(i) {
   674  			result.AppendNull()
   675  		} else {
   676  			result.AppendString(dstTimes[i].String())
   677  		}
   678  	}
   679  	return nil
   680  }
   681  
   682  func (b *builtinGreatestRealSig) vectorized() bool {
   683  	return true
   684  }
   685  
   686  func (b *builtinGreatestRealSig) vecEvalReal(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   687  	n := input.NumEvents()
   688  	buf, err := b.bufSlabPredictor.get(types.ETReal, n)
   689  	if err != nil {
   690  		return err
   691  	}
   692  	defer b.bufSlabPredictor.put(buf)
   693  	if err := b.args[0].VecEvalReal(b.ctx, input, result); err != nil {
   694  		return err
   695  	}
   696  
   697  	f64s := result.Float64s()
   698  	for j := 1; j < len(b.args); j++ {
   699  		if err := b.args[j].VecEvalReal(b.ctx, input, buf); err != nil {
   700  			return err
   701  		}
   702  
   703  		result.MergeNulls(buf)
   704  		v := buf.Float64s()
   705  		for i := 0; i < n; i++ {
   706  			if result.IsNull(i) {
   707  				continue
   708  			}
   709  			if v[i] > f64s[i] {
   710  				f64s[i] = v[i]
   711  			}
   712  		}
   713  	}
   714  	return nil
   715  }
   716  
   717  func (b *builtinLeastTimeSig) vectorized() bool {
   718  	return true
   719  }
   720  
   721  func (b *builtinLeastTimeSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   722  	n := input.NumEvents()
   723  	dst, err := b.bufSlabPredictor.get(types.ETTimestamp, n)
   724  	if err != nil {
   725  		return err
   726  	}
   727  	defer b.bufSlabPredictor.put(dst)
   728  
   729  	sc := b.ctx.GetStochastikVars().StmtCtx
   730  	dst.ResizeTime(n, false)
   731  	dstTimes := dst.Times()
   732  	for i := 0; i < n; i++ {
   733  		dstTimes[i] = types.NewTime(types.MaxDatetime, allegrosql.TypeDatetime, types.DefaultFsp)
   734  	}
   735  	var argTime types.Time
   736  
   737  	findInvalidTime := make([]bool, n)
   738  	invalidValue := make([]string, n)
   739  
   740  	for j := 0; j < len(b.args); j++ {
   741  		if err := b.args[j].VecEvalString(b.ctx, input, result); err != nil {
   742  			return err
   743  		}
   744  		dst.MergeNulls(result)
   745  		for i := 0; i < n; i++ {
   746  			if dst.IsNull(i) {
   747  				continue
   748  			}
   749  			argTime, err = types.ParseDatetime(sc, result.GetString(i))
   750  			if err != nil {
   751  				if err = handleInvalidTimeError(b.ctx, err); err != nil {
   752  					return err
   753  				} else if !findInvalidTime[i] {
   754  					// Make a deep copy here.
   755  					// Otherwise invalidValue will internally change with result.
   756  					invalidValue[i] = string(result.GetBytes(i))
   757  					findInvalidTime[i] = true
   758  				}
   759  				continue
   760  			}
   761  			if argTime.Compare(dstTimes[i]) < 0 {
   762  				dstTimes[i] = argTime
   763  			}
   764  		}
   765  	}
   766  	result.ReserveString(n)
   767  	for i := 0; i < n; i++ {
   768  		if dst.IsNull(i) {
   769  			result.AppendNull()
   770  			continue
   771  		}
   772  		if findInvalidTime[i] {
   773  			result.AppendString(invalidValue[i])
   774  		} else {
   775  			result.AppendString(dstTimes[i].String())
   776  		}
   777  	}
   778  	return nil
   779  }
   780  
   781  func (b *builtinGreatestStringSig) vectorized() bool {
   782  	return true
   783  }
   784  
   785  func (b *builtinGreatestStringSig) vecEvalString(input *chunk.Chunk, result *chunk.DeferredCauset) error {
   786  	if err := b.args[0].VecEvalString(b.ctx, input, result); err != nil {
   787  		return err
   788  	}
   789  
   790  	n := input.NumEvents()
   791  	buf1, err := b.bufSlabPredictor.get(types.ETString, n)
   792  	if err != nil {
   793  		return err
   794  	}
   795  	defer b.bufSlabPredictor.put(buf1)
   796  	buf2, err := b.bufSlabPredictor.get(types.ETString, n)
   797  	if err != nil {
   798  		return err
   799  	}
   800  	defer b.bufSlabPredictor.put(buf2)
   801  
   802  	src := result
   803  	arg := buf1
   804  	dst := buf2
   805  	dst.ReserveString(n)
   806  	for j := 1; j < len(b.args); j++ {
   807  		if err := b.args[j].VecEvalString(b.ctx, input, arg); err != nil {
   808  			return err
   809  		}
   810  		for i := 0; i < n; i++ {
   811  			if src.IsNull(i) || arg.IsNull(i) {
   812  				dst.AppendNull()
   813  				continue
   814  			}
   815  			srcStr := src.GetString(i)
   816  			argStr := arg.GetString(i)
   817  			if types.CompareString(srcStr, argStr, b.defCauslation) > 0 {
   818  				dst.AppendString(srcStr)
   819  			} else {
   820  				dst.AppendString(argStr)
   821  			}
   822  		}
   823  		src, dst = dst, src
   824  		arg.ReserveString(n)
   825  		dst.ReserveString(n)
   826  	}
   827  	if len(b.args)%2 == 0 {
   828  		src.CopyConstruct(result)
   829  	}
   830  	return nil
   831  }