github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/function/func_builtin.go (about)

     1  // Copyright 2021 - 2022 Matrix Origin
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package function
    16  
    17  import (
    18  	"bytes"
    19  	"context"
    20  	"encoding/json"
    21  	"fmt"
    22  	"math"
    23  	"math/rand"
    24  	"strings"
    25  	"time"
    26  	"unsafe"
    27  
    28  	"github.com/matrixorigin/matrixone/pkg/common/hashmap"
    29  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    30  	"github.com/matrixorigin/matrixone/pkg/common/runtime"
    31  	"github.com/matrixorigin/matrixone/pkg/common/util"
    32  	"github.com/matrixorigin/matrixone/pkg/config"
    33  	"github.com/matrixorigin/matrixone/pkg/container/hashtable"
    34  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    35  	"github.com/matrixorigin/matrixone/pkg/container/types"
    36  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    37  	"github.com/matrixorigin/matrixone/pkg/pb/plan"
    38  	"github.com/matrixorigin/matrixone/pkg/sql/plan/function/functionUtil"
    39  	"github.com/matrixorigin/matrixone/pkg/util/executor"
    40  	"github.com/matrixorigin/matrixone/pkg/util/export/table"
    41  	"github.com/matrixorigin/matrixone/pkg/util/trace/impl/motrace"
    42  	"github.com/matrixorigin/matrixone/pkg/util/trace/impl/motrace/statistic"
    43  	"github.com/matrixorigin/matrixone/pkg/vectorize/moarray"
    44  	"github.com/matrixorigin/matrixone/pkg/vectorize/momath"
    45  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    46  
    47  	"github.com/google/uuid"
    48  )
    49  
    50  func builtInDateDiff(parameters []*vector.Vector, result vector.FunctionResultWrapper, _ *process.Process, length int) error {
    51  	p1 := vector.GenerateFunctionFixedTypeParameter[types.Date](parameters[0])
    52  	p2 := vector.GenerateFunctionFixedTypeParameter[types.Date](parameters[1])
    53  	rs := vector.MustFunctionResult[int64](result)
    54  	for i := uint64(0); i < uint64(length); i++ {
    55  		v1, null1 := p1.GetValue(i)
    56  		v2, null2 := p2.GetValue(i)
    57  		if null1 || null2 {
    58  			if err := rs.Append(0, true); err != nil {
    59  				return err
    60  			}
    61  		} else {
    62  			if err := rs.Append(int64(v1-v2), false); err != nil {
    63  				return err
    64  			}
    65  		}
    66  	}
    67  	return nil
    68  }
    69  
    70  func builtInCurrentTimestamp(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
    71  	rs := vector.MustFunctionResult[types.Timestamp](result)
    72  
    73  	// TODO: not a good way to solve this problem. and will be fixed by file `specialRule.go`
    74  	scale := int32(6)
    75  	if len(ivecs) == 1 && !ivecs[0].IsConstNull() {
    76  		scale = int32(vector.MustFixedCol[int64](ivecs[0])[0])
    77  	}
    78  	rs.TempSetType(types.New(types.T_timestamp, 0, scale))
    79  
    80  	resultValue := types.UnixNanoToTimestamp(proc.UnixTime)
    81  	for i := uint64(0); i < uint64(length); i++ {
    82  		if err := rs.Append(resultValue, false); err != nil {
    83  			return err
    84  		}
    85  	}
    86  
    87  	return nil
    88  }
    89  
    90  func builtInSysdate(ivecs []*vector.Vector, result vector.FunctionResultWrapper, _ *process.Process, length int) error {
    91  	rs := vector.MustFunctionResult[types.Timestamp](result)
    92  
    93  	scale := int32(6)
    94  	if len(ivecs) == 1 && !ivecs[0].IsConstNull() {
    95  		scale = int32(vector.MustFixedCol[int64](ivecs[0])[0])
    96  	}
    97  	rs.TempSetType(types.New(types.T_timestamp, 0, scale))
    98  
    99  	resultValue := types.UnixNanoToTimestamp(time.Now().UnixNano())
   100  	for i := uint64(0); i < uint64(length); i++ {
   101  		if err := rs.Append(resultValue, false); err != nil {
   102  			return err
   103  		}
   104  	}
   105  
   106  	return nil
   107  }
   108  
   109  const (
   110  	onUpdateExpr = iota
   111  	defaultExpr
   112  	typNormal
   113  	typWithLen
   114  )
   115  
   116  func builtInMoShowVisibleBin(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   117  	p1 := vector.GenerateFunctionStrParameter(parameters[0])
   118  	p2 := vector.GenerateFunctionFixedTypeParameter[uint8](parameters[1])
   119  
   120  	tp, null := p2.GetValue(0)
   121  	if null {
   122  		return moerr.NewNotSupported(proc.Ctx, "show visible bin, the second argument must be in [0, 3], but got NULL")
   123  	}
   124  	if tp > 3 {
   125  		return moerr.NewNotSupported(proc.Ctx, fmt.Sprintf("show visible bin, the second argument must be in [0, 3], but got %d", tp))
   126  	}
   127  
   128  	var f func(s []byte) ([]byte, error)
   129  	rs := vector.MustFunctionResult[types.Varlena](result)
   130  	switch tp {
   131  	case onUpdateExpr:
   132  		f = func(s []byte) ([]byte, error) {
   133  			update := new(plan.OnUpdate)
   134  			err := types.Decode(s, update)
   135  			if err != nil {
   136  				return nil, err
   137  			}
   138  			return functionUtil.QuickStrToBytes(update.OriginString), nil
   139  		}
   140  	case defaultExpr:
   141  		f = func(s []byte) ([]byte, error) {
   142  			def := new(plan.Default)
   143  			err := types.Decode(s, def)
   144  			if err != nil {
   145  				return nil, err
   146  			}
   147  			return functionUtil.QuickStrToBytes(def.OriginString), nil
   148  		}
   149  	case typNormal:
   150  		f = func(s []byte) ([]byte, error) {
   151  			typ := new(types.Type)
   152  			err := types.Decode(s, typ)
   153  			if err != nil {
   154  				return nil, err
   155  			}
   156  			return functionUtil.QuickStrToBytes(typ.String()), nil
   157  		}
   158  	case typWithLen:
   159  		f = func(s []byte) ([]byte, error) {
   160  			typ := new(types.Type)
   161  			err := types.Decode(s, typ)
   162  			if err != nil {
   163  				return nil, err
   164  			}
   165  			ret := fmt.Sprintf("%s(%d)", typ.String(), typ.Width)
   166  			return functionUtil.QuickStrToBytes(ret), nil
   167  		}
   168  	}
   169  
   170  	for i := uint64(0); i < uint64(length); i++ {
   171  		v1, null1 := p1.GetStrValue(i)
   172  		if null1 || len(v1) == 0 {
   173  			if err := rs.AppendBytes(nil, true); err != nil {
   174  				return err
   175  			}
   176  		} else {
   177  			b, err := f(v1)
   178  			if err != nil {
   179  				return err
   180  			}
   181  			if b == nil {
   182  				if err := rs.AppendBytes(nil, true); err != nil {
   183  					return err
   184  				}
   185  			} else {
   186  				if err = rs.AppendBytes(b, false); err != nil {
   187  					return err
   188  				}
   189  			}
   190  		}
   191  	}
   192  
   193  	return nil
   194  }
   195  
   196  func builtInMoShowVisibleBinEnum(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   197  	p1 := vector.GenerateFunctionStrParameter(parameters[0])
   198  	enumVal := vector.GenerateFunctionStrParameter(parameters[1])
   199  
   200  	var f func([]byte, string) ([]byte, error)
   201  	rs := vector.MustFunctionResult[types.Varlena](result)
   202  	f = func(s []byte, enumStr string) ([]byte, error) {
   203  		typ := new(types.Type)
   204  		err := types.Decode(s, typ)
   205  		if err != nil {
   206  			return nil, err
   207  		}
   208  		if typ.Oid != types.T_enum {
   209  			return nil, moerr.NewNotSupported(proc.Ctx, "show visible bin enum, the type must be enum, but got %s", typ.String())
   210  		}
   211  
   212  		// get enum values
   213  		enums := strings.Split(enumStr, ",")
   214  		enumVal := ""
   215  		for i, e := range enums {
   216  			enumVal += fmt.Sprintf("'%s'", e)
   217  			if i < len(enums)-1 {
   218  				enumVal += ","
   219  			}
   220  		}
   221  		ret := fmt.Sprintf("%s(%s)", typ.String(), enumVal)
   222  		return functionUtil.QuickStrToBytes(ret), nil
   223  	}
   224  
   225  	for i := uint64(0); i < uint64(length); i++ {
   226  		v1, null1 := p1.GetStrValue(i)
   227  		enumStr, null2 := enumVal.GetStrValue(i)
   228  		if null1 || null2 || len(v1) == 0 || len(enumStr) == 0 {
   229  			if err := rs.AppendBytes(nil, true); err != nil {
   230  				return err
   231  			}
   232  		} else {
   233  			enumString := functionUtil.QuickBytesToStr(enumStr)
   234  			b, err := f(v1, enumString)
   235  			if err != nil {
   236  				return err
   237  			}
   238  			if b == nil {
   239  				if err := rs.AppendBytes(nil, true); err != nil {
   240  					return err
   241  				}
   242  			} else {
   243  				if err = rs.AppendBytes(b, false); err != nil {
   244  					return err
   245  				}
   246  			}
   247  		}
   248  	}
   249  
   250  	return nil
   251  }
   252  
   253  func builtInInternalCharLength(parameters []*vector.Vector, result vector.FunctionResultWrapper, _ *process.Process, length int) error {
   254  	p1 := vector.GenerateFunctionStrParameter(parameters[0])
   255  	rs := vector.MustFunctionResult[int64](result)
   256  	for i := uint64(0); i < uint64(length); i++ {
   257  		v, null := p1.GetStrValue(i)
   258  		if !null {
   259  			typ := types.Type{}
   260  			if err := types.Decode(v, &typ); err != nil {
   261  				return err
   262  			}
   263  			if typ.Oid.IsMySQLString() {
   264  				if err := rs.Append(int64(typ.Width), false); err != nil {
   265  					return err
   266  				}
   267  				continue
   268  			}
   269  		}
   270  		if err := rs.Append(0, true); err != nil {
   271  			return err
   272  		}
   273  	}
   274  	return nil
   275  }
   276  
   277  func builtInInternalCharSize(parameters []*vector.Vector, result vector.FunctionResultWrapper, _ *process.Process, length int) error {
   278  	p1 := vector.GenerateFunctionStrParameter(parameters[0])
   279  	rs := vector.MustFunctionResult[int64](result)
   280  	for i := uint64(0); i < uint64(length); i++ {
   281  		v, null := p1.GetStrValue(i)
   282  		if !null {
   283  			typ := types.Type{}
   284  			if err := types.Decode(v, &typ); err != nil {
   285  				return err
   286  			}
   287  			if typ.Oid.IsMySQLString() {
   288  				if err := rs.Append(int64(typ.GetSize()*typ.Width), false); err != nil {
   289  					return err
   290  				}
   291  				continue
   292  			}
   293  		}
   294  		if err := rs.Append(0, true); err != nil {
   295  			return err
   296  		}
   297  	}
   298  	return nil
   299  }
   300  
   301  func builtInInternalNumericPrecision(parameters []*vector.Vector, result vector.FunctionResultWrapper, _ *process.Process, length int) error {
   302  	p1 := vector.GenerateFunctionStrParameter(parameters[0])
   303  	rs := vector.MustFunctionResult[int64](result)
   304  	for i := uint64(0); i < uint64(length); i++ {
   305  		v, null := p1.GetStrValue(i)
   306  		if !null {
   307  			typ := types.Type{}
   308  			if err := types.Decode(v, &typ); err != nil {
   309  				return err
   310  			}
   311  			if typ.Oid.IsDecimal() {
   312  				if err := rs.Append(int64(typ.Width), false); err != nil {
   313  					return err
   314  				}
   315  				continue
   316  			}
   317  		}
   318  		if err := rs.Append(0, true); err != nil {
   319  			return err
   320  		}
   321  	}
   322  	return nil
   323  }
   324  
   325  func builtInInternalNumericScale(parameters []*vector.Vector, result vector.FunctionResultWrapper, _ *process.Process, length int) error {
   326  	p1 := vector.GenerateFunctionStrParameter(parameters[0])
   327  	rs := vector.MustFunctionResult[int64](result)
   328  	for i := uint64(0); i < uint64(length); i++ {
   329  		v, null := p1.GetStrValue(i)
   330  		if !null {
   331  			typ := types.Type{}
   332  			if err := types.Decode(v, &typ); err != nil {
   333  				return err
   334  			}
   335  			if typ.Oid.IsDecimal() {
   336  				if err := rs.Append(int64(typ.Scale), false); err != nil {
   337  					return err
   338  				}
   339  				continue
   340  			}
   341  		}
   342  		if err := rs.Append(0, true); err != nil {
   343  			return err
   344  		}
   345  	}
   346  	return nil
   347  }
   348  
   349  func builtInInternalDatetimeScale(parameters []*vector.Vector, result vector.FunctionResultWrapper, _ *process.Process, length int) error {
   350  	p1 := vector.GenerateFunctionStrParameter(parameters[0])
   351  	rs := vector.MustFunctionResult[int64](result)
   352  	for i := uint64(0); i < uint64(length); i++ {
   353  		v, null := p1.GetStrValue(i)
   354  		if !null {
   355  			typ := types.Type{}
   356  			if err := types.Decode(v, &typ); err != nil {
   357  				return err
   358  			}
   359  			if typ.Oid == types.T_datetime {
   360  				if err := rs.Append(int64(typ.Scale), false); err != nil {
   361  					return err
   362  				}
   363  				continue
   364  			}
   365  		}
   366  		if err := rs.Append(0, true); err != nil {
   367  			return err
   368  		}
   369  	}
   370  	return nil
   371  }
   372  
   373  func builtInInternalCharacterSet(parameters []*vector.Vector, result vector.FunctionResultWrapper, _ *process.Process, length int) error {
   374  	p1 := vector.GenerateFunctionStrParameter(parameters[0])
   375  	rs := vector.MustFunctionResult[int64](result)
   376  	for i := uint64(0); i < uint64(length); i++ {
   377  		v, null := p1.GetStrValue(i)
   378  		if !null {
   379  			typ := types.Type{}
   380  			if err := types.Decode(v, &typ); err != nil {
   381  				return err
   382  			}
   383  			if typ.Oid == types.T_varchar || typ.Oid == types.T_char ||
   384  				typ.Oid == types.T_blob || typ.Oid == types.T_text {
   385  				if err := rs.Append(int64(typ.Scale), false); err != nil {
   386  					return err
   387  				}
   388  				continue
   389  			}
   390  		}
   391  		if err := rs.Append(0, true); err != nil {
   392  			return err
   393  		}
   394  	}
   395  	return nil
   396  }
   397  
   398  func builtInConcatCheck(_ []overload, inputs []types.Type) checkResult {
   399  	if len(inputs) > 1 {
   400  		shouldCast := false
   401  
   402  		ret := make([]types.Type, len(inputs))
   403  		for i, source := range inputs {
   404  			if !source.Oid.IsMySQLString() {
   405  				c, _ := tryToMatch([]types.Type{source}, []types.T{types.T_varchar})
   406  				if c == matchFailed {
   407  					return newCheckResultWithFailure(failedFunctionParametersWrong)
   408  				}
   409  				if c == matchByCast {
   410  					shouldCast = true
   411  					ret[i] = types.T_varchar.ToType()
   412  				}
   413  			} else {
   414  				ret[i] = source
   415  			}
   416  		}
   417  		if shouldCast {
   418  			return newCheckResultWithCast(0, ret)
   419  		}
   420  		return newCheckResultWithSuccess(0)
   421  	}
   422  	return newCheckResultWithFailure(failedFunctionParametersWrong)
   423  }
   424  
   425  func builtInConcat(parameters []*vector.Vector, result vector.FunctionResultWrapper, _ *process.Process, length int) error {
   426  	rs := vector.MustFunctionResult[types.Varlena](result)
   427  	ps := make([]vector.FunctionParameterWrapper[types.Varlena], len(parameters))
   428  	for i := range ps {
   429  		ps[i] = vector.GenerateFunctionStrParameter(parameters[i])
   430  	}
   431  
   432  	for i := uint64(0); i < uint64(length); i++ {
   433  		var vs string
   434  		apv := true
   435  
   436  		for _, p := range ps {
   437  			v, null := p.GetStrValue(i)
   438  			if null {
   439  				if err := rs.AppendBytes(nil, true); err != nil {
   440  					return err
   441  				}
   442  				apv = false
   443  				break
   444  			} else {
   445  				vs += string(v)
   446  			}
   447  		}
   448  		if apv {
   449  			if err := rs.AppendBytes([]byte(vs), false); err != nil {
   450  				return err
   451  			}
   452  		}
   453  	}
   454  	return nil
   455  }
   456  
   457  const (
   458  	formatMask = "%Y/%m/%d"
   459  	regexpMask = `\d{1,4}/\d{1,2}/\d{1,2}`
   460  )
   461  
   462  // MOLogDate parse 'YYYY/MM/DD' date from input string.
   463  // return '0001-01-01' if input string not container 'YYYY/MM/DD' substr, until DateParse Function support return NULL for invalid date string.
   464  func builtInMoLogDate(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   465  	rs := vector.MustFunctionResult[types.Date](result)
   466  	p1 := vector.GenerateFunctionStrParameter(parameters[0])
   467  
   468  	op := newOpBuiltInRegexp()
   469  	generalTime := NewGeneralTime()
   470  	for i := uint64(0); i < uint64(length); i++ {
   471  		v, null := p1.GetStrValue(i)
   472  		if null {
   473  			if err := rs.Append(0, true); err != nil {
   474  				return err
   475  			}
   476  		} else {
   477  			expr := functionUtil.QuickBytesToStr(v)
   478  			match, parsedInput, err := op.regMap.regularSubstr(regexpMask, expr, 1, 1)
   479  			if err != nil {
   480  				return err
   481  			}
   482  			if !match {
   483  				if err = rs.Append(0, true); err != nil {
   484  					return err
   485  				}
   486  			} else {
   487  				generalTime.ResetTime()
   488  				success := coreStrToDate(proc.Ctx, generalTime, parsedInput, formatMask)
   489  				if success && types.ValidDate(int32(generalTime.year), generalTime.month, generalTime.day) {
   490  					val := types.DateFromCalendar(int32(generalTime.year), generalTime.month, generalTime.day)
   491  					if err = rs.Append(val, false); err != nil {
   492  						return err
   493  					}
   494  				} else {
   495  					if err = rs.Append(0, true); err != nil {
   496  						return err
   497  					}
   498  				}
   499  			}
   500  		}
   501  	}
   502  
   503  	return nil
   504  }
   505  
   506  // builtInPurgeLog act like `select mo_purge_log('rawlog,statement_info,metric', '2023-06-27')`
   507  func builtInPurgeLog(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   508  	rs := vector.MustFunctionResult[uint8](result)
   509  
   510  	p1 := vector.GenerateFunctionStrParameter(parameters[0])
   511  	p2 := vector.GenerateFunctionFixedTypeParameter[types.Date](parameters[1])
   512  
   513  	if proc.SessionInfo.AccountId != sysAccountID {
   514  		return moerr.NewNotSupported(proc.Ctx, "only support sys account")
   515  	}
   516  
   517  	for i := uint64(0); i < uint64(length); i++ {
   518  		v1, null1 := p1.GetStrValue(i)
   519  		v2, null2 := p2.GetValue(i)
   520  		// fixme: should we need to support null date?
   521  		if null1 || null2 {
   522  			rs.Append(uint8(1), true)
   523  			continue
   524  		}
   525  
   526  		v, ok := runtime.ProcessLevelRuntime().GetGlobalVariables(runtime.InternalSQLExecutor)
   527  		if !ok {
   528  			return moerr.NewNotSupported(proc.Ctx, "no implement sqlExecutor")
   529  		}
   530  		exec := v.(executor.SQLExecutor)
   531  		tables := table.GetAllTables()
   532  		tableNames := strings.Split(util.UnsafeBytesToString(v1), ",")
   533  		for _, tblName := range tableNames {
   534  			found := false
   535  			for _, tbl := range tables {
   536  				if tbl.TimestampColumn != nil && strings.TrimSpace(tblName) == tbl.Table {
   537  					found = true
   538  					break
   539  				}
   540  			}
   541  			if !found {
   542  				return moerr.NewNotSupported(proc.Ctx, "purge '%s'", tblName)
   543  			}
   544  		}
   545  
   546  		for _, tblName := range tableNames {
   547  			for _, tbl := range tables {
   548  				if strings.TrimSpace(tblName) == tbl.Table {
   549  					sql := fmt.Sprintf("delete from `%s`.`%s` where `%s` < %q",
   550  						tbl.Database, tbl.Table, tbl.TimestampColumn.Name, v2.String())
   551  					opts := executor.Options{}.WithDatabase(tbl.Database).
   552  						WithTxn(proc.TxnOperator).
   553  						WithTimeZone(proc.SessionInfo.TimeZone)
   554  					if proc.TxnOperator != nil {
   555  						opts = opts.WithDisableIncrStatement() // this option always with WithTxn()
   556  					}
   557  					res, err := exec.Exec(proc.Ctx, sql, opts)
   558  					if err != nil {
   559  						return err
   560  					}
   561  					res.Close()
   562  				}
   563  			}
   564  		}
   565  
   566  		rs.Append(uint8(0), false)
   567  	}
   568  
   569  	return nil
   570  }
   571  
   572  func builtInDatabase(_ []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   573  	rs := vector.MustFunctionResult[types.Varlena](result)
   574  
   575  	for i := uint64(0); i < uint64(length); i++ {
   576  		db := proc.SessionInfo.GetDatabase()
   577  		if err := rs.AppendBytes(functionUtil.QuickStrToBytes(db), false); err != nil {
   578  			return err
   579  		}
   580  	}
   581  	return nil
   582  }
   583  
   584  func builtInCurrentRole(_ []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   585  	rs := vector.MustFunctionResult[types.Varlena](result)
   586  	for i := uint64(0); i < uint64(length); i++ {
   587  		if err := rs.AppendBytes([]byte(proc.SessionInfo.GetRole()), false); err != nil {
   588  			return err
   589  		}
   590  	}
   591  	return nil
   592  }
   593  
   594  func builtInCurrentAccountID(_ []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   595  	rs := vector.MustFunctionResult[uint32](result)
   596  	for i := uint64(0); i < uint64(length); i++ {
   597  		if err := rs.Append(proc.SessionInfo.AccountId, false); err != nil {
   598  			return err
   599  		}
   600  	}
   601  	return nil
   602  }
   603  
   604  func builtInCurrentAccountName(_ []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   605  	rs := vector.MustFunctionResult[types.Varlena](result)
   606  	for i := uint64(0); i < uint64(length); i++ {
   607  		if err := rs.AppendBytes([]byte(proc.SessionInfo.Account), false); err != nil {
   608  			return err
   609  		}
   610  	}
   611  	return nil
   612  }
   613  
   614  func builtInCurrentRoleID(_ []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   615  	rs := vector.MustFunctionResult[uint32](result)
   616  	for i := uint64(0); i < uint64(length); i++ {
   617  		if err := rs.Append(proc.SessionInfo.RoleId, false); err != nil {
   618  			return err
   619  		}
   620  	}
   621  	return nil
   622  }
   623  
   624  func builtInCurrentRoleName(_ []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   625  	rs := vector.MustFunctionResult[types.Varlena](result)
   626  	for i := uint64(0); i < uint64(length); i++ {
   627  		if err := rs.AppendBytes([]byte(proc.SessionInfo.Role), false); err != nil {
   628  			return err
   629  		}
   630  	}
   631  	return nil
   632  }
   633  
   634  func builtInCurrentUserID(_ []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   635  	rs := vector.MustFunctionResult[uint32](result)
   636  	for i := uint64(0); i < uint64(length); i++ {
   637  		if err := rs.Append(proc.SessionInfo.UserId, false); err != nil {
   638  			return err
   639  		}
   640  	}
   641  	return nil
   642  }
   643  
   644  func builtInCurrentUserName(_ []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   645  	rs := vector.MustFunctionResult[types.Varlena](result)
   646  	for i := uint64(0); i < uint64(length); i++ {
   647  		if err := rs.AppendBytes([]byte(proc.SessionInfo.User), false); err != nil {
   648  			return err
   649  		}
   650  	}
   651  	return nil
   652  }
   653  
   654  const MaxTgtLen = int64(16 * 1024 * 1024)
   655  
   656  func doLpad(src string, tgtLen int64, pad string) (string, bool) {
   657  	srcRune, padRune := []rune(src), []rune(pad)
   658  	srcLen, padLen := len(srcRune), len(padRune)
   659  
   660  	if tgtLen < 0 || tgtLen > MaxTgtLen {
   661  		return "", true
   662  	} else if int(tgtLen) < srcLen {
   663  		return string(srcRune[:tgtLen]), false
   664  	} else if int(tgtLen) == srcLen {
   665  		return src, false
   666  	} else if padLen == 0 {
   667  		return "", false
   668  	} else {
   669  		r := int(tgtLen) - srcLen
   670  		p, m := r/padLen, r%padLen
   671  		return strings.Repeat(pad, p) + string(padRune[:m]) + src, false
   672  	}
   673  }
   674  
   675  func doRpad(src string, tgtLen int64, pad string) (string, bool) {
   676  	srcRune, padRune := []rune(src), []rune(pad)
   677  	srcLen, padLen := len(srcRune), len(padRune)
   678  
   679  	if tgtLen < 0 || tgtLen > MaxTgtLen {
   680  		return "", true
   681  	} else if int(tgtLen) < srcLen {
   682  		return string(srcRune[:tgtLen]), false
   683  	} else if int(tgtLen) == srcLen {
   684  		return src, false
   685  	} else if padLen == 0 {
   686  		return "", false
   687  	} else {
   688  		r := int(tgtLen) - srcLen
   689  		p, m := r/padLen, r%padLen
   690  		return src + strings.Repeat(pad, p) + string(padRune[:m]), false
   691  	}
   692  }
   693  
   694  func builtInRepeat(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   695  	// repeat the string n times.
   696  	repeatNTimes := func(base string, n int64) (r string, null bool) {
   697  		if n <= 0 {
   698  			return "", false
   699  		}
   700  
   701  		// return null if result is too long.
   702  		// I'm not sure if this is the right thing to do, MySql can repeat string with the result length at least 1,000,000.
   703  		// and there is no documentation about the limit of the result length.
   704  		sourceLen := int64(len(base))
   705  		if sourceLen*n > MaxTgtLen {
   706  			return "", true
   707  		}
   708  		return strings.Repeat(base, int(n)), false
   709  	}
   710  
   711  	p1 := vector.GenerateFunctionStrParameter(parameters[0])
   712  	p2 := vector.GenerateFunctionFixedTypeParameter[int64](parameters[1])
   713  	rs := vector.MustFunctionResult[types.Varlena](result)
   714  
   715  	var err error
   716  	rowCount := uint64(length)
   717  	for i := uint64(0); i < rowCount; i++ {
   718  		v1, null1 := p1.GetStrValue(i)
   719  		v2, null2 := p2.GetValue(i)
   720  		if null1 || null2 {
   721  			err = rs.AppendMustNullForBytesResult()
   722  		} else {
   723  			r, null := repeatNTimes(functionUtil.QuickBytesToStr(v1), v2)
   724  			if null {
   725  				err = rs.AppendMustNullForBytesResult()
   726  			} else {
   727  				err = rs.AppendBytes([]byte(r), false)
   728  			}
   729  		}
   730  		if err != nil {
   731  			return err
   732  		}
   733  	}
   734  	return nil
   735  }
   736  
   737  func builtInLpad(parameters []*vector.Vector, result vector.FunctionResultWrapper, _ *process.Process, length int) error {
   738  	p1 := vector.GenerateFunctionStrParameter(parameters[0])
   739  	p2 := vector.GenerateFunctionFixedTypeParameter[int64](parameters[1])
   740  	p3 := vector.GenerateFunctionStrParameter(parameters[2])
   741  
   742  	rs := vector.MustFunctionResult[types.Varlena](result)
   743  	for i := uint64(0); i < uint64(length); i++ {
   744  		v1, null1 := p1.GetStrValue(i)
   745  		v2, null2 := p2.GetValue(i)
   746  		v3, null3 := p3.GetStrValue(i)
   747  		if !(null1 || null2 || null3) {
   748  			rval, shouldNull := doLpad(string(v1), v2, string(v3))
   749  			if !shouldNull {
   750  				if err := rs.AppendBytes([]byte(rval), false); err != nil {
   751  					return err
   752  				}
   753  				continue
   754  			}
   755  		}
   756  		if err := rs.AppendBytes(nil, true); err != nil {
   757  			return err
   758  		}
   759  	}
   760  	return nil
   761  }
   762  
   763  func builtInRpad(parameters []*vector.Vector, result vector.FunctionResultWrapper, _ *process.Process, length int) error {
   764  	p1 := vector.GenerateFunctionStrParameter(parameters[0])
   765  	p2 := vector.GenerateFunctionFixedTypeParameter[int64](parameters[1])
   766  	p3 := vector.GenerateFunctionStrParameter(parameters[2])
   767  
   768  	rs := vector.MustFunctionResult[types.Varlena](result)
   769  	for i := uint64(0); i < uint64(length); i++ {
   770  		v1, null1 := p1.GetStrValue(i)
   771  		v2, null2 := p2.GetValue(i)
   772  		v3, null3 := p3.GetStrValue(i)
   773  		if !(null1 || null2 || null3) {
   774  			rval, shouldNull := doRpad(string(v1), v2, string(v3))
   775  			if !shouldNull {
   776  				if err := rs.AppendBytes([]byte(rval), false); err != nil {
   777  					return err
   778  				}
   779  				continue
   780  			}
   781  		}
   782  		if err := rs.AppendBytes(nil, true); err != nil {
   783  			return err
   784  		}
   785  	}
   786  	return nil
   787  }
   788  
   789  func builtInUUID(_ []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   790  	rs := vector.MustFunctionResult[types.Uuid](result)
   791  	for i := uint64(0); i < uint64(length); i++ {
   792  		val, err := uuid.NewV7()
   793  		if err != nil {
   794  			return moerr.NewInternalError(proc.Ctx, "newuuid failed")
   795  		}
   796  		if err = rs.Append(types.Uuid(val), false); err != nil {
   797  			return err
   798  		}
   799  	}
   800  	return nil
   801  }
   802  
   803  func builtInUnixTimestamp(parameters []*vector.Vector, result vector.FunctionResultWrapper, _ *process.Process, length int) error {
   804  	rs := vector.MustFunctionResult[int64](result)
   805  	if len(parameters) == 0 {
   806  		val := types.CurrentTimestamp().Unix()
   807  		for i := uint64(0); i < uint64(length); i++ {
   808  			if err := rs.Append(val, false); err != nil {
   809  				return nil
   810  			}
   811  		}
   812  		return nil
   813  	}
   814  
   815  	p1 := vector.GenerateFunctionFixedTypeParameter[types.Timestamp](parameters[0])
   816  	for i := uint64(0); i < uint64(length); i++ {
   817  		v1, null1 := p1.GetValue(i)
   818  		val := v1.Unix()
   819  		if val < 0 || null1 {
   820  			// XXX v1 < 0 need to raise error here.
   821  			if err := rs.Append(0, true); err != nil {
   822  				return err
   823  			}
   824  		} else {
   825  			if err := rs.Append(val, false); err != nil {
   826  				return err
   827  			}
   828  		}
   829  	}
   830  	return nil
   831  }
   832  
   833  func mustTimestamp(loc *time.Location, s string) types.Timestamp {
   834  	ts, err := types.ParseTimestamp(loc, s, 6)
   835  	if err != nil {
   836  		ts = 0
   837  	}
   838  	return ts
   839  }
   840  
   841  func builtInUnixTimestampVarcharToInt64(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   842  	p1 := vector.GenerateFunctionStrParameter(parameters[0])
   843  	rs := vector.MustFunctionResult[int64](result)
   844  
   845  	for i := uint64(0); i < uint64(length); i++ {
   846  		v1, null1 := p1.GetStrValue(i)
   847  		if null1 {
   848  			if err := rs.Append(0, true); err != nil {
   849  				return err
   850  			}
   851  		} else {
   852  			val := mustTimestamp(proc.SessionInfo.TimeZone, string(v1)).Unix()
   853  			if val < 0 {
   854  				if err := rs.Append(0, true); err != nil {
   855  					return err
   856  				}
   857  				continue
   858  			}
   859  			if err := rs.Append(val, false); err != nil {
   860  				return err
   861  			}
   862  		}
   863  	}
   864  	return nil
   865  }
   866  
   867  var _ = builtInUnixTimestampVarcharToFloat64
   868  
   869  func builtInUnixTimestampVarcharToFloat64(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   870  	p1 := vector.GenerateFunctionStrParameter(parameters[0])
   871  	rs := vector.MustFunctionResult[float64](result)
   872  
   873  	for i := uint64(0); i < uint64(length); i++ {
   874  		v1, null1 := p1.GetStrValue(i)
   875  		if null1 {
   876  			if err := rs.Append(0, true); err != nil {
   877  				return err
   878  			}
   879  		} else {
   880  			val := mustTimestamp(proc.SessionInfo.TimeZone, string(v1))
   881  			if err := rs.Append(val.UnixToFloat(), false); err != nil {
   882  				return err
   883  			}
   884  		}
   885  	}
   886  	return nil
   887  }
   888  
   889  func builtInUnixTimestampVarcharToDecimal128(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   890  	p1 := vector.GenerateFunctionStrParameter(parameters[0])
   891  	rs := vector.MustFunctionResult[types.Decimal128](result)
   892  
   893  	var d types.Decimal128
   894  	for i := uint64(0); i < uint64(length); i++ {
   895  		v1, null1 := p1.GetStrValue(i)
   896  		if null1 {
   897  			if err := rs.Append(d, true); err != nil {
   898  				return err
   899  			}
   900  		} else {
   901  			val, err := mustTimestamp(proc.SessionInfo.TimeZone, string(v1)).UnixToDecimal128()
   902  			if err != nil {
   903  				return err
   904  			}
   905  			if val.Compare(types.Decimal128{B0_63: 0, B64_127: 0}) <= 0 {
   906  				if err := rs.Append(d, true); err != nil {
   907  					return err
   908  				}
   909  			}
   910  			if err = rs.Append(val, false); err != nil {
   911  				return err
   912  			}
   913  		}
   914  	}
   915  	return nil
   916  }
   917  
   918  // XXX I just copy this function.
   919  func builtInHash(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   920  	fillStringGroupStr := func(keys [][]byte, vec *vector.Vector, n int, start int) {
   921  		area := vec.GetArea()
   922  		vs := vector.MustFixedCol[types.Varlena](vec)
   923  		if !vec.GetNulls().Any() {
   924  			for i := 0; i < n; i++ {
   925  				keys[i] = append(keys[i], byte(0))
   926  				keys[i] = append(keys[i], vs[i+start].GetByteSlice(area)...)
   927  			}
   928  		} else {
   929  			nsp := vec.GetNulls()
   930  			for i := 0; i < n; i++ {
   931  				hasNull := nsp.Contains(uint64(i + start))
   932  				if hasNull {
   933  					keys[i] = append(keys[i], byte(1))
   934  				} else {
   935  					keys[i] = append(keys[i], byte(0))
   936  					keys[i] = append(keys[i], vs[i+start].GetByteSlice(area)...)
   937  				}
   938  			}
   939  		}
   940  	}
   941  
   942  	fillGroupStr := func(keys [][]byte, vec *vector.Vector, n int, sz int, start int) {
   943  		data := unsafe.Slice(vector.GetPtrAt[byte](vec, 0), (n+start)*sz)
   944  		if !vec.GetNulls().Any() {
   945  			for i := 0; i < n; i++ {
   946  				keys[i] = append(keys[i], byte(0))
   947  				keys[i] = append(keys[i], data[(i+start)*sz:(i+start+1)*sz]...)
   948  			}
   949  		} else {
   950  			nsp := vec.GetNulls()
   951  			for i := 0; i < n; i++ {
   952  				isNull := nsp.Contains(uint64(i + start))
   953  				if isNull {
   954  					keys[i] = append(keys[i], byte(1))
   955  				} else {
   956  					keys[i] = append(keys[i], byte(0))
   957  					keys[i] = append(keys[i], data[(i+start)*sz:(i+start+1)*sz]...)
   958  				}
   959  			}
   960  		}
   961  	}
   962  
   963  	encodeHashKeys := func(keys [][]byte, vecs []*vector.Vector, start, count int) {
   964  		for _, vec := range vecs {
   965  			if vec.GetType().IsFixedLen() {
   966  				fillGroupStr(keys, vec, count, vec.GetType().TypeSize(), start)
   967  			} else {
   968  				fillStringGroupStr(keys, vec, count, start)
   969  			}
   970  		}
   971  		for i := 0; i < count; i++ {
   972  			if l := len(keys[i]); l < 16 {
   973  				keys[i] = append(keys[i], hashtable.StrKeyPadding[l:]...)
   974  			}
   975  		}
   976  	}
   977  
   978  	rs := vector.MustFunctionResult[int64](result)
   979  
   980  	keys := make([][]byte, hashmap.UnitLimit)
   981  	states := make([][3]uint64, hashmap.UnitLimit)
   982  	for i := 0; i < length; i += hashmap.UnitLimit {
   983  		n := length - i
   984  		if n > hashmap.UnitLimit {
   985  			n = hashmap.UnitLimit
   986  		}
   987  		for j := 0; j < n; j++ {
   988  			keys[j] = keys[j][:0]
   989  		}
   990  		encodeHashKeys(keys, parameters, i, n)
   991  
   992  		hashtable.BytesBatchGenHashStates(&keys[0], &states[0], n)
   993  		for j := 0; j < n; j++ {
   994  			rs.AppendMustValue(int64(states[j][0]))
   995  		}
   996  	}
   997  	return nil
   998  }
   999  
  1000  // BuiltInSerial have a similar function named SerialWithCompacted in the index_util
  1001  // Serial func is used by users, the function make true when input vec have ten
  1002  // rows, the output vec is ten rows, when the vectors have null value, the output
  1003  // vec will set the row null
  1004  // for example:
  1005  // input vec is [[1, 1, 1], [2, 2, null], [3, 3, 3]]
  1006  // result vec is [serial(1, 2, 3), serial(1, 2, 3), null]
  1007  func (op *opSerial) BuiltInSerial(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  1008  	rs := vector.MustFunctionResult[types.Varlena](result)
  1009  	op.tryExpand(length, proc.Mp())
  1010  
  1011  	bitMap := new(nulls.Nulls)
  1012  
  1013  	for _, v := range parameters {
  1014  		if v.IsConstNull() {
  1015  			nulls.AddRange(rs.GetResultVector().GetNulls(), 0, uint64(length))
  1016  			return nil
  1017  		}
  1018  		SerialHelper(v, bitMap, op.ps, false)
  1019  	}
  1020  
  1021  	//NOTE: make sure to use uint64(length) instead of len(op.ps[i])
  1022  	// as length of packer array could be larger than length of input vectors
  1023  	for i := uint64(0); i < uint64(length); i++ {
  1024  		if bitMap.Contains(i) {
  1025  			if err := rs.AppendBytes(nil, true); err != nil {
  1026  				return err
  1027  			}
  1028  		} else {
  1029  			if err := rs.AppendBytes(op.ps[i].GetBuf(), false); err != nil {
  1030  				return err
  1031  			}
  1032  		}
  1033  	}
  1034  	return nil
  1035  }
  1036  
  1037  func (op *opSerial) BuiltInSerialFull(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  1038  
  1039  	rs := vector.MustFunctionResult[types.Varlena](result)
  1040  	op.tryExpand(length, proc.Mp())
  1041  
  1042  	for _, v := range parameters {
  1043  		if v.IsConstNull() {
  1044  			for i := 0; i < v.Length(); i++ {
  1045  				op.ps[i].EncodeNull()
  1046  			}
  1047  			continue
  1048  		}
  1049  
  1050  		SerialHelper(v, nil, op.ps, true)
  1051  	}
  1052  
  1053  	//NOTE: make sure to use uint64(length) instead of len(op.ps[i])
  1054  	// as length of packer array could be larger than length of input vectors
  1055  	for i := uint64(0); i < uint64(length); i++ {
  1056  		if err := rs.AppendBytes(op.ps[i].GetBuf(), false); err != nil {
  1057  			return err
  1058  		}
  1059  	}
  1060  	return nil
  1061  }
  1062  
  1063  // SerialHelper is unified function used in builtInSerial and BuiltInSerialFull
  1064  // To use it inside builtInSerial, pass the bitMap pointer and set isFull false
  1065  // To use it inside BuiltInSerialFull, pass the bitMap as nil and set isFull to true
  1066  func SerialHelper(v *vector.Vector, bitMap *nulls.Nulls, ps []*types.Packer, isFull bool) {
  1067  
  1068  	if !isFull && bitMap == nil {
  1069  		// if you are using it inside the builtInSerial then, you should pass bitMap
  1070  		panic("for builtInSerial(), bitmap should not be nil")
  1071  	}
  1072  	hasNull := v.HasNull()
  1073  	switch v.GetType().Oid {
  1074  	case types.T_bool:
  1075  		s := vector.ExpandFixedCol[bool](v)
  1076  		if hasNull {
  1077  			for i, b := range s {
  1078  				if v.IsNull(uint64(i)) {
  1079  					if isFull {
  1080  						ps[i].EncodeNull()
  1081  					} else {
  1082  						nulls.Add(bitMap, uint64(i))
  1083  					}
  1084  				} else {
  1085  					ps[i].EncodeBool(b)
  1086  				}
  1087  			}
  1088  		} else {
  1089  			for i, b := range s {
  1090  				ps[i].EncodeBool(b)
  1091  			}
  1092  		}
  1093  	case types.T_bit:
  1094  		s := vector.ExpandFixedCol[uint64](v)
  1095  		for i, b := range s {
  1096  			if v.IsNull(uint64(i)) {
  1097  				if isFull {
  1098  					ps[i].EncodeNull()
  1099  				} else {
  1100  					nulls.Add(bitMap, uint64(i))
  1101  				}
  1102  			} else {
  1103  				ps[i].EncodeUint64(b)
  1104  			}
  1105  		}
  1106  	case types.T_int8:
  1107  		s := vector.ExpandFixedCol[int8](v)
  1108  		if hasNull {
  1109  			for i, b := range s {
  1110  				if v.IsNull(uint64(i)) {
  1111  					if isFull {
  1112  						ps[i].EncodeNull()
  1113  					} else {
  1114  						nulls.Add(bitMap, uint64(i))
  1115  					}
  1116  				} else {
  1117  					ps[i].EncodeInt8(b)
  1118  				}
  1119  			}
  1120  		} else {
  1121  			for i, b := range s {
  1122  				ps[i].EncodeInt8(b)
  1123  			}
  1124  		}
  1125  	case types.T_int16:
  1126  		s := vector.ExpandFixedCol[int16](v)
  1127  		if hasNull {
  1128  			for i, b := range s {
  1129  				if v.IsNull(uint64(i)) {
  1130  					if isFull {
  1131  						ps[i].EncodeNull()
  1132  					} else {
  1133  						nulls.Add(bitMap, uint64(i))
  1134  					}
  1135  				} else {
  1136  					ps[i].EncodeInt16(b)
  1137  				}
  1138  			}
  1139  		} else {
  1140  			for i, b := range s {
  1141  				ps[i].EncodeInt16(b)
  1142  			}
  1143  		}
  1144  	case types.T_int32:
  1145  		s := vector.ExpandFixedCol[int32](v)
  1146  		if hasNull {
  1147  			for i, b := range s {
  1148  				if v.IsNull(uint64(i)) {
  1149  					if isFull {
  1150  						ps[i].EncodeNull()
  1151  					} else {
  1152  						nulls.Add(bitMap, uint64(i))
  1153  					}
  1154  				} else {
  1155  					ps[i].EncodeInt32(b)
  1156  				}
  1157  			}
  1158  		} else {
  1159  			for i, b := range s {
  1160  				ps[i].EncodeInt32(b)
  1161  			}
  1162  		}
  1163  	case types.T_int64:
  1164  		s := vector.ExpandFixedCol[int64](v)
  1165  		if hasNull {
  1166  			for i, b := range s {
  1167  				if v.IsNull(uint64(i)) {
  1168  					if isFull {
  1169  						ps[i].EncodeNull()
  1170  					} else {
  1171  						nulls.Add(bitMap, uint64(i))
  1172  					}
  1173  				} else {
  1174  					ps[i].EncodeInt64(b)
  1175  				}
  1176  			}
  1177  		} else {
  1178  			for i, b := range s {
  1179  				ps[i].EncodeInt64(b)
  1180  			}
  1181  		}
  1182  	case types.T_uint8:
  1183  		s := vector.ExpandFixedCol[uint8](v)
  1184  		if hasNull {
  1185  			for i, b := range s {
  1186  				if v.IsNull(uint64(i)) {
  1187  					if isFull {
  1188  						ps[i].EncodeNull()
  1189  					} else {
  1190  						nulls.Add(bitMap, uint64(i))
  1191  					}
  1192  				} else {
  1193  					ps[i].EncodeUint8(b)
  1194  				}
  1195  			}
  1196  		} else {
  1197  			for i, b := range s {
  1198  				ps[i].EncodeUint8(b)
  1199  			}
  1200  		}
  1201  	case types.T_uint16:
  1202  		s := vector.ExpandFixedCol[uint16](v)
  1203  		if hasNull {
  1204  			for i, b := range s {
  1205  				if v.IsNull(uint64(i)) {
  1206  					if isFull {
  1207  						ps[i].EncodeNull()
  1208  					} else {
  1209  						nulls.Add(bitMap, uint64(i))
  1210  					}
  1211  				} else {
  1212  					ps[i].EncodeUint16(b)
  1213  				}
  1214  			}
  1215  		} else {
  1216  			for i, b := range s {
  1217  				ps[i].EncodeUint16(b)
  1218  			}
  1219  		}
  1220  	case types.T_uint32:
  1221  		s := vector.ExpandFixedCol[uint32](v)
  1222  		if hasNull {
  1223  			for i, b := range s {
  1224  				if v.IsNull(uint64(i)) {
  1225  					if isFull {
  1226  						ps[i].EncodeNull()
  1227  					} else {
  1228  						nulls.Add(bitMap, uint64(i))
  1229  					}
  1230  				} else {
  1231  					ps[i].EncodeUint32(b)
  1232  				}
  1233  			}
  1234  		} else {
  1235  			for i, b := range s {
  1236  				ps[i].EncodeUint32(b)
  1237  			}
  1238  		}
  1239  	case types.T_uint64:
  1240  		s := vector.ExpandFixedCol[uint64](v)
  1241  		if hasNull {
  1242  			for i, b := range s {
  1243  				if v.IsNull(uint64(i)) {
  1244  					if isFull {
  1245  						ps[i].EncodeNull()
  1246  					} else {
  1247  						nulls.Add(bitMap, uint64(i))
  1248  					}
  1249  				} else {
  1250  					ps[i].EncodeUint64(b)
  1251  				}
  1252  			}
  1253  		} else {
  1254  			for i, b := range s {
  1255  				ps[i].EncodeUint64(b)
  1256  			}
  1257  		}
  1258  	case types.T_float32:
  1259  		s := vector.ExpandFixedCol[float32](v)
  1260  		if hasNull {
  1261  			for i, b := range s {
  1262  				if v.IsNull(uint64(i)) {
  1263  					if isFull {
  1264  						ps[i].EncodeNull()
  1265  					} else {
  1266  						nulls.Add(bitMap, uint64(i))
  1267  					}
  1268  				} else {
  1269  					ps[i].EncodeFloat32(b)
  1270  				}
  1271  			}
  1272  		} else {
  1273  			for i, b := range s {
  1274  				ps[i].EncodeFloat32(b)
  1275  			}
  1276  		}
  1277  	case types.T_float64:
  1278  		s := vector.ExpandFixedCol[float64](v)
  1279  		if hasNull {
  1280  			for i, b := range s {
  1281  				if v.IsNull(uint64(i)) {
  1282  					if isFull {
  1283  						ps[i].EncodeNull()
  1284  					} else {
  1285  						nulls.Add(bitMap, uint64(i))
  1286  					}
  1287  				} else {
  1288  					ps[i].EncodeFloat64(b)
  1289  				}
  1290  			}
  1291  		} else {
  1292  			for i, b := range s {
  1293  				ps[i].EncodeFloat64(b)
  1294  			}
  1295  		}
  1296  	case types.T_date:
  1297  		s := vector.ExpandFixedCol[types.Date](v)
  1298  		if hasNull {
  1299  			for i, b := range s {
  1300  				if v.IsNull(uint64(i)) {
  1301  					if isFull {
  1302  						ps[i].EncodeNull()
  1303  					} else {
  1304  						nulls.Add(bitMap, uint64(i))
  1305  					}
  1306  				} else {
  1307  					ps[i].EncodeDate(b)
  1308  				}
  1309  			}
  1310  		} else {
  1311  			for i, b := range s {
  1312  				ps[i].EncodeDate(b)
  1313  			}
  1314  		}
  1315  	case types.T_time:
  1316  		s := vector.ExpandFixedCol[types.Time](v)
  1317  		if hasNull {
  1318  			for i, b := range s {
  1319  				if v.IsNull(uint64(i)) {
  1320  					if isFull {
  1321  						ps[i].EncodeNull()
  1322  					} else {
  1323  						nulls.Add(bitMap, uint64(i))
  1324  					}
  1325  				} else {
  1326  					ps[i].EncodeTime(b)
  1327  				}
  1328  			}
  1329  		} else {
  1330  			for i, b := range s {
  1331  				ps[i].EncodeTime(b)
  1332  			}
  1333  		}
  1334  	case types.T_datetime:
  1335  		s := vector.ExpandFixedCol[types.Datetime](v)
  1336  		if hasNull {
  1337  			for i, b := range s {
  1338  				if v.IsNull(uint64(i)) {
  1339  					if isFull {
  1340  						ps[i].EncodeNull()
  1341  					} else {
  1342  						nulls.Add(bitMap, uint64(i))
  1343  					}
  1344  				} else {
  1345  					ps[i].EncodeDatetime(b)
  1346  				}
  1347  			}
  1348  		} else {
  1349  			for i, b := range s {
  1350  				ps[i].EncodeDatetime(b)
  1351  			}
  1352  		}
  1353  	case types.T_timestamp:
  1354  		s := vector.ExpandFixedCol[types.Timestamp](v)
  1355  		if hasNull {
  1356  			for i, b := range s {
  1357  				if v.IsNull(uint64(i)) {
  1358  					if isFull {
  1359  						ps[i].EncodeNull()
  1360  					} else {
  1361  						nulls.Add(bitMap, uint64(i))
  1362  					}
  1363  				} else {
  1364  					ps[i].EncodeTimestamp(b)
  1365  				}
  1366  			}
  1367  		} else {
  1368  			for i, b := range s {
  1369  				ps[i].EncodeTimestamp(b)
  1370  			}
  1371  		}
  1372  	case types.T_enum:
  1373  		s := vector.MustFixedCol[types.Enum](v)
  1374  		if hasNull {
  1375  			for i, b := range s {
  1376  				if nulls.Contains(v.GetNulls(), uint64(i)) {
  1377  					if isFull {
  1378  						ps[i].EncodeNull()
  1379  					} else {
  1380  						nulls.Add(bitMap, uint64(i))
  1381  					}
  1382  				} else {
  1383  					ps[i].EncodeEnum(b)
  1384  				}
  1385  			}
  1386  		} else {
  1387  			for i, b := range s {
  1388  				ps[i].EncodeEnum(b)
  1389  			}
  1390  		}
  1391  	case types.T_decimal64:
  1392  		s := vector.ExpandFixedCol[types.Decimal64](v)
  1393  		if hasNull {
  1394  			for i, b := range s {
  1395  				if v.IsNull(uint64(i)) {
  1396  					if isFull {
  1397  						ps[i].EncodeNull()
  1398  					} else {
  1399  						nulls.Add(bitMap, uint64(i))
  1400  					}
  1401  				} else {
  1402  					ps[i].EncodeDecimal64(b)
  1403  				}
  1404  			}
  1405  		} else {
  1406  			for i, b := range s {
  1407  				ps[i].EncodeDecimal64(b)
  1408  			}
  1409  		}
  1410  	case types.T_decimal128:
  1411  		s := vector.ExpandFixedCol[types.Decimal128](v)
  1412  		if hasNull {
  1413  			for i, b := range s {
  1414  				if v.IsNull(uint64(i)) {
  1415  					if isFull {
  1416  						ps[i].EncodeNull()
  1417  					} else {
  1418  						nulls.Add(bitMap, uint64(i))
  1419  					}
  1420  				} else {
  1421  					ps[i].EncodeDecimal128(b)
  1422  				}
  1423  			}
  1424  		} else {
  1425  			for i, b := range s {
  1426  				ps[i].EncodeDecimal128(b)
  1427  			}
  1428  		}
  1429  	case types.T_json, types.T_char, types.T_varchar, types.T_binary, types.T_varbinary, types.T_blob, types.T_text,
  1430  		types.T_array_float32, types.T_array_float64:
  1431  		vs := vector.ExpandStrCol(v)
  1432  		if hasNull {
  1433  			for i := range vs {
  1434  				if v.IsNull(uint64(i)) {
  1435  					if isFull {
  1436  						ps[i].EncodeNull()
  1437  					} else {
  1438  						nulls.Add(bitMap, uint64(i))
  1439  					}
  1440  				} else {
  1441  					ps[i].EncodeStringType([]byte(vs[i]))
  1442  				}
  1443  			}
  1444  		} else {
  1445  			for i := range vs {
  1446  				ps[i].EncodeStringType([]byte(vs[i]))
  1447  			}
  1448  		}
  1449  	}
  1450  }
  1451  
  1452  // builtInSerialExtract is used to extract a tupleElement from the serial vector.
  1453  // For example:
  1454  //
  1455  //	serial_col = serial(floatCol, varchar3Col)
  1456  //	serial_extract(serial_col, 1, varchar(3)) will return 2
  1457  func builtInSerialExtract(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  1458  	p1 := vector.GenerateFunctionStrParameter(parameters[0])
  1459  	p2 := vector.GenerateFunctionFixedTypeParameter[int64](parameters[1])
  1460  	resTyp := parameters[2].GetType()
  1461  
  1462  	switch resTyp.Oid {
  1463  	case types.T_bit:
  1464  		rs := vector.MustFunctionResult[uint64](result)
  1465  		return serialExtractExceptStrings(p1, p2, rs, proc, length)
  1466  	case types.T_int8:
  1467  		rs := vector.MustFunctionResult[int8](result)
  1468  		return serialExtractExceptStrings(p1, p2, rs, proc, length)
  1469  	case types.T_int16:
  1470  		rs := vector.MustFunctionResult[int16](result)
  1471  		return serialExtractExceptStrings(p1, p2, rs, proc, length)
  1472  	case types.T_int32:
  1473  		rs := vector.MustFunctionResult[int32](result)
  1474  		return serialExtractExceptStrings(p1, p2, rs, proc, length)
  1475  	case types.T_int64:
  1476  		rs := vector.MustFunctionResult[int64](result)
  1477  		return serialExtractExceptStrings(p1, p2, rs, proc, length)
  1478  	case types.T_uint8:
  1479  		rs := vector.MustFunctionResult[uint8](result)
  1480  		return serialExtractExceptStrings(p1, p2, rs, proc, length)
  1481  	case types.T_uint16:
  1482  		rs := vector.MustFunctionResult[uint16](result)
  1483  		return serialExtractExceptStrings(p1, p2, rs, proc, length)
  1484  	case types.T_uint32:
  1485  		rs := vector.MustFunctionResult[uint32](result)
  1486  		return serialExtractExceptStrings(p1, p2, rs, proc, length)
  1487  	case types.T_uint64:
  1488  		rs := vector.MustFunctionResult[uint64](result)
  1489  		return serialExtractExceptStrings(p1, p2, rs, proc, length)
  1490  	case types.T_float32:
  1491  		rs := vector.MustFunctionResult[float32](result)
  1492  		return serialExtractExceptStrings(p1, p2, rs, proc, length)
  1493  	case types.T_float64:
  1494  		rs := vector.MustFunctionResult[float64](result)
  1495  		return serialExtractExceptStrings(p1, p2, rs, proc, length)
  1496  	case types.T_decimal64:
  1497  		rs := vector.MustFunctionResult[types.Decimal64](result)
  1498  		return serialExtractExceptStrings(p1, p2, rs, proc, length)
  1499  	case types.T_decimal128:
  1500  		rs := vector.MustFunctionResult[types.Decimal128](result)
  1501  		return serialExtractExceptStrings(p1, p2, rs, proc, length)
  1502  	case types.T_bool:
  1503  		rs := vector.MustFunctionResult[bool](result)
  1504  		return serialExtractExceptStrings(p1, p2, rs, proc, length)
  1505  	case types.T_date:
  1506  		rs := vector.MustFunctionResult[types.Date](result)
  1507  		return serialExtractExceptStrings(p1, p2, rs, proc, length)
  1508  	case types.T_datetime:
  1509  		rs := vector.MustFunctionResult[types.Datetime](result)
  1510  		return serialExtractExceptStrings(p1, p2, rs, proc, length)
  1511  	case types.T_time:
  1512  		rs := vector.MustFunctionResult[types.Time](result)
  1513  		return serialExtractExceptStrings(p1, p2, rs, proc, length)
  1514  	case types.T_timestamp:
  1515  		rs := vector.MustFunctionResult[types.Timestamp](result)
  1516  		return serialExtractExceptStrings(p1, p2, rs, proc, length)
  1517  
  1518  	case types.T_json, types.T_char, types.T_varchar, types.T_text,
  1519  		types.T_binary, types.T_varbinary, types.T_blob, types.T_array_float32, types.T_array_float64:
  1520  		rs := vector.MustFunctionResult[types.Varlena](result)
  1521  		return serialExtractForString(p1, p2, rs, proc, length)
  1522  	}
  1523  	return moerr.NewInternalError(proc.Ctx, "not supported type %s", resTyp.String())
  1524  
  1525  }
  1526  
  1527  func serialExtractExceptStrings[T types.Number | bool | types.Date | types.Datetime | types.Time | types.Timestamp](
  1528  	p1 vector.FunctionParameterWrapper[types.Varlena],
  1529  	p2 vector.FunctionParameterWrapper[int64],
  1530  	result *vector.FunctionResult[T], proc *process.Process, length int) error {
  1531  
  1532  	for i := uint64(0); i < uint64(length); i++ {
  1533  		v1, null := p1.GetStrValue(i)
  1534  		v2, null2 := p2.GetValue(i)
  1535  		if null || null2 {
  1536  			var nilVal T
  1537  			if err := result.Append(nilVal, true); err != nil {
  1538  				return err
  1539  			}
  1540  			continue
  1541  		}
  1542  
  1543  		tuple, schema, err := types.UnpackWithSchema(v1)
  1544  		if err != nil {
  1545  			return err
  1546  		}
  1547  
  1548  		if int(v2) >= len(tuple) {
  1549  			return moerr.NewInternalError(proc.Ctx, "index out of range")
  1550  		}
  1551  
  1552  		if schema[v2] == types.T_any {
  1553  			var nilVal T
  1554  			if err = result.Append(nilVal, true); err != nil {
  1555  				return err
  1556  			}
  1557  			continue
  1558  		}
  1559  
  1560  		if value, ok := tuple[v2].(T); ok {
  1561  			if err := result.Append(value, false); err != nil {
  1562  				return err
  1563  			}
  1564  		} else {
  1565  			return moerr.NewInternalError(proc.Ctx, "provided type did not match the expected type")
  1566  		}
  1567  	}
  1568  
  1569  	return nil
  1570  }
  1571  
  1572  func serialExtractForString(p1 vector.FunctionParameterWrapper[types.Varlena],
  1573  	p2 vector.FunctionParameterWrapper[int64],
  1574  	result *vector.FunctionResult[types.Varlena], proc *process.Process, length int) error {
  1575  	for i := uint64(0); i < uint64(length); i++ {
  1576  		v1, null := p1.GetStrValue(i)
  1577  		v2, null2 := p2.GetValue(i)
  1578  		if null || null2 {
  1579  			if err := result.AppendBytes(nil, true); err != nil {
  1580  				return err
  1581  			}
  1582  			continue
  1583  		}
  1584  
  1585  		tuple, schema, err := types.UnpackWithSchema(v1)
  1586  		if err != nil {
  1587  			return err
  1588  		}
  1589  
  1590  		if int(v2) >= len(tuple) {
  1591  			return moerr.NewInternalError(proc.Ctx, "index out of range")
  1592  		}
  1593  
  1594  		if schema[v2] == types.T_any {
  1595  			if err = result.AppendBytes(nil, true); err != nil {
  1596  				return err
  1597  			}
  1598  			continue
  1599  		}
  1600  
  1601  		if value, ok := tuple[v2].([]byte); ok {
  1602  			if err := result.AppendBytes(value, false); err != nil {
  1603  				return err
  1604  			}
  1605  		} else {
  1606  			return moerr.NewInternalError(proc.Ctx, "provided type did not match the expected type")
  1607  		}
  1608  	}
  1609  
  1610  	return nil
  1611  }
  1612  
  1613  // 24-hour seconds
  1614  const SecondsIn24Hours = 86400
  1615  
  1616  // The number of days in the year 0000 AD
  1617  const ADZeroDays = 366
  1618  
  1619  const (
  1620  	intervalUnitYEAR      = "YEAR"
  1621  	intervalUnitQUARTER   = "QUARTER"
  1622  	intervalUnitMONTH     = "MONTH"
  1623  	intervalUnitWEEK      = "WEEK"
  1624  	intervalUnitDAY       = "DAY"
  1625  	intervalUnitHOUR      = "HOUR"
  1626  	intervalUnitMINUTE    = "MINUTE"
  1627  	intervalUnitSECOND    = "SECOND"
  1628  	intervalUnitMICSECOND = "MICROSECOND"
  1629  )
  1630  
  1631  // ToDays: InMySQL: Given a date data, returns a day number (the number of days since year 0). Returns NULL if date is NULL.
  1632  // note:  but Matrxone think the date of the first year of the year is 0001-01-01, this function selects compatibility with MySQL
  1633  // reference linking: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_to-days
  1634  func builtInToDays(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  1635  	dateParams := vector.GenerateFunctionFixedTypeParameter[types.Datetime](parameters[0])
  1636  	rs := vector.MustFunctionResult[int64](result)
  1637  	for i := uint64(0); i < uint64(length); i++ {
  1638  		datetimeValue, isNull := dateParams.GetValue(i)
  1639  		if isNull {
  1640  			if err := rs.Append(0, true); err != nil {
  1641  				return err
  1642  			}
  1643  			continue
  1644  		}
  1645  		rs.Append(DateTimeDiff(intervalUnitDAY, types.ZeroDatetime, datetimeValue)+ADZeroDays, false)
  1646  	}
  1647  	return nil
  1648  }
  1649  
  1650  // DateTimeDiff returns t2 - t1 where t1 and t2 are datetime expressions.
  1651  // The unit for the result is given by the unit argument.
  1652  // The values for interval unit are "QUARTER","YEAR","MONTH", "DAY", "HOUR", "SECOND", "MICROSECOND"
  1653  func DateTimeDiff(intervalUnit string, t1 types.Datetime, t2 types.Datetime) int64 {
  1654  	seconds, microseconds, negative := calcDateTimeInterval(t2, t1, 1)
  1655  	months := uint(0)
  1656  	if intervalUnit == intervalUnitYEAR || intervalUnit == intervalUnitQUARTER ||
  1657  		intervalUnit == intervalUnitMONTH {
  1658  		var (
  1659  			yearBegin, yearEnd, monthBegin, monthEnd, dayBegin, dayEnd uint
  1660  			secondBegin, secondEnd, microsecondBegin, microsecondEnd   uint
  1661  		)
  1662  
  1663  		if negative {
  1664  			yearBegin = uint(t2.Year())
  1665  			yearEnd = uint(t1.Year())
  1666  			monthBegin = uint(t2.Month())
  1667  			monthEnd = uint(t1.Month())
  1668  			dayBegin = uint(t2.Day())
  1669  			dayEnd = uint(t1.Day())
  1670  			secondBegin = uint(int(t2.Hour())*3600 + int(t2.Minute())*60 + int(t2.Sec()))
  1671  			secondEnd = uint(int(t1.Hour())*3600 + int(t1.Minute())*60 + int(t1.Sec()))
  1672  			microsecondBegin = uint(t2.MicroSec())
  1673  			microsecondEnd = uint(t1.MicroSec())
  1674  		} else {
  1675  			yearBegin = uint(t1.Year())
  1676  			yearEnd = uint(t2.Year())
  1677  			monthBegin = uint(t1.Month())
  1678  			monthEnd = uint(t2.Month())
  1679  			dayBegin = uint(t1.Day())
  1680  			dayEnd = uint(t2.Day())
  1681  			secondBegin = uint(int(t1.Hour())*3600 + int(t1.Minute())*60 + int(t1.Sec()))
  1682  			secondEnd = uint(int(t2.Hour())*3600 + int(t2.Minute())*60 + int(t2.Sec()))
  1683  			microsecondBegin = uint(t1.MicroSec())
  1684  			microsecondEnd = uint(t2.MicroSec())
  1685  		}
  1686  
  1687  		// calculate years
  1688  		years := yearEnd - yearBegin
  1689  		if monthEnd < monthBegin ||
  1690  			(monthEnd == monthBegin && dayEnd < dayBegin) {
  1691  			years--
  1692  		}
  1693  
  1694  		// calculate months
  1695  		months = 12 * years
  1696  		if monthEnd < monthBegin ||
  1697  			(monthEnd == monthBegin && dayEnd < dayBegin) {
  1698  			months += 12 - (monthBegin - monthEnd)
  1699  		} else {
  1700  			months += monthEnd - monthBegin
  1701  		}
  1702  
  1703  		if dayEnd < dayBegin {
  1704  			months--
  1705  		} else if (dayEnd == dayBegin) &&
  1706  			((secondEnd < secondBegin) ||
  1707  				(secondEnd == secondBegin && microsecondEnd < microsecondBegin)) {
  1708  			months--
  1709  		}
  1710  	}
  1711  
  1712  	// negative
  1713  	negV := int64(1)
  1714  	if negative {
  1715  		negV = -1
  1716  	}
  1717  	switch intervalUnit {
  1718  	case intervalUnitYEAR:
  1719  		return int64(months) / 12 * negV
  1720  	case intervalUnitQUARTER:
  1721  		return int64(months) / 3 * negV
  1722  	case intervalUnitMONTH:
  1723  		return int64(months) * negV
  1724  	case intervalUnitWEEK:
  1725  		return int64(seconds) / SecondsIn24Hours / 7 * negV
  1726  	case intervalUnitDAY:
  1727  		return int64(seconds) / SecondsIn24Hours * negV
  1728  	case intervalUnitHOUR:
  1729  		return int64(seconds) / 3600 * negV
  1730  	case intervalUnitMINUTE:
  1731  		return int64(seconds) / 60 * negV
  1732  	case intervalUnitSECOND:
  1733  		return int64(seconds) * negV
  1734  	case intervalUnitMICSECOND:
  1735  		return int64(seconds*1000000+microseconds) * negV
  1736  	}
  1737  	return 0
  1738  }
  1739  
  1740  // calcDateTimeInterval: calculates time interval between two datetime values
  1741  func calcDateTimeInterval(t1 types.Datetime, t2 types.Datetime, sign int) (seconds, microseconds int, neg bool) {
  1742  	// Obtain the year, month, day, hour, minute, and second of the t2 datetime
  1743  	year := int(t2.Year())
  1744  	month := int(t2.Month())
  1745  	day := int(t2.Day())
  1746  	hour := int(t2.Hour())
  1747  	minute := int(t2.Minute())
  1748  	second := int(t2.Sec())
  1749  	microsecond := int(t2.MicroSec())
  1750  
  1751  	days1 := calcDaysSinceZero(int(t1.Year()), int(t1.Month()), int(t1.Day()))
  1752  	days2 := calcDaysSinceZero(year, month, day)
  1753  	days1 -= sign * days2
  1754  
  1755  	tmp := (int64(days1)*SecondsIn24Hours+
  1756  		int64(t1.Hour())*3600+int64(t1.Minute())*60+
  1757  		int64(t1.Sec())-
  1758  		int64(sign)*(int64(hour)*3600+
  1759  			int64(minute)*60+
  1760  			int64(second)))*1e6 +
  1761  		t1.MicroSec() - int64(sign)*int64(microsecond)
  1762  
  1763  	if tmp < 0 {
  1764  		tmp = -tmp
  1765  		neg = true
  1766  	}
  1767  	seconds = int(tmp / 1e6)
  1768  	microseconds = int(tmp % 1e6)
  1769  	return
  1770  }
  1771  
  1772  // calcDaynr calculates days since 0000-00-00.
  1773  func calcDaysSinceZero(year int, month int, day int) int {
  1774  	if year == 0 && month == 0 {
  1775  		return 0
  1776  	}
  1777  
  1778  	delsum := 365*year + 31*(month-1) + day
  1779  	if month <= 2 {
  1780  		year--
  1781  	} else {
  1782  		delsum -= (month*4 + 23) / 10
  1783  	}
  1784  	temp := ((year/100 + 1) * 3) / 4
  1785  	return delsum + year/4 - temp
  1786  }
  1787  
  1788  // Seconds in 0000 AD
  1789  const ADZeroSeconds = 31622400
  1790  
  1791  // ToSeconds: InMySQL: Given a date date, returns a day number (the number of days since year 0000). Returns NULL if date is NULL.
  1792  // note:  but Matrxone think the date of the first year of the year is 0001-01-01, this function selects compatibility with MySQL
  1793  // reference linking: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_to-seconds
  1794  func builtInToSeconds(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  1795  	dateParams := vector.GenerateFunctionFixedTypeParameter[types.Datetime](parameters[0])
  1796  	rs := vector.MustFunctionResult[int64](result)
  1797  	for i := uint64(0); i < uint64(length); i++ {
  1798  		datetimeValue, isNull := dateParams.GetValue(i)
  1799  		if isNull {
  1800  			if err := rs.Append(0, true); err != nil {
  1801  				return err
  1802  			}
  1803  			continue
  1804  		}
  1805  		rs.Append(DateTimeDiff(intervalUnitSECOND, types.ZeroDatetime, datetimeValue)+ADZeroSeconds, false)
  1806  	}
  1807  	return nil
  1808  }
  1809  
  1810  // CalcToSeconds: CalcToDays is used to return a day number (the number of days since year 0)
  1811  func CalcToSeconds(ctx context.Context, datetimes []types.Datetime, ns *nulls.Nulls) ([]int64, error) {
  1812  	res := make([]int64, len(datetimes))
  1813  	for idx, datetime := range datetimes {
  1814  		if nulls.Contains(ns, uint64(idx)) {
  1815  			continue
  1816  		}
  1817  		res[idx] = DateTimeDiff(intervalUnitSECOND, types.ZeroDatetime, datetime) + ADZeroSeconds
  1818  	}
  1819  	return res, nil
  1820  }
  1821  
  1822  func builtInSin(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  1823  	p1 := vector.GenerateFunctionFixedTypeParameter[float64](parameters[0])
  1824  	rs := vector.MustFunctionResult[float64](result)
  1825  	for i := uint64(0); i < uint64(length); i++ {
  1826  		v, null := p1.GetValue(i)
  1827  		if null {
  1828  			if err := rs.Append(0, true); err != nil {
  1829  				return err
  1830  			}
  1831  		} else {
  1832  			sinValue, err := momath.Sin(v)
  1833  			if err != nil {
  1834  				return err
  1835  			}
  1836  			if err = rs.Append(sinValue, false); err != nil {
  1837  				return err
  1838  			}
  1839  		}
  1840  	}
  1841  	return nil
  1842  }
  1843  
  1844  func builtInSinh(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  1845  	p1 := vector.GenerateFunctionFixedTypeParameter[float64](parameters[0])
  1846  	rs := vector.MustFunctionResult[float64](result)
  1847  	for i := uint64(0); i < uint64(length); i++ {
  1848  		v, null := p1.GetValue(i)
  1849  		if null {
  1850  			if err := rs.Append(0, true); err != nil {
  1851  				return err
  1852  			}
  1853  		} else {
  1854  			sinValue, err := momath.Sinh(v)
  1855  			if err != nil {
  1856  				return err
  1857  			}
  1858  			if err = rs.Append(sinValue, false); err != nil {
  1859  				return err
  1860  			}
  1861  		}
  1862  	}
  1863  	return nil
  1864  }
  1865  
  1866  func builtInCos(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  1867  	p1 := vector.GenerateFunctionFixedTypeParameter[float64](parameters[0])
  1868  	rs := vector.MustFunctionResult[float64](result)
  1869  	for i := uint64(0); i < uint64(length); i++ {
  1870  		v, null := p1.GetValue(i)
  1871  		if null {
  1872  			if err := rs.Append(0, true); err != nil {
  1873  				return err
  1874  			}
  1875  		} else {
  1876  			sinValue, err := momath.Cos(v)
  1877  			if err != nil {
  1878  				return err
  1879  			}
  1880  			if err = rs.Append(sinValue, false); err != nil {
  1881  				return err
  1882  			}
  1883  		}
  1884  	}
  1885  	return nil
  1886  }
  1887  
  1888  func builtInCot(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  1889  	p1 := vector.GenerateFunctionFixedTypeParameter[float64](parameters[0])
  1890  	rs := vector.MustFunctionResult[float64](result)
  1891  	for i := uint64(0); i < uint64(length); i++ {
  1892  		v, null := p1.GetValue(i)
  1893  		if null {
  1894  			if err := rs.Append(0, true); err != nil {
  1895  				return err
  1896  			}
  1897  		} else {
  1898  			sinValue, err := momath.Cot(v)
  1899  			if err != nil {
  1900  				return err
  1901  			}
  1902  			if err = rs.Append(sinValue, false); err != nil {
  1903  				return err
  1904  			}
  1905  		}
  1906  	}
  1907  	return nil
  1908  }
  1909  
  1910  func builtInTan(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  1911  	p1 := vector.GenerateFunctionFixedTypeParameter[float64](parameters[0])
  1912  	rs := vector.MustFunctionResult[float64](result)
  1913  	for i := uint64(0); i < uint64(length); i++ {
  1914  		v, null := p1.GetValue(i)
  1915  		if null {
  1916  			if err := rs.Append(0, true); err != nil {
  1917  				return err
  1918  			}
  1919  		} else {
  1920  			sinValue, err := momath.Tan(v)
  1921  			if err != nil {
  1922  				return err
  1923  			}
  1924  			if err = rs.Append(sinValue, false); err != nil {
  1925  				return err
  1926  			}
  1927  		}
  1928  	}
  1929  	return nil
  1930  }
  1931  
  1932  func builtInExp(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  1933  	p1 := vector.GenerateFunctionFixedTypeParameter[float64](parameters[0])
  1934  	rs := vector.MustFunctionResult[float64](result)
  1935  	for i := uint64(0); i < uint64(length); i++ {
  1936  		v, null := p1.GetValue(i)
  1937  		if null {
  1938  			if err := rs.Append(0, true); err != nil {
  1939  				return err
  1940  			}
  1941  		} else {
  1942  			sinValue, err := momath.Exp(v)
  1943  			if err != nil {
  1944  				return err
  1945  			}
  1946  			if err = rs.Append(sinValue, false); err != nil {
  1947  				return err
  1948  			}
  1949  		}
  1950  	}
  1951  	return nil
  1952  }
  1953  
  1954  func builtInSqrt(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  1955  	return opUnaryFixedToFixedWithErrorCheck[float64, float64](parameters, result, proc, length, func(v float64) (float64, error) {
  1956  		return momath.Sqrt(v)
  1957  	})
  1958  }
  1959  
  1960  func builtInSqrtArray[T types.RealNumbers](parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  1961  	return opUnaryBytesToBytesWithErrorCheck(parameters, result, proc, length, func(in []byte) (out []byte, err error) {
  1962  		_in := types.BytesToArray[T](in)
  1963  
  1964  		_out, err := moarray.Sqrt(_in)
  1965  		if err != nil {
  1966  			return nil, err
  1967  		}
  1968  		return types.ArrayToBytes[float64](_out), nil
  1969  
  1970  	})
  1971  }
  1972  
  1973  func builtInACos(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  1974  	p1 := vector.GenerateFunctionFixedTypeParameter[float64](parameters[0])
  1975  	rs := vector.MustFunctionResult[float64](result)
  1976  	for i := uint64(0); i < uint64(length); i++ {
  1977  		v, null := p1.GetValue(i)
  1978  		if null {
  1979  			if err := rs.Append(0, true); err != nil {
  1980  				return err
  1981  			}
  1982  		} else {
  1983  			sinValue, err := momath.Acos(v)
  1984  			if err != nil {
  1985  				return err
  1986  			}
  1987  			if err = rs.Append(sinValue, false); err != nil {
  1988  				return err
  1989  			}
  1990  		}
  1991  	}
  1992  	return nil
  1993  }
  1994  
  1995  func builtInATan(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  1996  	p1 := vector.GenerateFunctionFixedTypeParameter[float64](parameters[0])
  1997  	rs := vector.MustFunctionResult[float64](result)
  1998  	for i := uint64(0); i < uint64(length); i++ {
  1999  		v, null := p1.GetValue(i)
  2000  		if null {
  2001  			if err := rs.Append(0, true); err != nil {
  2002  				return err
  2003  			}
  2004  		} else {
  2005  			sinValue, err := momath.Atan(v)
  2006  			if err != nil {
  2007  				return err
  2008  			}
  2009  			if err = rs.Append(sinValue, false); err != nil {
  2010  				return err
  2011  			}
  2012  		}
  2013  	}
  2014  	return nil
  2015  }
  2016  
  2017  func builtInATan2(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  2018  	p1 := vector.GenerateFunctionFixedTypeParameter[float64](parameters[0])
  2019  	p2 := vector.GenerateFunctionFixedTypeParameter[float64](parameters[1])
  2020  	rs := vector.MustFunctionResult[float64](result)
  2021  	for i := uint64(0); i < uint64(length); i++ {
  2022  		v1, null1 := p1.GetValue(i)
  2023  		v2, null2 := p2.GetValue(i)
  2024  		if null1 || null2 {
  2025  			if err := rs.Append(0, true); err != nil {
  2026  				return err
  2027  			}
  2028  		} else {
  2029  			if v1 == 0 {
  2030  				return moerr.NewInvalidArg(proc.Ctx, "Atan first input", 0)
  2031  			}
  2032  			if err := rs.Append(math.Atan(v2/v1), false); err != nil {
  2033  				return err
  2034  			}
  2035  		}
  2036  	}
  2037  	return nil
  2038  }
  2039  
  2040  func builtInLn(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  2041  	p1 := vector.GenerateFunctionFixedTypeParameter[float64](parameters[0])
  2042  	rs := vector.MustFunctionResult[float64](result)
  2043  	for i := uint64(0); i < uint64(length); i++ {
  2044  		v, null := p1.GetValue(i)
  2045  		if null {
  2046  			if err := rs.Append(0, true); err != nil {
  2047  				return err
  2048  			}
  2049  		} else {
  2050  			sinValue, err := momath.Ln(v)
  2051  			if err != nil {
  2052  				return err
  2053  			}
  2054  			if err = rs.Append(sinValue, false); err != nil {
  2055  				return err
  2056  			}
  2057  		}
  2058  	}
  2059  	return nil
  2060  }
  2061  
  2062  func builtInLog(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  2063  	p1 := vector.GenerateFunctionFixedTypeParameter[float64](parameters[0])
  2064  	p2 := vector.GenerateFunctionFixedTypeParameter[float64](parameters[1])
  2065  	rs := vector.MustFunctionResult[float64](result)
  2066  	for i := uint64(0); i < uint64(length); i++ {
  2067  		v1, null1 := p1.GetValue(i)
  2068  		v2, null2 := p2.GetValue(i)
  2069  		if null1 || null2 {
  2070  			if err := rs.Append(0, true); err != nil {
  2071  				return err
  2072  			}
  2073  		} else {
  2074  			if v1 == float64(1) {
  2075  				return moerr.NewInvalidArg(proc.Ctx, "log base", 1)
  2076  			}
  2077  			tempV1, err := momath.Ln(v1)
  2078  			if err != nil {
  2079  				return moerr.NewInvalidArg(proc.Ctx, "log input", "<= 0")
  2080  			}
  2081  			tempV2, err := momath.Ln(v2)
  2082  			if err != nil {
  2083  				return moerr.NewInvalidArg(proc.Ctx, "log input", "<= 0")
  2084  			}
  2085  			if err = rs.Append(tempV2/tempV1, false); err != nil {
  2086  				return err
  2087  			}
  2088  		}
  2089  	}
  2090  	return nil
  2091  }
  2092  
  2093  func builtInLog2(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  2094  	p1 := vector.GenerateFunctionFixedTypeParameter[float64](parameters[0])
  2095  	rs := vector.MustFunctionResult[float64](result)
  2096  	for i := uint64(0); i < uint64(length); i++ {
  2097  		v, null := p1.GetValue(i)
  2098  		if null {
  2099  			if err := rs.Append(0, true); err != nil {
  2100  				return err
  2101  			}
  2102  		} else {
  2103  			log2Value, err := momath.Log2(v)
  2104  			if err != nil {
  2105  				return err
  2106  			}
  2107  			if err = rs.Append(log2Value, false); err != nil {
  2108  				return err
  2109  			}
  2110  		}
  2111  	}
  2112  	return nil
  2113  }
  2114  
  2115  func builtInLog10(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  2116  	p1 := vector.GenerateFunctionFixedTypeParameter[float64](parameters[0])
  2117  	rs := vector.MustFunctionResult[float64](result)
  2118  	for i := uint64(0); i < uint64(length); i++ {
  2119  		v, null := p1.GetValue(i)
  2120  		if null {
  2121  			if err := rs.Append(0, true); err != nil {
  2122  				return err
  2123  			}
  2124  		} else {
  2125  			log10Value, err := momath.Lg(v)
  2126  			if err != nil {
  2127  				return err
  2128  			}
  2129  			if err = rs.Append(log10Value, false); err != nil {
  2130  				return err
  2131  			}
  2132  		}
  2133  	}
  2134  	return nil
  2135  }
  2136  
  2137  type opBuiltInRand struct {
  2138  	seed *rand.Rand
  2139  }
  2140  
  2141  var _ = newOpBuiltInRand().builtInRand
  2142  
  2143  func newOpBuiltInRand() *opBuiltInRand {
  2144  	return new(opBuiltInRand)
  2145  }
  2146  
  2147  func builtInRand(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  2148  	rs := vector.MustFunctionResult[float64](result)
  2149  	for i := uint64(0); i < uint64(length); i++ {
  2150  		v := rand.Float64()
  2151  		if err := rs.Append(v, false); err != nil {
  2152  			return err
  2153  		}
  2154  	}
  2155  	return nil
  2156  }
  2157  
  2158  func (op *opBuiltInRand) builtInRand(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  2159  	if !parameters[0].IsConst() {
  2160  		return moerr.NewInvalidArg(proc.Ctx, "parameter of rand", "column")
  2161  	}
  2162  	if parameters[0].IsConstNull() {
  2163  		return moerr.NewInvalidArg(proc.Ctx, "parameter of rand", "null")
  2164  	}
  2165  
  2166  	p1 := vector.GenerateFunctionFixedTypeParameter[int64](parameters[0])
  2167  	rs := vector.MustFunctionResult[float64](result)
  2168  
  2169  	if op.seed == nil {
  2170  		seedNumber, _ := p1.GetValue(0)
  2171  		op.seed = rand.New(rand.NewSource(seedNumber))
  2172  	}
  2173  
  2174  	for i := uint64(0); i < uint64(length); i++ {
  2175  		v := op.seed.Float64()
  2176  		if err := rs.Append(v, false); err != nil {
  2177  			return err
  2178  		}
  2179  	}
  2180  	return nil
  2181  }
  2182  
  2183  func builtInConvertFake(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  2184  	// ignore the second parameter and just set result the same to the first parameter.
  2185  	return opUnaryBytesToBytes(parameters, result, proc, length, func(v []byte) []byte {
  2186  		return v
  2187  	})
  2188  }
  2189  
  2190  func builtInToUpper(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  2191  	return opUnaryBytesToBytes(parameters, result, proc, length, func(v []byte) []byte {
  2192  		return bytes.ToUpper(v)
  2193  	})
  2194  }
  2195  
  2196  func builtInToLower(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  2197  	return opUnaryBytesToBytes(parameters, result, proc, length, func(v []byte) []byte {
  2198  		return bytes.ToLower(v)
  2199  	})
  2200  }
  2201  
  2202  // buildInMOCU extract cu or calculate cu from parameters
  2203  // example:
  2204  // - select mo_cu('[1,2,3,4,5,6,7,8]', 134123)
  2205  // - select mo_cu('[1,2,3,4,5,6,7,8]', 134123, 'total')
  2206  // - select mo_cu('[1,2,3,4,5,6,7,8]', 134123, 'cpu')
  2207  // - select mo_cu('[1,2,3,4,5,6,7,8]', 134123, 'mem')
  2208  // - select mo_cu('[1,2,3,4,5,6,7,8]', 134123, 'ioin')
  2209  // - select mo_cu('[1,2,3,4,5,6,7,8]', 134123, 'ioout')
  2210  // - select mo_cu('[1,2,3,4,5,6,7,8]', 134123, 'network')
  2211  func buildInMOCU(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  2212  	return buildInMOCUWithCfg(parameters, result, proc, length, nil)
  2213  }
  2214  
  2215  func buildInMOCUv1(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
  2216  	cfg := motrace.GetCUConfigV1()
  2217  	return buildInMOCUWithCfg(parameters, result, proc, length, cfg)
  2218  }
  2219  
  2220  func buildInMOCUWithCfg(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int, cfg *config.OBCUConfig) error {
  2221  	var (
  2222  		cu     float64
  2223  		p3     vector.FunctionParameterWrapper[types.Varlena]
  2224  		stats  statistic.StatsArray
  2225  		target []byte
  2226  		null3  bool
  2227  	)
  2228  	rs := vector.MustFunctionResult[float64](result)
  2229  
  2230  	p1 := vector.GenerateFunctionStrParameter(parameters[0])
  2231  	p2 := vector.GenerateFunctionFixedTypeParameter[int64](parameters[1])
  2232  
  2233  	if len(parameters) == 3 {
  2234  		p3 = vector.GenerateFunctionStrParameter(parameters[2])
  2235  	}
  2236  
  2237  	for i := uint64(0); i < uint64(length); i++ {
  2238  		statsJsonArrayStr, null1 := p1.GetStrValue(i) /* stats json array */
  2239  		durationNS, null2 := p2.GetValue(i)           /* duration_ns */
  2240  		if p3 == nil {
  2241  			target, null3 = []byte("total"), false
  2242  		} else {
  2243  			target, null3 = p3.GetStrValue(i)
  2244  		}
  2245  
  2246  		if null1 || null2 || null3 {
  2247  			rs.Append(float64(0), true)
  2248  			continue
  2249  		}
  2250  
  2251  		if len(statsJsonArrayStr) == 0 {
  2252  			rs.Append(float64(0), true)
  2253  			continue
  2254  		}
  2255  
  2256  		if err := json.Unmarshal(statsJsonArrayStr, &stats); err != nil {
  2257  			rs.Append(float64(0), true)
  2258  			//return moerr.NewInternalError(proc.Ctx, "failed to parse json arr: %v", err)
  2259  		}
  2260  
  2261  		switch util.UnsafeBytesToString(target) {
  2262  		case "cpu":
  2263  			cu = motrace.CalculateCUCpu(int64(stats.GetTimeConsumed()), cfg)
  2264  		case "mem":
  2265  			cu = motrace.CalculateCUMem(int64(stats.GetMemorySize()), durationNS, cfg)
  2266  		case "ioin":
  2267  			cu = motrace.CalculateCUIOIn(int64(stats.GetS3IOInputCount()), cfg)
  2268  		case "ioout":
  2269  			cu = motrace.CalculateCUIOOut(int64(stats.GetS3IOOutputCount()), cfg)
  2270  		case "network":
  2271  			cu = motrace.CalculateCUTraffic(int64(stats.GetOutTrafficBytes()), stats.GetConnType(), cfg)
  2272  		case "total":
  2273  			cu = motrace.CalculateCUWithCfg(stats, durationNS, cfg)
  2274  		default:
  2275  			rs.Append(float64(0), true)
  2276  			continue
  2277  		}
  2278  		rs.Append(cu, false)
  2279  	}
  2280  
  2281  	return nil
  2282  }