github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/util/eval_expr_util.go (about)

     1  // Copyright 2021 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 util
    16  
    17  import (
    18  	"context"
    19  	"encoding/hex"
    20  	"fmt"
    21  	"go/constant"
    22  	"math"
    23  	"strconv"
    24  	"strings"
    25  	"time"
    26  	"unicode/utf8"
    27  
    28  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    29  	"github.com/matrixorigin/matrixone/pkg/container/bytejson"
    30  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    31  	"github.com/matrixorigin/matrixone/pkg/container/types"
    32  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    33  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
    34  	"github.com/matrixorigin/matrixone/pkg/sql/plan/function"
    35  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    36  	"golang.org/x/exp/constraints"
    37  )
    38  
    39  func getVal(val any) string {
    40  	switch v := val.(type) {
    41  	case []byte:
    42  		return string(v)
    43  	case string:
    44  		return v
    45  	default:
    46  		return fmt.Sprintf("%v", val)
    47  	}
    48  }
    49  
    50  func HexToInt(hex string) (uint64, error) {
    51  	s := hex[2:]
    52  	return strconv.ParseUint(s, 16, 64)
    53  }
    54  
    55  func BinaryToInt(b string) (uint64, error) {
    56  	s := b[2:]
    57  	return strconv.ParseUint(s, 2, 64)
    58  }
    59  
    60  func ScoreBinaryToInt(s string) (uint64, error) {
    61  	if len(s) > 8 {
    62  		return 0, moerr.NewInvalidInputNoCtx("s is too long, len(s) =  %v", len(s))
    63  	}
    64  
    65  	v := uint64(0)
    66  	for i := 0; i < len(s); i++ {
    67  		v = v<<8 | uint64(s[i])
    68  	}
    69  	return v, nil
    70  }
    71  
    72  func DecodeBinaryString(s string) ([]byte, error) {
    73  	b := make([]byte, (len(s)+7)/8)
    74  	padding := strings.Repeat("0", len(b)*8-len(s))
    75  	s = padding + s
    76  	for i, j := 0, 0; i < len(s); i, j = i+8, j+1 {
    77  		val, err := strconv.ParseUint(s[i:i+8], 2, 8)
    78  		if err != nil {
    79  			return b, err
    80  		}
    81  		b[j] = byte(val)
    82  	}
    83  	return b, nil
    84  }
    85  
    86  func GenVectorByVarValue(proc *process.Process, typ types.Type, val any) (*vector.Vector, error) {
    87  	if val == nil {
    88  		vec := vector.NewConstNull(typ, 1, proc.Mp())
    89  		return vec, nil
    90  	} else {
    91  		strVal := getVal(val)
    92  		return vector.NewConstBytes(typ, []byte(strVal), 1, proc.Mp())
    93  	}
    94  }
    95  
    96  func AppendAnyToStringVector(proc *process.Process, val any, vec *vector.Vector) error {
    97  	if val == nil {
    98  		return vector.AppendBytes(vec, []byte{}, true, proc.Mp())
    99  	} else {
   100  		strVal := getVal(val)
   101  		return vector.AppendBytes(vec, []byte(strVal), false, proc.Mp())
   102  	}
   103  }
   104  
   105  func SetAnyToStringVector(proc *process.Process, val any, vec *vector.Vector, idx int) error {
   106  	if val == nil {
   107  		vec.GetNulls().Set(uint64(idx))
   108  		return nil
   109  	} else {
   110  		strVal := getVal(val)
   111  		return vector.SetBytesAt(vec, idx, []byte(strVal), proc.Mp())
   112  	}
   113  }
   114  
   115  func SetBytesToAnyVector(ctx context.Context, val string, row int,
   116  	isNull bool, vec *vector.Vector, proc *process.Process) error {
   117  	if isNull {
   118  		vec.GetNulls().Set(uint64(row))
   119  		return nil
   120  	} else {
   121  		vec.GetNulls().Unset(uint64(row))
   122  	}
   123  	switch vec.GetType().Oid {
   124  	case types.T_bool:
   125  		v, err := types.ParseBool(val)
   126  		if err != nil {
   127  			return err
   128  		}
   129  		return vector.SetFixedAt(vec, row, v)
   130  	case types.T_bit:
   131  		width := int(vec.GetType().Width)
   132  		v, err := strconv.ParseUint(val, 0, width)
   133  		if err != nil {
   134  			return moerr.NewOutOfRange(ctx, fmt.Sprintf("bit(%d)", width), "value '%v'", val)
   135  		}
   136  		return vector.SetFixedAt(vec, row, v)
   137  	case types.T_int8:
   138  		v, err := strconv.ParseInt(val, 0, 8)
   139  		if err != nil {
   140  			return moerr.NewOutOfRange(ctx, "int8", "value '%v'", val)
   141  		}
   142  		return vector.SetFixedAt(vec, row, int8(v))
   143  	case types.T_int16:
   144  		v, err := strconv.ParseInt(val, 0, 16)
   145  		if err != nil {
   146  			return moerr.NewOutOfRange(ctx, "int16", "value '%v'", val)
   147  		}
   148  		return vector.SetFixedAt(vec, row, int16(v))
   149  	case types.T_int32:
   150  		v, err := strconv.ParseInt(val, 0, 32)
   151  		if err != nil {
   152  			return moerr.NewOutOfRange(ctx, "int32", "value '%v'", val)
   153  		}
   154  		return vector.SetFixedAt(vec, row, int32(v))
   155  	case types.T_int64:
   156  		v, err := strconv.ParseInt(val, 0, 64)
   157  		if err != nil {
   158  			return moerr.NewOutOfRange(ctx, "int64", "value '%v'", val)
   159  		}
   160  		return vector.SetFixedAt(vec, row, int64(v))
   161  	case types.T_uint8:
   162  		v, err := strconv.ParseUint(val, 0, 8)
   163  		if err != nil {
   164  			return moerr.NewOutOfRange(ctx, "uint8", "value '%v'", val)
   165  		}
   166  		return vector.SetFixedAt(vec, row, uint8(v))
   167  	case types.T_uint16:
   168  		v, err := strconv.ParseUint(val, 0, 16)
   169  		if err != nil {
   170  			return moerr.NewOutOfRange(ctx, "uint16", "value '%v'", val)
   171  		}
   172  		return vector.SetFixedAt(vec, row, uint16(v))
   173  	case types.T_uint32:
   174  		v, err := strconv.ParseUint(val, 0, 32)
   175  		if err != nil {
   176  			return moerr.NewOutOfRange(ctx, "uint32", "value '%v'", val)
   177  		}
   178  		return vector.SetFixedAt(vec, row, uint32(v))
   179  	case types.T_uint64:
   180  		v, err := strconv.ParseUint(val, 0, 64)
   181  		if err != nil {
   182  			return moerr.NewOutOfRange(ctx, "uint64", "value '%v'", val)
   183  		}
   184  		return vector.SetFixedAt(vec, row, uint64(v))
   185  	case types.T_float32:
   186  		v, err := strconv.ParseFloat(val, 32)
   187  		if err != nil {
   188  			return moerr.NewOutOfRange(ctx, "float32", "value '%v'", val)
   189  		}
   190  		return vector.SetFixedAt(vec, row, float32(v))
   191  	case types.T_float64:
   192  		v, err := strconv.ParseFloat(val, 64)
   193  		if err != nil {
   194  			return moerr.NewOutOfRange(ctx, "float64", "value '%v'", val)
   195  		}
   196  		return vector.SetFixedAt(vec, row, float64(v))
   197  	case types.T_decimal64:
   198  		v, err := types.ParseDecimal64(val, vec.GetType().Width, vec.GetType().Scale)
   199  		if err != nil {
   200  			return err
   201  		}
   202  		return vector.SetFixedAt(vec, row, v)
   203  	case types.T_decimal128:
   204  		v, err := types.ParseDecimal128(val, vec.GetType().Width, vec.GetType().Scale)
   205  		if err != nil {
   206  			return err
   207  		}
   208  		return vector.SetFixedAt(vec, row, v)
   209  	case types.T_char, types.T_varchar, types.T_blob, types.T_binary, types.T_varbinary, types.T_text:
   210  		return vector.SetBytesAt(vec, row, []byte(val), proc.Mp())
   211  	case types.T_array_float32:
   212  		v, err := types.StringToArrayToBytes[float32](val)
   213  		if err != nil {
   214  			return err
   215  		}
   216  		return vector.SetBytesAt(vec, row, v, proc.Mp())
   217  	case types.T_array_float64:
   218  		v, err := types.StringToArrayToBytes[float64](val)
   219  		if err != nil {
   220  			return err
   221  		}
   222  		return vector.SetBytesAt(vec, row, v, proc.Mp())
   223  	case types.T_json:
   224  		return vector.SetBytesAt(vec, row, []byte(val), proc.Mp())
   225  	case types.T_time:
   226  		v, err := types.ParseTime(val, vec.GetType().Scale)
   227  		if err != nil {
   228  			return err
   229  		}
   230  		return vector.SetFixedAt(vec, row, v)
   231  	case types.T_datetime:
   232  		v, err := types.ParseDatetime(val, vec.GetType().Scale)
   233  		if err != nil {
   234  			return err
   235  		}
   236  		return vector.SetFixedAt(vec, row, v)
   237  	case types.T_timestamp:
   238  		v, err := types.ParseTimestamp(time.Local, val, vec.GetType().Scale)
   239  		if err != nil {
   240  			return err
   241  		}
   242  		return vector.SetFixedAt(vec, row, v)
   243  	case types.T_date:
   244  		v, err := types.ParseDateCast(val)
   245  		if err != nil {
   246  			return err
   247  		}
   248  		return vector.SetFixedAt(vec, row, v)
   249  	case types.T_enum:
   250  		v, err := strconv.ParseUint(val, 0, 16)
   251  		if err != nil {
   252  			return moerr.NewOutOfRange(ctx, "enum", "value '%v'", val)
   253  		}
   254  		return vector.SetFixedAt(vec, row, types.Enum(v))
   255  	default:
   256  		panic(fmt.Sprintf("unsupported type %v", vec.GetType().Oid))
   257  	}
   258  }
   259  
   260  func SetInsertValue(proc *process.Process, numVal *tree.NumVal, vec *vector.Vector) (bool, error) {
   261  	switch vec.GetType().Oid {
   262  	case types.T_bool:
   263  		return setInsertValueBool(proc, numVal, vec)
   264  	case types.T_bit:
   265  		return setInsertValueBit(proc, numVal, vec)
   266  	case types.T_int8:
   267  		return setInsertValueNumber[int8](proc, numVal, vec)
   268  	case types.T_int16:
   269  		return setInsertValueNumber[int16](proc, numVal, vec)
   270  	case types.T_int32:
   271  		return setInsertValueNumber[int32](proc, numVal, vec)
   272  	case types.T_int64:
   273  		return setInsertValueNumber[int64](proc, numVal, vec)
   274  	case types.T_uint8:
   275  		return setInsertValueNumber[uint8](proc, numVal, vec)
   276  	case types.T_uint16:
   277  		return setInsertValueNumber[uint16](proc, numVal, vec)
   278  	case types.T_uint32:
   279  		return setInsertValueNumber[uint32](proc, numVal, vec)
   280  	case types.T_uint64:
   281  		return setInsertValueNumber[uint64](proc, numVal, vec)
   282  	case types.T_float32:
   283  		return setInsertValueNumber[float32](proc, numVal, vec)
   284  	case types.T_float64:
   285  		return setInsertValueNumber[float64](proc, numVal, vec)
   286  	case types.T_decimal64:
   287  		return setInsertValueDecimal64(proc, numVal, vec)
   288  	case types.T_decimal128:
   289  		return setInsertValueDecimal128(proc, numVal, vec)
   290  	case types.T_char, types.T_varchar, types.T_blob, types.T_binary, types.T_varbinary, types.T_text,
   291  		types.T_array_float32, types.T_array_float64:
   292  		return setInsertValueString(proc, numVal, vec)
   293  	case types.T_json:
   294  		return setInsertValueJSON(proc, numVal, vec)
   295  	case types.T_uuid:
   296  		return setInsertValueUuid(proc, numVal, vec)
   297  	case types.T_time:
   298  		return setInsertValueTime(proc, numVal, vec)
   299  	case types.T_date:
   300  		return setInsertValueDate(proc, numVal, vec)
   301  	case types.T_datetime:
   302  		return setInsertValueDateTime(proc, numVal, vec)
   303  	case types.T_timestamp:
   304  		return setInsertValueTimeStamp(proc, numVal, vec)
   305  	case types.T_enum:
   306  		return false, nil
   307  	}
   308  
   309  	return false, nil
   310  }
   311  
   312  func setInsertValueTimeStamp(proc *process.Process, numVal *tree.NumVal, vec *vector.Vector) (canInsert bool, err error) {
   313  	canInsert = true
   314  
   315  	appendIntegerTimeStamp := func(val int64) error {
   316  		if val < 0 || uint64(val) > 32536771199 {
   317  			return vector.AppendFixed[types.Timestamp](vec, 0, true, proc.GetMPool())
   318  		}
   319  		result := types.UnixToTimestamp(val)
   320  		return vector.AppendFixed(vec, result, false, proc.Mp())
   321  	}
   322  
   323  	switch numVal.ValType {
   324  	case tree.P_null:
   325  		err = vector.AppendFixed[types.Timestamp](vec, 0, true, proc.GetMPool())
   326  
   327  	case tree.P_int64:
   328  		val, ok := constant.Int64Val(numVal.Value)
   329  		if !ok {
   330  			return false, moerr.NewInvalidInput(proc.Ctx, "invalid int value '%s'", numVal.Value.String())
   331  		}
   332  		err = appendIntegerTimeStamp(val)
   333  
   334  	case tree.P_uint64:
   335  		val, ok := constant.Uint64Val(numVal.Value)
   336  		if !ok {
   337  			return false, moerr.NewInvalidInput(proc.Ctx, "invalid uint value '%s'", numVal.Value.String())
   338  		}
   339  		err = appendIntegerTimeStamp(int64(val))
   340  
   341  	case tree.P_decimal:
   342  		canInsert = false
   343  
   344  	case tree.P_float64:
   345  		canInsert = false
   346  
   347  	case tree.P_hexnum:
   348  		var val uint64
   349  		if val, err = HexToInt(numVal.OrigString()); err != nil {
   350  			return false, err
   351  		}
   352  		err = appendIntegerTimeStamp(int64(val))
   353  
   354  	case tree.P_char:
   355  		s := numVal.OrigString()
   356  		if len(s) == 0 {
   357  			err = vector.AppendFixed[types.Timestamp](vec, 0, true, proc.GetMPool())
   358  		} else {
   359  			typ := vec.GetType()
   360  			var val types.Timestamp
   361  			zone := time.Local
   362  			if proc != nil {
   363  				zone = proc.SessionInfo.TimeZone
   364  			}
   365  			val, err = types.ParseTimestamp(zone, s, typ.Scale)
   366  			if err != nil {
   367  				return
   368  			}
   369  			err = vector.AppendFixed(vec, val, false, proc.Mp())
   370  		}
   371  
   372  	case tree.P_bit:
   373  		var val uint64
   374  		if val, err = BinaryToInt(numVal.OrigString()); err != nil {
   375  			return false, err
   376  		}
   377  		err = appendIntegerTimeStamp(int64(val))
   378  
   379  	case tree.P_nulltext:
   380  		err = vector.AppendFixed[types.Timestamp](vec, 0, true, proc.GetMPool())
   381  	default:
   382  		canInsert = false
   383  	}
   384  	return
   385  }
   386  
   387  func setInsertValueDateTime(proc *process.Process, numVal *tree.NumVal, vec *vector.Vector) (canInsert bool, err error) {
   388  	canInsert = true
   389  
   390  	switch numVal.ValType {
   391  	case tree.P_null:
   392  		err = vector.AppendFixed[types.Datetime](vec, 0, true, proc.GetMPool())
   393  
   394  	case tree.P_int64:
   395  		canInsert = false
   396  
   397  	case tree.P_uint64:
   398  		canInsert = false
   399  
   400  	case tree.P_decimal:
   401  		canInsert = false
   402  
   403  	case tree.P_float64:
   404  		canInsert = false
   405  
   406  	case tree.P_hexnum:
   407  		canInsert = false
   408  
   409  	case tree.P_char:
   410  		s := numVal.OrigString()
   411  		if len(s) == 0 {
   412  			err = vector.AppendFixed[types.Datetime](vec, 0, true, proc.GetMPool())
   413  		} else {
   414  			typ := vec.GetType()
   415  			var val types.Datetime
   416  			val, err = types.ParseDatetime(s, typ.Scale)
   417  			if err != nil {
   418  				return
   419  			}
   420  			err = vector.AppendFixed(vec, val, false, proc.Mp())
   421  		}
   422  
   423  	case tree.P_bool:
   424  		canInsert = false
   425  	case tree.P_ScoreBinary:
   426  		canInsert = false
   427  	case tree.P_bit:
   428  		canInsert = false
   429  	case tree.P_nulltext:
   430  		err = vector.AppendFixed[types.Timestamp](vec, 0, true, proc.GetMPool())
   431  	default:
   432  		canInsert = false
   433  	}
   434  	return
   435  }
   436  
   437  func setInsertValueTime(proc *process.Process, numVal *tree.NumVal, vec *vector.Vector) (canInsert bool, err error) {
   438  	canInsert = true
   439  
   440  	appendIntegerTime := func(val int64) error {
   441  		typ := vec.GetType()
   442  		if val < types.MinInputIntTime || val > types.MaxInputIntTime {
   443  			return moerr.NewOutOfRange(proc.Ctx, "time", "value %d", val)
   444  		}
   445  		result, err := types.ParseInt64ToTime(val, typ.Scale)
   446  		if err != nil {
   447  			return err
   448  		}
   449  		return vector.AppendFixed(vec, result, false, proc.Mp())
   450  	}
   451  
   452  	switch numVal.ValType {
   453  	case tree.P_null:
   454  		err = vector.AppendFixed[types.Time](vec, 0, true, proc.GetMPool())
   455  
   456  	case tree.P_int64:
   457  		val, ok := constant.Int64Val(numVal.Value)
   458  		if !ok {
   459  			return false, moerr.NewInvalidInput(proc.Ctx, "invalid int value '%s'", numVal.Value.String())
   460  		}
   461  		err = appendIntegerTime(val)
   462  
   463  	case tree.P_uint64:
   464  		val, ok := constant.Uint64Val(numVal.Value)
   465  		if !ok {
   466  			return false, moerr.NewInvalidInput(proc.Ctx, "invalid uint value '%s'", numVal.Value.String())
   467  		}
   468  		err = appendIntegerTime(int64(val))
   469  
   470  	case tree.P_decimal:
   471  		canInsert = false
   472  
   473  	case tree.P_float64:
   474  		canInsert = false
   475  
   476  	case tree.P_hexnum:
   477  		var val uint64
   478  		if val, err = HexToInt(numVal.OrigString()); err != nil {
   479  			return false, err
   480  		}
   481  		err = appendIntegerTime(int64(val))
   482  
   483  	case tree.P_char:
   484  		s := numVal.OrigString()
   485  		if len(s) == 0 {
   486  			err = vector.AppendFixed[types.Time](vec, 0, true, proc.GetMPool())
   487  		} else {
   488  			typ := vec.GetType()
   489  			var val types.Time
   490  			val, err = types.ParseTime(s, typ.Scale)
   491  			if err != nil {
   492  				return
   493  			}
   494  			err = vector.AppendFixed(vec, val, false, proc.Mp())
   495  		}
   496  
   497  	case tree.P_bit:
   498  		var val uint64
   499  		if val, err = BinaryToInt(numVal.OrigString()); err != nil {
   500  			return false, err
   501  		}
   502  		err = appendIntegerTime(int64(val))
   503  
   504  	case tree.P_nulltext:
   505  		err = vector.AppendFixed[types.Time](vec, 0, true, proc.GetMPool())
   506  
   507  	default:
   508  		canInsert = false
   509  	}
   510  	return
   511  }
   512  
   513  func setInsertValueDate(proc *process.Process, numVal *tree.NumVal, vec *vector.Vector) (canInsert bool, err error) {
   514  	canInsert = true
   515  
   516  	switch numVal.ValType {
   517  	case tree.P_null:
   518  		err = vector.AppendFixed[types.Date](vec, 0, true, proc.GetMPool())
   519  
   520  	case tree.P_int64:
   521  		canInsert = false
   522  
   523  	case tree.P_uint64:
   524  		canInsert = false
   525  
   526  	case tree.P_decimal:
   527  		canInsert = false
   528  
   529  	case tree.P_float64:
   530  		canInsert = false
   531  
   532  	case tree.P_hexnum:
   533  		canInsert = false
   534  
   535  	case tree.P_char:
   536  		s := numVal.OrigString()
   537  		var val types.Date
   538  		if len(s) == 0 {
   539  			err = vector.AppendFixed[types.Date](vec, 0, true, proc.GetMPool())
   540  		} else {
   541  			val, err = types.ParseDateCast(s)
   542  			if err != nil {
   543  				return
   544  			}
   545  			err = vector.AppendFixed(vec, val, false, proc.Mp())
   546  		}
   547  
   548  	case tree.P_bool:
   549  		canInsert = false
   550  	case tree.P_ScoreBinary:
   551  		canInsert = false
   552  	case tree.P_bit:
   553  		canInsert = false
   554  	case tree.P_nulltext:
   555  		err = vector.AppendFixed[types.Time](vec, 0, true, proc.GetMPool())
   556  	default:
   557  		canInsert = false
   558  	}
   559  	return
   560  }
   561  
   562  func setInsertValueUuid(proc *process.Process, numVal *tree.NumVal, vec *vector.Vector) (canInsert bool, err error) {
   563  	canInsert = true
   564  
   565  	switch numVal.ValType {
   566  	case tree.P_null:
   567  		err = vector.AppendFixed[types.Uuid](vec, types.Uuid{}, true, proc.GetMPool())
   568  
   569  	case tree.P_int64:
   570  		canInsert = false
   571  
   572  	case tree.P_uint64:
   573  		canInsert = false
   574  
   575  	case tree.P_decimal:
   576  		canInsert = false
   577  
   578  	case tree.P_float64:
   579  		canInsert = false
   580  
   581  	case tree.P_hexnum:
   582  		canInsert = false
   583  
   584  	case tree.P_char:
   585  		s := numVal.OrigString()
   586  		var val types.Uuid
   587  		val, err = types.ParseUuid(s)
   588  		if err != nil {
   589  			return
   590  		}
   591  		err = vector.AppendFixed(vec, val, false, proc.Mp())
   592  
   593  	case tree.P_bool:
   594  		canInsert = false
   595  	case tree.P_ScoreBinary:
   596  		canInsert = false
   597  	case tree.P_bit:
   598  		canInsert = false
   599  	case tree.P_nulltext:
   600  		err = vector.AppendFixed[types.Timestamp](vec, 0, true, proc.GetMPool())
   601  	default:
   602  		canInsert = false
   603  	}
   604  	return
   605  }
   606  
   607  func setInsertValueBool(proc *process.Process, numVal *tree.NumVal, vec *vector.Vector) (canInsert bool, err error) {
   608  	canInsert = true
   609  	switch numVal.ValType {
   610  	case tree.P_null:
   611  		err = vector.AppendBytes(vec, nil, true, proc.Mp())
   612  
   613  	case tree.P_bool:
   614  		val := constant.BoolVal(numVal.Value)
   615  		err = vector.AppendFixed[bool](vec, val, false, proc.Mp())
   616  
   617  	case tree.P_int64:
   618  		val, ok := constant.Int64Val(numVal.Value)
   619  		if !ok {
   620  			return false, moerr.NewInvalidInput(proc.Ctx, "invalid int value '%s'", numVal.Value.String())
   621  		}
   622  		err = vector.AppendFixed[bool](vec, val == 1, false, proc.Mp())
   623  
   624  	case tree.P_uint64:
   625  		val, ok := constant.Uint64Val(numVal.Value)
   626  		if !ok {
   627  			return false, moerr.NewInvalidInput(proc.Ctx, "invalid uint value '%s'", numVal.Value.String())
   628  		}
   629  		err = vector.AppendFixed[bool](vec, val == 1, false, proc.Mp())
   630  
   631  	case tree.P_decimal:
   632  		canInsert = false
   633  	case tree.P_float64:
   634  		canInsert = false
   635  	case tree.P_hexnum:
   636  		canInsert = false
   637  	case tree.P_ScoreBinary:
   638  		canInsert = false
   639  	case tree.P_bit:
   640  		canInsert = false
   641  	case tree.P_char:
   642  		originStr := numVal.OrigString()
   643  		if len(originStr) == 4 && strings.ToLower(originStr) == "true" {
   644  			err = vector.AppendFixed[bool](vec, true, false, proc.Mp())
   645  		} else {
   646  			err = vector.AppendFixed[bool](vec, false, false, proc.Mp())
   647  		}
   648  
   649  	case tree.P_nulltext:
   650  		err = vector.AppendBytes(vec, nil, true, proc.Mp())
   651  	default:
   652  		canInsert = false
   653  	}
   654  	return
   655  }
   656  
   657  func setInsertValueString(proc *process.Process, numVal *tree.NumVal, vec *vector.Vector) (canInsert bool, err error) {
   658  	canInsert = true
   659  
   660  	checkStrLen := func(s string) ([]byte, error) {
   661  		typ := vec.GetType()
   662  		destLen := int(typ.Width)
   663  		if typ.Oid != types.T_text && typ.Oid != types.T_binary && destLen != 0 && !typ.Oid.IsArrayRelate() {
   664  			if utf8.RuneCountInString(s) > destLen {
   665  				return nil, function.FormatCastErrorForInsertValue(proc.Ctx, s, *typ, fmt.Sprintf("Src length %v is larger than Dest length %v", len(s), destLen))
   666  			}
   667  		}
   668  		var v []byte
   669  		if typ.Oid.IsArrayRelate() {
   670  			// Assuming that input s is of type "[1,2,3]"
   671  
   672  			switch typ.Oid {
   673  			case types.T_array_float32:
   674  				_v, err := types.StringToArray[float32](s)
   675  				if err != nil {
   676  					return nil, err
   677  				}
   678  
   679  				if len(_v) != destLen {
   680  					return nil, moerr.NewArrayDefMismatchNoCtx(int(typ.Width), len(_v))
   681  				}
   682  
   683  				v = types.ArrayToBytes[float32](_v)
   684  
   685  			case types.T_array_float64:
   686  				_v, err := types.StringToArray[float64](s)
   687  				if err != nil {
   688  					return nil, err
   689  				}
   690  
   691  				if len(_v) != destLen {
   692  					return nil, moerr.NewArrayDefMismatchNoCtx(int(typ.Width), len(_v))
   693  				}
   694  
   695  				v = types.ArrayToBytes[float64](_v)
   696  			default:
   697  				return nil, moerr.NewInternalErrorNoCtx("%s is not supported array type", typ.String())
   698  
   699  			}
   700  
   701  		} else {
   702  			v = []byte(s)
   703  		}
   704  
   705  		if typ.Oid == types.T_binary && len(v) < int(typ.Width) {
   706  			add0 := int(typ.Width) - len(v)
   707  			for ; add0 != 0; add0-- {
   708  				v = append(v, 0)
   709  			}
   710  		}
   711  		return v, nil
   712  	}
   713  
   714  	switch numVal.ValType {
   715  	case tree.P_null:
   716  		err = vector.AppendBytes(vec, nil, true, proc.Mp())
   717  
   718  	case tree.P_bool:
   719  		var s string
   720  		if constant.BoolVal(numVal.Value) {
   721  			s = "1"
   722  		} else {
   723  			s = "0"
   724  		}
   725  		var val []byte
   726  		val, err = checkStrLen(s)
   727  		if err != nil {
   728  			return
   729  		}
   730  		err = vector.AppendBytes(vec, val, false, proc.Mp())
   731  
   732  	case tree.P_int64, tree.P_uint64, tree.P_char, tree.P_decimal, tree.P_float64:
   733  		s := numVal.OrigString()
   734  		var val []byte
   735  		val, err = checkStrLen(s)
   736  		if err != nil {
   737  			return
   738  		}
   739  		err = vector.AppendBytes(vec, val, false, proc.Mp())
   740  
   741  	case tree.P_hexnum:
   742  		s := numVal.OrigString()[2:]
   743  		var val []byte
   744  		if val, err = hex.DecodeString(s); err != nil {
   745  			return
   746  		}
   747  		err = vector.AppendBytes(vec, val, false, proc.Mp())
   748  
   749  	case tree.P_bit:
   750  		s := numVal.OrigString()[2:]
   751  		var val []byte
   752  		if val, err = DecodeBinaryString(s); err != nil {
   753  			return
   754  		}
   755  		err = vector.AppendBytes(vec, val, false, proc.Mp())
   756  
   757  	case tree.P_nulltext:
   758  		err = vector.AppendBytes(vec, nil, true, proc.Mp())
   759  
   760  	default:
   761  		canInsert = false
   762  	}
   763  	return
   764  }
   765  
   766  func setInsertValueJSON(proc *process.Process, numVal *tree.NumVal, vec *vector.Vector) (canInsert bool, err error) {
   767  	canInsert = true
   768  	switch numVal.ValType {
   769  	case tree.P_null:
   770  		err = vector.AppendBytes(vec, nil, true, proc.Mp())
   771  	default:
   772  		var json bytejson.ByteJson
   773  		originStr := numVal.OrigString()
   774  		json, err = types.ParseStringToByteJson(originStr)
   775  		if err != nil {
   776  			return false, err
   777  		}
   778  		var val []byte
   779  		val, err = types.EncodeJson(json)
   780  		if err != nil {
   781  			return false, err
   782  		}
   783  		err = vector.AppendBytes(vec, val, false, proc.Mp())
   784  	}
   785  	return
   786  }
   787  
   788  func checkOverFlow[T1, T2 constraints.Integer | constraints.Float](ctx context.Context, typ *types.Type, val T1, n *nulls.Nulls) error {
   789  	if typ.Scale >= 0 && typ.Width > 0 {
   790  		var max_value float64
   791  		if typ.Oid == types.T_float32 || typ.Oid == types.T_float64 {
   792  			pow := math.Pow10(int(typ.Scale))
   793  			max_value = math.Pow10(int(typ.Width - typ.Scale))
   794  			max_value -= 1.0 / pow
   795  		} else {
   796  			max_value = math.Pow10(int(typ.Width-typ.Scale)) - 1
   797  		}
   798  		if float64(val) < -max_value || float64(val) > max_value {
   799  			return moerr.NewOutOfRange(ctx, "float", "value '%v'", val)
   800  		}
   801  	} else {
   802  		return function.OverflowForNumericToNumeric[T1, T2](ctx, []T1{val}, n)
   803  	}
   804  	return nil
   805  }
   806  
   807  func setInsertValueNumber[T constraints.Integer | constraints.Float](proc *process.Process, numVal *tree.NumVal, vec *vector.Vector) (canInsert bool, err error) {
   808  	canInsert = true
   809  	switch numVal.ValType {
   810  	case tree.P_null:
   811  		err = vector.AppendBytes(vec, nil, true, proc.Mp())
   812  
   813  	case tree.P_bool:
   814  		val := constant.BoolVal(numVal.Value)
   815  		if val {
   816  			err = vector.AppendFixed(vec, T(1), false, proc.Mp())
   817  		} else {
   818  			err = vector.AppendFixed(vec, T(0), false, proc.Mp())
   819  		}
   820  		vec.GetType()
   821  
   822  	case tree.P_int64:
   823  		val, ok := constant.Int64Val(numVal.Value)
   824  		if !ok {
   825  			return false, moerr.NewInvalidInput(proc.Ctx, "invalid int value '%s'", numVal.Value.String())
   826  		}
   827  		err = checkOverFlow[int64, T](proc.Ctx, vec.GetType(), val, vec.GetNulls())
   828  		if err != nil {
   829  			return false, err
   830  		}
   831  		err = vector.AppendFixed(vec, T(val), false, proc.Mp())
   832  
   833  	case tree.P_uint64:
   834  		val, ok := constant.Uint64Val(numVal.Value)
   835  		if !ok {
   836  			return false, moerr.NewInvalidInput(proc.Ctx, "invalid uint value '%s'", numVal.Value.String())
   837  		}
   838  		err = checkOverFlow[uint64, T](proc.Ctx, vec.GetType(), val, vec.GetNulls())
   839  		if err != nil {
   840  			return false, err
   841  		}
   842  		err = vector.AppendFixed(vec, T(val), false, proc.Mp())
   843  
   844  	case tree.P_float64:
   845  		val, ok := constant.Float64Val(numVal.Value)
   846  		if canInsert = ok; canInsert {
   847  			var v T
   848  			if err = checkOverFlow[float64, T](proc.Ctx, vec.GetType(), val,
   849  				vec.GetNulls()); err != nil {
   850  				return false, err
   851  			}
   852  			if vec.GetType().Scale < 0 || vec.GetType().Width == 0 {
   853  				v = T(val)
   854  			} else {
   855  				v, err = floatNumToFixFloat[T](val, numVal.OrigString(), vec.GetType())
   856  				if err != nil {
   857  					return false, err
   858  				}
   859  			}
   860  			if err = vector.AppendFixed(vec, v, false, proc.Mp()); err != nil {
   861  				return false, err
   862  			}
   863  		}
   864  
   865  	case tree.P_hexnum:
   866  		var val uint64
   867  		if val, err = HexToInt(numVal.OrigString()); err != nil {
   868  			return false, err
   869  		}
   870  		if err = checkOverFlow[uint64, T](proc.Ctx, vec.GetType(), val, vec.GetNulls()); err != nil {
   871  			return false, err
   872  		}
   873  		err = vector.AppendFixed(vec, T(val), false, proc.Mp())
   874  
   875  	case tree.P_bit:
   876  		var val uint64
   877  		if val, err = BinaryToInt(numVal.OrigString()); err != nil {
   878  			return false, err
   879  		}
   880  		if err = checkOverFlow[uint64, T](proc.Ctx, vec.GetType(), val, vec.GetNulls()); err != nil {
   881  			return false, err
   882  		}
   883  		err = vector.AppendFixed(vec, T(val), false, proc.Mp())
   884  
   885  	case tree.P_nulltext:
   886  		err = vector.AppendBytes(vec, nil, true, proc.Mp())
   887  
   888  	default:
   889  		canInsert = false
   890  	}
   891  	return
   892  }
   893  
   894  func setInsertValueDecimal64(proc *process.Process, numVal *tree.NumVal, vec *vector.Vector) (canInsert bool, err error) {
   895  	canInsert = true
   896  	appendWithStr := func(str string) error {
   897  		typ := vec.GetType()
   898  		result, err := types.ParseDecimal64(str, typ.Width, typ.Scale)
   899  		if err != nil {
   900  			return err
   901  		}
   902  		return vector.AppendFixed(vec, result, false, proc.Mp())
   903  	}
   904  	appendWithUnSigned := func(v uint64) error {
   905  		typ := vec.GetType()
   906  		result, _ := types.Decimal64(v).Scale(typ.Scale)
   907  		return vector.AppendFixed(vec, result, false, proc.Mp())
   908  	}
   909  
   910  	switch numVal.ValType {
   911  	case tree.P_null:
   912  		err = vector.AppendBytes(vec, nil, true, proc.Mp())
   913  
   914  	case tree.P_int64:
   915  		val, ok := constant.Int64Val(numVal.Value)
   916  		if !ok {
   917  			return false, moerr.NewInvalidInput(proc.Ctx, "invalid int value '%s'", numVal.Value.String())
   918  		}
   919  		err = appendWithUnSigned(uint64(val))
   920  
   921  	case tree.P_uint64:
   922  		val, ok := constant.Uint64Val(numVal.Value)
   923  		if !ok {
   924  			return false, moerr.NewInvalidInput(proc.Ctx, "invalid uint value '%s'", numVal.Value.String())
   925  		}
   926  		err = appendWithUnSigned(uint64(val))
   927  
   928  	case tree.P_decimal, tree.P_char, tree.P_float64:
   929  		originStr := numVal.OrigString()
   930  		err = appendWithStr(originStr)
   931  
   932  	case tree.P_hexnum:
   933  		var val uint64
   934  		if val, err = HexToInt(numVal.OrigString()); err != nil {
   935  			return false, err
   936  		}
   937  		err = appendWithUnSigned(val)
   938  
   939  	case tree.P_bit:
   940  		var val uint64
   941  		if val, err = BinaryToInt(numVal.OrigString()); err != nil {
   942  			return false, err
   943  		}
   944  		err = appendWithUnSigned(val)
   945  
   946  	case tree.P_nulltext:
   947  		err = vector.AppendBytes(vec, nil, true, proc.Mp())
   948  
   949  	default:
   950  		canInsert = false
   951  	}
   952  	return
   953  }
   954  
   955  func setInsertValueDecimal128(proc *process.Process, numVal *tree.NumVal, vec *vector.Vector) (canInsert bool, err error) {
   956  	canInsert = true
   957  	appendWithStr := func(str string) error {
   958  		typ := vec.GetType()
   959  		result, err := types.ParseDecimal128(str, typ.Width, typ.Scale)
   960  		if err != nil {
   961  			return err
   962  		}
   963  		return vector.AppendFixed(vec, result, false, proc.Mp())
   964  	}
   965  	appendWithUnSigned := func(v uint64) error {
   966  		typ := vec.GetType()
   967  		result := types.Decimal128{B0_63: v, B64_127: 0}
   968  		result, _ = result.Scale(typ.Scale)
   969  		return vector.AppendFixed(vec, result, false, proc.Mp())
   970  	}
   971  
   972  	switch numVal.ValType {
   973  	case tree.P_null:
   974  		err = vector.AppendBytes(vec, nil, true, proc.Mp())
   975  
   976  	case tree.P_int64:
   977  		val, ok := constant.Int64Val(numVal.Value)
   978  		if !ok {
   979  			return false, moerr.NewInvalidInput(proc.Ctx, "invalid int value '%s'", numVal.Value.String())
   980  		}
   981  		err = appendWithUnSigned(uint64(val))
   982  
   983  	case tree.P_uint64:
   984  		val, ok := constant.Uint64Val(numVal.Value)
   985  		if !ok {
   986  			return false, moerr.NewInvalidInput(proc.Ctx, "invalid uint value '%s'", numVal.Value.String())
   987  		}
   988  		err = appendWithUnSigned(uint64(val))
   989  
   990  	case tree.P_decimal, tree.P_char, tree.P_float64:
   991  		originStr := numVal.OrigString()
   992  		err = appendWithStr(originStr)
   993  
   994  	case tree.P_hexnum:
   995  		var val uint64
   996  		if val, err = HexToInt(numVal.OrigString()); err != nil {
   997  			return false, err
   998  		}
   999  		err = appendWithUnSigned(val)
  1000  
  1001  	case tree.P_bit:
  1002  		var val uint64
  1003  		if val, err = BinaryToInt(numVal.OrigString()); err != nil {
  1004  			return false, err
  1005  		}
  1006  		err = appendWithUnSigned(val)
  1007  
  1008  	case tree.P_nulltext:
  1009  		err = vector.AppendBytes(vec, nil, true, proc.Mp())
  1010  
  1011  	default:
  1012  		canInsert = false
  1013  	}
  1014  	return
  1015  }
  1016  
  1017  func floatNumToFixFloat[T constraints.Float | constraints.Integer](
  1018  	from float64, originStr string, typ *types.Type) (T, error) {
  1019  
  1020  	pow := math.Pow10(int(typ.Scale))
  1021  	max_value := math.Pow10(int(typ.Width - typ.Scale))
  1022  	max_value -= 1.0 / pow
  1023  
  1024  	tmp := math.Round((from-math.Floor(from))*pow) / pow
  1025  	v := math.Floor(from) + tmp
  1026  	if v < -max_value || v > max_value {
  1027  		if originStr == "" {
  1028  			return 0, moerr.NewOutOfRange(context.TODO(), "float", "value '%v'", from)
  1029  		} else {
  1030  			return 0, moerr.NewOutOfRange(context.TODO(), "float", "value '%s'", originStr)
  1031  		}
  1032  	}
  1033  	return T(v), nil
  1034  }
  1035  
  1036  func setInsertValueBit(proc *process.Process, numVal *tree.NumVal, vec *vector.Vector) (canInsert bool, err error) {
  1037  	var ok bool
  1038  	canInsert = true
  1039  	width := vec.GetType().Width
  1040  
  1041  	switch numVal.ValType {
  1042  	case tree.P_null:
  1043  		err = vector.AppendBytes(vec, nil, true, proc.Mp())
  1044  
  1045  	case tree.P_bool:
  1046  		var val uint64
  1047  		if constant.BoolVal(numVal.Value) {
  1048  			val = 1
  1049  		}
  1050  		err = vector.AppendFixed(vec, val, false, proc.Mp())
  1051  
  1052  	case tree.P_char:
  1053  		s := numVal.OrigString()
  1054  		if len(s) > 8 {
  1055  			err = moerr.NewInvalidInput(proc.Ctx, "data too long")
  1056  			return
  1057  		}
  1058  
  1059  		var val uint64
  1060  		for i := 0; i < len(s); i++ {
  1061  			val = (val << 8) | uint64(s[i])
  1062  		}
  1063  		if val > uint64(1<<width-1) {
  1064  			err = moerr.NewInvalidInput(proc.Ctx, "data too long, type width = %d, val = %b", width, val)
  1065  			return
  1066  		}
  1067  		err = vector.AppendFixed(vec, val, false, proc.Mp())
  1068  
  1069  	case tree.P_float64:
  1070  		var val float64
  1071  		if val, ok = constant.Float64Val(numVal.Value); !ok {
  1072  			err = moerr.NewInvalidInput(proc.Ctx, "invalid float value '%s'", numVal.Value.String())
  1073  			return
  1074  		} else if val < 0 {
  1075  			err = moerr.NewInvalidInput(proc.Ctx, "unsupported negative value %v", val)
  1076  			return
  1077  		} else if uint64(math.Round(val)) > uint64(1<<width-1) {
  1078  			err = moerr.NewInvalidInput(proc.Ctx, "data too long, type width = %d, val = %b", width, val)
  1079  			return
  1080  		}
  1081  		err = vector.AppendFixed(vec, uint64(math.Round(val)), false, proc.Mp())
  1082  
  1083  	case tree.P_int64:
  1084  		var val int64
  1085  		if val, ok = constant.Int64Val(numVal.Value); !ok {
  1086  			err = moerr.NewInvalidInput(proc.Ctx, "invalid int value '%s'", numVal.Value.String())
  1087  			return
  1088  		} else if val < 0 {
  1089  			err = moerr.NewInvalidInput(proc.Ctx, "unsupported negative value %d", val)
  1090  			return
  1091  		} else if uint64(val) > uint64(1<<width-1) {
  1092  			err = moerr.NewInvalidInput(proc.Ctx, "data too long, type width = %d, val = %b", width, val)
  1093  			return
  1094  		}
  1095  		err = vector.AppendFixed(vec, uint64(val), false, proc.Mp())
  1096  
  1097  	case tree.P_uint64:
  1098  		var val uint64
  1099  		if val, ok = constant.Uint64Val(numVal.Value); !ok {
  1100  			err = moerr.NewInvalidInput(proc.Ctx, "invalid int value '%s'", numVal.Value.String())
  1101  			return
  1102  		} else if val > uint64(1<<width-1) {
  1103  			err = moerr.NewInvalidInput(proc.Ctx, "data too long, type width = %d, val = %b", width, val)
  1104  			return
  1105  		}
  1106  		err = vector.AppendFixed(vec, val, false, proc.Mp())
  1107  
  1108  	case tree.P_hexnum:
  1109  		var val uint64
  1110  		if val, err = HexToInt(numVal.OrigString()); err != nil {
  1111  			return
  1112  		} else if val > uint64(1<<width-1) {
  1113  			err = moerr.NewInvalidInput(proc.Ctx, "data too long, type width = %d, val = %b", width, val)
  1114  			return
  1115  		}
  1116  		err = vector.AppendFixed(vec, val, false, proc.Mp())
  1117  
  1118  	case tree.P_bit:
  1119  		var val uint64
  1120  		if val, err = BinaryToInt(numVal.OrigString()); err != nil {
  1121  			return
  1122  		} else if val > uint64(1<<width-1) {
  1123  			err = moerr.NewInvalidInput(proc.Ctx, "data too long, type width = %d, val = %b", width, val)
  1124  			return
  1125  		}
  1126  		err = vector.AppendFixed(vec, val, false, proc.Mp())
  1127  
  1128  	case tree.P_ScoreBinary:
  1129  		var val uint64
  1130  		if val, err = ScoreBinaryToInt(numVal.OrigString()); err != nil {
  1131  			return
  1132  		} else if val > uint64(1<<width-1) {
  1133  			err = moerr.NewInvalidInput(proc.Ctx, "data too long, type width = %d, val = %b", width, val)
  1134  			return
  1135  		}
  1136  		err = vector.AppendFixed(vec, val, false, proc.Mp())
  1137  
  1138  	default:
  1139  		canInsert = false
  1140  	}
  1141  	return
  1142  }