github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/builtin_miscellaneous.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  // // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  
    13  package memex
    14  
    15  import (
    16  	"bytes"
    17  	"encoding/binary"
    18  	"fmt"
    19  	"math"
    20  	"net"
    21  	"strings"
    22  	"time"
    23  
    24  	"github.com/google/uuid"
    25  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    26  	"github.com/whtcorpsinc/milevadb/stochastikctx"
    27  	"github.com/whtcorpsinc/milevadb/types"
    28  	"github.com/whtcorpsinc/milevadb/types/json"
    29  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    30  	"github.com/whtcorpsinc/fidelpb/go-fidelpb"
    31  )
    32  
    33  var (
    34  	_ functionClass = &sleepFunctionClass{}
    35  	_ functionClass = &lockFunctionClass{}
    36  	_ functionClass = &releaseLockFunctionClass{}
    37  	_ functionClass = &anyValueFunctionClass{}
    38  	_ functionClass = &defaultFunctionClass{}
    39  	_ functionClass = &inetAtonFunctionClass{}
    40  	_ functionClass = &inetNtoaFunctionClass{}
    41  	_ functionClass = &inet6AtonFunctionClass{}
    42  	_ functionClass = &inet6NtoaFunctionClass{}
    43  	_ functionClass = &isFreeLockFunctionClass{}
    44  	_ functionClass = &isIPv4FunctionClass{}
    45  	_ functionClass = &isIPv4CompatFunctionClass{}
    46  	_ functionClass = &isIPv4MappedFunctionClass{}
    47  	_ functionClass = &isIPv6FunctionClass{}
    48  	_ functionClass = &isUsedLockFunctionClass{}
    49  	_ functionClass = &masterPosWaitFunctionClass{}
    50  	_ functionClass = &nameConstFunctionClass{}
    51  	_ functionClass = &releaseAllLocksFunctionClass{}
    52  	_ functionClass = &uuidFunctionClass{}
    53  	_ functionClass = &uuidShortFunctionClass{}
    54  )
    55  
    56  var (
    57  	_ builtinFunc = &builtinSleepSig{}
    58  	_ builtinFunc = &builtinLockSig{}
    59  	_ builtinFunc = &builtinReleaseLockSig{}
    60  	_ builtinFunc = &builtinDecimalAnyValueSig{}
    61  	_ builtinFunc = &builtinDurationAnyValueSig{}
    62  	_ builtinFunc = &builtinIntAnyValueSig{}
    63  	_ builtinFunc = &builtinJSONAnyValueSig{}
    64  	_ builtinFunc = &builtinRealAnyValueSig{}
    65  	_ builtinFunc = &builtinStringAnyValueSig{}
    66  	_ builtinFunc = &builtinTimeAnyValueSig{}
    67  	_ builtinFunc = &builtinInetAtonSig{}
    68  	_ builtinFunc = &builtinInetNtoaSig{}
    69  	_ builtinFunc = &builtinInet6AtonSig{}
    70  	_ builtinFunc = &builtinInet6NtoaSig{}
    71  	_ builtinFunc = &builtinIsIPv4Sig{}
    72  	_ builtinFunc = &builtinIsIPv4CompatSig{}
    73  	_ builtinFunc = &builtinIsIPv4MappedSig{}
    74  	_ builtinFunc = &builtinIsIPv6Sig{}
    75  	_ builtinFunc = &builtinUUIDSig{}
    76  
    77  	_ builtinFunc = &builtinNameConstIntSig{}
    78  	_ builtinFunc = &builtinNameConstRealSig{}
    79  	_ builtinFunc = &builtinNameConstDecimalSig{}
    80  	_ builtinFunc = &builtinNameConstTimeSig{}
    81  	_ builtinFunc = &builtinNameConstDurationSig{}
    82  	_ builtinFunc = &builtinNameConstStringSig{}
    83  	_ builtinFunc = &builtinNameConstJSONSig{}
    84  )
    85  
    86  type sleepFunctionClass struct {
    87  	baseFunctionClass
    88  }
    89  
    90  func (c *sleepFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) {
    91  	if err := c.verifyArgs(args); err != nil {
    92  		return nil, err
    93  	}
    94  	bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, types.ETReal)
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  	bf.tp.Flen = 21
    99  	sig := &builtinSleepSig{bf}
   100  	return sig, nil
   101  }
   102  
   103  type builtinSleepSig struct {
   104  	baseBuiltinFunc
   105  }
   106  
   107  func (b *builtinSleepSig) Clone() builtinFunc {
   108  	newSig := &builtinSleepSig{}
   109  	newSig.cloneFrom(&b.baseBuiltinFunc)
   110  	return newSig
   111  }
   112  
   113  // evalInt evals a builtinSleepSig.
   114  // See https://dev.allegrosql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_sleep
   115  func (b *builtinSleepSig) evalInt(event chunk.Event) (int64, bool, error) {
   116  	val, isNull, err := b.args[0].EvalReal(b.ctx, event)
   117  	if err != nil {
   118  		return 0, isNull, err
   119  	}
   120  
   121  	sessVars := b.ctx.GetStochastikVars()
   122  	if isNull || val < 0 {
   123  		if sessVars.StrictALLEGROSQLMode {
   124  			return 0, false, errIncorrectArgs.GenWithStackByArgs("sleep")
   125  		}
   126  		err := errIncorrectArgs.GenWithStackByArgs("sleep")
   127  		sessVars.StmtCtx.AppendWarning(err)
   128  		return 0, false, nil
   129  	}
   130  
   131  	if val > math.MaxFloat64/float64(time.Second.Nanoseconds()) {
   132  		return 0, false, errIncorrectArgs.GenWithStackByArgs("sleep")
   133  	}
   134  
   135  	if isKilled := doSleep(val, sessVars); isKilled {
   136  		return 1, false, nil
   137  	}
   138  
   139  	return 0, false, nil
   140  }
   141  
   142  type lockFunctionClass struct {
   143  	baseFunctionClass
   144  }
   145  
   146  func (c *lockFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) {
   147  	if err := c.verifyArgs(args); err != nil {
   148  		return nil, err
   149  	}
   150  	bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, types.ETString, types.ETInt)
   151  	if err != nil {
   152  		return nil, err
   153  	}
   154  	sig := &builtinLockSig{bf}
   155  	bf.tp.Flen = 1
   156  	return sig, nil
   157  }
   158  
   159  type builtinLockSig struct {
   160  	baseBuiltinFunc
   161  }
   162  
   163  func (b *builtinLockSig) Clone() builtinFunc {
   164  	newSig := &builtinLockSig{}
   165  	newSig.cloneFrom(&b.baseBuiltinFunc)
   166  	return newSig
   167  }
   168  
   169  // evalInt evals a builtinLockSig.
   170  // See https://dev.allegrosql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_get-dagger
   171  // The dagger function will do nothing.
   172  // Warning: get_lock() function is parsed but ignored.
   173  func (b *builtinLockSig) evalInt(_ chunk.Event) (int64, bool, error) {
   174  	return 1, false, nil
   175  }
   176  
   177  type releaseLockFunctionClass struct {
   178  	baseFunctionClass
   179  }
   180  
   181  func (c *releaseLockFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) {
   182  	if err := c.verifyArgs(args); err != nil {
   183  		return nil, err
   184  	}
   185  	bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, types.ETString)
   186  	if err != nil {
   187  		return nil, err
   188  	}
   189  	sig := &builtinReleaseLockSig{bf}
   190  	bf.tp.Flen = 1
   191  	return sig, nil
   192  }
   193  
   194  type builtinReleaseLockSig struct {
   195  	baseBuiltinFunc
   196  }
   197  
   198  func (b *builtinReleaseLockSig) Clone() builtinFunc {
   199  	newSig := &builtinReleaseLockSig{}
   200  	newSig.cloneFrom(&b.baseBuiltinFunc)
   201  	return newSig
   202  }
   203  
   204  // evalInt evals a builtinReleaseLockSig.
   205  // See https://dev.allegrosql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_release-dagger
   206  // The release dagger function will do nothing.
   207  // Warning: release_lock() function is parsed but ignored.
   208  func (b *builtinReleaseLockSig) evalInt(_ chunk.Event) (int64, bool, error) {
   209  	return 1, false, nil
   210  }
   211  
   212  type anyValueFunctionClass struct {
   213  	baseFunctionClass
   214  }
   215  
   216  func (c *anyValueFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) {
   217  	if err := c.verifyArgs(args); err != nil {
   218  		return nil, err
   219  	}
   220  	argTp := args[0].GetType().EvalType()
   221  	bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, argTp, argTp)
   222  	if err != nil {
   223  		return nil, err
   224  	}
   225  	args[0].GetType().Flag |= bf.tp.Flag
   226  	*bf.tp = *args[0].GetType()
   227  	var sig builtinFunc
   228  	switch argTp {
   229  	case types.ETDecimal:
   230  		sig = &builtinDecimalAnyValueSig{bf}
   231  		sig.setPbCode(fidelpb.ScalarFuncSig_DecimalAnyValue)
   232  	case types.ETDuration:
   233  		sig = &builtinDurationAnyValueSig{bf}
   234  		sig.setPbCode(fidelpb.ScalarFuncSig_DurationAnyValue)
   235  	case types.ETInt:
   236  		bf.tp.Decimal = 0
   237  		sig = &builtinIntAnyValueSig{bf}
   238  		sig.setPbCode(fidelpb.ScalarFuncSig_IntAnyValue)
   239  	case types.ETJson:
   240  		sig = &builtinJSONAnyValueSig{bf}
   241  		sig.setPbCode(fidelpb.ScalarFuncSig_JSONAnyValue)
   242  	case types.ETReal:
   243  		sig = &builtinRealAnyValueSig{bf}
   244  		sig.setPbCode(fidelpb.ScalarFuncSig_RealAnyValue)
   245  	case types.ETString:
   246  		bf.tp.Decimal = types.UnspecifiedLength
   247  		sig = &builtinStringAnyValueSig{bf}
   248  		sig.setPbCode(fidelpb.ScalarFuncSig_StringAnyValue)
   249  	case types.ETDatetime, types.ETTimestamp:
   250  		bf.tp.Charset, bf.tp.DefCauslate, bf.tp.Flag = allegrosql.DefaultCharset, allegrosql.DefaultDefCauslationName, 0
   251  		sig = &builtinTimeAnyValueSig{bf}
   252  		sig.setPbCode(fidelpb.ScalarFuncSig_TimeAnyValue)
   253  	default:
   254  		return nil, errIncorrectArgs.GenWithStackByArgs("ANY_VALUE")
   255  	}
   256  	return sig, nil
   257  }
   258  
   259  type builtinDecimalAnyValueSig struct {
   260  	baseBuiltinFunc
   261  }
   262  
   263  func (b *builtinDecimalAnyValueSig) Clone() builtinFunc {
   264  	newSig := &builtinDecimalAnyValueSig{}
   265  	newSig.cloneFrom(&b.baseBuiltinFunc)
   266  	return newSig
   267  }
   268  
   269  // evalDecimal evals a builtinDecimalAnyValueSig.
   270  // See https://dev.allegrosql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_any-value
   271  func (b *builtinDecimalAnyValueSig) evalDecimal(event chunk.Event) (*types.MyDecimal, bool, error) {
   272  	return b.args[0].EvalDecimal(b.ctx, event)
   273  }
   274  
   275  type builtinDurationAnyValueSig struct {
   276  	baseBuiltinFunc
   277  }
   278  
   279  func (b *builtinDurationAnyValueSig) Clone() builtinFunc {
   280  	newSig := &builtinDurationAnyValueSig{}
   281  	newSig.cloneFrom(&b.baseBuiltinFunc)
   282  	return newSig
   283  }
   284  
   285  // evalDuration evals a builtinDurationAnyValueSig.
   286  // See https://dev.allegrosql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_any-value
   287  func (b *builtinDurationAnyValueSig) evalDuration(event chunk.Event) (types.Duration, bool, error) {
   288  	return b.args[0].EvalDuration(b.ctx, event)
   289  }
   290  
   291  type builtinIntAnyValueSig struct {
   292  	baseBuiltinFunc
   293  }
   294  
   295  func (b *builtinIntAnyValueSig) Clone() builtinFunc {
   296  	newSig := &builtinIntAnyValueSig{}
   297  	newSig.cloneFrom(&b.baseBuiltinFunc)
   298  	return newSig
   299  }
   300  
   301  // evalInt evals a builtinIntAnyValueSig.
   302  // See https://dev.allegrosql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_any-value
   303  func (b *builtinIntAnyValueSig) evalInt(event chunk.Event) (int64, bool, error) {
   304  	return b.args[0].EvalInt(b.ctx, event)
   305  }
   306  
   307  type builtinJSONAnyValueSig struct {
   308  	baseBuiltinFunc
   309  }
   310  
   311  func (b *builtinJSONAnyValueSig) Clone() builtinFunc {
   312  	newSig := &builtinJSONAnyValueSig{}
   313  	newSig.cloneFrom(&b.baseBuiltinFunc)
   314  	return newSig
   315  }
   316  
   317  // evalJSON evals a builtinJSONAnyValueSig.
   318  // See https://dev.allegrosql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_any-value
   319  func (b *builtinJSONAnyValueSig) evalJSON(event chunk.Event) (json.BinaryJSON, bool, error) {
   320  	return b.args[0].EvalJSON(b.ctx, event)
   321  }
   322  
   323  type builtinRealAnyValueSig struct {
   324  	baseBuiltinFunc
   325  }
   326  
   327  func (b *builtinRealAnyValueSig) Clone() builtinFunc {
   328  	newSig := &builtinRealAnyValueSig{}
   329  	newSig.cloneFrom(&b.baseBuiltinFunc)
   330  	return newSig
   331  }
   332  
   333  // evalReal evals a builtinRealAnyValueSig.
   334  // See https://dev.allegrosql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_any-value
   335  func (b *builtinRealAnyValueSig) evalReal(event chunk.Event) (float64, bool, error) {
   336  	return b.args[0].EvalReal(b.ctx, event)
   337  }
   338  
   339  type builtinStringAnyValueSig struct {
   340  	baseBuiltinFunc
   341  }
   342  
   343  func (b *builtinStringAnyValueSig) Clone() builtinFunc {
   344  	newSig := &builtinStringAnyValueSig{}
   345  	newSig.cloneFrom(&b.baseBuiltinFunc)
   346  	return newSig
   347  }
   348  
   349  // evalString evals a builtinStringAnyValueSig.
   350  // See https://dev.allegrosql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_any-value
   351  func (b *builtinStringAnyValueSig) evalString(event chunk.Event) (string, bool, error) {
   352  	return b.args[0].EvalString(b.ctx, event)
   353  }
   354  
   355  type builtinTimeAnyValueSig struct {
   356  	baseBuiltinFunc
   357  }
   358  
   359  func (b *builtinTimeAnyValueSig) Clone() builtinFunc {
   360  	newSig := &builtinTimeAnyValueSig{}
   361  	newSig.cloneFrom(&b.baseBuiltinFunc)
   362  	return newSig
   363  }
   364  
   365  // evalTime evals a builtinTimeAnyValueSig.
   366  // See https://dev.allegrosql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_any-value
   367  func (b *builtinTimeAnyValueSig) evalTime(event chunk.Event) (types.Time, bool, error) {
   368  	return b.args[0].EvalTime(b.ctx, event)
   369  }
   370  
   371  type defaultFunctionClass struct {
   372  	baseFunctionClass
   373  }
   374  
   375  func (c *defaultFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) {
   376  	return nil, errFunctionNotExists.GenWithStackByArgs("FUNCTION", "DEFAULT")
   377  }
   378  
   379  type inetAtonFunctionClass struct {
   380  	baseFunctionClass
   381  }
   382  
   383  func (c *inetAtonFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) {
   384  	if err := c.verifyArgs(args); err != nil {
   385  		return nil, err
   386  	}
   387  	bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, types.ETString)
   388  	if err != nil {
   389  		return nil, err
   390  	}
   391  	bf.tp.Flen = 21
   392  	bf.tp.Flag |= allegrosql.UnsignedFlag
   393  	sig := &builtinInetAtonSig{bf}
   394  	return sig, nil
   395  }
   396  
   397  type builtinInetAtonSig struct {
   398  	baseBuiltinFunc
   399  }
   400  
   401  func (b *builtinInetAtonSig) Clone() builtinFunc {
   402  	newSig := &builtinInetAtonSig{}
   403  	newSig.cloneFrom(&b.baseBuiltinFunc)
   404  	return newSig
   405  }
   406  
   407  // evalInt evals a builtinInetAtonSig.
   408  // See https://dev.allegrosql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_inet-aton
   409  func (b *builtinInetAtonSig) evalInt(event chunk.Event) (int64, bool, error) {
   410  	val, isNull, err := b.args[0].EvalString(b.ctx, event)
   411  	if err != nil || isNull {
   412  		return 0, true, err
   413  	}
   414  	// ip address should not end with '.'.
   415  	if len(val) == 0 || val[len(val)-1] == '.' {
   416  		return 0, true, nil
   417  	}
   418  
   419  	var (
   420  		byteResult, result uint64
   421  		dotCount           int
   422  	)
   423  	for _, c := range val {
   424  		if c >= '0' && c <= '9' {
   425  			digit := uint64(c - '0')
   426  			byteResult = byteResult*10 + digit
   427  			if byteResult > 255 {
   428  				return 0, true, nil
   429  			}
   430  		} else if c == '.' {
   431  			dotCount++
   432  			if dotCount > 3 {
   433  				return 0, true, nil
   434  			}
   435  			result = (result << 8) + byteResult
   436  			byteResult = 0
   437  		} else {
   438  			return 0, true, nil
   439  		}
   440  	}
   441  	// 127 		-> 0.0.0.127
   442  	// 127.255 	-> 127.0.0.255
   443  	// 127.256	-> NULL
   444  	// 127.2.1	-> 127.2.0.1
   445  	switch dotCount {
   446  	case 1:
   447  		result <<= 8
   448  		fallthrough
   449  	case 2:
   450  		result <<= 8
   451  	}
   452  	return int64((result << 8) + byteResult), false, nil
   453  }
   454  
   455  type inetNtoaFunctionClass struct {
   456  	baseFunctionClass
   457  }
   458  
   459  func (c *inetNtoaFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) {
   460  	if err := c.verifyArgs(args); err != nil {
   461  		return nil, err
   462  	}
   463  	bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETString, types.ETInt)
   464  	if err != nil {
   465  		return nil, err
   466  	}
   467  	bf.tp.Charset, bf.tp.DefCauslate = ctx.GetStochastikVars().GetCharsetInfo()
   468  	bf.tp.Flen = 93
   469  	bf.tp.Decimal = 0
   470  	sig := &builtinInetNtoaSig{bf}
   471  	return sig, nil
   472  }
   473  
   474  type builtinInetNtoaSig struct {
   475  	baseBuiltinFunc
   476  }
   477  
   478  func (b *builtinInetNtoaSig) Clone() builtinFunc {
   479  	newSig := &builtinInetNtoaSig{}
   480  	newSig.cloneFrom(&b.baseBuiltinFunc)
   481  	return newSig
   482  }
   483  
   484  // evalString evals a builtinInetNtoaSig.
   485  // See https://dev.allegrosql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_inet-ntoa
   486  func (b *builtinInetNtoaSig) evalString(event chunk.Event) (string, bool, error) {
   487  	val, isNull, err := b.args[0].EvalInt(b.ctx, event)
   488  	if err != nil || isNull {
   489  		return "", true, err
   490  	}
   491  
   492  	if val < 0 || uint64(val) > math.MaxUint32 {
   493  		//not an IPv4 address.
   494  		return "", true, nil
   495  	}
   496  	ip := make(net.IP, net.IPv4len)
   497  	binary.BigEndian.PutUint32(ip, uint32(val))
   498  	ipv4 := ip.To4()
   499  	if ipv4 == nil {
   500  		//Not a vaild ipv4 address.
   501  		return "", true, nil
   502  	}
   503  
   504  	return ipv4.String(), false, nil
   505  }
   506  
   507  type inet6AtonFunctionClass struct {
   508  	baseFunctionClass
   509  }
   510  
   511  func (c *inet6AtonFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) {
   512  	if err := c.verifyArgs(args); err != nil {
   513  		return nil, err
   514  	}
   515  	bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETString, types.ETString)
   516  	if err != nil {
   517  		return nil, err
   518  	}
   519  	bf.tp.Flen = 16
   520  	types.SetBinChsClnFlag(bf.tp)
   521  	bf.tp.Decimal = 0
   522  	sig := &builtinInet6AtonSig{bf}
   523  	return sig, nil
   524  }
   525  
   526  type builtinInet6AtonSig struct {
   527  	baseBuiltinFunc
   528  }
   529  
   530  func (b *builtinInet6AtonSig) Clone() builtinFunc {
   531  	newSig := &builtinInet6AtonSig{}
   532  	newSig.cloneFrom(&b.baseBuiltinFunc)
   533  	return newSig
   534  }
   535  
   536  // evalString evals a builtinInet6AtonSig.
   537  // See https://dev.allegrosql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_inet6-aton
   538  func (b *builtinInet6AtonSig) evalString(event chunk.Event) (string, bool, error) {
   539  	val, isNull, err := b.args[0].EvalString(b.ctx, event)
   540  	if err != nil || isNull {
   541  		return "", true, err
   542  	}
   543  
   544  	if len(val) == 0 {
   545  		return "", true, nil
   546  	}
   547  
   548  	ip := net.ParseIP(val)
   549  	if ip == nil {
   550  		return "", true, nil
   551  	}
   552  
   553  	var isMappedIpv6 bool
   554  	if ip.To4() != nil && strings.Contains(val, ":") {
   555  		//mapped ipv6 address.
   556  		isMappedIpv6 = true
   557  	}
   558  
   559  	var result []byte
   560  	if isMappedIpv6 || ip.To4() == nil {
   561  		result = make([]byte, net.IPv6len)
   562  	} else {
   563  		result = make([]byte, net.IPv4len)
   564  	}
   565  
   566  	if isMappedIpv6 {
   567  		copy(result[12:], ip.To4())
   568  		result[11] = 0xff
   569  		result[10] = 0xff
   570  	} else if ip.To4() == nil {
   571  		copy(result, ip.To16())
   572  	} else {
   573  		copy(result, ip.To4())
   574  	}
   575  
   576  	return string(result[:]), false, nil
   577  }
   578  
   579  type inet6NtoaFunctionClass struct {
   580  	baseFunctionClass
   581  }
   582  
   583  func (c *inet6NtoaFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) {
   584  	if err := c.verifyArgs(args); err != nil {
   585  		return nil, err
   586  	}
   587  	bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETString, types.ETString)
   588  	if err != nil {
   589  		return nil, err
   590  	}
   591  	bf.tp.Charset, bf.tp.DefCauslate = ctx.GetStochastikVars().GetCharsetInfo()
   592  	bf.tp.Flen = 117
   593  	bf.tp.Decimal = 0
   594  	sig := &builtinInet6NtoaSig{bf}
   595  	return sig, nil
   596  }
   597  
   598  type builtinInet6NtoaSig struct {
   599  	baseBuiltinFunc
   600  }
   601  
   602  func (b *builtinInet6NtoaSig) Clone() builtinFunc {
   603  	newSig := &builtinInet6NtoaSig{}
   604  	newSig.cloneFrom(&b.baseBuiltinFunc)
   605  	return newSig
   606  }
   607  
   608  // evalString evals a builtinInet6NtoaSig.
   609  // See https://dev.allegrosql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_inet6-ntoa
   610  func (b *builtinInet6NtoaSig) evalString(event chunk.Event) (string, bool, error) {
   611  	val, isNull, err := b.args[0].EvalString(b.ctx, event)
   612  	if err != nil || isNull {
   613  		return "", true, err
   614  	}
   615  	ip := net.IP(val).String()
   616  	if len(val) == net.IPv6len && !strings.Contains(ip, ":") {
   617  		ip = fmt.Sprintf("::ffff:%s", ip)
   618  	}
   619  
   620  	if net.ParseIP(ip) == nil {
   621  		return "", true, nil
   622  	}
   623  
   624  	return ip, false, nil
   625  }
   626  
   627  type isFreeLockFunctionClass struct {
   628  	baseFunctionClass
   629  }
   630  
   631  func (c *isFreeLockFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) {
   632  	return nil, errFunctionNotExists.GenWithStackByArgs("FUNCTION", "IS_FREE_LOCK")
   633  }
   634  
   635  type isIPv4FunctionClass struct {
   636  	baseFunctionClass
   637  }
   638  
   639  func (c *isIPv4FunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) {
   640  	if err := c.verifyArgs(args); err != nil {
   641  		return nil, err
   642  	}
   643  	bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, types.ETString)
   644  	if err != nil {
   645  		return nil, err
   646  	}
   647  	bf.tp.Flen = 1
   648  	sig := &builtinIsIPv4Sig{bf}
   649  	return sig, nil
   650  }
   651  
   652  type builtinIsIPv4Sig struct {
   653  	baseBuiltinFunc
   654  }
   655  
   656  func (b *builtinIsIPv4Sig) Clone() builtinFunc {
   657  	newSig := &builtinIsIPv4Sig{}
   658  	newSig.cloneFrom(&b.baseBuiltinFunc)
   659  	return newSig
   660  }
   661  
   662  // evalInt evals a builtinIsIPv4Sig.
   663  // See https://dev.allegrosql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_is-ipv4
   664  func (b *builtinIsIPv4Sig) evalInt(event chunk.Event) (int64, bool, error) {
   665  	val, isNull, err := b.args[0].EvalString(b.ctx, event)
   666  	if err != nil || isNull {
   667  		return 0, err != nil, err
   668  	}
   669  	if isIPv4(val) {
   670  		return 1, false, nil
   671  	}
   672  	return 0, false, nil
   673  }
   674  
   675  // isIPv4 checks IPv4 address which satisfying the format A.B.C.D(0<=A/B/C/D<=255).
   676  // Mapped IPv6 address like '::ffff:1.2.3.4' would return false.
   677  func isIPv4(ip string) bool {
   678  	// acc: keep the decimal value of each segment under check, which should between 0 and 255 for valid IPv4 address.
   679  	// fidel: sentinel for '.'
   680  	dots, acc, fidel := 0, 0, true
   681  	for _, c := range ip {
   682  		switch {
   683  		case '0' <= c && c <= '9':
   684  			acc = acc*10 + int(c-'0')
   685  			fidel = false
   686  		case c == '.':
   687  			dots++
   688  			if dots > 3 || acc > 255 || fidel {
   689  				return false
   690  			}
   691  			acc, fidel = 0, true
   692  		default:
   693  			return false
   694  		}
   695  	}
   696  	if dots != 3 || acc > 255 || fidel {
   697  		return false
   698  	}
   699  	return true
   700  }
   701  
   702  type isIPv4CompatFunctionClass struct {
   703  	baseFunctionClass
   704  }
   705  
   706  func (c *isIPv4CompatFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) {
   707  	if err := c.verifyArgs(args); err != nil {
   708  		return nil, err
   709  	}
   710  	bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, types.ETString)
   711  	if err != nil {
   712  		return nil, err
   713  	}
   714  	bf.tp.Flen = 1
   715  	sig := &builtinIsIPv4CompatSig{bf}
   716  	return sig, nil
   717  }
   718  
   719  type builtinIsIPv4CompatSig struct {
   720  	baseBuiltinFunc
   721  }
   722  
   723  func (b *builtinIsIPv4CompatSig) Clone() builtinFunc {
   724  	newSig := &builtinIsIPv4CompatSig{}
   725  	newSig.cloneFrom(&b.baseBuiltinFunc)
   726  	return newSig
   727  }
   728  
   729  // evalInt evals Is_IPv4_Compat
   730  // See https://dev.allegrosql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_is-ipv4-compat
   731  func (b *builtinIsIPv4CompatSig) evalInt(event chunk.Event) (int64, bool, error) {
   732  	val, isNull, err := b.args[0].EvalString(b.ctx, event)
   733  	if err != nil || isNull {
   734  		return 0, err != nil, err
   735  	}
   736  
   737  	ipAddress := []byte(val)
   738  	if len(ipAddress) != net.IPv6len {
   739  		//Not an IPv6 address, return false
   740  		return 0, false, nil
   741  	}
   742  
   743  	prefixCompat := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
   744  	if !bytes.HasPrefix(ipAddress, prefixCompat) {
   745  		return 0, false, nil
   746  	}
   747  	return 1, false, nil
   748  }
   749  
   750  type isIPv4MappedFunctionClass struct {
   751  	baseFunctionClass
   752  }
   753  
   754  func (c *isIPv4MappedFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) {
   755  	if err := c.verifyArgs(args); err != nil {
   756  		return nil, err
   757  	}
   758  	bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, types.ETString)
   759  	if err != nil {
   760  		return nil, err
   761  	}
   762  	bf.tp.Flen = 1
   763  	sig := &builtinIsIPv4MappedSig{bf}
   764  	return sig, nil
   765  }
   766  
   767  type builtinIsIPv4MappedSig struct {
   768  	baseBuiltinFunc
   769  }
   770  
   771  func (b *builtinIsIPv4MappedSig) Clone() builtinFunc {
   772  	newSig := &builtinIsIPv4MappedSig{}
   773  	newSig.cloneFrom(&b.baseBuiltinFunc)
   774  	return newSig
   775  }
   776  
   777  // evalInt evals Is_IPv4_Mapped
   778  // See https://dev.allegrosql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_is-ipv4-mapped
   779  func (b *builtinIsIPv4MappedSig) evalInt(event chunk.Event) (int64, bool, error) {
   780  	val, isNull, err := b.args[0].EvalString(b.ctx, event)
   781  	if err != nil || isNull {
   782  		return 0, err != nil, err
   783  	}
   784  
   785  	ipAddress := []byte(val)
   786  	if len(ipAddress) != net.IPv6len {
   787  		//Not an IPv6 address, return false
   788  		return 0, false, nil
   789  	}
   790  
   791  	prefixMapped := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}
   792  	if !bytes.HasPrefix(ipAddress, prefixMapped) {
   793  		return 0, false, nil
   794  	}
   795  	return 1, false, nil
   796  }
   797  
   798  type isIPv6FunctionClass struct {
   799  	baseFunctionClass
   800  }
   801  
   802  func (c *isIPv6FunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) {
   803  	if err := c.verifyArgs(args); err != nil {
   804  		return nil, err
   805  	}
   806  	bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETInt, types.ETString)
   807  	if err != nil {
   808  		return nil, err
   809  	}
   810  	bf.tp.Flen = 1
   811  	sig := &builtinIsIPv6Sig{bf}
   812  	return sig, nil
   813  }
   814  
   815  type builtinIsIPv6Sig struct {
   816  	baseBuiltinFunc
   817  }
   818  
   819  func (b *builtinIsIPv6Sig) Clone() builtinFunc {
   820  	newSig := &builtinIsIPv6Sig{}
   821  	newSig.cloneFrom(&b.baseBuiltinFunc)
   822  	return newSig
   823  }
   824  
   825  // evalInt evals a builtinIsIPv6Sig.
   826  // See https://dev.allegrosql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_is-ipv6
   827  func (b *builtinIsIPv6Sig) evalInt(event chunk.Event) (int64, bool, error) {
   828  	val, isNull, err := b.args[0].EvalString(b.ctx, event)
   829  	if err != nil || isNull {
   830  		return 0, err != nil, err
   831  	}
   832  	ip := net.ParseIP(val)
   833  	if ip != nil && !isIPv4(val) {
   834  		return 1, false, nil
   835  	}
   836  	return 0, false, nil
   837  }
   838  
   839  type isUsedLockFunctionClass struct {
   840  	baseFunctionClass
   841  }
   842  
   843  func (c *isUsedLockFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) {
   844  	return nil, errFunctionNotExists.GenWithStackByArgs("FUNCTION", "IS_USED_LOCK")
   845  }
   846  
   847  type masterPosWaitFunctionClass struct {
   848  	baseFunctionClass
   849  }
   850  
   851  func (c *masterPosWaitFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) {
   852  	return nil, errFunctionNotExists.GenWithStackByArgs("FUNCTION", "MASTER_POS_WAIT")
   853  }
   854  
   855  type nameConstFunctionClass struct {
   856  	baseFunctionClass
   857  }
   858  
   859  func (c *nameConstFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) {
   860  	if err := c.verifyArgs(args); err != nil {
   861  		return nil, err
   862  	}
   863  	argTp := args[1].GetType().EvalType()
   864  	bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, argTp, types.ETString, argTp)
   865  	if err != nil {
   866  		return nil, err
   867  	}
   868  	*bf.tp = *args[1].GetType()
   869  	var sig builtinFunc
   870  	switch argTp {
   871  	case types.ETDecimal:
   872  		sig = &builtinNameConstDecimalSig{bf}
   873  	case types.ETDuration:
   874  		sig = &builtinNameConstDurationSig{bf}
   875  	case types.ETInt:
   876  		bf.tp.Decimal = 0
   877  		sig = &builtinNameConstIntSig{bf}
   878  	case types.ETJson:
   879  		sig = &builtinNameConstJSONSig{bf}
   880  	case types.ETReal:
   881  		sig = &builtinNameConstRealSig{bf}
   882  	case types.ETString:
   883  		bf.tp.Decimal = types.UnspecifiedLength
   884  		sig = &builtinNameConstStringSig{bf}
   885  	case types.ETDatetime, types.ETTimestamp:
   886  		bf.tp.Charset, bf.tp.DefCauslate, bf.tp.Flag = allegrosql.DefaultCharset, allegrosql.DefaultDefCauslationName, 0
   887  		sig = &builtinNameConstTimeSig{bf}
   888  	default:
   889  		return nil, errIncorrectArgs.GenWithStackByArgs("NAME_CONST")
   890  	}
   891  	return sig, nil
   892  }
   893  
   894  type builtinNameConstDecimalSig struct {
   895  	baseBuiltinFunc
   896  }
   897  
   898  func (b *builtinNameConstDecimalSig) Clone() builtinFunc {
   899  	newSig := &builtinNameConstDecimalSig{}
   900  	newSig.cloneFrom(&b.baseBuiltinFunc)
   901  	return newSig
   902  }
   903  
   904  func (b *builtinNameConstDecimalSig) evalDecimal(event chunk.Event) (*types.MyDecimal, bool, error) {
   905  	return b.args[1].EvalDecimal(b.ctx, event)
   906  }
   907  
   908  type builtinNameConstIntSig struct {
   909  	baseBuiltinFunc
   910  }
   911  
   912  func (b *builtinNameConstIntSig) Clone() builtinFunc {
   913  	newSig := &builtinNameConstIntSig{}
   914  	newSig.cloneFrom(&b.baseBuiltinFunc)
   915  	return newSig
   916  }
   917  
   918  func (b *builtinNameConstIntSig) evalInt(event chunk.Event) (int64, bool, error) {
   919  	return b.args[1].EvalInt(b.ctx, event)
   920  }
   921  
   922  type builtinNameConstRealSig struct {
   923  	baseBuiltinFunc
   924  }
   925  
   926  func (b *builtinNameConstRealSig) Clone() builtinFunc {
   927  	newSig := &builtinNameConstRealSig{}
   928  	newSig.cloneFrom(&b.baseBuiltinFunc)
   929  	return newSig
   930  }
   931  
   932  func (b *builtinNameConstRealSig) evalReal(event chunk.Event) (float64, bool, error) {
   933  	return b.args[1].EvalReal(b.ctx, event)
   934  }
   935  
   936  type builtinNameConstStringSig struct {
   937  	baseBuiltinFunc
   938  }
   939  
   940  func (b *builtinNameConstStringSig) Clone() builtinFunc {
   941  	newSig := &builtinNameConstStringSig{}
   942  	newSig.cloneFrom(&b.baseBuiltinFunc)
   943  	return newSig
   944  }
   945  
   946  func (b *builtinNameConstStringSig) evalString(event chunk.Event) (string, bool, error) {
   947  	return b.args[1].EvalString(b.ctx, event)
   948  }
   949  
   950  type builtinNameConstJSONSig struct {
   951  	baseBuiltinFunc
   952  }
   953  
   954  func (b *builtinNameConstJSONSig) Clone() builtinFunc {
   955  	newSig := &builtinNameConstJSONSig{}
   956  	newSig.cloneFrom(&b.baseBuiltinFunc)
   957  	return newSig
   958  }
   959  
   960  func (b *builtinNameConstJSONSig) evalJSON(event chunk.Event) (json.BinaryJSON, bool, error) {
   961  	return b.args[1].EvalJSON(b.ctx, event)
   962  }
   963  
   964  type builtinNameConstDurationSig struct {
   965  	baseBuiltinFunc
   966  }
   967  
   968  func (b *builtinNameConstDurationSig) Clone() builtinFunc {
   969  	newSig := &builtinNameConstDurationSig{}
   970  	newSig.cloneFrom(&b.baseBuiltinFunc)
   971  	return newSig
   972  }
   973  
   974  func (b *builtinNameConstDurationSig) evalDuration(event chunk.Event) (types.Duration, bool, error) {
   975  	return b.args[1].EvalDuration(b.ctx, event)
   976  }
   977  
   978  type builtinNameConstTimeSig struct {
   979  	baseBuiltinFunc
   980  }
   981  
   982  func (b *builtinNameConstTimeSig) Clone() builtinFunc {
   983  	newSig := &builtinNameConstTimeSig{}
   984  	newSig.cloneFrom(&b.baseBuiltinFunc)
   985  	return newSig
   986  }
   987  
   988  func (b *builtinNameConstTimeSig) evalTime(event chunk.Event) (types.Time, bool, error) {
   989  	return b.args[1].EvalTime(b.ctx, event)
   990  }
   991  
   992  type releaseAllLocksFunctionClass struct {
   993  	baseFunctionClass
   994  }
   995  
   996  func (c *releaseAllLocksFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) {
   997  	return nil, errFunctionNotExists.GenWithStackByArgs("FUNCTION", "RELEASE_ALL_LOCKS")
   998  }
   999  
  1000  type uuidFunctionClass struct {
  1001  	baseFunctionClass
  1002  }
  1003  
  1004  func (c *uuidFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) {
  1005  	if err := c.verifyArgs(args); err != nil {
  1006  		return nil, err
  1007  	}
  1008  	bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETString)
  1009  	if err != nil {
  1010  		return nil, err
  1011  	}
  1012  	bf.tp.Charset, bf.tp.DefCauslate = ctx.GetStochastikVars().GetCharsetInfo()
  1013  	bf.tp.Flen = 36
  1014  	sig := &builtinUUIDSig{bf}
  1015  	return sig, nil
  1016  }
  1017  
  1018  type builtinUUIDSig struct {
  1019  	baseBuiltinFunc
  1020  }
  1021  
  1022  func (b *builtinUUIDSig) Clone() builtinFunc {
  1023  	newSig := &builtinUUIDSig{}
  1024  	newSig.cloneFrom(&b.baseBuiltinFunc)
  1025  	return newSig
  1026  }
  1027  
  1028  // evalString evals a builtinUUIDSig.
  1029  // See https://dev.allegrosql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_uuid
  1030  func (b *builtinUUIDSig) evalString(_ chunk.Event) (d string, isNull bool, err error) {
  1031  	var id uuid.UUID
  1032  	id, err = uuid.NewUUID()
  1033  	if err != nil {
  1034  		return
  1035  	}
  1036  	d = id.String()
  1037  	return
  1038  }
  1039  
  1040  type uuidShortFunctionClass struct {
  1041  	baseFunctionClass
  1042  }
  1043  
  1044  func (c *uuidShortFunctionClass) getFunction(ctx stochastikctx.Context, args []Expression) (builtinFunc, error) {
  1045  	return nil, errFunctionNotExists.GenWithStackByArgs("FUNCTION", "UUID_SHORT")
  1046  }