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

     1  // Copyright 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  	"context"
    19  	"encoding/hex"
    20  	"fmt"
    21  	"math"
    22  	"slices"
    23  	"strconv"
    24  	"strings"
    25  	"time"
    26  	"unicode/utf8"
    27  	"unsafe"
    28  
    29  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    30  	"github.com/matrixorigin/matrixone/pkg/common/util"
    31  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    32  	"github.com/matrixorigin/matrixone/pkg/container/types"
    33  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    34  	"github.com/matrixorigin/matrixone/pkg/vectorize/moarray"
    35  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    36  	"golang.org/x/exp/constraints"
    37  )
    38  
    39  // XXX need this one to make a pretty function register.
    40  var supportedTypeCast = map[types.T][]types.T{
    41  	types.T_any: {
    42  		types.T_bool,
    43  		types.T_bit,
    44  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
    45  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
    46  		types.T_char, types.T_varchar, types.T_blob, types.T_text, types.T_json,
    47  		types.T_binary, types.T_varbinary,
    48  		types.T_float32, types.T_float64,
    49  		types.T_decimal64, types.T_decimal128,
    50  		types.T_date, types.T_datetime,
    51  		types.T_time, types.T_timestamp,
    52  		types.T_array_float32, types.T_array_float64,
    53  	},
    54  
    55  	types.T_bool: {
    56  		types.T_bool,
    57  		types.T_bit,
    58  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
    59  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
    60  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
    61  		types.T_binary, types.T_varbinary,
    62  	},
    63  
    64  	types.T_bit: {
    65  		types.T_bool,
    66  		types.T_bit,
    67  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
    68  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
    69  		types.T_float32, types.T_float64,
    70  		types.T_decimal64, types.T_decimal128,
    71  		types.T_time, types.T_timestamp,
    72  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
    73  		types.T_binary, types.T_varbinary, types.T_enum,
    74  	},
    75  
    76  	types.T_int8: {
    77  		types.T_bool,
    78  		types.T_bit,
    79  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
    80  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
    81  		types.T_float32, types.T_float64,
    82  		types.T_decimal64, types.T_decimal128,
    83  		types.T_time, types.T_timestamp,
    84  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
    85  		types.T_binary, types.T_varbinary,
    86  	},
    87  
    88  	types.T_int16: {
    89  		types.T_bool,
    90  		types.T_bit,
    91  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
    92  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
    93  		types.T_float32, types.T_float64,
    94  		types.T_decimal64, types.T_decimal128,
    95  		types.T_time, types.T_timestamp,
    96  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
    97  		types.T_binary, types.T_varbinary,
    98  	},
    99  
   100  	types.T_int32: {
   101  		types.T_bool,
   102  		types.T_bit,
   103  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   104  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   105  		types.T_float32, types.T_float64,
   106  		types.T_decimal64, types.T_decimal128,
   107  		types.T_time, types.T_timestamp,
   108  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   109  		types.T_binary, types.T_varbinary,
   110  	},
   111  
   112  	types.T_int64: {
   113  		types.T_bool,
   114  		types.T_bit,
   115  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   116  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   117  		types.T_float32, types.T_float64,
   118  		types.T_decimal64, types.T_decimal128,
   119  		types.T_time, types.T_timestamp,
   120  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   121  		types.T_binary, types.T_varbinary, types.T_enum,
   122  	},
   123  
   124  	types.T_uint8: {
   125  		types.T_bool,
   126  		types.T_bit,
   127  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   128  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   129  		types.T_float32, types.T_float64,
   130  		types.T_decimal64, types.T_decimal128,
   131  		types.T_time, types.T_timestamp,
   132  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   133  		types.T_binary, types.T_varbinary, types.T_enum,
   134  	},
   135  
   136  	types.T_uint16: {
   137  		types.T_bool,
   138  		types.T_bit,
   139  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   140  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   141  		types.T_float32, types.T_float64,
   142  		types.T_decimal64, types.T_decimal128,
   143  		types.T_time, types.T_timestamp,
   144  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   145  		types.T_binary, types.T_varbinary, types.T_enum,
   146  	},
   147  
   148  	types.T_uint32: {
   149  		types.T_bool,
   150  		types.T_bit,
   151  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   152  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   153  		types.T_float32, types.T_float64,
   154  		types.T_decimal64, types.T_decimal128,
   155  		types.T_time, types.T_timestamp,
   156  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   157  		types.T_binary, types.T_varbinary, types.T_enum,
   158  	},
   159  
   160  	types.T_uint64: {
   161  		types.T_bool,
   162  		types.T_bit,
   163  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   164  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   165  		types.T_float32, types.T_float64,
   166  		types.T_decimal64, types.T_decimal128,
   167  		types.T_time, types.T_timestamp,
   168  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   169  		types.T_binary, types.T_varbinary, types.T_enum,
   170  	},
   171  
   172  	types.T_float32: {
   173  		types.T_bool,
   174  		types.T_bit,
   175  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   176  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   177  		types.T_float32, types.T_float64,
   178  		types.T_decimal64, types.T_decimal128,
   179  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   180  		types.T_binary, types.T_varbinary,
   181  	},
   182  
   183  	types.T_float64: {
   184  		types.T_bool,
   185  		types.T_bit,
   186  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   187  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   188  		types.T_float32, types.T_float64,
   189  		types.T_decimal64, types.T_decimal128,
   190  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   191  		types.T_binary, types.T_varbinary,
   192  	},
   193  
   194  	types.T_date: {
   195  		types.T_int32, types.T_int64,
   196  		types.T_date, types.T_datetime,
   197  		types.T_time, types.T_timestamp,
   198  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   199  		types.T_binary, types.T_varbinary,
   200  	},
   201  
   202  	types.T_datetime: {
   203  		types.T_int32, types.T_int64,
   204  		types.T_date, types.T_datetime,
   205  		types.T_time, types.T_timestamp,
   206  		types.T_decimal64, types.T_decimal128,
   207  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   208  		types.T_binary, types.T_varbinary,
   209  	},
   210  
   211  	types.T_timestamp: {
   212  		types.T_int32, types.T_int64,
   213  		types.T_date, types.T_datetime,
   214  		types.T_timestamp,
   215  		types.T_decimal64, types.T_decimal128,
   216  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   217  		types.T_binary, types.T_varbinary,
   218  	},
   219  
   220  	types.T_time: {
   221  		types.T_date, types.T_datetime,
   222  		types.T_time,
   223  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   224  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   225  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   226  		types.T_decimal64, types.T_decimal128,
   227  		types.T_binary, types.T_varbinary,
   228  		types.T_bit,
   229  	},
   230  
   231  	types.T_decimal64: {
   232  		types.T_bit,
   233  		types.T_float32, types.T_float64,
   234  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   235  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   236  		types.T_decimal64, types.T_decimal128,
   237  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   238  		types.T_binary, types.T_varbinary,
   239  		types.T_time, types.T_timestamp,
   240  	},
   241  
   242  	types.T_decimal128: {
   243  		types.T_bit,
   244  		types.T_float32, types.T_float64,
   245  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   246  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   247  		types.T_decimal64, types.T_decimal128,
   248  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   249  		types.T_binary, types.T_varbinary,
   250  	},
   251  
   252  	types.T_char: {
   253  		types.T_bit,
   254  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   255  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   256  		types.T_float32, types.T_float64,
   257  		types.T_decimal64, types.T_decimal128,
   258  		types.T_bool,
   259  		types.T_json,
   260  		types.T_uuid,
   261  		types.T_date, types.T_datetime,
   262  		types.T_time, types.T_timestamp,
   263  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   264  		types.T_binary, types.T_varbinary,
   265  	},
   266  
   267  	types.T_varchar: {
   268  		types.T_bit,
   269  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   270  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   271  		types.T_float32, types.T_float64,
   272  		types.T_decimal64, types.T_decimal128,
   273  		types.T_bool,
   274  		types.T_json,
   275  		types.T_uuid,
   276  		types.T_date, types.T_datetime,
   277  		types.T_time, types.T_timestamp,
   278  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   279  		types.T_binary, types.T_varbinary,
   280  		types.T_array_float32, types.T_array_float64,
   281  	},
   282  
   283  	types.T_binary: {
   284  		types.T_bit,
   285  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   286  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   287  		types.T_float32, types.T_float64,
   288  		types.T_decimal64, types.T_decimal128,
   289  		types.T_bool,
   290  		types.T_uuid,
   291  		types.T_date, types.T_datetime,
   292  		types.T_time, types.T_timestamp,
   293  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   294  		types.T_varbinary, types.T_binary,
   295  	},
   296  
   297  	types.T_varbinary: {
   298  		types.T_bit,
   299  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   300  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   301  		types.T_float32, types.T_float64,
   302  		types.T_decimal64, types.T_decimal128,
   303  		types.T_bool,
   304  		types.T_uuid,
   305  		types.T_date, types.T_datetime,
   306  		types.T_time, types.T_timestamp,
   307  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   308  		types.T_binary, types.T_varbinary,
   309  	},
   310  
   311  	types.T_blob: {
   312  		types.T_bit,
   313  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   314  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   315  		types.T_float32, types.T_float64,
   316  		types.T_decimal64, types.T_decimal128,
   317  		types.T_bool,
   318  		types.T_json,
   319  		types.T_uuid,
   320  		types.T_date, types.T_datetime,
   321  		types.T_time, types.T_timestamp,
   322  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   323  		types.T_binary, types.T_varbinary,
   324  		types.T_array_float32, types.T_array_float64,
   325  	},
   326  
   327  	types.T_text: {
   328  		types.T_bit,
   329  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   330  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   331  		types.T_float32, types.T_float64,
   332  		types.T_decimal64, types.T_decimal128,
   333  		types.T_bool,
   334  		types.T_json,
   335  		types.T_uuid,
   336  		types.T_date, types.T_datetime,
   337  		types.T_time, types.T_timestamp,
   338  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   339  		types.T_binary, types.T_varbinary,
   340  		types.T_array_float32, types.T_array_float64,
   341  	},
   342  
   343  	types.T_json: {
   344  		types.T_char, types.T_varchar, types.T_text,
   345  	},
   346  
   347  	types.T_uuid: {
   348  		types.T_char, types.T_varchar, types.T_blob,
   349  		types.T_binary, types.T_varbinary, types.T_text,
   350  	},
   351  
   352  	types.T_TS: {
   353  		types.T_TS,
   354  	},
   355  
   356  	types.T_Rowid: {
   357  		types.T_Rowid,
   358  	},
   359  
   360  	types.T_enum: {
   361  		types.T_enum, types.T_uint16, types.T_uint8, types.T_uint32, types.T_uint64, types.T_uint128,
   362  		types.T_char, types.T_varchar, types.T_blob,
   363  		types.T_binary, types.T_varbinary, types.T_text,
   364  	},
   365  
   366  	types.T_array_float32: {
   367  		types.T_array_float32, types.T_array_float64,
   368  	},
   369  	types.T_array_float64: {
   370  		types.T_array_float32, types.T_array_float64,
   371  	},
   372  }
   373  
   374  func IfTypeCastSupported(sourceType, targetType types.T) bool {
   375  	supportList, ok := supportedTypeCast[sourceType]
   376  	if ok {
   377  		for _, t := range supportList {
   378  			if t == targetType {
   379  				return true
   380  			}
   381  		}
   382  	}
   383  	return false
   384  }
   385  
   386  func NewCast(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   387  	var err error
   388  	// Cast Parameter1 as Type Parameter2
   389  	fromType := parameters[0].GetType()
   390  	toType := parameters[1].GetType()
   391  	from := parameters[0]
   392  	switch fromType.Oid {
   393  	case types.T_any: // scalar null
   394  		err = scalarNullToOthers(proc.Ctx, *toType, result, length)
   395  	case types.T_bool:
   396  		s := vector.GenerateFunctionFixedTypeParameter[bool](from)
   397  		err = boolToOthers(proc.Ctx, s, *toType, result, length)
   398  	case types.T_bit:
   399  		s := vector.GenerateFunctionFixedTypeParameter[uint64](from)
   400  		err = bitToOthers(proc.Ctx, s, *toType, result, length)
   401  	case types.T_int8:
   402  		s := vector.GenerateFunctionFixedTypeParameter[int8](from)
   403  		err = int8ToOthers(proc.Ctx, s, *toType, result, length)
   404  	case types.T_int16:
   405  		s := vector.GenerateFunctionFixedTypeParameter[int16](from)
   406  		err = int16ToOthers(proc.Ctx, s, *toType, result, length)
   407  	case types.T_int32:
   408  		s := vector.GenerateFunctionFixedTypeParameter[int32](from)
   409  		err = int32ToOthers(proc.Ctx, s, *toType, result, length)
   410  	case types.T_int64:
   411  		s := vector.GenerateFunctionFixedTypeParameter[int64](from)
   412  		err = int64ToOthers(proc.Ctx, s, *toType, result, length)
   413  	case types.T_uint8:
   414  		s := vector.GenerateFunctionFixedTypeParameter[uint8](from)
   415  		err = uint8ToOthers(proc.Ctx, s, *toType, result, length)
   416  	case types.T_uint16:
   417  		s := vector.GenerateFunctionFixedTypeParameter[uint16](from)
   418  		err = uint16ToOthers(proc.Ctx, s, *toType, result, length)
   419  	case types.T_uint32:
   420  		s := vector.GenerateFunctionFixedTypeParameter[uint32](from)
   421  		err = uint32ToOthers(proc.Ctx, s, *toType, result, length)
   422  	case types.T_uint64:
   423  		s := vector.GenerateFunctionFixedTypeParameter[uint64](from)
   424  		err = uint64ToOthers(proc.Ctx, s, *toType, result, length)
   425  	case types.T_float32:
   426  		s := vector.GenerateFunctionFixedTypeParameter[float32](from)
   427  		err = float32ToOthers(proc.Ctx, s, *toType, result, length)
   428  	case types.T_float64:
   429  		s := vector.GenerateFunctionFixedTypeParameter[float64](from)
   430  		err = float64ToOthers(proc.Ctx, s, *toType, result, length)
   431  	case types.T_decimal64:
   432  		s := vector.GenerateFunctionFixedTypeParameter[types.Decimal64](from)
   433  		err = decimal64ToOthers(proc.Ctx, s, *toType, result, length)
   434  	case types.T_decimal128:
   435  		s := vector.GenerateFunctionFixedTypeParameter[types.Decimal128](from)
   436  		err = decimal128ToOthers(proc.Ctx, s, *toType, result, length)
   437  	case types.T_date:
   438  		s := vector.GenerateFunctionFixedTypeParameter[types.Date](from)
   439  		err = dateToOthers(proc, s, *toType, result, length)
   440  	case types.T_datetime:
   441  		s := vector.GenerateFunctionFixedTypeParameter[types.Datetime](from)
   442  		err = datetimeToOthers(proc, s, *toType, result, length)
   443  	case types.T_time:
   444  		s := vector.GenerateFunctionFixedTypeParameter[types.Time](from)
   445  		err = timeToOthers(proc.Ctx, s, *toType, result, length)
   446  	case types.T_timestamp:
   447  		s := vector.GenerateFunctionFixedTypeParameter[types.Timestamp](from)
   448  		err = timestampToOthers(proc, s, *toType, result, length)
   449  	case types.T_char, types.T_varchar, types.T_binary, types.T_varbinary, types.T_blob, types.T_text:
   450  		s := vector.GenerateFunctionStrParameter(from)
   451  		err = strTypeToOthers(proc, s, *toType, result, length)
   452  	case types.T_array_float32, types.T_array_float64:
   453  		//NOTE: Don't mix T_array and T_varchar.
   454  		// T_varchar will have "[1,2,3]" string
   455  		// T_array will have "@@@#@!#@!@#!" binary.
   456  		s := vector.GenerateFunctionStrParameter(from)
   457  		err = arrayTypeToOthers(proc, s, *toType, result, length)
   458  	case types.T_uuid:
   459  		s := vector.GenerateFunctionFixedTypeParameter[types.Uuid](from)
   460  		err = uuidToOthers(proc.Ctx, s, *toType, result, length)
   461  	case types.T_TS:
   462  		s := vector.GenerateFunctionFixedTypeParameter[types.TS](from)
   463  		err = tsToOthers(proc.Ctx, s, *toType, result, length)
   464  	case types.T_Rowid:
   465  		s := vector.GenerateFunctionFixedTypeParameter[types.Rowid](from)
   466  		err = rowidToOthers(proc.Ctx, s, *toType, result, length)
   467  	case types.T_Blockid:
   468  		s := vector.GenerateFunctionFixedTypeParameter[types.Blockid](from)
   469  		err = blockidToOthers(proc.Ctx, s, *toType, result, length)
   470  	case types.T_json:
   471  		s := vector.GenerateFunctionStrParameter(from)
   472  		err = jsonToOthers(proc.Ctx, s, *toType, result, length)
   473  	case types.T_enum:
   474  		s := vector.GenerateFunctionFixedTypeParameter[types.Enum](from)
   475  		err = enumToOthers(proc.Ctx, s, *toType, result, length)
   476  	default:
   477  		// XXX we set the function here to adapt to the BVT cases.
   478  		err = formatCastError(proc.Ctx, from, *toType, "")
   479  	}
   480  	return err
   481  }
   482  
   483  func scalarNullToOthers(ctx context.Context,
   484  	totype types.Type, result vector.FunctionResultWrapper, length int) error {
   485  	switch totype.Oid {
   486  	case types.T_bool:
   487  		return appendNulls[bool](result, length)
   488  	case types.T_bit:
   489  		return appendNulls[uint64](result, length)
   490  	case types.T_int8:
   491  		return appendNulls[int8](result, length)
   492  	case types.T_int16:
   493  		return appendNulls[int16](result, length)
   494  	case types.T_int32:
   495  		return appendNulls[int32](result, length)
   496  	case types.T_int64:
   497  		return appendNulls[int64](result, length)
   498  	case types.T_uint8:
   499  		return appendNulls[uint8](result, length)
   500  	case types.T_uint16:
   501  		return appendNulls[uint16](result, length)
   502  	case types.T_uint32:
   503  		return appendNulls[uint32](result, length)
   504  	case types.T_uint64:
   505  		return appendNulls[uint64](result, length)
   506  	case types.T_char, types.T_varchar, types.T_blob,
   507  		types.T_binary, types.T_varbinary, types.T_text, types.T_json,
   508  		types.T_array_float32, types.T_array_float64:
   509  		return appendNulls[types.Varlena](result, length)
   510  	case types.T_float32:
   511  		return appendNulls[float32](result, length)
   512  	case types.T_float64:
   513  		return appendNulls[float64](result, length)
   514  	case types.T_decimal64:
   515  		return appendNulls[types.Decimal64](result, length)
   516  	case types.T_decimal128:
   517  		return appendNulls[types.Decimal128](result, length)
   518  	case types.T_date:
   519  		return appendNulls[types.Date](result, length)
   520  	case types.T_datetime:
   521  		return appendNulls[types.Datetime](result, length)
   522  	case types.T_time:
   523  		return appendNulls[types.Time](result, length)
   524  	case types.T_timestamp:
   525  		return appendNulls[types.Timestamp](result, length)
   526  	}
   527  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from NULL to %s", totype))
   528  }
   529  
   530  func boolToOthers(ctx context.Context,
   531  	source vector.FunctionParameterWrapper[bool],
   532  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
   533  	switch toType.Oid {
   534  	case types.T_bool:
   535  		rs := vector.MustFunctionResult[bool](result)
   536  		return rs.DupFromParameter(source, length)
   537  	case types.T_bit:
   538  		rs := vector.MustFunctionResult[uint64](result)
   539  		return boolToInteger(source, rs, length)
   540  	case types.T_char, types.T_varchar, types.T_binary,
   541  		types.T_varbinary, types.T_blob, types.T_text:
   542  		// string type.
   543  		rs := vector.MustFunctionResult[types.Varlena](result)
   544  		return boolToStr(source, rs, length, toType)
   545  	case types.T_int8:
   546  		rs := vector.MustFunctionResult[int8](result)
   547  		return boolToInteger(source, rs, length)
   548  	case types.T_int16:
   549  		rs := vector.MustFunctionResult[int16](result)
   550  		return boolToInteger(source, rs, length)
   551  	case types.T_int32:
   552  		rs := vector.MustFunctionResult[int32](result)
   553  		return boolToInteger(source, rs, length)
   554  	case types.T_int64:
   555  		rs := vector.MustFunctionResult[int64](result)
   556  		return boolToInteger(source, rs, length)
   557  	case types.T_uint8:
   558  		rs := vector.MustFunctionResult[uint8](result)
   559  		return boolToInteger(source, rs, length)
   560  	case types.T_uint16:
   561  		rs := vector.MustFunctionResult[uint16](result)
   562  		return boolToInteger(source, rs, length)
   563  	case types.T_uint32:
   564  		rs := vector.MustFunctionResult[uint32](result)
   565  		return boolToInteger(source, rs, length)
   566  	case types.T_uint64:
   567  		rs := vector.MustFunctionResult[uint64](result)
   568  		return boolToInteger(source, rs, length)
   569  	}
   570  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from bool to %s", toType))
   571  }
   572  
   573  func bitToOthers(ctx context.Context,
   574  	source vector.FunctionParameterWrapper[uint64],
   575  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
   576  	switch toType.Oid {
   577  	case types.T_bool:
   578  		rs := vector.MustFunctionResult[bool](result)
   579  		return numericToBool(source, rs, length)
   580  	case types.T_bit:
   581  		rs := vector.MustFunctionResult[uint64](result)
   582  		return numericToBit(ctx, source, rs, int(toType.Width), length)
   583  	case types.T_int8:
   584  		rs := vector.MustFunctionResult[int8](result)
   585  		return numericToNumeric(ctx, source, rs, length)
   586  	case types.T_int16:
   587  		rs := vector.MustFunctionResult[int16](result)
   588  		return numericToNumeric(ctx, source, rs, length)
   589  	case types.T_int32:
   590  		rs := vector.MustFunctionResult[int32](result)
   591  		return numericToNumeric(ctx, source, rs, length)
   592  	case types.T_int64:
   593  		rs := vector.MustFunctionResult[int64](result)
   594  		return numericToNumeric(ctx, source, rs, length)
   595  	case types.T_uint8:
   596  		rs := vector.MustFunctionResult[uint8](result)
   597  		return numericToNumeric(ctx, source, rs, length)
   598  	case types.T_uint16:
   599  		rs := vector.MustFunctionResult[uint16](result)
   600  		return numericToNumeric(ctx, source, rs, length)
   601  	case types.T_uint32:
   602  		rs := vector.MustFunctionResult[uint32](result)
   603  		return numericToNumeric(ctx, source, rs, length)
   604  	case types.T_uint64:
   605  		rs := vector.MustFunctionResult[uint64](result)
   606  		return numericToNumeric(ctx, source, rs, length)
   607  	case types.T_float32:
   608  		rs := vector.MustFunctionResult[float32](result)
   609  		return numericToNumeric(ctx, source, rs, length)
   610  	case types.T_float64:
   611  		rs := vector.MustFunctionResult[float64](result)
   612  		return numericToNumeric(ctx, source, rs, length)
   613  	case types.T_decimal64:
   614  		rs := vector.MustFunctionResult[types.Decimal64](result)
   615  		return unsignedToDecimal64(source, rs, length)
   616  	case types.T_decimal128:
   617  		rs := vector.MustFunctionResult[types.Decimal128](result)
   618  		return unsignedToDecimal128(source, rs, length)
   619  	case types.T_char, types.T_varchar, types.T_text,
   620  		types.T_binary, types.T_varbinary, types.T_blob:
   621  		rs := vector.MustFunctionResult[types.Varlena](result)
   622  		return bitToStr(ctx, source, rs, length, toType)
   623  	case types.T_time:
   624  		rs := vector.MustFunctionResult[types.Time](result)
   625  		return integerToTime(ctx, source, rs, length)
   626  	case types.T_timestamp:
   627  		rs := vector.MustFunctionResult[types.Timestamp](result)
   628  		return integerToTimestamp(source, rs, length)
   629  	case types.T_enum:
   630  		rs := vector.MustFunctionResult[types.Enum](result)
   631  		return integerToEnum(ctx, source, rs, length)
   632  	}
   633  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from %s to %s", source.GetType(), toType))
   634  }
   635  
   636  // although we can merge the int8ToOthers / int16ToOthers ... into intToOthers (use the generic).
   637  // but for extensibility, we didn't do that.
   638  // uint and float are the same.
   639  func int8ToOthers(ctx context.Context,
   640  	source vector.FunctionParameterWrapper[int8],
   641  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
   642  	switch toType.Oid {
   643  	case types.T_bool:
   644  		rs := vector.MustFunctionResult[bool](result)
   645  		return numericToBool(source, rs, length)
   646  	case types.T_bit:
   647  		rs := vector.MustFunctionResult[uint64](result)
   648  		return numericToBit(ctx, source, rs, int(toType.Width), length)
   649  	case types.T_int8:
   650  		rs := vector.MustFunctionResult[int8](result)
   651  		return rs.DupFromParameter(source, length)
   652  	case types.T_int16:
   653  		rs := vector.MustFunctionResult[int16](result)
   654  		return numericToNumeric(ctx, source, rs, length)
   655  	case types.T_int32:
   656  		rs := vector.MustFunctionResult[int32](result)
   657  		return numericToNumeric(ctx, source, rs, length)
   658  	case types.T_int64:
   659  		rs := vector.MustFunctionResult[int64](result)
   660  		return numericToNumeric(ctx, source, rs, length)
   661  	case types.T_uint8:
   662  		rs := vector.MustFunctionResult[uint8](result)
   663  		return numericToNumeric(ctx, source, rs, length)
   664  	case types.T_uint16:
   665  		rs := vector.MustFunctionResult[uint16](result)
   666  		return numericToNumeric(ctx, source, rs, length)
   667  	case types.T_uint32:
   668  		rs := vector.MustFunctionResult[uint32](result)
   669  		return numericToNumeric(ctx, source, rs, length)
   670  	case types.T_uint64:
   671  		rs := vector.MustFunctionResult[uint64](result)
   672  		return numericToNumeric(ctx, source, rs, length)
   673  	case types.T_float32:
   674  		rs := vector.MustFunctionResult[float32](result)
   675  		return numericToNumeric(ctx, source, rs, length)
   676  	case types.T_float64:
   677  		rs := vector.MustFunctionResult[float64](result)
   678  		return numericToNumeric(ctx, source, rs, length)
   679  	case types.T_decimal64:
   680  		rs := vector.MustFunctionResult[types.Decimal64](result)
   681  		return signedToDecimal64(source, rs, length)
   682  	case types.T_decimal128:
   683  		rs := vector.MustFunctionResult[types.Decimal128](result)
   684  		return signedToDecimal128(source, rs, length)
   685  	case types.T_char, types.T_varchar, types.T_blob,
   686  		types.T_binary, types.T_text, types.T_varbinary:
   687  		// string type.
   688  		rs := vector.MustFunctionResult[types.Varlena](result)
   689  		return signedToStr(ctx, source, rs, length, toType)
   690  	case types.T_time:
   691  		rs := vector.MustFunctionResult[types.Time](result)
   692  		return integerToTime(ctx, source, rs, length)
   693  	case types.T_timestamp:
   694  		rs := vector.MustFunctionResult[types.Timestamp](result)
   695  		return integerToTimestamp(source, rs, length)
   696  	}
   697  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from int8 to %s", toType))
   698  }
   699  
   700  func int16ToOthers(ctx context.Context,
   701  	source vector.FunctionParameterWrapper[int16],
   702  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
   703  	switch toType.Oid {
   704  	case types.T_bool:
   705  		rs := vector.MustFunctionResult[bool](result)
   706  		return numericToBool(source, rs, length)
   707  	case types.T_bit:
   708  		rs := vector.MustFunctionResult[uint64](result)
   709  		return numericToBit(ctx, source, rs, int(toType.Width), length)
   710  	case types.T_int8:
   711  		rs := vector.MustFunctionResult[int8](result)
   712  		return numericToNumeric(ctx, source, rs, length)
   713  	case types.T_int16:
   714  		rs := vector.MustFunctionResult[int16](result)
   715  		return rs.DupFromParameter(source, length)
   716  	case types.T_int32:
   717  		rs := vector.MustFunctionResult[int32](result)
   718  		return numericToNumeric(ctx, source, rs, length)
   719  	case types.T_int64:
   720  		rs := vector.MustFunctionResult[int64](result)
   721  		return numericToNumeric(ctx, source, rs, length)
   722  	case types.T_uint8:
   723  		rs := vector.MustFunctionResult[uint8](result)
   724  		return numericToNumeric(ctx, source, rs, length)
   725  	case types.T_uint16:
   726  		rs := vector.MustFunctionResult[uint16](result)
   727  		return numericToNumeric(ctx, source, rs, length)
   728  	case types.T_uint32:
   729  		rs := vector.MustFunctionResult[uint32](result)
   730  		return numericToNumeric(ctx, source, rs, length)
   731  	case types.T_uint64:
   732  		rs := vector.MustFunctionResult[uint64](result)
   733  		return numericToNumeric(ctx, source, rs, length)
   734  	case types.T_float32:
   735  		rs := vector.MustFunctionResult[float32](result)
   736  		return numericToNumeric(ctx, source, rs, length)
   737  	case types.T_float64:
   738  		rs := vector.MustFunctionResult[float64](result)
   739  		return numericToNumeric(ctx, source, rs, length)
   740  	case types.T_decimal64:
   741  		rs := vector.MustFunctionResult[types.Decimal64](result)
   742  		return signedToDecimal64(source, rs, length)
   743  	case types.T_decimal128:
   744  		rs := vector.MustFunctionResult[types.Decimal128](result)
   745  		return signedToDecimal128(source, rs, length)
   746  	case types.T_char, types.T_varchar, types.T_blob,
   747  		types.T_binary, types.T_text, types.T_varbinary:
   748  		// string type.
   749  		rs := vector.MustFunctionResult[types.Varlena](result)
   750  		return signedToStr(ctx, source, rs, length, toType)
   751  	case types.T_time:
   752  		rs := vector.MustFunctionResult[types.Time](result)
   753  		return integerToTime(ctx, source, rs, length)
   754  	case types.T_timestamp:
   755  		rs := vector.MustFunctionResult[types.Timestamp](result)
   756  		return integerToTimestamp(source, rs, length)
   757  	}
   758  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from int16 to %s", toType))
   759  }
   760  
   761  func int32ToOthers(ctx context.Context,
   762  	source vector.FunctionParameterWrapper[int32],
   763  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
   764  	switch toType.Oid {
   765  	case types.T_bool:
   766  		rs := vector.MustFunctionResult[bool](result)
   767  		return numericToBool(source, rs, length)
   768  	case types.T_bit:
   769  		rs := vector.MustFunctionResult[uint64](result)
   770  		return numericToBit(ctx, source, rs, int(toType.Width), length)
   771  	case types.T_int8:
   772  		rs := vector.MustFunctionResult[int8](result)
   773  		return numericToNumeric(ctx, source, rs, length)
   774  	case types.T_int16:
   775  		rs := vector.MustFunctionResult[int16](result)
   776  		return numericToNumeric(ctx, source, rs, length)
   777  	case types.T_int32:
   778  		rs := vector.MustFunctionResult[int32](result)
   779  		return rs.DupFromParameter(source, length)
   780  	case types.T_int64:
   781  		rs := vector.MustFunctionResult[int64](result)
   782  		return numericToNumeric(ctx, source, rs, length)
   783  	case types.T_uint8:
   784  		rs := vector.MustFunctionResult[uint8](result)
   785  		return numericToNumeric(ctx, source, rs, length)
   786  	case types.T_uint16:
   787  		rs := vector.MustFunctionResult[uint16](result)
   788  		return numericToNumeric(ctx, source, rs, length)
   789  	case types.T_uint32:
   790  		rs := vector.MustFunctionResult[uint32](result)
   791  		return numericToNumeric(ctx, source, rs, length)
   792  	case types.T_uint64:
   793  		rs := vector.MustFunctionResult[uint64](result)
   794  		return numericToNumeric(ctx, source, rs, length)
   795  	case types.T_float32:
   796  		rs := vector.MustFunctionResult[float32](result)
   797  		return numericToNumeric(ctx, source, rs, length)
   798  	case types.T_float64:
   799  		rs := vector.MustFunctionResult[float64](result)
   800  		return numericToNumeric(ctx, source, rs, length)
   801  	case types.T_decimal64:
   802  		rs := vector.MustFunctionResult[types.Decimal64](result)
   803  		return signedToDecimal64(source, rs, length)
   804  	case types.T_decimal128:
   805  		rs := vector.MustFunctionResult[types.Decimal128](result)
   806  		return signedToDecimal128(source, rs, length)
   807  	case types.T_char, types.T_varchar, types.T_blob,
   808  		types.T_binary, types.T_text, types.T_varbinary:
   809  		// string type.
   810  		rs := vector.MustFunctionResult[types.Varlena](result)
   811  		return signedToStr(ctx, source, rs, length, toType)
   812  	case types.T_time:
   813  		rs := vector.MustFunctionResult[types.Time](result)
   814  		return integerToTime(ctx, source, rs, length)
   815  	case types.T_timestamp:
   816  		rs := vector.MustFunctionResult[types.Timestamp](result)
   817  		return integerToTimestamp(source, rs, length)
   818  	}
   819  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from int32 to %s", toType))
   820  }
   821  
   822  func int64ToOthers(ctx context.Context,
   823  	source vector.FunctionParameterWrapper[int64],
   824  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
   825  	switch toType.Oid {
   826  	case types.T_bool:
   827  		rs := vector.MustFunctionResult[bool](result)
   828  		return numericToBool(source, rs, length)
   829  	case types.T_bit:
   830  		rs := vector.MustFunctionResult[uint64](result)
   831  		return numericToBit(ctx, source, rs, int(toType.Width), length)
   832  	case types.T_int8:
   833  		rs := vector.MustFunctionResult[int8](result)
   834  		return numericToNumeric(ctx, source, rs, length)
   835  	case types.T_int16:
   836  		rs := vector.MustFunctionResult[int16](result)
   837  		return numericToNumeric(ctx, source, rs, length)
   838  	case types.T_int32:
   839  		rs := vector.MustFunctionResult[int32](result)
   840  		return numericToNumeric(ctx, source, rs, length)
   841  	case types.T_int64:
   842  		rs := vector.MustFunctionResult[int64](result)
   843  		return rs.DupFromParameter(source, length)
   844  	case types.T_uint8:
   845  		rs := vector.MustFunctionResult[uint8](result)
   846  		return numericToNumeric(ctx, source, rs, length)
   847  	case types.T_uint16:
   848  		rs := vector.MustFunctionResult[uint16](result)
   849  		return numericToNumeric(ctx, source, rs, length)
   850  	case types.T_uint32:
   851  		rs := vector.MustFunctionResult[uint32](result)
   852  		return numericToNumeric(ctx, source, rs, length)
   853  	case types.T_uint64:
   854  		rs := vector.MustFunctionResult[uint64](result)
   855  		return numericToNumeric(ctx, source, rs, length)
   856  	case types.T_float32:
   857  		rs := vector.MustFunctionResult[float32](result)
   858  		return numericToNumeric(ctx, source, rs, length)
   859  	case types.T_float64:
   860  		rs := vector.MustFunctionResult[float64](result)
   861  		return numericToNumeric(ctx, source, rs, length)
   862  	case types.T_decimal64:
   863  		rs := vector.MustFunctionResult[types.Decimal64](result)
   864  		return signedToDecimal64(source, rs, length)
   865  	case types.T_decimal128:
   866  		rs := vector.MustFunctionResult[types.Decimal128](result)
   867  		return signedToDecimal128(source, rs, length)
   868  	case types.T_char, types.T_varchar, types.T_blob,
   869  		types.T_binary, types.T_varbinary, types.T_text:
   870  		// string type.
   871  		rs := vector.MustFunctionResult[types.Varlena](result)
   872  		return signedToStr(ctx, source, rs, length, toType)
   873  	case types.T_time:
   874  		rs := vector.MustFunctionResult[types.Time](result)
   875  		return integerToTime(ctx, source, rs, length)
   876  	case types.T_timestamp:
   877  		rs := vector.MustFunctionResult[types.Timestamp](result)
   878  		return integerToTimestamp(source, rs, length)
   879  	case types.T_enum:
   880  		rs := vector.MustFunctionResult[types.Enum](result)
   881  		return integerToEnum(ctx, source, rs, length)
   882  	}
   883  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from int64 to %s", toType))
   884  }
   885  
   886  func uint8ToOthers(ctx context.Context,
   887  	source vector.FunctionParameterWrapper[uint8],
   888  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
   889  	switch toType.Oid {
   890  	case types.T_bool:
   891  		rs := vector.MustFunctionResult[bool](result)
   892  		return numericToBool(source, rs, length)
   893  	case types.T_bit:
   894  		rs := vector.MustFunctionResult[uint64](result)
   895  		return numericToBit(ctx, source, rs, int(toType.Width), length)
   896  	case types.T_int8:
   897  		rs := vector.MustFunctionResult[int8](result)
   898  		return numericToNumeric(ctx, source, rs, length)
   899  	case types.T_int16:
   900  		rs := vector.MustFunctionResult[int16](result)
   901  		return numericToNumeric(ctx, source, rs, length)
   902  	case types.T_int32:
   903  		rs := vector.MustFunctionResult[int32](result)
   904  		return numericToNumeric(ctx, source, rs, length)
   905  	case types.T_int64:
   906  		rs := vector.MustFunctionResult[int64](result)
   907  		return numericToNumeric(ctx, source, rs, length)
   908  	case types.T_uint8:
   909  		rs := vector.MustFunctionResult[uint8](result)
   910  		return rs.DupFromParameter(source, length)
   911  	case types.T_uint16:
   912  		rs := vector.MustFunctionResult[uint16](result)
   913  		return numericToNumeric(ctx, source, rs, length)
   914  	case types.T_uint32:
   915  		rs := vector.MustFunctionResult[uint32](result)
   916  		return numericToNumeric(ctx, source, rs, length)
   917  	case types.T_uint64:
   918  		rs := vector.MustFunctionResult[uint64](result)
   919  		return numericToNumeric(ctx, source, rs, length)
   920  	case types.T_float32:
   921  		rs := vector.MustFunctionResult[float32](result)
   922  		return numericToNumeric(ctx, source, rs, length)
   923  	case types.T_float64:
   924  		rs := vector.MustFunctionResult[float64](result)
   925  		return numericToNumeric(ctx, source, rs, length)
   926  	case types.T_decimal64:
   927  		rs := vector.MustFunctionResult[types.Decimal64](result)
   928  		return unsignedToDecimal64(source, rs, length)
   929  	case types.T_decimal128:
   930  		rs := vector.MustFunctionResult[types.Decimal128](result)
   931  		return unsignedToDecimal128(source, rs, length)
   932  	case types.T_char, types.T_varchar, types.T_blob,
   933  		types.T_binary, types.T_text, types.T_varbinary:
   934  		rs := vector.MustFunctionResult[types.Varlena](result)
   935  		return unsignedToStr(ctx, source, rs, length, toType)
   936  	case types.T_time:
   937  		rs := vector.MustFunctionResult[types.Time](result)
   938  		return integerToTime(ctx, source, rs, length)
   939  	case types.T_timestamp:
   940  		rs := vector.MustFunctionResult[types.Timestamp](result)
   941  		return integerToTimestamp(source, rs, length)
   942  	case types.T_enum:
   943  		rs := vector.MustFunctionResult[types.Enum](result)
   944  		return integerToEnum(ctx, source, rs, length)
   945  	}
   946  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from uint8 to %s", toType))
   947  }
   948  
   949  func uint16ToOthers(ctx context.Context,
   950  	source vector.FunctionParameterWrapper[uint16],
   951  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
   952  	switch toType.Oid {
   953  	case types.T_bool:
   954  		rs := vector.MustFunctionResult[bool](result)
   955  		return numericToBool(source, rs, length)
   956  	case types.T_bit:
   957  		rs := vector.MustFunctionResult[uint64](result)
   958  		return numericToBit(ctx, source, rs, int(toType.Width), length)
   959  	case types.T_int8:
   960  		rs := vector.MustFunctionResult[int8](result)
   961  		return numericToNumeric(ctx, source, rs, length)
   962  	case types.T_int16:
   963  		rs := vector.MustFunctionResult[int16](result)
   964  		return numericToNumeric(ctx, source, rs, length)
   965  	case types.T_int32:
   966  		rs := vector.MustFunctionResult[int32](result)
   967  		return numericToNumeric(ctx, source, rs, length)
   968  	case types.T_int64:
   969  		rs := vector.MustFunctionResult[int64](result)
   970  		return numericToNumeric(ctx, source, rs, length)
   971  	case types.T_uint8:
   972  		rs := vector.MustFunctionResult[uint8](result)
   973  		return numericToNumeric(ctx, source, rs, length)
   974  	case types.T_uint16:
   975  		rs := vector.MustFunctionResult[uint16](result)
   976  		return rs.DupFromParameter(source, length)
   977  	case types.T_uint32:
   978  		rs := vector.MustFunctionResult[uint32](result)
   979  		return numericToNumeric(ctx, source, rs, length)
   980  	case types.T_uint64:
   981  		rs := vector.MustFunctionResult[uint64](result)
   982  		return numericToNumeric(ctx, source, rs, length)
   983  	case types.T_float32:
   984  		rs := vector.MustFunctionResult[float32](result)
   985  		return numericToNumeric(ctx, source, rs, length)
   986  	case types.T_float64:
   987  		rs := vector.MustFunctionResult[float64](result)
   988  		return numericToNumeric(ctx, source, rs, length)
   989  	case types.T_decimal64:
   990  		rs := vector.MustFunctionResult[types.Decimal64](result)
   991  		return unsignedToDecimal64(source, rs, length)
   992  	case types.T_decimal128:
   993  		rs := vector.MustFunctionResult[types.Decimal128](result)
   994  		return unsignedToDecimal128(source, rs, length)
   995  	case types.T_char, types.T_varchar, types.T_blob,
   996  		types.T_binary, types.T_text, types.T_varbinary:
   997  		rs := vector.MustFunctionResult[types.Varlena](result)
   998  		return unsignedToStr(ctx, source, rs, length, toType)
   999  	case types.T_time:
  1000  		rs := vector.MustFunctionResult[types.Time](result)
  1001  		return integerToTime(ctx, source, rs, length)
  1002  	case types.T_timestamp:
  1003  		rs := vector.MustFunctionResult[types.Timestamp](result)
  1004  		return integerToTimestamp(source, rs, length)
  1005  	case types.T_enum:
  1006  		rs := vector.MustFunctionResult[types.Enum](result)
  1007  		return integerToEnum(ctx, source, rs, length)
  1008  	}
  1009  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from uint16 to %s", toType))
  1010  }
  1011  
  1012  func uint32ToOthers(ctx context.Context,
  1013  	source vector.FunctionParameterWrapper[uint32],
  1014  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1015  	switch toType.Oid {
  1016  	case types.T_bool:
  1017  		rs := vector.MustFunctionResult[bool](result)
  1018  		return numericToBool(source, rs, length)
  1019  	case types.T_bit:
  1020  		rs := vector.MustFunctionResult[uint64](result)
  1021  		return numericToBit(ctx, source, rs, int(toType.Width), length)
  1022  	case types.T_int8:
  1023  		rs := vector.MustFunctionResult[int8](result)
  1024  		return numericToNumeric(ctx, source, rs, length)
  1025  	case types.T_int16:
  1026  		rs := vector.MustFunctionResult[int16](result)
  1027  		return numericToNumeric(ctx, source, rs, length)
  1028  	case types.T_int32:
  1029  		rs := vector.MustFunctionResult[int32](result)
  1030  		return numericToNumeric(ctx, source, rs, length)
  1031  	case types.T_int64:
  1032  		rs := vector.MustFunctionResult[int64](result)
  1033  		return numericToNumeric(ctx, source, rs, length)
  1034  	case types.T_uint8:
  1035  		rs := vector.MustFunctionResult[uint8](result)
  1036  		return numericToNumeric(ctx, source, rs, length)
  1037  	case types.T_uint16:
  1038  		rs := vector.MustFunctionResult[uint16](result)
  1039  		return numericToNumeric(ctx, source, rs, length)
  1040  	case types.T_uint32:
  1041  		rs := vector.MustFunctionResult[uint32](result)
  1042  		return rs.DupFromParameter(source, length)
  1043  	case types.T_uint64:
  1044  		rs := vector.MustFunctionResult[uint64](result)
  1045  		return numericToNumeric(ctx, source, rs, length)
  1046  	case types.T_float32:
  1047  		rs := vector.MustFunctionResult[float32](result)
  1048  		return numericToNumeric(ctx, source, rs, length)
  1049  	case types.T_float64:
  1050  		rs := vector.MustFunctionResult[float64](result)
  1051  		return numericToNumeric(ctx, source, rs, length)
  1052  	case types.T_decimal64:
  1053  		rs := vector.MustFunctionResult[types.Decimal64](result)
  1054  		return unsignedToDecimal64(source, rs, length)
  1055  	case types.T_decimal128:
  1056  		rs := vector.MustFunctionResult[types.Decimal128](result)
  1057  		return unsignedToDecimal128(source, rs, length)
  1058  	case types.T_char, types.T_varchar, types.T_blob,
  1059  		types.T_binary, types.T_text, types.T_varbinary:
  1060  		rs := vector.MustFunctionResult[types.Varlena](result)
  1061  		return unsignedToStr(ctx, source, rs, length, toType)
  1062  	case types.T_time:
  1063  		rs := vector.MustFunctionResult[types.Time](result)
  1064  		return integerToTime(ctx, source, rs, length)
  1065  	case types.T_timestamp:
  1066  		rs := vector.MustFunctionResult[types.Timestamp](result)
  1067  		return integerToTimestamp(source, rs, length)
  1068  	case types.T_enum:
  1069  		rs := vector.MustFunctionResult[types.Enum](result)
  1070  		return integerToEnum(ctx, source, rs, length)
  1071  	}
  1072  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from uint32 to %s", toType))
  1073  }
  1074  
  1075  func uint64ToOthers(ctx context.Context,
  1076  	source vector.FunctionParameterWrapper[uint64],
  1077  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1078  	switch toType.Oid {
  1079  	case types.T_bool:
  1080  		rs := vector.MustFunctionResult[bool](result)
  1081  		return numericToBool(source, rs, length)
  1082  	case types.T_bit:
  1083  		rs := vector.MustFunctionResult[uint64](result)
  1084  		return numericToBit(ctx, source, rs, int(toType.Width), length)
  1085  	case types.T_int8:
  1086  		rs := vector.MustFunctionResult[int8](result)
  1087  		return numericToNumeric(ctx, source, rs, length)
  1088  	case types.T_int16:
  1089  		rs := vector.MustFunctionResult[int16](result)
  1090  		return numericToNumeric(ctx, source, rs, length)
  1091  	case types.T_int32:
  1092  		rs := vector.MustFunctionResult[int32](result)
  1093  		return numericToNumeric(ctx, source, rs, length)
  1094  	case types.T_int64:
  1095  		rs := vector.MustFunctionResult[int64](result)
  1096  		return numericToNumeric(ctx, source, rs, length)
  1097  	case types.T_uint8:
  1098  		rs := vector.MustFunctionResult[uint8](result)
  1099  		return numericToNumeric(ctx, source, rs, length)
  1100  	case types.T_uint16:
  1101  		rs := vector.MustFunctionResult[uint16](result)
  1102  		return numericToNumeric(ctx, source, rs, length)
  1103  	case types.T_uint32:
  1104  		rs := vector.MustFunctionResult[uint32](result)
  1105  		return numericToNumeric(ctx, source, rs, length)
  1106  	case types.T_uint64:
  1107  		rs := vector.MustFunctionResult[uint64](result)
  1108  		return rs.DupFromParameter(source, length)
  1109  	case types.T_float32:
  1110  		rs := vector.MustFunctionResult[float32](result)
  1111  		return numericToNumeric(ctx, source, rs, length)
  1112  	case types.T_float64:
  1113  		rs := vector.MustFunctionResult[float64](result)
  1114  		return numericToNumeric(ctx, source, rs, length)
  1115  	case types.T_decimal64:
  1116  		rs := vector.MustFunctionResult[types.Decimal64](result)
  1117  		return unsignedToDecimal64(source, rs, length)
  1118  	case types.T_decimal128:
  1119  		rs := vector.MustFunctionResult[types.Decimal128](result)
  1120  		return unsignedToDecimal128(source, rs, length)
  1121  	case types.T_char, types.T_varchar, types.T_blob,
  1122  		types.T_binary, types.T_text, types.T_varbinary:
  1123  		rs := vector.MustFunctionResult[types.Varlena](result)
  1124  		return unsignedToStr(ctx, source, rs, length, toType)
  1125  	case types.T_time:
  1126  		rs := vector.MustFunctionResult[types.Time](result)
  1127  		return integerToTime(ctx, source, rs, length)
  1128  	case types.T_timestamp:
  1129  		rs := vector.MustFunctionResult[types.Timestamp](result)
  1130  		return integerToTimestamp(source, rs, length)
  1131  	case types.T_enum:
  1132  		rs := vector.MustFunctionResult[types.Enum](result)
  1133  		return integerToEnum(ctx, source, rs, length)
  1134  	}
  1135  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from uint64 to %s", toType))
  1136  }
  1137  
  1138  func float32ToOthers(ctx context.Context,
  1139  	source vector.FunctionParameterWrapper[float32],
  1140  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1141  	switch toType.Oid {
  1142  	case types.T_bool:
  1143  		rs := vector.MustFunctionResult[bool](result)
  1144  		return numericToBool(source, rs, length)
  1145  	case types.T_bit:
  1146  		rs := vector.MustFunctionResult[uint64](result)
  1147  		return numericToBit(ctx, source, rs, int(toType.Width), length)
  1148  	case types.T_int8:
  1149  		rs := vector.MustFunctionResult[int8](result)
  1150  		return floatToInteger(ctx, source, rs, length)
  1151  	case types.T_int16:
  1152  		rs := vector.MustFunctionResult[int16](result)
  1153  		return floatToInteger(ctx, source, rs, length)
  1154  	case types.T_int32:
  1155  		rs := vector.MustFunctionResult[int32](result)
  1156  		return floatToInteger(ctx, source, rs, length)
  1157  	case types.T_int64:
  1158  		rs := vector.MustFunctionResult[int64](result)
  1159  		return floatToInteger(ctx, source, rs, length)
  1160  	case types.T_uint8:
  1161  		rs := vector.MustFunctionResult[uint8](result)
  1162  		return floatToInteger(ctx, source, rs, length)
  1163  	case types.T_uint16:
  1164  		rs := vector.MustFunctionResult[uint16](result)
  1165  		return floatToInteger(ctx, source, rs, length)
  1166  	case types.T_uint32:
  1167  		rs := vector.MustFunctionResult[uint32](result)
  1168  		return floatToInteger(ctx, source, rs, length)
  1169  	case types.T_uint64:
  1170  		rs := vector.MustFunctionResult[uint64](result)
  1171  		return floatToInteger(ctx, source, rs, length)
  1172  	case types.T_float32:
  1173  		rs := vector.MustFunctionResult[float32](result)
  1174  		if rs.GetType().Scale >= 0 && rs.GetType().Width > 0 {
  1175  			return floatToFixFloat(ctx, source, rs, length)
  1176  		}
  1177  		return rs.DupFromParameter(source, length)
  1178  	case types.T_float64:
  1179  		rs := vector.MustFunctionResult[float64](result)
  1180  		if rs.GetType().Scale >= 0 && rs.GetType().Width > 0 {
  1181  			return floatToFixFloat(ctx, source, rs, length)
  1182  		}
  1183  		return numericToNumeric(ctx, source, rs, length)
  1184  	case types.T_decimal64:
  1185  		rs := vector.MustFunctionResult[types.Decimal64](result)
  1186  		return floatToDecimal64(source, rs, length)
  1187  	case types.T_decimal128:
  1188  		rs := vector.MustFunctionResult[types.Decimal128](result)
  1189  		return floatToDecimal128(source, rs, length)
  1190  	case types.T_char, types.T_varchar, types.T_blob,
  1191  		types.T_binary, types.T_text, types.T_varbinary:
  1192  		rs := vector.MustFunctionResult[types.Varlena](result)
  1193  		return floatToStr(ctx, source, rs, length, toType)
  1194  	}
  1195  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from float32 to %s", toType))
  1196  }
  1197  
  1198  func float64ToOthers(ctx context.Context,
  1199  	source vector.FunctionParameterWrapper[float64],
  1200  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1201  	switch toType.Oid {
  1202  	case types.T_bool:
  1203  		rs := vector.MustFunctionResult[bool](result)
  1204  		return numericToBool(source, rs, length)
  1205  	case types.T_bit:
  1206  		rs := vector.MustFunctionResult[uint64](result)
  1207  		return numericToBit(ctx, source, rs, int(toType.Width), length)
  1208  	case types.T_int8:
  1209  		rs := vector.MustFunctionResult[int8](result)
  1210  		return floatToInteger(ctx, source, rs, length)
  1211  	case types.T_int16:
  1212  		rs := vector.MustFunctionResult[int16](result)
  1213  		return floatToInteger(ctx, source, rs, length)
  1214  	case types.T_int32:
  1215  		rs := vector.MustFunctionResult[int32](result)
  1216  		return floatToInteger(ctx, source, rs, length)
  1217  	case types.T_int64:
  1218  		rs := vector.MustFunctionResult[int64](result)
  1219  		return floatToInteger(ctx, source, rs, length)
  1220  	case types.T_uint8:
  1221  		rs := vector.MustFunctionResult[uint8](result)
  1222  		return floatToInteger(ctx, source, rs, length)
  1223  	case types.T_uint16:
  1224  		rs := vector.MustFunctionResult[uint16](result)
  1225  		return floatToInteger(ctx, source, rs, length)
  1226  	case types.T_uint32:
  1227  		rs := vector.MustFunctionResult[uint32](result)
  1228  		return floatToInteger(ctx, source, rs, length)
  1229  	case types.T_uint64:
  1230  		rs := vector.MustFunctionResult[uint64](result)
  1231  		return floatToInteger(ctx, source, rs, length)
  1232  	case types.T_float32:
  1233  		rs := vector.MustFunctionResult[float32](result)
  1234  		if rs.GetType().Scale >= 0 && rs.GetType().Width > 0 {
  1235  			return floatToFixFloat(ctx, source, rs, length)
  1236  		}
  1237  		return numericToNumeric(ctx, source, rs, length)
  1238  	case types.T_float64:
  1239  		rs := vector.MustFunctionResult[float64](result)
  1240  		if rs.GetType().Scale >= 0 && rs.GetType().Width > 0 {
  1241  			return floatToFixFloat(ctx, source, rs, length)
  1242  		}
  1243  		return rs.DupFromParameter(source, length)
  1244  	case types.T_decimal64:
  1245  		rs := vector.MustFunctionResult[types.Decimal64](result)
  1246  		return floatToDecimal64(source, rs, length)
  1247  	case types.T_decimal128:
  1248  		rs := vector.MustFunctionResult[types.Decimal128](result)
  1249  		return floatToDecimal128(source, rs, length)
  1250  	case types.T_char, types.T_varchar, types.T_blob,
  1251  		types.T_binary, types.T_text, types.T_varbinary:
  1252  		rs := vector.MustFunctionResult[types.Varlena](result)
  1253  		return floatToStr(ctx, source, rs, length, toType)
  1254  	}
  1255  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from float64 to %s", toType))
  1256  }
  1257  
  1258  func dateToOthers(proc *process.Process,
  1259  	source vector.FunctionParameterWrapper[types.Date],
  1260  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1261  	switch toType.Oid {
  1262  	case types.T_int32:
  1263  		rs := vector.MustFunctionResult[int32](result)
  1264  		return dateToSigned(source, rs, length)
  1265  	case types.T_int64:
  1266  		rs := vector.MustFunctionResult[int64](result)
  1267  		return dateToSigned(source, rs, length)
  1268  	case types.T_date:
  1269  		rs := vector.MustFunctionResult[types.Date](result)
  1270  		return rs.DupFromParameter(source, length)
  1271  	case types.T_time:
  1272  		rs := vector.MustFunctionResult[types.Time](result)
  1273  		return dateToTime(source, rs, length)
  1274  	case types.T_timestamp:
  1275  		zone := time.Local
  1276  		if proc != nil {
  1277  			zone = proc.SessionInfo.TimeZone
  1278  		}
  1279  		rs := vector.MustFunctionResult[types.Timestamp](result)
  1280  		return dateToTimestamp(source, rs, length, zone)
  1281  	case types.T_datetime:
  1282  		rs := vector.MustFunctionResult[types.Datetime](result)
  1283  		return dateToDatetime(source, rs, length)
  1284  	case types.T_char, types.T_varchar, types.T_blob,
  1285  		types.T_binary, types.T_varbinary, types.T_text:
  1286  		rs := vector.MustFunctionResult[types.Varlena](result)
  1287  		return dateToStr(proc.Ctx, source, rs, length, toType)
  1288  	}
  1289  	return moerr.NewInternalError(proc.Ctx, fmt.Sprintf("unsupported cast from date to %s", toType))
  1290  }
  1291  
  1292  func datetimeToOthers(proc *process.Process,
  1293  	source vector.FunctionParameterWrapper[types.Datetime],
  1294  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1295  	switch toType.Oid {
  1296  	case types.T_int32:
  1297  		rs := vector.MustFunctionResult[int32](result)
  1298  		return datetimeToInt32(proc.Ctx, source, rs, length)
  1299  	case types.T_int64:
  1300  		rs := vector.MustFunctionResult[int64](result)
  1301  		return datetimeToInt64(source, rs, length)
  1302  	case types.T_timestamp:
  1303  		zone := time.Local
  1304  		if proc != nil {
  1305  			zone = proc.SessionInfo.TimeZone
  1306  		}
  1307  		rs := vector.MustFunctionResult[types.Timestamp](result)
  1308  		return datetimeToTimestamp(source, rs, length, zone)
  1309  	case types.T_date:
  1310  		rs := vector.MustFunctionResult[types.Date](result)
  1311  		return datetimeToDate(source, rs, length)
  1312  	case types.T_datetime:
  1313  		rs := vector.MustFunctionResult[types.Datetime](result)
  1314  		v := source.GetSourceVector()
  1315  		v.SetType(toType)
  1316  		return rs.DupFromParameter(source, length)
  1317  	case types.T_time:
  1318  		rs := vector.MustFunctionResult[types.Time](result)
  1319  		return datetimeToTime(source, rs, length)
  1320  	case types.T_char, types.T_varchar, types.T_blob,
  1321  		types.T_binary, types.T_varbinary, types.T_text:
  1322  		rs := vector.MustFunctionResult[types.Varlena](result)
  1323  		return datetimeToStr(proc.Ctx, source, rs, length, toType)
  1324  	case types.T_decimal64:
  1325  		rs := vector.MustFunctionResult[types.Decimal64](result)
  1326  		return datetimeToDecimal64(proc.Ctx, source, rs, length)
  1327  	case types.T_decimal128:
  1328  		rs := vector.MustFunctionResult[types.Decimal128](result)
  1329  		return datetimeToDecimal128(proc.Ctx, source, rs, length)
  1330  	}
  1331  	return moerr.NewInternalError(proc.Ctx, fmt.Sprintf("unsupported cast from datetime to %s", toType))
  1332  }
  1333  
  1334  func timestampToOthers(proc *process.Process,
  1335  	source vector.FunctionParameterWrapper[types.Timestamp],
  1336  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1337  	zone := time.Local
  1338  	if proc != nil {
  1339  		zone = proc.SessionInfo.TimeZone
  1340  	}
  1341  
  1342  	switch toType.Oid {
  1343  	case types.T_int32:
  1344  		rs := vector.MustFunctionResult[int32](result)
  1345  		return timestampToInt32(proc.Ctx, source, rs, length)
  1346  	case types.T_int64:
  1347  		rs := vector.MustFunctionResult[int64](result)
  1348  		return timestampToInt64(source, rs, length)
  1349  	case types.T_date:
  1350  		rs := vector.MustFunctionResult[types.Date](result)
  1351  		return timestampToDate(proc.Ctx, source, rs, length, zone)
  1352  	case types.T_datetime:
  1353  		rs := vector.MustFunctionResult[types.Datetime](result)
  1354  		return timestampToDatetime(proc.Ctx, source, rs, length, zone)
  1355  	case types.T_timestamp:
  1356  		rs := vector.MustFunctionResult[types.Timestamp](result)
  1357  		v := source.GetSourceVector()
  1358  		v.SetType(toType)
  1359  		return rs.DupFromParameter(source, length)
  1360  	case types.T_char, types.T_varchar, types.T_blob,
  1361  		types.T_binary, types.T_varbinary, types.T_text:
  1362  		rs := vector.MustFunctionResult[types.Varlena](result)
  1363  		return timestampToStr(proc.Ctx, source, rs, length, zone, toType)
  1364  	case types.T_decimal64:
  1365  		rs := vector.MustFunctionResult[types.Decimal64](result)
  1366  		return timestampToDecimal64(proc.Ctx, source, rs, length)
  1367  	case types.T_decimal128:
  1368  		rs := vector.MustFunctionResult[types.Decimal128](result)
  1369  		return timestampToDecimal128(proc.Ctx, source, rs, length)
  1370  	}
  1371  	return moerr.NewInternalError(proc.Ctx, fmt.Sprintf("unsupported cast from timestamp to %s", toType))
  1372  }
  1373  
  1374  func timeToOthers(ctx context.Context,
  1375  	source vector.FunctionParameterWrapper[types.Time],
  1376  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1377  	switch toType.Oid {
  1378  	case types.T_bit:
  1379  		rs := vector.MustFunctionResult[uint64](result)
  1380  		return timeToInteger(ctx, source, rs, length)
  1381  	case types.T_int8:
  1382  		rs := vector.MustFunctionResult[int8](result)
  1383  		return timeToInteger(ctx, source, rs, length)
  1384  	case types.T_int16:
  1385  		rs := vector.MustFunctionResult[int16](result)
  1386  		return timeToInteger(ctx, source, rs, length)
  1387  	case types.T_int32:
  1388  		rs := vector.MustFunctionResult[int32](result)
  1389  		return timeToInteger(ctx, source, rs, length)
  1390  	case types.T_int64:
  1391  		rs := vector.MustFunctionResult[int64](result)
  1392  		return timeToInteger(ctx, source, rs, length)
  1393  	case types.T_uint8:
  1394  		rs := vector.MustFunctionResult[uint8](result)
  1395  		return timeToInteger(ctx, source, rs, length)
  1396  	case types.T_uint16:
  1397  		rs := vector.MustFunctionResult[uint16](result)
  1398  		return timeToInteger(ctx, source, rs, length)
  1399  	case types.T_uint32:
  1400  		rs := vector.MustFunctionResult[uint32](result)
  1401  		return timeToInteger(ctx, source, rs, length)
  1402  	case types.T_uint64:
  1403  		rs := vector.MustFunctionResult[uint64](result)
  1404  		return timeToInteger(ctx, source, rs, length)
  1405  	case types.T_date:
  1406  		rs := vector.MustFunctionResult[types.Date](result)
  1407  		return timeToDate(source, rs, length)
  1408  	case types.T_datetime:
  1409  		rs := vector.MustFunctionResult[types.Datetime](result)
  1410  		return timeToDatetime(source, rs, length)
  1411  	case types.T_time:
  1412  		rs := vector.MustFunctionResult[types.Time](result)
  1413  		v := source.GetSourceVector()
  1414  		v.SetType(toType)
  1415  		return rs.DupFromParameter(source, length)
  1416  	case types.T_char, types.T_varchar, types.T_blob,
  1417  		types.T_binary, types.T_varbinary, types.T_text:
  1418  		rs := vector.MustFunctionResult[types.Varlena](result)
  1419  		return timeToStr(ctx, source, rs, length, toType)
  1420  	case types.T_decimal64:
  1421  		rs := vector.MustFunctionResult[types.Decimal64](result)
  1422  		return timeToDecimal64(ctx, source, rs, length)
  1423  	case types.T_decimal128:
  1424  		rs := vector.MustFunctionResult[types.Decimal128](result)
  1425  		return timeToDecimal128(ctx, source, rs, length)
  1426  	}
  1427  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from time to %s", toType))
  1428  }
  1429  
  1430  func decimal64ToOthers(ctx context.Context,
  1431  	source vector.FunctionParameterWrapper[types.Decimal64],
  1432  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1433  	switch toType.Oid {
  1434  	case types.T_bit:
  1435  		rs := vector.MustFunctionResult[uint64](result)
  1436  		return decimal64ToBit(ctx, source, rs, int(toType.Width), length)
  1437  	case types.T_float32:
  1438  		rs := vector.MustFunctionResult[float32](result)
  1439  		return decimal64ToFloat(ctx, source, rs, length, 32)
  1440  	case types.T_float64:
  1441  		rs := vector.MustFunctionResult[float64](result)
  1442  		return decimal64ToFloat(ctx, source, rs, length, 64)
  1443  	case types.T_int8:
  1444  		rs := vector.MustFunctionResult[int8](result)
  1445  		return decimal64ToSigned(ctx, source, rs, 8, length)
  1446  	case types.T_int16:
  1447  		rs := vector.MustFunctionResult[int16](result)
  1448  		return decimal64ToSigned(ctx, source, rs, 16, length)
  1449  	case types.T_int32:
  1450  		rs := vector.MustFunctionResult[int32](result)
  1451  		return decimal64ToSigned(ctx, source, rs, 32, length)
  1452  	case types.T_int64:
  1453  		rs := vector.MustFunctionResult[int64](result)
  1454  		return decimal64ToSigned(ctx, source, rs, 64, length)
  1455  	case types.T_uint8:
  1456  		rs := vector.MustFunctionResult[uint8](result)
  1457  		return decimal64ToUnsigned(ctx, source, rs, 8, length)
  1458  	case types.T_uint16:
  1459  		rs := vector.MustFunctionResult[uint16](result)
  1460  		return decimal64ToUnsigned(ctx, source, rs, 16, length)
  1461  	case types.T_uint32:
  1462  		rs := vector.MustFunctionResult[uint32](result)
  1463  		return decimal64ToUnsigned(ctx, source, rs, 32, length)
  1464  	case types.T_uint64:
  1465  		rs := vector.MustFunctionResult[uint64](result)
  1466  		return decimal64ToUnsigned(ctx, source, rs, 64, length)
  1467  	case types.T_decimal64:
  1468  		rs := vector.MustFunctionResult[types.Decimal64](result)
  1469  		if source.GetType().Scale == toType.Scale && source.GetType().Width >= toType.Width {
  1470  			if err := rs.DupFromParameter(source, length); err != nil {
  1471  				return err
  1472  			}
  1473  			v := rs.GetResultVector()
  1474  			v.SetType(toType)
  1475  			return nil
  1476  		}
  1477  		return decimal64ToDecimal64(source, rs, length)
  1478  	case types.T_decimal128:
  1479  		rs := vector.MustFunctionResult[types.Decimal128](result)
  1480  		return decimal64ToDecimal128Array(source, rs, length)
  1481  	case types.T_timestamp:
  1482  		rs := vector.MustFunctionResult[types.Timestamp](result)
  1483  		return decimal64ToTimestamp(source, rs, length)
  1484  	case types.T_time:
  1485  		rs := vector.MustFunctionResult[types.Time](result)
  1486  		return decimal64ToTime(source, rs, length)
  1487  	case types.T_char, types.T_varchar, types.T_blob,
  1488  		types.T_binary, types.T_varbinary, types.T_text:
  1489  		rs := vector.MustFunctionResult[types.Varlena](result)
  1490  		return decimal64ToStr(ctx, source, rs, length, toType)
  1491  	}
  1492  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from decimal64 to %s", toType))
  1493  }
  1494  
  1495  func decimal128ToOthers(ctx context.Context,
  1496  	source vector.FunctionParameterWrapper[types.Decimal128],
  1497  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1498  	switch toType.Oid {
  1499  	case types.T_bit:
  1500  		rs := vector.MustFunctionResult[uint64](result)
  1501  		return decimal128ToBit(ctx, source, rs, int(toType.Width), length)
  1502  	case types.T_int8:
  1503  		rs := vector.MustFunctionResult[int8](result)
  1504  		return decimal128ToSigned(ctx, source, rs, 8, length)
  1505  	case types.T_int16:
  1506  		rs := vector.MustFunctionResult[int16](result)
  1507  		return decimal128ToSigned(ctx, source, rs, 16, length)
  1508  	case types.T_int32:
  1509  		rs := vector.MustFunctionResult[int32](result)
  1510  		return decimal128ToSigned(ctx, source, rs, 32, length)
  1511  	case types.T_int64:
  1512  		rs := vector.MustFunctionResult[int64](result)
  1513  		return decimal128ToSigned(ctx, source, rs, 64, length)
  1514  	case types.T_uint8:
  1515  		rs := vector.MustFunctionResult[uint8](result)
  1516  		return decimal128ToUnsigned(ctx, source, rs, 8, length)
  1517  	case types.T_uint16:
  1518  		rs := vector.MustFunctionResult[uint16](result)
  1519  		return decimal128ToUnsigned(ctx, source, rs, 16, length)
  1520  	case types.T_uint32:
  1521  		rs := vector.MustFunctionResult[uint32](result)
  1522  		return decimal128ToUnsigned(ctx, source, rs, 32, length)
  1523  	case types.T_uint64:
  1524  		rs := vector.MustFunctionResult[uint64](result)
  1525  		return decimal128ToUnsigned(ctx, source, rs, 64, length)
  1526  	case types.T_decimal64:
  1527  		rs := vector.MustFunctionResult[types.Decimal64](result)
  1528  		return decimal128ToDecimal64(ctx, source, rs, length)
  1529  	case types.T_decimal128:
  1530  		rs := vector.MustFunctionResult[types.Decimal128](result)
  1531  		if source.GetType().Scale == toType.Scale && source.GetType().Width >= toType.Width {
  1532  			if err := rs.DupFromParameter(source, length); err != nil {
  1533  				return err
  1534  			}
  1535  			v := source.GetSourceVector()
  1536  			v.SetType(toType)
  1537  			return nil
  1538  		}
  1539  		return decimal128ToDecimal128(source, rs, length)
  1540  	case types.T_float32:
  1541  		rs := vector.MustFunctionResult[float32](result)
  1542  		return decimal128ToFloat(ctx, source, rs, length, 32)
  1543  	case types.T_float64:
  1544  		rs := vector.MustFunctionResult[float64](result)
  1545  		return decimal128ToFloat(ctx, source, rs, length, 64)
  1546  	case types.T_time:
  1547  		rs := vector.MustFunctionResult[types.Time](result)
  1548  		return decimal128ToTime(source, rs, length)
  1549  	case types.T_timestamp:
  1550  		rs := vector.MustFunctionResult[types.Timestamp](result)
  1551  		return decimal128ToTimestamp(source, rs, length)
  1552  	case types.T_char, types.T_varchar, types.T_blob,
  1553  		types.T_binary, types.T_varbinary, types.T_text:
  1554  		rs := vector.MustFunctionResult[types.Varlena](result)
  1555  		return decimal128ToStr(ctx, source, rs, length, toType)
  1556  	}
  1557  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from decimal128 to %s", toType))
  1558  }
  1559  
  1560  func strTypeToOthers(proc *process.Process,
  1561  	source vector.FunctionParameterWrapper[types.Varlena],
  1562  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1563  	ctx := proc.Ctx
  1564  
  1565  	fromType := source.GetType()
  1566  	if fromType.Oid == types.T_blob {
  1567  		// For handling BLOB to ARRAY implicit casting.
  1568  		// This is used for VECTOR FAST/BINARY IO.
  1569  		// SQL: insert into t2 values(2, decode("7e98b23e9e10383b2f41133f", "hex"));
  1570  		switch toType.Oid {
  1571  		case types.T_array_float32:
  1572  			rs := vector.MustFunctionResult[types.Varlena](result)
  1573  			return blobToArray[float32](ctx, source, rs, length, toType)
  1574  		case types.T_array_float64:
  1575  			rs := vector.MustFunctionResult[types.Varlena](result)
  1576  			return blobToArray[float64](ctx, source, rs, length, toType)
  1577  			// NOTE 1: don't add `switch default` and panic here. If `T_blob` to `ARRAY` is not required,
  1578  			// then continue to the `str` to `Other` code.
  1579  			// NOTE 2: don't create a switch T_blob case in NewCast() as
  1580  			// we only want to handle BLOB-->ARRAY condition separately and
  1581  			// rest of the flow is similar to strTypeToOthers.
  1582  		}
  1583  	}
  1584  	switch toType.Oid {
  1585  	case types.T_bit:
  1586  		rs := vector.MustFunctionResult[uint64](result)
  1587  		return strToBit(ctx, source, rs, int(toType.Width), length)
  1588  	case types.T_int8:
  1589  		rs := vector.MustFunctionResult[int8](result)
  1590  		return strToSigned(ctx, source, rs, 8, length)
  1591  	case types.T_int16:
  1592  		rs := vector.MustFunctionResult[int16](result)
  1593  		return strToSigned(ctx, source, rs, 16, length)
  1594  	case types.T_int32:
  1595  		rs := vector.MustFunctionResult[int32](result)
  1596  		return strToSigned(ctx, source, rs, 32, length)
  1597  	case types.T_int64:
  1598  		rs := vector.MustFunctionResult[int64](result)
  1599  		return strToSigned(ctx, source, rs, 64, length)
  1600  	case types.T_uint8:
  1601  		rs := vector.MustFunctionResult[uint8](result)
  1602  		return strToUnsigned(ctx, source, rs, 8, length)
  1603  	case types.T_uint16:
  1604  		rs := vector.MustFunctionResult[uint16](result)
  1605  		return strToUnsigned(ctx, source, rs, 16, length)
  1606  	case types.T_uint32:
  1607  		rs := vector.MustFunctionResult[uint32](result)
  1608  		return strToUnsigned(ctx, source, rs, 32, length)
  1609  	case types.T_uint64:
  1610  		rs := vector.MustFunctionResult[uint64](result)
  1611  		return strToUnsigned(ctx, source, rs, 64, length)
  1612  	case types.T_float32:
  1613  		rs := vector.MustFunctionResult[float32](result)
  1614  		return strToFloat(ctx, source, rs, 32, length)
  1615  	case types.T_float64:
  1616  		rs := vector.MustFunctionResult[float64](result)
  1617  		return strToFloat(ctx, source, rs, 64, length)
  1618  	case types.T_decimal64:
  1619  		rs := vector.MustFunctionResult[types.Decimal64](result)
  1620  		return strToDecimal64(source, rs, length)
  1621  	case types.T_decimal128:
  1622  		rs := vector.MustFunctionResult[types.Decimal128](result)
  1623  		return strToDecimal128(source, rs, length)
  1624  	case types.T_bool:
  1625  		rs := vector.MustFunctionResult[bool](result)
  1626  		return strToBool(source, rs, length)
  1627  	case types.T_json:
  1628  		rs := vector.MustFunctionResult[types.Varlena](result)
  1629  		return strToJson(source, rs, length)
  1630  	case types.T_uuid:
  1631  		rs := vector.MustFunctionResult[types.Uuid](result)
  1632  		return strToUuid(source, rs, length)
  1633  	case types.T_date:
  1634  		rs := vector.MustFunctionResult[types.Date](result)
  1635  		return strToDate(source, rs, length)
  1636  	case types.T_datetime:
  1637  		rs := vector.MustFunctionResult[types.Datetime](result)
  1638  		return strToDatetime(source, rs, length)
  1639  	case types.T_time:
  1640  		rs := vector.MustFunctionResult[types.Time](result)
  1641  		return strToTime(source, rs, length)
  1642  	case types.T_timestamp:
  1643  		rs := vector.MustFunctionResult[types.Timestamp](result)
  1644  		zone := time.Local
  1645  		if proc != nil {
  1646  			zone = proc.SessionInfo.TimeZone
  1647  		}
  1648  		return strToTimestamp(source, rs, zone, length)
  1649  	case types.T_char, types.T_varchar, types.T_text,
  1650  		types.T_binary, types.T_varbinary, types.T_blob:
  1651  		rs := vector.MustFunctionResult[types.Varlena](result)
  1652  		return strToStr(ctx, source, rs, length, toType)
  1653  	case types.T_array_float32:
  1654  		rs := vector.MustFunctionResult[types.Varlena](result)
  1655  		return strToArray[float32](ctx, source, rs, length, toType)
  1656  	case types.T_array_float64:
  1657  		rs := vector.MustFunctionResult[types.Varlena](result)
  1658  		return strToArray[float64](ctx, source, rs, length, toType)
  1659  	}
  1660  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from %s to %s", source.GetType(), toType))
  1661  }
  1662  
  1663  func arrayTypeToOthers(proc *process.Process,
  1664  	source vector.FunctionParameterWrapper[types.Varlena],
  1665  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1666  	ctx := proc.Ctx
  1667  	rs := vector.MustFunctionResult[types.Varlena](result)
  1668  	fromType := source.GetType()
  1669  
  1670  	switch fromType.Oid {
  1671  	case types.T_array_float32:
  1672  		switch toType.Oid {
  1673  		case types.T_array_float32:
  1674  			return arrayToArray[float32, float32](proc.Ctx, source, rs, length, toType)
  1675  		case types.T_array_float64:
  1676  			return arrayToArray[float32, float64](proc.Ctx, source, rs, length, toType)
  1677  		}
  1678  	case types.T_array_float64:
  1679  		switch toType.Oid {
  1680  		case types.T_array_float32:
  1681  			return arrayToArray[float64, float32](proc.Ctx, source, rs, length, toType)
  1682  		case types.T_array_float64:
  1683  			return arrayToArray[float64, float64](proc.Ctx, source, rs, length, toType)
  1684  		}
  1685  	}
  1686  
  1687  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from %s to %s", fromType, toType))
  1688  }
  1689  
  1690  func uuidToOthers(ctx context.Context,
  1691  	source vector.FunctionParameterWrapper[types.Uuid],
  1692  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1693  	switch toType.Oid {
  1694  	case types.T_char, types.T_varchar, types.T_blob,
  1695  		types.T_binary, types.T_varbinary, types.T_text:
  1696  		rs := vector.MustFunctionResult[types.Varlena](result)
  1697  		return uuidToStr(ctx, source, rs, length, toType)
  1698  	}
  1699  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from uuid to %s", toType))
  1700  }
  1701  
  1702  func tsToOthers(ctx context.Context,
  1703  	source vector.FunctionParameterWrapper[types.TS],
  1704  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1705  	if toType.Oid == types.T_TS {
  1706  		rs := vector.MustFunctionResult[types.TS](result)
  1707  		return rs.DupFromParameter(source, length)
  1708  	}
  1709  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from ts to %s", toType))
  1710  }
  1711  
  1712  func rowidToOthers(ctx context.Context,
  1713  	source vector.FunctionParameterWrapper[types.Rowid],
  1714  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1715  	if toType.Oid == types.T_Rowid {
  1716  		rs := vector.MustFunctionResult[types.Rowid](result)
  1717  		return rs.DupFromParameter(source, length)
  1718  	}
  1719  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from rowid to %s", toType))
  1720  }
  1721  
  1722  func blockidToOthers(ctx context.Context,
  1723  	source vector.FunctionParameterWrapper[types.Blockid],
  1724  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1725  	if toType.Oid == types.T_Blockid {
  1726  		rs := vector.MustFunctionResult[types.Blockid](result)
  1727  		return rs.DupFromParameter(source, length)
  1728  	}
  1729  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from blockid to %s", toType))
  1730  }
  1731  
  1732  func jsonToOthers(ctx context.Context,
  1733  	source vector.FunctionParameterWrapper[types.Varlena],
  1734  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1735  	switch toType.Oid {
  1736  	case types.T_char, types.T_varchar, types.T_text:
  1737  		rs := vector.MustFunctionResult[types.Varlena](result)
  1738  		return jsonToStr(ctx, source, rs, length)
  1739  	}
  1740  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from json to %s", toType))
  1741  }
  1742  
  1743  func enumToOthers(ctx context.Context,
  1744  	source vector.FunctionParameterWrapper[types.Enum],
  1745  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1746  	switch toType.Oid {
  1747  	case types.T_uint16, types.T_uint8, types.T_uint32, types.T_uint64, types.T_uint128:
  1748  		rs := vector.MustFunctionResult[uint16](result)
  1749  		return enumToUint16(source, rs, length)
  1750  	case types.T_char, types.T_varchar, types.T_binary, types.T_varbinary, types.T_blob, types.T_text:
  1751  		rs := vector.MustFunctionResult[types.Varlena](result)
  1752  		return enumToStr(ctx, source, rs, length)
  1753  	}
  1754  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from enum to %s", toType.String()))
  1755  }
  1756  
  1757  func integerToFixFloat[T1, T2 constraints.Integer | constraints.Float](
  1758  	ctx context.Context,
  1759  	from vector.FunctionParameterWrapper[T1], to *vector.FunctionResult[T2], length uint64) error {
  1760  
  1761  	max_value := math.Pow10(int(to.GetType().Width-to.GetType().Scale)) - 1
  1762  	var i uint64
  1763  	var dftValue T2
  1764  	for i = 0; i < length; i++ {
  1765  		v, isnull := from.GetValue(i)
  1766  		if isnull {
  1767  			if err := to.Append(dftValue, true); err != nil {
  1768  				return err
  1769  			}
  1770  		} else {
  1771  			if float64(v) < -max_value || float64(v) > max_value {
  1772  				return moerr.NewOutOfRange(ctx, "float", "value '%v'", v)
  1773  			}
  1774  			if err := to.Append(T2(v), false); err != nil {
  1775  				return err
  1776  			}
  1777  		}
  1778  	}
  1779  	return nil
  1780  }
  1781  
  1782  func floatToFixFloat[T1, T2 constraints.Float](
  1783  	ctx context.Context,
  1784  	from vector.FunctionParameterWrapper[T1], to *vector.FunctionResult[T2], length int) error {
  1785  
  1786  	pow := math.Pow10(int(to.GetType().Scale))
  1787  	max_value := math.Pow10(int(to.GetType().Width - to.GetType().Scale))
  1788  	max_value -= 1.0 / pow
  1789  	var i uint64
  1790  	var dftValue T2
  1791  	for i = 0; i < uint64(length); i++ {
  1792  		v, isnull := from.GetValue(i)
  1793  		if isnull {
  1794  			if err := to.Append(dftValue, true); err != nil {
  1795  				return err
  1796  			}
  1797  		} else {
  1798  			v2 := float64(v)
  1799  			tmp := math.Round((v2-math.Floor(v2))*pow) / pow
  1800  			v2 = math.Floor(v2) + tmp
  1801  			if v2 < -max_value || v2 > max_value {
  1802  				return moerr.NewOutOfRange(ctx, "float", "value '%v'", v)
  1803  			}
  1804  			if err := to.Append(T2(v2), false); err != nil {
  1805  				return err
  1806  			}
  1807  		}
  1808  	}
  1809  	return nil
  1810  }
  1811  
  1812  func floatNumToFixFloat[T1 constraints.Float](
  1813  	ctx context.Context, from float64, to *vector.FunctionResult[T1], originStr string) (T1, error) {
  1814  
  1815  	pow := math.Pow10(int(to.GetType().Scale))
  1816  	max_value := math.Pow10(int(to.GetType().Width - to.GetType().Scale))
  1817  	max_value -= 1.0 / pow
  1818  
  1819  	tmp := math.Round((from-math.Floor(from))*pow) / pow
  1820  	v := math.Floor(from) + tmp
  1821  	if v < -max_value || v > max_value {
  1822  		if originStr == "" {
  1823  			return 0, moerr.NewOutOfRange(ctx, "float", "value '%v'", from)
  1824  		} else {
  1825  			return 0, moerr.NewOutOfRange(ctx, "float", "value '%s'", originStr)
  1826  		}
  1827  	}
  1828  	return T1(v), nil
  1829  }
  1830  
  1831  // XXX do not use it to cast float to integer, please use floatToInteger
  1832  func numericToNumeric[T1, T2 constraints.Integer | constraints.Float](
  1833  	ctx context.Context,
  1834  	from vector.FunctionParameterWrapper[T1], to *vector.FunctionResult[T2], length int) error {
  1835  	var i uint64
  1836  	var dftValue T2
  1837  	times := uint64(length)
  1838  
  1839  	if to.GetType().Scale >= 0 && to.GetType().Width > 0 {
  1840  		return integerToFixFloat(ctx, from, to, times)
  1841  	}
  1842  
  1843  	if err := overflowForNumericToNumeric[T1, T2](ctx, from.UnSafeGetAllValue(),
  1844  		from.GetSourceVector().GetNulls()); err != nil {
  1845  		return err
  1846  	}
  1847  
  1848  	for i = 0; i < times; i++ {
  1849  		v, isnull := from.GetValue(i)
  1850  		if isnull {
  1851  			if err := to.Append(dftValue, true); err != nil {
  1852  				return err
  1853  			}
  1854  		} else {
  1855  			if err := to.Append(T2(v), false); err != nil {
  1856  				return err
  1857  			}
  1858  		}
  1859  	}
  1860  	return nil
  1861  }
  1862  
  1863  func numericToBit[T constraints.Integer | constraints.Float](
  1864  	ctx context.Context,
  1865  	from vector.FunctionParameterWrapper[T],
  1866  	to *vector.FunctionResult[uint64], bitSize int,
  1867  	length int) error {
  1868  	for i := 0; i < length; i++ {
  1869  		v, null := from.GetValue(uint64(i))
  1870  		if null {
  1871  			if err := to.Append(uint64(0), true); err != nil {
  1872  				return err
  1873  			}
  1874  		} else {
  1875  			var val uint64
  1876  			switch any(v).(type) {
  1877  			case float32, float64:
  1878  				val = uint64(math.Round(float64(v)))
  1879  			default:
  1880  				val = uint64(v)
  1881  			}
  1882  
  1883  			if val > uint64(1<<bitSize-1) {
  1884  				return moerr.NewOutOfRange(ctx, fmt.Sprintf("int%d", bitSize), "value %d", val)
  1885  			}
  1886  			if err := to.Append(val, false); err != nil {
  1887  				return err
  1888  			}
  1889  		}
  1890  	}
  1891  	return nil
  1892  }
  1893  
  1894  // XXX do not use it to cast float to integer, please use floatToInteger
  1895  func floatToInteger[T1 constraints.Float, T2 constraints.Integer](
  1896  	ctx context.Context,
  1897  	from vector.FunctionParameterWrapper[T1], to *vector.FunctionResult[T2],
  1898  	length int) error {
  1899  	var i uint64
  1900  	var dftValue T2
  1901  	times := uint64(length)
  1902  	if err := overflowForNumericToNumeric[T1, T2](ctx, from.UnSafeGetAllValue(), from.GetSourceVector().GetNulls()); err != nil {
  1903  		return err
  1904  	}
  1905  	for i = 0; i < times; i++ {
  1906  		v, isnull := from.GetValue(i)
  1907  		if isnull {
  1908  			if err := to.Append(dftValue, true); err != nil {
  1909  				return err
  1910  			}
  1911  		} else {
  1912  			if err := to.Append(T2(math.Round(float64(v))), false); err != nil {
  1913  				return err
  1914  			}
  1915  		}
  1916  	}
  1917  	return nil
  1918  }
  1919  
  1920  func numericToBool[T constraints.Integer | constraints.Float](
  1921  	from vector.FunctionParameterWrapper[T],
  1922  	to *vector.FunctionResult[bool], length int) error {
  1923  	var i uint64
  1924  	l := uint64(length)
  1925  	for i = 0; i < l; i++ {
  1926  		v, null := from.GetValue(i)
  1927  		err := to.Append(v != 0, null)
  1928  		if err != nil {
  1929  			return err
  1930  		}
  1931  	}
  1932  	return nil
  1933  }
  1934  
  1935  func boolToStr(
  1936  	from vector.FunctionParameterWrapper[bool],
  1937  	to *vector.FunctionResult[types.Varlena], length int, toType types.Type) error {
  1938  	var i uint64
  1939  	l := uint64(length)
  1940  	// Here cast using cast(data_type as binary[(n)]).
  1941  	if toType.Oid == types.T_binary && toType.Scale == -1 {
  1942  		for i = 0; i < l; i++ {
  1943  			v, null := from.GetValue(i)
  1944  			var v1 []byte
  1945  			if v {
  1946  				v1 = []byte("1")
  1947  			} else {
  1948  				v1 = []byte("0")
  1949  			}
  1950  			if err := explicitCastToBinary(toType, v1, null, to); err != nil {
  1951  				return err
  1952  			}
  1953  		}
  1954  		return nil
  1955  	}
  1956  	for i = 0; i < l; i++ {
  1957  		v, null := from.GetValue(i)
  1958  		if null {
  1959  			if err := to.AppendBytes(nil, true); err != nil {
  1960  				return err
  1961  			}
  1962  		} else {
  1963  			result := []byte("0")
  1964  			if v {
  1965  				result = []byte("1")
  1966  			}
  1967  			if toType.Oid == types.T_binary {
  1968  				for len(result) < int(toType.Width) {
  1969  					result = append(result, 0)
  1970  				}
  1971  			}
  1972  			if err := to.AppendBytes(result, false); err != nil {
  1973  				return err
  1974  			}
  1975  		}
  1976  	}
  1977  	return nil
  1978  }
  1979  
  1980  func boolToInteger[T constraints.Integer](
  1981  	from vector.FunctionParameterWrapper[bool],
  1982  	to *vector.FunctionResult[T], length int) error {
  1983  	var i uint64
  1984  	l := uint64(length)
  1985  	var dft T
  1986  	for i = 0; i < l; i++ {
  1987  		v, null := from.GetValue(i)
  1988  		if null {
  1989  			if err := to.Append(dft, true); err != nil {
  1990  				return err
  1991  			}
  1992  		} else {
  1993  			if v {
  1994  				if err := to.Append(1, false); err != nil {
  1995  					return err
  1996  				}
  1997  			} else {
  1998  				if err := to.Append(0, false); err != nil {
  1999  					return err
  2000  				}
  2001  			}
  2002  		}
  2003  	}
  2004  	return nil
  2005  }
  2006  
  2007  func bitToStr(
  2008  	ctx context.Context,
  2009  	from vector.FunctionParameterWrapper[uint64],
  2010  	to *vector.FunctionResult[types.Varlena], length int, toType types.Type) error {
  2011  
  2012  	uint64ToBytes := func(v uint64) []byte {
  2013  		b := types.EncodeUint64(&v)
  2014  		// remove leading zero bytes
  2015  		l := len(b)
  2016  		for l > 1 && b[l-1] == byte(0) {
  2017  			l -= 1
  2018  		}
  2019  		return b[:l]
  2020  	}
  2021  
  2022  	if toType.Oid == types.T_binary && toType.Scale == -1 {
  2023  		for i := 0; i < length; i++ {
  2024  			v, null := from.GetValue(uint64(i))
  2025  			b := uint64ToBytes(v)
  2026  			if err := explicitCastToBinary(toType, b, null, to); err != nil {
  2027  				return err
  2028  			}
  2029  		}
  2030  		return nil
  2031  	}
  2032  
  2033  	for i := 0; i < length; i++ {
  2034  		v, null := from.GetValue(uint64(i))
  2035  		if null {
  2036  			if err := to.AppendBytes(nil, true); err != nil {
  2037  				return err
  2038  			}
  2039  			continue
  2040  		}
  2041  
  2042  		b := uint64ToBytes(v)
  2043  		if toType.Oid == types.T_binary || toType.Oid == types.T_varbinary {
  2044  			if len(b) > int(toType.Width) {
  2045  				return moerr.NewDataTruncatedNoCtx("Bit", "truncated for binary/varbinary")
  2046  			}
  2047  		}
  2048  
  2049  		slices.Reverse(b)
  2050  		if toType.Oid == types.T_binary {
  2051  			for len(b) < int(toType.Width) {
  2052  				b = append(b, byte(0))
  2053  			}
  2054  		}
  2055  		if len(b) > int(toType.Width) && toType.Oid != types.T_text && toType.Oid != types.T_blob {
  2056  			return formatCastError(ctx, from.GetSourceVector(), toType, fmt.Sprintf(
  2057  				"%v is larger than Dest length %v", v, toType.Width))
  2058  		}
  2059  		if err := to.AppendBytes(b, false); err != nil {
  2060  			return err
  2061  		}
  2062  	}
  2063  	return nil
  2064  }
  2065  
  2066  func signedToDecimal64[T1 constraints.Signed](
  2067  	from vector.FunctionParameterWrapper[T1],
  2068  	to *vector.FunctionResult[types.Decimal64], length int) error {
  2069  	var i uint64
  2070  	l := uint64(length)
  2071  	var dft types.Decimal64
  2072  	totype := to.GetType()
  2073  	for i = 0; i < l; i++ {
  2074  		v, null := from.GetValue(i)
  2075  		if null {
  2076  			if err := to.Append(dft, true); err != nil {
  2077  				return err
  2078  			}
  2079  		} else {
  2080  			result, _ := types.Decimal64(uint64(v)).Scale(totype.Scale)
  2081  			if err := to.Append(result, false); err != nil {
  2082  				return err
  2083  			}
  2084  		}
  2085  	}
  2086  	return nil
  2087  }
  2088  
  2089  func signedToDecimal128[T1 constraints.Signed](
  2090  	from vector.FunctionParameterWrapper[T1],
  2091  	to *vector.FunctionResult[types.Decimal128], length int) error {
  2092  	var i uint64
  2093  	l := uint64(length)
  2094  	var dft types.Decimal128
  2095  	totype := to.GetType()
  2096  	for i = 0; i < l; i++ {
  2097  		v, null := from.GetValue(i)
  2098  		if null {
  2099  			if err := to.Append(dft, true); err != nil {
  2100  				return err
  2101  			}
  2102  		} else {
  2103  			result := types.Decimal128{B0_63: uint64(v), B64_127: 0}
  2104  			if v < 0 {
  2105  				result.B64_127 = ^result.B64_127
  2106  			}
  2107  			result, _ = result.Scale(totype.Scale)
  2108  			if err := to.Append(result, false); err != nil {
  2109  				return err
  2110  			}
  2111  		}
  2112  	}
  2113  	return nil
  2114  }
  2115  
  2116  func unsignedToDecimal64[T1 constraints.Unsigned](
  2117  	from vector.FunctionParameterWrapper[T1],
  2118  	to *vector.FunctionResult[types.Decimal64], length int) error {
  2119  	var i uint64
  2120  	l := uint64(length)
  2121  	var dft types.Decimal64
  2122  	totype := to.GetType()
  2123  	for i = 0; i < l; i++ {
  2124  		v, null := from.GetValue(i)
  2125  		if null {
  2126  			if err := to.Append(dft, true); err != nil {
  2127  				return err
  2128  			}
  2129  		} else {
  2130  			result := types.Decimal64(uint64(v))
  2131  			result, _ = result.Scale(totype.Scale)
  2132  			if err := to.Append(result, false); err != nil {
  2133  				return err
  2134  			}
  2135  		}
  2136  	}
  2137  	return nil
  2138  }
  2139  
  2140  func unsignedToDecimal128[T1 constraints.Unsigned](
  2141  	from vector.FunctionParameterWrapper[T1],
  2142  	to *vector.FunctionResult[types.Decimal128], length int) error {
  2143  	var i uint64
  2144  	l := uint64(length)
  2145  	var dft types.Decimal128
  2146  	totype := to.GetType()
  2147  	for i = 0; i < l; i++ {
  2148  		v, null := from.GetValue(i)
  2149  		if null {
  2150  			if err := to.Append(dft, true); err != nil {
  2151  				return err
  2152  			}
  2153  		} else {
  2154  			result := types.Decimal128{B0_63: uint64(v), B64_127: 0}
  2155  			result, _ = result.Scale(totype.Scale)
  2156  			if err := to.Append(result, false); err != nil {
  2157  				return err
  2158  			}
  2159  		}
  2160  	}
  2161  	return nil
  2162  }
  2163  
  2164  func floatToDecimal64[T constraints.Float](
  2165  	from vector.FunctionParameterWrapper[T],
  2166  	to *vector.FunctionResult[types.Decimal64], length int) error {
  2167  	var i uint64
  2168  	l := uint64(length)
  2169  	var dft types.Decimal64
  2170  	toType := to.GetType()
  2171  
  2172  	for i = 0; i < l; i++ {
  2173  		v, null := from.GetValue(i)
  2174  		if null {
  2175  			if err := to.Append(dft, true); err != nil {
  2176  				return err
  2177  			}
  2178  		} else {
  2179  			result64, err := types.Decimal64FromFloat64(float64(v), toType.Width, toType.Scale)
  2180  			if err != nil {
  2181  				return err
  2182  			}
  2183  			if err = to.Append(result64, false); err != nil {
  2184  				return err
  2185  			}
  2186  		}
  2187  	}
  2188  	return nil
  2189  }
  2190  
  2191  func floatToDecimal128[T constraints.Float](
  2192  	from vector.FunctionParameterWrapper[T],
  2193  	to *vector.FunctionResult[types.Decimal128], length int) error {
  2194  	var i uint64
  2195  	l := uint64(length)
  2196  	var dft types.Decimal128
  2197  	toType := to.GetType()
  2198  
  2199  	for i = 0; i < l; i++ {
  2200  		v, null := from.GetValue(i)
  2201  		if null {
  2202  			if err := to.Append(dft, true); err != nil {
  2203  				return err
  2204  			}
  2205  		} else {
  2206  			result128, err := types.Decimal128FromFloat64(float64(v), toType.Width, toType.Scale)
  2207  			if err != nil {
  2208  				return err
  2209  			}
  2210  			if err = to.Append(result128, false); err != nil {
  2211  				return err
  2212  			}
  2213  		}
  2214  	}
  2215  	return nil
  2216  }
  2217  
  2218  func signedToStr[T constraints.Integer](
  2219  	ctx context.Context,
  2220  	from vector.FunctionParameterWrapper[T],
  2221  	to *vector.FunctionResult[types.Varlena], length int, toType types.Type) error {
  2222  	var i uint64
  2223  	l := uint64(length)
  2224  	// Here cast using cast(data_type as binary[(n)]).
  2225  	if toType.Oid == types.T_binary && toType.Scale == -1 {
  2226  		for i = 0; i < l; i++ {
  2227  			v, null := from.GetValue(i)
  2228  			v1 := []byte(strconv.FormatInt(int64(v), 10))
  2229  			if err := explicitCastToBinary(toType, v1, null, to); err != nil {
  2230  				return err
  2231  			}
  2232  		}
  2233  		return nil
  2234  	}
  2235  
  2236  	for i = 0; i < l; i++ {
  2237  		v, null := from.GetValue(i)
  2238  		if null {
  2239  			if err := to.AppendBytes(nil, true); err != nil {
  2240  				return err
  2241  			}
  2242  		} else {
  2243  			result := []byte(strconv.FormatInt(int64(v), 10))
  2244  			if toType.Oid == types.T_binary || toType.Oid == types.T_varbinary {
  2245  				if int32(len(result)) > toType.Width {
  2246  					return moerr.NewDataTruncatedNoCtx("Signed", " truncated for binary/varbinary")
  2247  				}
  2248  			}
  2249  			if toType.Oid == types.T_binary && len(result) < int(toType.Width) {
  2250  				add0 := int(toType.Width) - len(result)
  2251  				for ; add0 != 0; add0-- {
  2252  					result = append(result, 0)
  2253  				}
  2254  			}
  2255  			if len(result) > int(toType.Width) && toType.Oid != types.T_text && toType.Oid != types.T_blob {
  2256  				return formatCastError(ctx, from.GetSourceVector(), toType, fmt.Sprintf(
  2257  					"%v is larger than Dest length %v", v, toType.Width))
  2258  			}
  2259  			if err := to.AppendBytes(result, false); err != nil {
  2260  				return err
  2261  			}
  2262  		}
  2263  	}
  2264  	return nil
  2265  }
  2266  
  2267  func unsignedToStr[T constraints.Unsigned](
  2268  	ctx context.Context,
  2269  	from vector.FunctionParameterWrapper[T],
  2270  	to *vector.FunctionResult[types.Varlena], length int, toType types.Type) error {
  2271  	var i uint64
  2272  	l := uint64(length)
  2273  	// Here cast using cast(data_type as binary[(n)]).
  2274  	if toType.Oid == types.T_binary && toType.Scale == -1 {
  2275  		for i = 0; i < l; i++ {
  2276  			v, null := from.GetValue(i)
  2277  			v1 := []byte(strconv.FormatUint(uint64(v), 10))
  2278  			if err := explicitCastToBinary(toType, v1, null, to); err != nil {
  2279  				return err
  2280  			}
  2281  		}
  2282  		return nil
  2283  	}
  2284  	for i = 0; i < l; i++ {
  2285  		v, null := from.GetValue(i)
  2286  		if null {
  2287  			if err := to.AppendBytes(nil, true); err != nil {
  2288  				return err
  2289  			}
  2290  		} else {
  2291  			result := []byte(strconv.FormatUint(uint64(v), 10))
  2292  			if toType.Oid == types.T_binary || toType.Oid == types.T_varbinary {
  2293  				if int32(len(result)) > toType.Width {
  2294  					return moerr.NewDataTruncatedNoCtx("Unsigned", "truncated for binary/varbinary")
  2295  				}
  2296  			}
  2297  			if toType.Oid == types.T_binary && len(result) < int(toType.Width) {
  2298  				add0 := int(toType.Width) - len(result)
  2299  				for ; add0 != 0; add0-- {
  2300  					result = append(result, 0)
  2301  				}
  2302  			}
  2303  			if len(result) > int(toType.Width) && toType.Oid != types.T_text && toType.Oid != types.T_blob {
  2304  				return formatCastError(ctx, from.GetSourceVector(), toType, fmt.Sprintf(
  2305  					"%v is larger than Dest length %v", v, toType.Width))
  2306  			}
  2307  			if err := to.AppendBytes(result, false); err != nil {
  2308  				return err
  2309  			}
  2310  		}
  2311  	}
  2312  	return nil
  2313  }
  2314  
  2315  func floatToStr[T constraints.Float](
  2316  	ctx context.Context,
  2317  	from vector.FunctionParameterWrapper[T],
  2318  	to *vector.FunctionResult[types.Varlena], length int, toType types.Type) error {
  2319  	var i uint64
  2320  	l := uint64(length)
  2321  	bitSize := int(unsafe.Sizeof(T(0)) * 8)
  2322  	// Here cast using cast(data_type as binary[(n)]).
  2323  	if toType.Oid == types.T_binary && toType.Scale == -1 {
  2324  		for i = 0; i < l; i++ {
  2325  			v, null := from.GetValue(i)
  2326  			v1 := floatToBytes(float64(v), bitSize)
  2327  			if err := explicitCastToBinary(toType, v1, null, to); err != nil {
  2328  				return err
  2329  			}
  2330  		}
  2331  		return nil
  2332  	}
  2333  	for i = 0; i < l; i++ {
  2334  		v, null := from.GetValue(i)
  2335  		if null {
  2336  			if err := to.AppendBytes(nil, true); err != nil {
  2337  				return err
  2338  			}
  2339  		} else {
  2340  			// float to string, [-14,15] convert to exponent.
  2341  			result := floatToBytes(float64(v), bitSize)
  2342  			if toType.Oid == types.T_binary || toType.Oid == types.T_varbinary {
  2343  				if int32(len(result)) > toType.Width {
  2344  					return moerr.NewDataTruncatedNoCtx("Float", "truncated for binary/varbinary")
  2345  				}
  2346  			}
  2347  			if toType.Oid == types.T_binary && len(result) < int(toType.Width) {
  2348  				add0 := int(toType.Width) - len(result)
  2349  				for ; add0 != 0; add0-- {
  2350  					result = append(result, 0)
  2351  				}
  2352  			}
  2353  			if len(result) > int(toType.Width) && toType.Oid != types.T_text && toType.Oid != types.T_blob {
  2354  				return formatCastError(ctx, from.GetSourceVector(), toType, fmt.Sprintf(
  2355  					"%v is larger than Dest length %v", v, toType.Width))
  2356  			}
  2357  			if err := to.AppendBytes(result, false); err != nil {
  2358  				return err
  2359  			}
  2360  		}
  2361  	}
  2362  	return nil
  2363  }
  2364  
  2365  func integerToTimestamp[T constraints.Integer](
  2366  	from vector.FunctionParameterWrapper[T],
  2367  	to *vector.FunctionResult[types.Timestamp], length int) error {
  2368  	var i uint64
  2369  	l := uint64(length)
  2370  	var dft types.Timestamp
  2371  	// XXX what is the 32536771199
  2372  	for i = 0; i < l; i++ {
  2373  		v, null := from.GetValue(i)
  2374  		if null || v < 0 || uint64(v) > 32536771199 {
  2375  			if err := to.Append(dft, true); err != nil {
  2376  				return err
  2377  			}
  2378  		} else {
  2379  			result := types.UnixToTimestamp(int64(v))
  2380  			if err := to.Append(result, false); err != nil {
  2381  				return err
  2382  			}
  2383  		}
  2384  	}
  2385  	return nil
  2386  }
  2387  
  2388  func integerToTime[T constraints.Integer](
  2389  	ctx context.Context,
  2390  	from vector.FunctionParameterWrapper[T],
  2391  	to *vector.FunctionResult[types.Time], length int) error {
  2392  	var i uint64
  2393  	l := uint64(length)
  2394  	var dft types.Time
  2395  	toType := to.GetType()
  2396  	for i = 0; i < l; i++ {
  2397  		v, null := from.GetValue(i)
  2398  		vI64 := int64(v)
  2399  		if null {
  2400  			if err := to.Append(dft, true); err != nil {
  2401  				return err
  2402  			}
  2403  		} else {
  2404  			if vI64 < types.MinInputIntTime || vI64 > types.MaxInputIntTime {
  2405  				return moerr.NewOutOfRange(ctx, "time", "value %d", v)
  2406  			}
  2407  			result, err := types.ParseInt64ToTime(vI64, toType.Scale)
  2408  			if err != nil {
  2409  				return err
  2410  			}
  2411  			if err = to.Append(result, false); err != nil {
  2412  				return err
  2413  			}
  2414  		}
  2415  	}
  2416  	return nil
  2417  }
  2418  
  2419  func integerToEnum[T constraints.Integer](
  2420  	ctx context.Context,
  2421  	from vector.FunctionParameterWrapper[T],
  2422  	to *vector.FunctionResult[types.Enum], length int) error {
  2423  	var i uint64
  2424  	l := uint64(length)
  2425  	var dft types.Enum
  2426  	for i = 0; i < l; i++ {
  2427  		v, null := from.GetValue(i)
  2428  		vI64 := int64(v)
  2429  		if null {
  2430  			if err := to.Append(dft, true); err != nil {
  2431  				return err
  2432  			}
  2433  		} else {
  2434  			if vI64 < 1 || vI64 > types.MaxEnumLen {
  2435  				return moerr.NewOutOfRange(ctx, "enum", "value %d", v)
  2436  			}
  2437  			result, err := types.ParseIntToEnum(vI64)
  2438  			if err != nil {
  2439  				return err
  2440  			}
  2441  			if err = to.Append(result, false); err != nil {
  2442  				return err
  2443  			}
  2444  		}
  2445  	}
  2446  	return nil
  2447  }
  2448  
  2449  func dateToSigned[T int32 | int64](
  2450  	from vector.FunctionParameterWrapper[types.Date],
  2451  	to *vector.FunctionResult[T], length int) error {
  2452  	var i uint64
  2453  	for i = 0; i < uint64(length); i++ {
  2454  		v, null := from.GetValue(i)
  2455  		if null {
  2456  			if err := to.Append(0, true); err != nil {
  2457  				return err
  2458  			}
  2459  		} else {
  2460  			val := v.DaysSinceUnixEpoch()
  2461  			if err := to.Append(T(val), false); err != nil {
  2462  				return err
  2463  			}
  2464  		}
  2465  	}
  2466  	return nil
  2467  }
  2468  
  2469  func dateToTime(
  2470  	from vector.FunctionParameterWrapper[types.Date],
  2471  	to *vector.FunctionResult[types.Time], length int) error {
  2472  	var i uint64
  2473  	l := uint64(length)
  2474  	for i = 0; i < l; i++ {
  2475  		v, null := from.GetValue(i)
  2476  		if null {
  2477  			if err := to.Append(0, true); err != nil {
  2478  				return err
  2479  			}
  2480  		} else {
  2481  			if err := to.Append(v.ToTime(), false); err != nil {
  2482  				return err
  2483  			}
  2484  		}
  2485  	}
  2486  	return nil
  2487  }
  2488  
  2489  func datetimeToTime(
  2490  	from vector.FunctionParameterWrapper[types.Datetime],
  2491  	to *vector.FunctionResult[types.Time], length int) error {
  2492  	var i uint64
  2493  	l := uint64(length)
  2494  	totype := to.GetType()
  2495  	for i = 0; i < l; i++ {
  2496  		v, null := from.GetValue(i)
  2497  		if null {
  2498  			if err := to.Append(0, true); err != nil {
  2499  				return err
  2500  			}
  2501  		} else {
  2502  			if err := to.Append(v.ToTime(totype.Scale), false); err != nil {
  2503  				return err
  2504  			}
  2505  		}
  2506  	}
  2507  	return nil
  2508  }
  2509  
  2510  func dateToTimestamp(
  2511  	from vector.FunctionParameterWrapper[types.Date],
  2512  	to *vector.FunctionResult[types.Timestamp], length int,
  2513  	zone *time.Location) error {
  2514  	var i uint64
  2515  	l := uint64(length)
  2516  	for i = 0; i < l; i++ {
  2517  		v, null := from.GetValue(i)
  2518  		if null {
  2519  			if err := to.Append(0, true); err != nil {
  2520  				return err
  2521  			}
  2522  		} else {
  2523  			if err := to.Append(v.ToTimestamp(zone), false); err != nil {
  2524  				return err
  2525  			}
  2526  		}
  2527  	}
  2528  	return nil
  2529  }
  2530  
  2531  func datetimeToInt32(
  2532  	ctx context.Context,
  2533  	from vector.FunctionParameterWrapper[types.Datetime],
  2534  	to *vector.FunctionResult[int32], length int) error {
  2535  	var i uint64
  2536  	l := uint64(length)
  2537  	for i = 0; i < l; i++ {
  2538  		v, null := from.GetValue(i)
  2539  		if null {
  2540  			if err := to.Append(0, true); err != nil {
  2541  				return err
  2542  			}
  2543  		} else {
  2544  			val := v.SecsSinceUnixEpoch()
  2545  			if val < math.MinInt32 || val > math.MaxInt32 {
  2546  				return moerr.NewOutOfRange(ctx, "int32", "value '%v'", val)
  2547  			}
  2548  			if err := to.Append(int32(val), false); err != nil {
  2549  				return err
  2550  			}
  2551  		}
  2552  	}
  2553  	return nil
  2554  }
  2555  
  2556  func datetimeToInt64(
  2557  	from vector.FunctionParameterWrapper[types.Datetime],
  2558  	to *vector.FunctionResult[int64], length int) error {
  2559  	var i uint64
  2560  	l := uint64(length)
  2561  	for i = 0; i < l; i++ {
  2562  		v, null := from.GetValue(i)
  2563  		if null {
  2564  			if err := to.Append(0, true); err != nil {
  2565  				return err
  2566  			}
  2567  		} else {
  2568  			val := v.SecsSinceUnixEpoch()
  2569  			if err := to.Append(val, false); err != nil {
  2570  				return err
  2571  			}
  2572  		}
  2573  	}
  2574  	return nil
  2575  }
  2576  
  2577  func datetimeToDecimal64(
  2578  	ctx context.Context,
  2579  	from vector.FunctionParameterWrapper[types.Datetime],
  2580  	to *vector.FunctionResult[types.Decimal64], length int) error {
  2581  	var i uint64
  2582  	l := uint64(length)
  2583  	var dft types.Decimal64
  2584  	for ; i < l; i++ {
  2585  		v, null := from.GetValue(i)
  2586  		if null {
  2587  			if err := to.Append(dft, true); err != nil {
  2588  				return err
  2589  			}
  2590  		} else {
  2591  			result, err := v.ToDecimal64().Scale(to.GetType().Scale - 6)
  2592  			if err != nil {
  2593  				return err
  2594  			}
  2595  			if err = to.Append(result, false); err != nil {
  2596  				return err
  2597  			}
  2598  		}
  2599  	}
  2600  	return nil
  2601  }
  2602  
  2603  func datetimeToDecimal128(
  2604  	ctx context.Context,
  2605  	from vector.FunctionParameterWrapper[types.Datetime],
  2606  	to *vector.FunctionResult[types.Decimal128], length int) error {
  2607  	var i uint64
  2608  	l := uint64(length)
  2609  	var dft types.Decimal128
  2610  	for ; i < l; i++ {
  2611  		v, null := from.GetValue(i)
  2612  		if null {
  2613  			if err := to.Append(dft, true); err != nil {
  2614  				return err
  2615  			}
  2616  		} else {
  2617  			result, err := v.ToDecimal128().Scale(to.GetType().Scale - 6)
  2618  			if err != nil {
  2619  				return err
  2620  			}
  2621  			if err = to.Append(result, false); err != nil {
  2622  				return err
  2623  			}
  2624  		}
  2625  	}
  2626  	return nil
  2627  }
  2628  
  2629  func datetimeToTimestamp(
  2630  	from vector.FunctionParameterWrapper[types.Datetime],
  2631  	to *vector.FunctionResult[types.Timestamp], length int,
  2632  	zone *time.Location) error {
  2633  	var i uint64
  2634  	l := uint64(length)
  2635  	for i = 0; i < l; i++ {
  2636  		v, null := from.GetValue(i)
  2637  		if null {
  2638  			if err := to.Append(0, true); err != nil {
  2639  				return err
  2640  			}
  2641  		} else {
  2642  			if err := to.Append(v.ToTimestamp(zone), false); err != nil {
  2643  				return err
  2644  			}
  2645  		}
  2646  	}
  2647  	return nil
  2648  }
  2649  
  2650  func dateToDatetime(
  2651  	from vector.FunctionParameterWrapper[types.Date],
  2652  	to *vector.FunctionResult[types.Datetime], length int) error {
  2653  	var i uint64
  2654  	l := uint64(length)
  2655  	for i = 0; i < l; i++ {
  2656  		v, null := from.GetValue(i)
  2657  		if null {
  2658  			if err := to.Append(0, true); err != nil {
  2659  				return err
  2660  			}
  2661  		} else {
  2662  			if err := to.Append(v.ToDatetime(), false); err != nil {
  2663  				return err
  2664  			}
  2665  		}
  2666  	}
  2667  	return nil
  2668  }
  2669  
  2670  func timestampToDatetime(
  2671  	ctx context.Context,
  2672  	from vector.FunctionParameterWrapper[types.Timestamp],
  2673  	to *vector.FunctionResult[types.Datetime], length int,
  2674  	zone *time.Location) error {
  2675  	var i uint64
  2676  	l := uint64(length)
  2677  	for i = 0; i < l; i++ {
  2678  		v, null := from.GetValue(i)
  2679  		if null {
  2680  			if err := to.Append(0, true); err != nil {
  2681  				return err
  2682  			}
  2683  		} else {
  2684  			result := v.ToDatetime(zone)
  2685  			if err := to.Append(result, false); err != nil {
  2686  				return err
  2687  			}
  2688  		}
  2689  	}
  2690  	return nil
  2691  }
  2692  
  2693  func timeToDatetime(
  2694  	from vector.FunctionParameterWrapper[types.Time],
  2695  	to *vector.FunctionResult[types.Datetime], length int) error {
  2696  	var i uint64
  2697  	l := uint64(length)
  2698  	totype := to.GetType()
  2699  	for i = 0; i < l; i++ {
  2700  		v, null := from.GetValue(i)
  2701  		if null {
  2702  			if err := to.Append(0, true); err != nil {
  2703  				return err
  2704  			}
  2705  		} else {
  2706  			if err := to.Append(v.ToDatetime(totype.Scale), false); err != nil {
  2707  				return err
  2708  			}
  2709  		}
  2710  	}
  2711  	return nil
  2712  }
  2713  
  2714  func datetimeToDate(
  2715  	from vector.FunctionParameterWrapper[types.Datetime],
  2716  	to *vector.FunctionResult[types.Date], length int) error {
  2717  	var i uint64
  2718  	l := uint64(length)
  2719  	for i = 0; i < l; i++ {
  2720  		v, null := from.GetValue(i)
  2721  		if null {
  2722  			if err := to.Append(0, true); err != nil {
  2723  				return err
  2724  			}
  2725  		} else {
  2726  			if err := to.Append(v.ToDate(), false); err != nil {
  2727  				return err
  2728  			}
  2729  		}
  2730  	}
  2731  	return nil
  2732  }
  2733  
  2734  func timestampToInt32(
  2735  	ctx context.Context,
  2736  	from vector.FunctionParameterWrapper[types.Timestamp],
  2737  	to *vector.FunctionResult[int32], length int) error {
  2738  	var i uint64
  2739  	for i = 0; i < uint64(length); i++ {
  2740  		v, null := from.GetValue(i)
  2741  		if null {
  2742  			if err := to.Append(0, true); err != nil {
  2743  				return err
  2744  			}
  2745  		} else {
  2746  			val := v.Unix()
  2747  			if val < math.MinInt32 || val > math.MaxInt32 {
  2748  				return moerr.NewOutOfRange(ctx, "int32", "value '%v'", val)
  2749  			}
  2750  			if err := to.Append(int32(val), false); err != nil {
  2751  				return err
  2752  			}
  2753  		}
  2754  	}
  2755  	return nil
  2756  }
  2757  
  2758  func timestampToInt64(
  2759  	from vector.FunctionParameterWrapper[types.Timestamp],
  2760  	to *vector.FunctionResult[int64], length int) error {
  2761  	var i uint64
  2762  	for i = 0; i < uint64(length); i++ {
  2763  		v, null := from.GetValue(i)
  2764  		if null {
  2765  			if err := to.Append(0, true); err != nil {
  2766  				return err
  2767  			}
  2768  		} else {
  2769  			val := v.Unix()
  2770  			if err := to.Append(val, false); err != nil {
  2771  				return err
  2772  			}
  2773  		}
  2774  	}
  2775  	return nil
  2776  }
  2777  
  2778  func timestampToDate(
  2779  	ctx context.Context,
  2780  	from vector.FunctionParameterWrapper[types.Timestamp],
  2781  	to *vector.FunctionResult[types.Date], length int,
  2782  	zone *time.Location) error {
  2783  	var i uint64
  2784  	l := uint64(length)
  2785  	for i = 0; i < l; i++ {
  2786  		v, null := from.GetValue(i)
  2787  		if null {
  2788  			if err := to.Append(0, true); err != nil {
  2789  				return err
  2790  			}
  2791  		} else {
  2792  			// XXX I'm not sure if it's a good way to convert it to datetime first.
  2793  			// but I just follow the old logic of old code.
  2794  			result := v.ToDatetime(zone)
  2795  			if err := to.Append(result.ToDate(), false); err != nil {
  2796  				return err
  2797  			}
  2798  		}
  2799  	}
  2800  	return nil
  2801  }
  2802  
  2803  func timestampToDecimal64(
  2804  	ctx context.Context,
  2805  	from vector.FunctionParameterWrapper[types.Timestamp],
  2806  	to *vector.FunctionResult[types.Decimal64], length int) error {
  2807  	var i uint64
  2808  	l := uint64(length)
  2809  	var dft types.Decimal64
  2810  	for ; i < l; i++ {
  2811  		v, null := from.GetValue(i)
  2812  		if null {
  2813  			if err := to.Append(dft, true); err != nil {
  2814  				return err
  2815  			}
  2816  		} else {
  2817  			result, err := v.UnixToDecimal64()
  2818  			if err != nil {
  2819  				return err
  2820  			}
  2821  			result, err = result.Scale(to.GetType().Scale - 6)
  2822  			if err != nil {
  2823  				return err
  2824  			}
  2825  			if err = to.Append(result, false); err != nil {
  2826  				return err
  2827  			}
  2828  		}
  2829  	}
  2830  	return nil
  2831  }
  2832  
  2833  func timestampToDecimal128(
  2834  	ctx context.Context,
  2835  	from vector.FunctionParameterWrapper[types.Timestamp],
  2836  	to *vector.FunctionResult[types.Decimal128], length int) error {
  2837  	var i uint64
  2838  	l := uint64(length)
  2839  	var dft types.Decimal128
  2840  	for ; i < l; i++ {
  2841  		v, null := from.GetValue(i)
  2842  		if null {
  2843  			if err := to.Append(dft, true); err != nil {
  2844  				return err
  2845  			}
  2846  		} else {
  2847  			result, err := v.UnixToDecimal128()
  2848  			if err != nil {
  2849  				return err
  2850  			}
  2851  			result, err = result.Scale(to.GetType().Scale - 6)
  2852  			if err != nil {
  2853  				return err
  2854  			}
  2855  			if err = to.Append(result, false); err != nil {
  2856  				return err
  2857  			}
  2858  		}
  2859  	}
  2860  	return nil
  2861  }
  2862  
  2863  func timeToInteger[T constraints.Integer](
  2864  	ctx context.Context,
  2865  	from vector.FunctionParameterWrapper[types.Time],
  2866  	to *vector.FunctionResult[T], length int) error {
  2867  	var i uint64
  2868  	l := uint64(length)
  2869  	var dft T
  2870  	for i = 0; i < l; i++ {
  2871  		v, null := from.GetValue(i)
  2872  		if null {
  2873  			if err := to.Append(dft, true); err != nil {
  2874  				return err
  2875  			}
  2876  		} else {
  2877  			r := v.ToInt64()
  2878  			// XXX we may need an elegant method to do overflow check.
  2879  			if err := overflowForNumericToNumeric[int64, T](ctx, []int64{r}, nil); err != nil {
  2880  				return err
  2881  			}
  2882  			if err := to.Append(T(r), false); err != nil {
  2883  				return err
  2884  			}
  2885  		}
  2886  	}
  2887  	return nil
  2888  }
  2889  
  2890  func timeToDate(
  2891  	from vector.FunctionParameterWrapper[types.Time],
  2892  	to *vector.FunctionResult[types.Date], length int) error {
  2893  	var i uint64
  2894  	l := uint64(length)
  2895  	for i = 0; i < l; i++ {
  2896  		v, null := from.GetValue(i)
  2897  		if null {
  2898  			if err := to.Append(0, true); err != nil {
  2899  				return err
  2900  			}
  2901  		} else {
  2902  			if err := to.Append(v.ToDate(), false); err != nil {
  2903  				return err
  2904  			}
  2905  		}
  2906  	}
  2907  	return nil
  2908  }
  2909  
  2910  func dateToStr(
  2911  	ctx context.Context,
  2912  	from vector.FunctionParameterWrapper[types.Date],
  2913  	to *vector.FunctionResult[types.Varlena], length int, toType types.Type) error {
  2914  	var i uint64
  2915  	l := uint64(length)
  2916  	// Here cast using cast(data_type as binary[(n)]).
  2917  	if toType.Oid == types.T_binary && toType.Scale == -1 {
  2918  		for i = 0; i < l; i++ {
  2919  			v, null := from.GetValue(i)
  2920  			v1 := []byte(v.String())
  2921  			if err := explicitCastToBinary(toType, v1, null, to); err != nil {
  2922  				return err
  2923  			}
  2924  		}
  2925  		return nil
  2926  	}
  2927  	for i = 0; i < l; i++ {
  2928  		v, null := from.GetValue(i)
  2929  		if null {
  2930  			if err := to.AppendBytes(nil, true); err != nil {
  2931  				return err
  2932  			}
  2933  		} else {
  2934  			result := []byte(v.String())
  2935  			if toType.Oid == types.T_binary || toType.Oid == types.T_varbinary {
  2936  				if int32(len(result)) > toType.Width {
  2937  					return moerr.NewDataTruncatedNoCtx("Date", "truncated for binary/varbinary")
  2938  				}
  2939  			}
  2940  			if toType.Oid == types.T_binary && len(result) < int(toType.Width) {
  2941  				add0 := int(toType.Width) - len(result)
  2942  				for ; add0 != 0; add0-- {
  2943  					result = append(result, 0)
  2944  				}
  2945  			}
  2946  			if len(result) > int(toType.Width) && toType.Oid != types.T_text && toType.Oid != types.T_blob {
  2947  				return formatCastError(ctx, from.GetSourceVector(), toType, fmt.Sprintf(
  2948  					"%v is larger than Dest length %v", v.String(), toType.Width))
  2949  			}
  2950  			if err := to.AppendBytes(result, false); err != nil {
  2951  				return err
  2952  			}
  2953  		}
  2954  	}
  2955  	return nil
  2956  }
  2957  
  2958  func datetimeToStr(
  2959  	ctx context.Context,
  2960  	from vector.FunctionParameterWrapper[types.Datetime],
  2961  	to *vector.FunctionResult[types.Varlena], length int, toType types.Type) error {
  2962  	var i uint64
  2963  	l := uint64(length)
  2964  	fromType := from.GetType()
  2965  	// Here cast using cast(data_type as binary[(n)]).
  2966  	if toType.Oid == types.T_binary && toType.Scale == -1 {
  2967  		for i = 0; i < l; i++ {
  2968  			v, null := from.GetValue(i)
  2969  			v1 := []byte(v.String2(fromType.Scale))
  2970  			if err := explicitCastToBinary(toType, v1, null, to); err != nil {
  2971  				return err
  2972  			}
  2973  		}
  2974  		return nil
  2975  	}
  2976  	for i = 0; i < l; i++ {
  2977  		v, null := from.GetValue(i)
  2978  		if null {
  2979  			if err := to.AppendBytes(nil, true); err != nil {
  2980  				return err
  2981  			}
  2982  		} else {
  2983  			result := []byte(v.String2(fromType.Scale))
  2984  			if toType.Oid == types.T_binary || toType.Oid == types.T_varbinary {
  2985  				if int32(len(result)) > toType.Width {
  2986  					return moerr.NewDataTruncatedNoCtx("Datetime", "truncated for binary/varbinary")
  2987  				}
  2988  			}
  2989  			if toType.Oid == types.T_binary && len(result) < int(toType.Width) {
  2990  				add0 := int(toType.Width) - len(result)
  2991  				for ; add0 != 0; add0-- {
  2992  					result = append(result, 0)
  2993  				}
  2994  			}
  2995  			if len(result) > int(toType.Width) && toType.Oid != types.T_text && toType.Oid != types.T_blob {
  2996  				return formatCastError(ctx, from.GetSourceVector(), toType, fmt.Sprintf(
  2997  					"%v is larger than Dest length %v", v.String(), toType.Width))
  2998  			}
  2999  			if err := to.AppendBytes(result, false); err != nil {
  3000  				return err
  3001  			}
  3002  		}
  3003  	}
  3004  	return nil
  3005  }
  3006  
  3007  func timestampToStr(
  3008  	ctx context.Context,
  3009  	from vector.FunctionParameterWrapper[types.Timestamp],
  3010  	to *vector.FunctionResult[types.Varlena], length int,
  3011  	zone *time.Location, toType types.Type) error {
  3012  	var i uint64
  3013  	l := uint64(length)
  3014  	fromType := from.GetType()
  3015  	// Here cast using cast(data_type as binary[(n)]).
  3016  	if toType.Oid == types.T_binary && toType.Scale == -1 {
  3017  		for i = 0; i < l; i++ {
  3018  			v, null := from.GetValue(i)
  3019  			v1 := []byte(v.String2(zone, fromType.Scale))
  3020  			if err := explicitCastToBinary(toType, v1, null, to); err != nil {
  3021  				return err
  3022  			}
  3023  		}
  3024  		return nil
  3025  	}
  3026  	for i = 0; i < l; i++ {
  3027  		v, null := from.GetValue(i)
  3028  		if null {
  3029  			if err := to.AppendBytes(nil, true); err != nil {
  3030  				return err
  3031  			}
  3032  		} else {
  3033  			result := []byte(v.String2(zone, fromType.Scale))
  3034  			if toType.Oid == types.T_binary || toType.Oid == types.T_varbinary {
  3035  				if int32(len(result)) > toType.Width {
  3036  					return moerr.NewDataTruncatedNoCtx("TimeStamp", "truncated for binary/varbinary")
  3037  				}
  3038  			}
  3039  			if toType.Oid == types.T_binary && len(result) < int(toType.Width) {
  3040  				add0 := int(toType.Width) - len(result)
  3041  				for ; add0 != 0; add0-- {
  3042  					result = append(result, 0)
  3043  				}
  3044  			}
  3045  			if len(result) > int(toType.Width) && toType.Oid != types.T_text && toType.Oid != types.T_blob {
  3046  				return formatCastError(ctx, from.GetSourceVector(), toType, fmt.Sprintf(
  3047  					"%v is larger than Dest length %v", v.String(), toType.Width))
  3048  			}
  3049  			if err := to.AppendBytes(result, false); err != nil {
  3050  				return err
  3051  			}
  3052  		}
  3053  	}
  3054  	return nil
  3055  }
  3056  
  3057  func timeToStr(
  3058  	ctx context.Context,
  3059  	from vector.FunctionParameterWrapper[types.Time],
  3060  	to *vector.FunctionResult[types.Varlena], length int, toType types.Type) error {
  3061  	var i uint64
  3062  	l := uint64(length)
  3063  	fromType := from.GetType()
  3064  	// Here cast using cast(data_type as binary[(n)]).
  3065  	if toType.Oid == types.T_binary && toType.Scale == -1 {
  3066  		for i = 0; i < l; i++ {
  3067  			v, null := from.GetValue(i)
  3068  			v1 := []byte(v.String2(fromType.Scale))
  3069  			if err := explicitCastToBinary(toType, v1, null, to); err != nil {
  3070  				return err
  3071  			}
  3072  		}
  3073  		return nil
  3074  	}
  3075  	for i = 0; i < l; i++ {
  3076  		v, null := from.GetValue(i)
  3077  		if null {
  3078  			if err := to.AppendBytes(nil, true); err != nil {
  3079  				return err
  3080  			}
  3081  		} else {
  3082  			result := []byte(v.String2(fromType.Scale))
  3083  			if toType.Oid == types.T_binary || toType.Oid == types.T_varbinary {
  3084  				if int32(len(result)) > toType.Width {
  3085  					return moerr.NewDataTruncatedNoCtx("Time", "truncated for binary/varbinary")
  3086  				}
  3087  			}
  3088  			if toType.Oid == types.T_binary && len(result) < int(toType.Width) {
  3089  				add0 := int(toType.Width) - len(result)
  3090  				for ; add0 != 0; add0-- {
  3091  					result = append(result, 0)
  3092  				}
  3093  			}
  3094  			if len(result) > int(toType.Width) && toType.Oid != types.T_text && toType.Oid != types.T_blob {
  3095  				return formatCastError(ctx, from.GetSourceVector(), toType, fmt.Sprintf(
  3096  					"%v is larger than Dest length %v", v.String(), toType.Width))
  3097  			}
  3098  			if err := to.AppendBytes(result, false); err != nil {
  3099  				return err
  3100  			}
  3101  		}
  3102  	}
  3103  	return nil
  3104  }
  3105  
  3106  func timeToDecimal64(
  3107  	ctx context.Context,
  3108  	from vector.FunctionParameterWrapper[types.Time],
  3109  	to *vector.FunctionResult[types.Decimal64], length int) error {
  3110  	var i uint64
  3111  	l := uint64(length)
  3112  	var dft types.Decimal64
  3113  	fromType := from.GetType()
  3114  	totype := to.GetType()
  3115  	for ; i < l; i++ {
  3116  		v, null := from.GetValue(i)
  3117  		if null {
  3118  			if err := to.Append(dft, true); err != nil {
  3119  				return err
  3120  			}
  3121  		} else {
  3122  			result, err := v.ToDecimal64(ctx, totype.Width, fromType.Scale)
  3123  			if err != nil {
  3124  				return err
  3125  			}
  3126  			result, err = result.Scale(totype.Scale - fromType.Scale)
  3127  			if err != nil {
  3128  				return err
  3129  			}
  3130  			if err = to.Append(result, false); err != nil {
  3131  				return err
  3132  			}
  3133  		}
  3134  	}
  3135  	return nil
  3136  }
  3137  
  3138  func timeToDecimal128(
  3139  	ctx context.Context,
  3140  	from vector.FunctionParameterWrapper[types.Time],
  3141  	to *vector.FunctionResult[types.Decimal128], length int) error {
  3142  	var i uint64
  3143  	l := uint64(length)
  3144  	var dft types.Decimal128
  3145  	fromType := from.GetType()
  3146  	totype := to.GetType()
  3147  	for ; i < l; i++ {
  3148  		v, null := from.GetValue(i)
  3149  		if null {
  3150  			if err := to.Append(dft, true); err != nil {
  3151  				return err
  3152  			}
  3153  		} else {
  3154  			result, err := v.ToDecimal128(ctx, totype.Width, fromType.Scale)
  3155  			if err != nil {
  3156  				return err
  3157  			}
  3158  			result, err = result.Scale(totype.Scale - fromType.Scale)
  3159  			if err != nil {
  3160  				return err
  3161  			}
  3162  			if err = to.Append(result, false); err != nil {
  3163  				return err
  3164  			}
  3165  		}
  3166  	}
  3167  	return nil
  3168  }
  3169  
  3170  func decimal64ToSigned[T constraints.Signed](
  3171  	ctx context.Context,
  3172  	from vector.FunctionParameterWrapper[types.Decimal64],
  3173  	to *vector.FunctionResult[T], bitSize int, length int) error {
  3174  	var i uint64
  3175  	l := uint64(length)
  3176  	fromTyp := from.GetType()
  3177  	for i = 0; i < l; i++ {
  3178  		v, null := from.GetValue(i)
  3179  		if null {
  3180  			if err := to.Append(0, true); err != nil {
  3181  				return err
  3182  			}
  3183  		} else {
  3184  			x, _ := v.Scale(-fromTyp.Scale)
  3185  			xStr := x.Format(0)
  3186  			result, err := strconv.ParseInt(xStr, 10, bitSize)
  3187  			if err != nil {
  3188  				return moerr.NewOutOfRange(ctx,
  3189  					fmt.Sprintf("int%d", bitSize),
  3190  					"value '%v'", xStr)
  3191  			}
  3192  			err = to.Append(T(result), false)
  3193  			if err != nil {
  3194  				return err
  3195  			}
  3196  		}
  3197  	}
  3198  	return nil
  3199  }
  3200  
  3201  func decimal128ToSigned[T constraints.Signed](
  3202  	ctx context.Context,
  3203  	from vector.FunctionParameterWrapper[types.Decimal128],
  3204  	to *vector.FunctionResult[T], bitSize int, length int) error {
  3205  	var i uint64
  3206  	l := uint64(length)
  3207  	fromTyp := from.GetType()
  3208  	for i = 0; i < l; i++ {
  3209  		v, null := from.GetValue(i)
  3210  		if null {
  3211  			if err := to.Append(0, true); err != nil {
  3212  				return err
  3213  			}
  3214  		} else {
  3215  			x, _ := v.Scale(-fromTyp.Scale)
  3216  			xStr := x.Format(0)
  3217  			result, err := strconv.ParseInt(xStr, 10, bitSize)
  3218  			if err != nil {
  3219  				return moerr.NewOutOfRange(ctx,
  3220  					fmt.Sprintf("int%d", bitSize),
  3221  					"value '%v'", xStr)
  3222  			}
  3223  			err = to.Append(T(result), false)
  3224  			if err != nil {
  3225  				return err
  3226  			}
  3227  		}
  3228  	}
  3229  	return nil
  3230  }
  3231  
  3232  func decimal64ToUnsigned[T constraints.Unsigned](
  3233  	ctx context.Context,
  3234  	from vector.FunctionParameterWrapper[types.Decimal64],
  3235  	to *vector.FunctionResult[T], bitSize int,
  3236  	length int) error {
  3237  	var i uint64
  3238  	l := uint64(length)
  3239  	fromType := from.GetType()
  3240  	for i = 0; i < l; i++ {
  3241  		v, null := from.GetValue(i)
  3242  		if null {
  3243  			if err := to.Append(0, true); err != nil {
  3244  				return err
  3245  			}
  3246  		} else {
  3247  			xStr := v.Format(fromType.Scale)
  3248  			xStr = strings.Split(xStr, ".")[0]
  3249  			result, err := strconv.ParseUint(xStr, 10, bitSize)
  3250  			if err != nil {
  3251  				return moerr.NewOutOfRange(ctx,
  3252  					fmt.Sprintf("uint%d", bitSize),
  3253  					"value '%v'", xStr)
  3254  			}
  3255  			err = to.Append(T(result), false)
  3256  			if err != nil {
  3257  				return err
  3258  			}
  3259  		}
  3260  	}
  3261  	return nil
  3262  }
  3263  
  3264  func decimal128ToUnsigned[T constraints.Unsigned](
  3265  	ctx context.Context,
  3266  	from vector.FunctionParameterWrapper[types.Decimal128],
  3267  	to *vector.FunctionResult[T], bitSize int,
  3268  	length int) error {
  3269  	var i uint64
  3270  	l := uint64(length)
  3271  	fromType := from.GetType()
  3272  	for i = 0; i < l; i++ {
  3273  		v, null := from.GetValue(i)
  3274  		if null {
  3275  			if err := to.Append(0, true); err != nil {
  3276  				return err
  3277  			}
  3278  		} else {
  3279  			xStr := v.Format(fromType.Scale)
  3280  			xStr = strings.Split(xStr, ".")[0]
  3281  			result, err := strconv.ParseUint(xStr, 10, bitSize)
  3282  			if err != nil {
  3283  				return moerr.NewOutOfRange(ctx,
  3284  					fmt.Sprintf("uint%d", bitSize),
  3285  					"value '%v'", xStr)
  3286  			}
  3287  			err = to.Append(T(result), false)
  3288  			if err != nil {
  3289  				return err
  3290  			}
  3291  		}
  3292  	}
  3293  	return nil
  3294  }
  3295  
  3296  func decimal64ToTime(
  3297  	from vector.FunctionParameterWrapper[types.Decimal64],
  3298  	to *vector.FunctionResult[types.Time], length int) error {
  3299  	var i uint64
  3300  	l := uint64(length)
  3301  	fromtype := from.GetType()
  3302  	totype := to.GetType()
  3303  	for i = 0; i < l; i++ {
  3304  		v, null := from.GetValue(i)
  3305  		if null {
  3306  			if err := to.Append(0, true); err != nil {
  3307  				return err
  3308  			}
  3309  		} else {
  3310  			result, err := types.ParseDecimal64ToTime(v, fromtype.Scale, totype.Scale)
  3311  			if err != nil {
  3312  				return err
  3313  			}
  3314  			if err = to.Append(result, false); err != nil {
  3315  				return err
  3316  			}
  3317  		}
  3318  	}
  3319  	return nil
  3320  }
  3321  
  3322  func decimal128ToTime(
  3323  	from vector.FunctionParameterWrapper[types.Decimal128],
  3324  	to *vector.FunctionResult[types.Time], length int) error {
  3325  	var i uint64
  3326  	l := uint64(length)
  3327  	fromtype := from.GetType()
  3328  	totype := to.GetType()
  3329  	for i = 0; i < l; i++ {
  3330  		v, null := from.GetValue(i)
  3331  		if null {
  3332  			if err := to.Append(0, true); err != nil {
  3333  				return err
  3334  			}
  3335  		} else {
  3336  			result, err := types.ParseDecimal128ToTime(v, fromtype.Scale, totype.Scale)
  3337  			if err != nil {
  3338  				return err
  3339  			}
  3340  			if err = to.Append(result, false); err != nil {
  3341  				return err
  3342  			}
  3343  		}
  3344  	}
  3345  	return nil
  3346  }
  3347  
  3348  func decimal64ToTimestamp(
  3349  	from vector.FunctionParameterWrapper[types.Decimal64],
  3350  	to *vector.FunctionResult[types.Timestamp], length int) error {
  3351  	var i uint64
  3352  	l := uint64(length)
  3353  	for i = 0; i < l; i++ {
  3354  		v, null := from.GetValue(i)
  3355  		if null {
  3356  			if err := to.Append(0, true); err != nil {
  3357  				return err
  3358  			}
  3359  		} else {
  3360  			ts := types.Timestamp(int64(v))
  3361  			if err := to.Append(ts, false); err != nil {
  3362  				return err
  3363  			}
  3364  		}
  3365  	}
  3366  	return nil
  3367  }
  3368  
  3369  func decimal128ToTimestamp(
  3370  	from vector.FunctionParameterWrapper[types.Decimal128],
  3371  	to *vector.FunctionResult[types.Timestamp], length int) error {
  3372  	var i uint64
  3373  	l := uint64(length)
  3374  	for i = 0; i < l; i++ {
  3375  		v, null := from.GetValue(i)
  3376  		if null {
  3377  			if err := to.Append(0, true); err != nil {
  3378  				return err
  3379  			}
  3380  		} else {
  3381  			ts := types.Timestamp(int64(v.B0_63))
  3382  			if err := to.Append(ts, false); err != nil {
  3383  				return err
  3384  			}
  3385  		}
  3386  	}
  3387  	return nil
  3388  }
  3389  
  3390  func decimal64ToFloat[T constraints.Float](
  3391  	ctx context.Context,
  3392  	from vector.FunctionParameterWrapper[types.Decimal64],
  3393  	to *vector.FunctionResult[T], length int, bitSize int) error {
  3394  	// IF float32, then bitSize should be 32. IF float64, then 64
  3395  	var i uint64
  3396  	l := uint64(length)
  3397  	fromType := from.GetType()
  3398  	for i = 0; i < l; i++ {
  3399  		v, null := from.GetValue(i)
  3400  		if null {
  3401  			if err := to.Append(0, true); err != nil {
  3402  				return err
  3403  			}
  3404  		} else {
  3405  			xStr := v.Format(fromType.Scale)
  3406  			result, err := strconv.ParseFloat(xStr, bitSize)
  3407  			if err != nil {
  3408  				return moerr.NewOutOfRange(ctx, "float32", "value '%v'", xStr)
  3409  			}
  3410  			if bitSize == 32 {
  3411  				result, _ = strconv.ParseFloat(xStr, 64)
  3412  			}
  3413  			if to.GetType().Scale < 0 || to.GetType().Width == 0 {
  3414  				if err = to.Append(T(result), false); err != nil {
  3415  					return err
  3416  				}
  3417  			} else {
  3418  				v2, err := floatNumToFixFloat(ctx, result, to, xStr)
  3419  				if err != nil {
  3420  					return err
  3421  				}
  3422  				if err = to.Append(T(v2), false); err != nil {
  3423  					return err
  3424  				}
  3425  			}
  3426  		}
  3427  	}
  3428  	return nil
  3429  }
  3430  
  3431  func decimal128ToFloat[T constraints.Float](
  3432  	ctx context.Context,
  3433  	from vector.FunctionParameterWrapper[types.Decimal128],
  3434  	to *vector.FunctionResult[T], length int, bitSize int) error {
  3435  	// IF float32, then bitSize should be 32. IF float64, then 64
  3436  	var i uint64
  3437  	l := uint64(length)
  3438  	fromType := from.GetType()
  3439  	for i = 0; i < l; i++ {
  3440  		v, null := from.GetValue(i)
  3441  		if null {
  3442  			if err := to.Append(0, true); err != nil {
  3443  				return err
  3444  			}
  3445  		} else {
  3446  			xStr := v.Format(fromType.Scale)
  3447  			result, err := strconv.ParseFloat(xStr, bitSize)
  3448  			if err != nil {
  3449  				return moerr.NewOutOfRange(ctx, "float32", "value '%v'", xStr)
  3450  			}
  3451  			if bitSize == 32 {
  3452  				result, _ = strconv.ParseFloat(xStr, 64)
  3453  			}
  3454  			if to.GetType().Scale < 0 || to.GetType().Width == 0 {
  3455  				if err = to.Append(T(result), false); err != nil {
  3456  					return err
  3457  				}
  3458  			} else {
  3459  				v2, err := floatNumToFixFloat(ctx, result, to, xStr)
  3460  				if err != nil {
  3461  					return err
  3462  				}
  3463  				if err = to.Append(T(v2), false); err != nil {
  3464  					return err
  3465  				}
  3466  			}
  3467  		}
  3468  	}
  3469  	return nil
  3470  }
  3471  
  3472  func decimal64ToDecimal64(
  3473  	from vector.FunctionParameterWrapper[types.Decimal64],
  3474  	to *vector.FunctionResult[types.Decimal64], length int) error {
  3475  	var i uint64
  3476  	l := uint64(length)
  3477  	var dft types.Decimal64
  3478  	fromtype := from.GetType()
  3479  	totype := to.GetType()
  3480  	for i = 0; i < l; i++ {
  3481  		v, null := from.GetValue(i)
  3482  		if null {
  3483  			if err := to.Append(dft, true); err != nil {
  3484  				return err
  3485  			}
  3486  		} else {
  3487  			if totype.Width < fromtype.Width {
  3488  				dec := v.Format(fromtype.Scale)
  3489  				result, err := types.ParseDecimal64(dec, totype.Width, totype.Scale)
  3490  				if err != nil {
  3491  					return err
  3492  				}
  3493  				if err = to.Append(result, false); err != nil {
  3494  					return err
  3495  				}
  3496  			} else {
  3497  				result, err := v.Scale(totype.Scale - fromtype.Scale)
  3498  				if err != nil {
  3499  					return err
  3500  				}
  3501  				if err = to.Append(result, false); err != nil {
  3502  					return err
  3503  				}
  3504  			}
  3505  		}
  3506  	}
  3507  	return nil
  3508  }
  3509  
  3510  func decimal64ToDecimal128Array(
  3511  	from vector.FunctionParameterWrapper[types.Decimal64],
  3512  	to *vector.FunctionResult[types.Decimal128], length int) error {
  3513  	var i uint64
  3514  	l := uint64(length)
  3515  	fromtype := from.GetType()
  3516  	totype := to.GetType()
  3517  
  3518  	if !from.WithAnyNullValue() {
  3519  		v := vector.MustFixedCol[types.Decimal64](from.GetSourceVector())
  3520  		if totype.Width < fromtype.Width {
  3521  			for i = 0; i < l; i++ {
  3522  				fromdec := types.Decimal128{B0_63: uint64(v[i]), B64_127: 0}
  3523  				if v[i].Sign() {
  3524  					fromdec.B64_127 = ^fromdec.B64_127
  3525  				}
  3526  				dec := fromdec.Format(fromtype.Scale)
  3527  				result, err := types.ParseDecimal128(dec, totype.Width, totype.Scale)
  3528  				if err != nil {
  3529  					return err
  3530  				}
  3531  				if err = to.Append(result, false); err != nil {
  3532  					return err
  3533  				}
  3534  			}
  3535  		} else {
  3536  			if totype.Scale == fromtype.Scale {
  3537  				for i = 0; i < l; i++ {
  3538  					fromdec := types.Decimal128{B0_63: uint64(v[i]), B64_127: 0}
  3539  					if v[i].Sign() {
  3540  						fromdec.B64_127 = ^fromdec.B64_127
  3541  					}
  3542  					to.AppendMustValue(fromdec)
  3543  				}
  3544  			} else {
  3545  				for i = 0; i < l; i++ {
  3546  					fromdec := types.Decimal128{B0_63: uint64(v[i]), B64_127: 0}
  3547  					if v[i].Sign() {
  3548  						fromdec.B64_127 = ^fromdec.B64_127
  3549  					}
  3550  					result, err := fromdec.Scale(totype.Scale - fromtype.Scale)
  3551  					if err != nil {
  3552  						return err
  3553  					}
  3554  					if err = to.Append(result, false); err != nil {
  3555  						return err
  3556  					}
  3557  				}
  3558  			}
  3559  		}
  3560  	} else {
  3561  		// with any null value
  3562  		var dft types.Decimal128
  3563  		for i = 0; i < l; i++ {
  3564  			v, null := from.GetValue(i)
  3565  			if null {
  3566  				if err := to.Append(dft, true); err != nil {
  3567  					return err
  3568  				}
  3569  			} else {
  3570  				fromdec := types.Decimal128{B0_63: uint64(v), B64_127: 0}
  3571  				if v.Sign() {
  3572  					fromdec.B64_127 = ^fromdec.B64_127
  3573  				}
  3574  				if totype.Width < fromtype.Width {
  3575  					dec := fromdec.Format(fromtype.Scale)
  3576  					result, err := types.ParseDecimal128(dec, totype.Width, totype.Scale)
  3577  					if err != nil {
  3578  						return err
  3579  					}
  3580  					if err = to.Append(result, false); err != nil {
  3581  						return err
  3582  					}
  3583  				} else {
  3584  					if totype.Scale == fromtype.Scale {
  3585  						to.AppendMustValue(fromdec)
  3586  					} else {
  3587  						result, err := fromdec.Scale(totype.Scale - fromtype.Scale)
  3588  						if err != nil {
  3589  							return err
  3590  						}
  3591  						if err = to.Append(result, false); err != nil {
  3592  							return err
  3593  						}
  3594  					}
  3595  				}
  3596  			}
  3597  		}
  3598  	}
  3599  	return nil
  3600  }
  3601  
  3602  // the scale of decimal128 is guaranteed to be less than 18
  3603  // this cast function is too slow, and therefore only temporary, rewrite needed
  3604  func decimal128ToDecimal64(
  3605  	ctx context.Context,
  3606  	from vector.FunctionParameterWrapper[types.Decimal128],
  3607  	to *vector.FunctionResult[types.Decimal64], length int) error {
  3608  	var i uint64
  3609  	l := uint64(length)
  3610  	var dft types.Decimal64
  3611  	fromtype := from.GetType()
  3612  	totype := to.GetType()
  3613  	for i = 0; i < l; i++ {
  3614  		v, null := from.GetValue(i)
  3615  		if null {
  3616  			if err := to.Append(dft, true); err != nil {
  3617  				return err
  3618  			}
  3619  		} else {
  3620  			dec := v.Format(fromtype.Scale)
  3621  			result, err := types.ParseDecimal64(dec, totype.Width, totype.Scale)
  3622  			if err != nil {
  3623  				return err
  3624  			}
  3625  			if err = to.Append(result, false); err != nil {
  3626  				return err
  3627  			}
  3628  		}
  3629  	}
  3630  	return nil
  3631  }
  3632  
  3633  func decimal128ToDecimal128(
  3634  	from vector.FunctionParameterWrapper[types.Decimal128],
  3635  	to *vector.FunctionResult[types.Decimal128], length int) error {
  3636  	var i uint64
  3637  	l := uint64(length)
  3638  	var dft types.Decimal128
  3639  	fromtype := from.GetType()
  3640  	totype := to.GetType()
  3641  	for i = 0; i < l; i++ {
  3642  		v, null := from.GetValue(i)
  3643  		if null {
  3644  			if err := to.Append(dft, true); err != nil {
  3645  				return err
  3646  			}
  3647  		} else {
  3648  			if totype.Width < fromtype.Width {
  3649  				dec := v.Format(fromtype.Scale)
  3650  				result, err := types.ParseDecimal128(dec, totype.Width, totype.Scale)
  3651  				if err != nil {
  3652  					return err
  3653  				}
  3654  				if err = to.Append(result, false); err != nil {
  3655  					return err
  3656  				}
  3657  			} else {
  3658  				result, err := v.Scale(totype.Scale - fromtype.Scale)
  3659  				if err != nil {
  3660  					return err
  3661  				}
  3662  				if err = to.Append(result, false); err != nil {
  3663  					return err
  3664  				}
  3665  			}
  3666  		}
  3667  	}
  3668  	return nil
  3669  }
  3670  
  3671  func decimal64ToStr(
  3672  	ctx context.Context,
  3673  	from vector.FunctionParameterWrapper[types.Decimal64],
  3674  	to *vector.FunctionResult[types.Varlena], length int, toType types.Type) error {
  3675  	var i uint64
  3676  	l := uint64(length)
  3677  	fromType := from.GetType()
  3678  	// Here cast using cast(data_type as binary[(n)]).
  3679  	if toType.Oid == types.T_binary && toType.Scale == -1 {
  3680  		for i = 0; i < l; i++ {
  3681  			v, null := from.GetValue(i)
  3682  			v1 := []byte(v.Format(fromType.Scale))
  3683  			if err := explicitCastToBinary(toType, v1, null, to); err != nil {
  3684  				return err
  3685  			}
  3686  		}
  3687  		return nil
  3688  	}
  3689  	for i = 0; i < l; i++ {
  3690  		v, null := from.GetValue(i)
  3691  		if null {
  3692  			if err := to.AppendBytes(nil, true); err != nil {
  3693  				return err
  3694  			}
  3695  		} else {
  3696  			result := []byte(v.Format(fromType.Scale))
  3697  			if toType.Oid == types.T_binary || toType.Oid == types.T_varbinary {
  3698  				if int32(len(result)) > toType.Width {
  3699  					return moerr.NewDataTruncatedNoCtx("Decimal64", "truncated for binary/varbinary")
  3700  				}
  3701  			}
  3702  			if toType.Oid == types.T_binary && len(result) < int(toType.Width) {
  3703  				add0 := int(toType.Width) - len(result)
  3704  				for ; add0 != 0; add0-- {
  3705  					result = append(result, 0)
  3706  				}
  3707  			}
  3708  			if len(result) > int(toType.Width) && toType.Oid != types.T_text && toType.Oid != types.T_blob {
  3709  				return formatCastError(ctx, from.GetSourceVector(), toType, fmt.Sprintf(
  3710  					"%v is larger than Dest length %v", v.Format(fromType.Scale), toType.Width))
  3711  			}
  3712  			if err := to.AppendBytes(result, false); err != nil {
  3713  				return err
  3714  			}
  3715  		}
  3716  	}
  3717  	return nil
  3718  }
  3719  
  3720  func decimal128ToStr(
  3721  	ctx context.Context,
  3722  	from vector.FunctionParameterWrapper[types.Decimal128],
  3723  	to *vector.FunctionResult[types.Varlena], length int, toType types.Type) error {
  3724  	var i uint64
  3725  	l := uint64(length)
  3726  	fromType := from.GetType()
  3727  	// Here cast using cast(data_type as binary[(n)]).
  3728  	if toType.Oid == types.T_binary && toType.Scale == -1 {
  3729  		for i = 0; i < l; i++ {
  3730  			v, null := from.GetValue(i)
  3731  			v1 := []byte(v.Format(fromType.Scale))
  3732  			if err := explicitCastToBinary(toType, v1, null, to); err != nil {
  3733  				return err
  3734  			}
  3735  		}
  3736  		return nil
  3737  	}
  3738  	for i = 0; i < l; i++ {
  3739  		v, null := from.GetValue(i)
  3740  		if null {
  3741  			if err := to.AppendBytes(nil, true); err != nil {
  3742  				return err
  3743  			}
  3744  		} else {
  3745  			result := []byte(v.Format(fromType.Scale))
  3746  			if toType.Oid == types.T_binary || toType.Oid == types.T_varbinary {
  3747  				if int32(len(result)) > toType.Width {
  3748  					return moerr.NewDataTruncatedNoCtx("Decimal128", "truncated for binary/varbinary")
  3749  				}
  3750  			}
  3751  			if toType.Oid == types.T_binary && len(result) < int(toType.Width) {
  3752  				add0 := int(toType.Width) - len(result)
  3753  				for ; add0 != 0; add0-- {
  3754  					result = append(result, 0)
  3755  				}
  3756  			}
  3757  			if len(result) > int(toType.Width) && toType.Oid != types.T_text && toType.Oid != types.T_blob {
  3758  				return formatCastError(ctx, from.GetSourceVector(), toType, fmt.Sprintf(
  3759  					"%v is larger than Dest length %v", v.Format(fromType.Scale), toType.Width))
  3760  			}
  3761  			if err := to.AppendBytes(result, false); err != nil {
  3762  				return err
  3763  			}
  3764  		}
  3765  	}
  3766  	return nil
  3767  }
  3768  
  3769  func decimal64ToBit(
  3770  	ctx context.Context,
  3771  	from vector.FunctionParameterWrapper[types.Decimal64],
  3772  	to *vector.FunctionResult[uint64], bitSize int, length int) error {
  3773  	for i := 0; i < length; i++ {
  3774  		v, null := from.GetValue(uint64(i))
  3775  		if null {
  3776  			if err := to.Append(uint64(0), true); err != nil {
  3777  				return err
  3778  			}
  3779  		} else {
  3780  			var result uint64
  3781  			var err error
  3782  			xStr := v.Format(from.GetType().Scale)
  3783  			xStr = strings.Split(xStr, ".")[0]
  3784  			if result, err = strconv.ParseUint(xStr, 10, bitSize); err != nil {
  3785  				return moerr.NewOutOfRange(ctx, fmt.Sprintf("bit(%d)", bitSize), "value '%v'", xStr)
  3786  			}
  3787  			if err = to.Append(result, false); err != nil {
  3788  				return err
  3789  			}
  3790  		}
  3791  	}
  3792  	return nil
  3793  }
  3794  
  3795  func decimal128ToBit(
  3796  	ctx context.Context,
  3797  	from vector.FunctionParameterWrapper[types.Decimal128],
  3798  	to *vector.FunctionResult[uint64], bitSize int, length int) error {
  3799  	for i := 0; i < length; i++ {
  3800  		v, null := from.GetValue(uint64(i))
  3801  		if null {
  3802  			if err := to.Append(uint64(0), true); err != nil {
  3803  				return err
  3804  			}
  3805  		} else {
  3806  			var result uint64
  3807  			var err error
  3808  			xStr := v.Format(from.GetType().Scale)
  3809  			xStr = strings.Split(xStr, ".")[0]
  3810  			if result, err = strconv.ParseUint(xStr, 10, bitSize); err != nil {
  3811  				return moerr.NewOutOfRange(ctx, fmt.Sprintf("bit(%d)", bitSize), "value '%v'", xStr)
  3812  			}
  3813  			if err = to.Append(result, false); err != nil {
  3814  				return err
  3815  			}
  3816  		}
  3817  	}
  3818  	return nil
  3819  }
  3820  
  3821  func strToSigned[T constraints.Signed](
  3822  	ctx context.Context,
  3823  	from vector.FunctionParameterWrapper[types.Varlena],
  3824  	to *vector.FunctionResult[T], bitSize int,
  3825  	length int) error {
  3826  	var i uint64
  3827  	var l = uint64(length)
  3828  	isBinary := from.GetSourceVector().GetIsBin()
  3829  
  3830  	var result T
  3831  	for i = 0; i < l; i++ {
  3832  		v, null := from.GetStrValue(i)
  3833  		if null {
  3834  			if err := to.Append(0, true); err != nil {
  3835  				return err
  3836  			}
  3837  		} else {
  3838  			if isBinary {
  3839  				r, err := strconv.ParseInt(
  3840  					hex.EncodeToString(v), 16, 64)
  3841  				if err != nil {
  3842  					if strings.Contains(err.Error(), "value out of range") {
  3843  						// the string maybe non-visible,don't print it
  3844  						return moerr.NewOutOfRange(ctx, "int", "")
  3845  					}
  3846  					return moerr.NewInvalidArg(ctx, "cast to int", r)
  3847  				}
  3848  				result = T(r)
  3849  			} else {
  3850  				s := strings.TrimSpace(convertByteSliceToString(v))
  3851  				var r int64
  3852  				var err error
  3853  				if strings.HasPrefix(s, "0x") || strings.HasPrefix(s, "0X") {
  3854  					r, err = strconv.ParseInt(s[2:], 16, bitSize)
  3855  				} else {
  3856  					r, err = strconv.ParseInt(s, 10, bitSize)
  3857  				}
  3858  				if err != nil {
  3859  					// XXX I'm not sure if we should return the int8 / int16 / int64 info. or
  3860  					// just return the int. the old code just return the int. too much bvt result needs to update.
  3861  					if strings.Contains(err.Error(), "value out of range") {
  3862  						return moerr.NewOutOfRange(ctx, fmt.Sprintf("int%d", bitSize), "value '%s'", s)
  3863  					}
  3864  					return moerr.NewInvalidArg(ctx, "cast to int", s)
  3865  				}
  3866  				result = T(r)
  3867  			}
  3868  			if err := to.Append(result, false); err != nil {
  3869  				return err
  3870  			}
  3871  		}
  3872  	}
  3873  	return nil
  3874  }
  3875  
  3876  func strToUnsigned[T constraints.Unsigned](
  3877  	ctx context.Context,
  3878  	from vector.FunctionParameterWrapper[types.Varlena],
  3879  	to *vector.FunctionResult[T], bitSize int,
  3880  	length int) error {
  3881  	var i uint64
  3882  	var l = uint64(length)
  3883  	isBinary := from.GetSourceVector().GetIsBin()
  3884  
  3885  	var val uint64
  3886  	var tErr error
  3887  	for i = 0; i < l; i++ {
  3888  		v, null := from.GetStrValue(i)
  3889  		if null {
  3890  			if err := to.Append(0, true); err != nil {
  3891  				return err
  3892  			}
  3893  		} else {
  3894  			var res *string
  3895  			if isBinary {
  3896  				s := hex.EncodeToString(v)
  3897  				res = &s
  3898  				val, tErr = strconv.ParseUint(s, 16, 64)
  3899  			} else {
  3900  				s := strings.TrimSpace(convertByteSliceToString(v))
  3901  				res = &s
  3902  				if strings.HasPrefix(s, "0x") || strings.HasPrefix(s, "0X") {
  3903  					val, tErr = strconv.ParseUint(s[2:], 16, bitSize)
  3904  				} else {
  3905  					val, tErr = strconv.ParseUint(s, 10, bitSize)
  3906  				}
  3907  			}
  3908  			if tErr != nil {
  3909  				if strings.Contains(tErr.Error(), "value out of range") {
  3910  					return moerr.NewOutOfRange(ctx, fmt.Sprintf("uint%d", bitSize), "value '%s'", *res)
  3911  				}
  3912  				return moerr.NewInvalidArg(ctx, fmt.Sprintf("cast to uint%d", bitSize), *res)
  3913  			}
  3914  			if err := to.Append(T(val), false); err != nil {
  3915  				return err
  3916  			}
  3917  		}
  3918  	}
  3919  	return nil
  3920  }
  3921  
  3922  func strToFloat[T constraints.Float](
  3923  	ctx context.Context,
  3924  	from vector.FunctionParameterWrapper[types.Varlena],
  3925  	to *vector.FunctionResult[T], bitSize int,
  3926  	length int) error {
  3927  	var i uint64
  3928  	var l = uint64(length)
  3929  	isBinary := from.GetSourceVector().GetIsBin()
  3930  
  3931  	var result T
  3932  	var tErr error
  3933  	var r1 uint64
  3934  	var r2 float64
  3935  	for i = 0; i < l; i++ {
  3936  		v, null := from.GetStrValue(i)
  3937  		if null {
  3938  			if err := to.Append(0, true); err != nil {
  3939  				return err
  3940  			}
  3941  		} else {
  3942  			if isBinary {
  3943  				s := hex.EncodeToString(v)
  3944  				r1, tErr = strconv.ParseUint(s, 16, 64)
  3945  				if tErr != nil {
  3946  					if strings.Contains(tErr.Error(), "value out of range") {
  3947  						return moerr.NewOutOfRange(ctx, "float", "value '%s'", s)
  3948  					}
  3949  					return moerr.NewInvalidArg(ctx, "cast to float", s)
  3950  				}
  3951  				if to.GetType().Scale < 0 || to.GetType().Width == 0 {
  3952  					result = T(r1)
  3953  				} else {
  3954  					v2, err := floatNumToFixFloat(ctx, float64(r1), to, "")
  3955  					if err != nil {
  3956  						return err
  3957  					}
  3958  					result = T(v2)
  3959  				}
  3960  			} else {
  3961  				s := convertByteSliceToString(v)
  3962  				r2, tErr = strconv.ParseFloat(s, bitSize)
  3963  				if tErr != nil {
  3964  					return tErr
  3965  				}
  3966  				if bitSize == 32 {
  3967  					r2, _ = strconv.ParseFloat(s, 64)
  3968  				}
  3969  				if to.GetType().Scale < 0 || to.GetType().Width == 0 {
  3970  					result = T(r2)
  3971  				} else {
  3972  					v2, err := floatNumToFixFloat(ctx, r2, to, s)
  3973  					if err != nil {
  3974  						return err
  3975  					}
  3976  					result = T(v2)
  3977  				}
  3978  			}
  3979  			if err := to.Append(result, false); err != nil {
  3980  				return err
  3981  			}
  3982  		}
  3983  	}
  3984  	return nil
  3985  }
  3986  
  3987  func strToDecimal64(
  3988  	from vector.FunctionParameterWrapper[types.Varlena],
  3989  	to *vector.FunctionResult[types.Decimal64], length int,
  3990  ) error {
  3991  	var i uint64
  3992  	var l = uint64(length)
  3993  	var dft types.Decimal64
  3994  	totype := to.GetType()
  3995  	isb := from.GetSourceVector().GetIsBin()
  3996  	for i = 0; i < l; i++ {
  3997  		v, null := from.GetStrValue(i)
  3998  		if null {
  3999  			if err := to.Append(dft, true); err != nil {
  4000  				return err
  4001  			}
  4002  		} else {
  4003  			s := convertByteSliceToString(v)
  4004  			if !isb {
  4005  				result, err := types.ParseDecimal64(s, totype.Width, totype.Scale)
  4006  				if err != nil {
  4007  					return err
  4008  				}
  4009  				if err = to.Append(result, false); err != nil {
  4010  					return err
  4011  				}
  4012  			} else {
  4013  				result, err := types.ParseDecimal64FromByte(s, totype.Width, totype.Scale)
  4014  				if err != nil {
  4015  					return err
  4016  				}
  4017  				if err = to.Append(result, false); err != nil {
  4018  					return err
  4019  				}
  4020  			}
  4021  		}
  4022  	}
  4023  	return nil
  4024  }
  4025  
  4026  func strToDecimal128(
  4027  	from vector.FunctionParameterWrapper[types.Varlena],
  4028  	to *vector.FunctionResult[types.Decimal128], length int,
  4029  ) error {
  4030  	var i uint64
  4031  	var l = uint64(length)
  4032  	var dft types.Decimal128
  4033  	totype := to.GetType()
  4034  	isb := from.GetSourceVector().GetIsBin()
  4035  	for i = 0; i < l; i++ {
  4036  		v, null := from.GetStrValue(i)
  4037  		if null {
  4038  			if err := to.Append(dft, true); err != nil {
  4039  				return err
  4040  			}
  4041  		} else {
  4042  			s := convertByteSliceToString(v)
  4043  			if !isb {
  4044  				result, err := types.ParseDecimal128(s, totype.Width, totype.Scale)
  4045  				if err != nil {
  4046  					return err
  4047  				}
  4048  				if err = to.Append(result, false); err != nil {
  4049  					return err
  4050  				}
  4051  			} else {
  4052  				result, err := types.ParseDecimal128FromByte(s, totype.Width, totype.Scale)
  4053  				if err != nil {
  4054  					return err
  4055  				}
  4056  				if err = to.Append(result, false); err != nil {
  4057  					return err
  4058  				}
  4059  			}
  4060  		}
  4061  	}
  4062  	return nil
  4063  }
  4064  
  4065  func strToBool(
  4066  	from vector.FunctionParameterWrapper[types.Varlena],
  4067  	to *vector.FunctionResult[bool], length int) error {
  4068  	var i uint64
  4069  	var l = uint64(length)
  4070  	for i = 0; i < l; i++ {
  4071  		v, null := from.GetStrValue(i)
  4072  		if null {
  4073  			if err := to.Append(false, true); err != nil {
  4074  				return err
  4075  			}
  4076  		} else {
  4077  			s := convertByteSliceToString(v)
  4078  			val, err := types.ParseBool(s)
  4079  			if err != nil {
  4080  				return err
  4081  			}
  4082  			if err = to.Append(val, false); err != nil {
  4083  				return err
  4084  			}
  4085  		}
  4086  	}
  4087  	return nil
  4088  }
  4089  
  4090  func strToUuid(
  4091  	from vector.FunctionParameterWrapper[types.Varlena],
  4092  	to *vector.FunctionResult[types.Uuid], length int) error {
  4093  	var i uint64
  4094  	var l = uint64(length)
  4095  	var dft types.Uuid
  4096  	for i = 0; i < l; i++ {
  4097  		v, null := from.GetStrValue(i)
  4098  		if null {
  4099  			if err := to.Append(dft, true); err != nil {
  4100  				return err
  4101  			}
  4102  		} else {
  4103  			s := convertByteSliceToString(v)
  4104  			val, err := types.ParseUuid(s)
  4105  			if err != nil {
  4106  				return err
  4107  			}
  4108  			if err = to.Append(val, false); err != nil {
  4109  				return err
  4110  			}
  4111  		}
  4112  	}
  4113  	return nil
  4114  }
  4115  
  4116  func strToJson(
  4117  	from vector.FunctionParameterWrapper[types.Varlena],
  4118  	to *vector.FunctionResult[types.Varlena], length int) error {
  4119  	var i uint64
  4120  	var l = uint64(length)
  4121  	for i = 0; i < l; i++ {
  4122  		v, null := from.GetStrValue(i)
  4123  		if null {
  4124  			if err := to.AppendBytes(nil, true); err != nil {
  4125  				return err
  4126  			}
  4127  		} else {
  4128  			s := convertByteSliceToString(v)
  4129  			json, err := types.ParseStringToByteJson(s)
  4130  			if err != nil {
  4131  				return err
  4132  			}
  4133  			val, err := types.EncodeJson(json)
  4134  			if err != nil {
  4135  				return err
  4136  			}
  4137  			if err = to.AppendBytes(val, false); err != nil {
  4138  				return err
  4139  			}
  4140  		}
  4141  	}
  4142  	return nil
  4143  }
  4144  
  4145  func strToDate(
  4146  	from vector.FunctionParameterWrapper[types.Varlena],
  4147  	to *vector.FunctionResult[types.Date], length int) error {
  4148  	var i uint64
  4149  	var l = uint64(length)
  4150  	var dft types.Date
  4151  	for i = 0; i < l; i++ {
  4152  		v, null := from.GetStrValue(i)
  4153  		if null || len(v) == 0 {
  4154  			if err := to.Append(dft, true); err != nil {
  4155  				return err
  4156  			}
  4157  		} else {
  4158  			s := convertByteSliceToString(v)
  4159  			val, err := types.ParseDateCast(s)
  4160  			if err != nil {
  4161  				return err
  4162  			}
  4163  			if err = to.Append(val, false); err != nil {
  4164  				return err
  4165  			}
  4166  		}
  4167  	}
  4168  	return nil
  4169  }
  4170  
  4171  func strToTime(
  4172  	from vector.FunctionParameterWrapper[types.Varlena],
  4173  	to *vector.FunctionResult[types.Time], length int) error {
  4174  	var i uint64
  4175  	var l = uint64(length)
  4176  	var dft types.Time
  4177  	totype := to.GetType()
  4178  	for i = 0; i < l; i++ {
  4179  		v, null := from.GetStrValue(i)
  4180  		if null || len(v) == 0 {
  4181  			if err := to.Append(dft, true); err != nil {
  4182  				return err
  4183  			}
  4184  		} else {
  4185  			s := convertByteSliceToString(v)
  4186  			val, err := types.ParseTime(s, totype.Scale)
  4187  			if err != nil {
  4188  				return err
  4189  			}
  4190  			if err = to.Append(val, false); err != nil {
  4191  				return err
  4192  			}
  4193  		}
  4194  	}
  4195  	return nil
  4196  }
  4197  
  4198  func strToDatetime(
  4199  	from vector.FunctionParameterWrapper[types.Varlena],
  4200  	to *vector.FunctionResult[types.Datetime], length int) error {
  4201  	var i uint64
  4202  	var l = uint64(length)
  4203  	var dft types.Datetime
  4204  	totype := to.GetType()
  4205  	for i = 0; i < l; i++ {
  4206  		v, null := from.GetStrValue(i)
  4207  		if null || len(v) == 0 {
  4208  			if err := to.Append(dft, true); err != nil {
  4209  				return err
  4210  			}
  4211  		} else {
  4212  			s := convertByteSliceToString(v)
  4213  			val, err := types.ParseDatetime(s, totype.Scale)
  4214  			if err != nil {
  4215  				return err
  4216  			}
  4217  			if err = to.Append(val, false); err != nil {
  4218  				return err
  4219  			}
  4220  		}
  4221  	}
  4222  	return nil
  4223  }
  4224  
  4225  func strToTimestamp(
  4226  	from vector.FunctionParameterWrapper[types.Varlena],
  4227  	to *vector.FunctionResult[types.Timestamp],
  4228  	zone *time.Location, length int) error {
  4229  	var i uint64
  4230  	var l = uint64(length)
  4231  	var dft types.Timestamp
  4232  	totype := to.GetType()
  4233  	for i = 0; i < l; i++ {
  4234  		v, null := from.GetStrValue(i)
  4235  		if null || len(v) == 0 {
  4236  			if err := to.Append(dft, true); err != nil {
  4237  				return err
  4238  			}
  4239  		} else {
  4240  			s := convertByteSliceToString(v)
  4241  			val, err := types.ParseTimestamp(zone, s, totype.Scale)
  4242  			if err != nil {
  4243  				return err
  4244  			}
  4245  			if err = to.Append(val, false); err != nil {
  4246  				return err
  4247  			}
  4248  		}
  4249  	}
  4250  	return nil
  4251  }
  4252  
  4253  func strToStr(
  4254  	ctx context.Context,
  4255  	from vector.FunctionParameterWrapper[types.Varlena],
  4256  	to *vector.FunctionResult[types.Varlena], length int, toType types.Type) error {
  4257  	totype := to.GetType()
  4258  	destLen := int(totype.Width)
  4259  	var i uint64
  4260  	var l = uint64(length)
  4261  	// Here cast using cast(data_type as binary[(n)]).
  4262  	if toType.Oid == types.T_binary && toType.Scale == -1 {
  4263  		for i = 0; i < l; i++ {
  4264  			v, null := from.GetStrValue(i)
  4265  			if err := explicitCastToBinary(toType, v, null, to); err != nil {
  4266  				return err
  4267  			}
  4268  		}
  4269  		return nil
  4270  	}
  4271  	if totype.Oid != types.T_text && destLen != 0 {
  4272  		for i = 0; i < l; i++ {
  4273  			v, null := from.GetStrValue(i)
  4274  			if null {
  4275  				if err := to.AppendBytes(nil, true); err != nil {
  4276  					return err
  4277  				}
  4278  				continue
  4279  			}
  4280  			// check the length.
  4281  			s := convertByteSliceToString(v)
  4282  			if utf8.RuneCountInString(s) > destLen {
  4283  				return formatCastError(ctx, from.GetSourceVector(), totype, fmt.Sprintf(
  4284  					"Src length %v is larger than Dest length %v", len(s), destLen))
  4285  			}
  4286  			if toType.Oid == types.T_binary && len(v) < int(toType.Width) {
  4287  				add0 := int(toType.Width) - len(v)
  4288  				for ; add0 != 0; add0-- {
  4289  					v = append(v, 0)
  4290  				}
  4291  			}
  4292  			if err := to.AppendBytes(v, false); err != nil {
  4293  				return err
  4294  			}
  4295  		}
  4296  	} else {
  4297  		for i = 0; i < l; i++ {
  4298  			v, null := from.GetStrValue(i)
  4299  			if null {
  4300  				if err := to.AppendBytes(nil, true); err != nil {
  4301  					return err
  4302  				}
  4303  				continue
  4304  			}
  4305  			if err := to.AppendBytes(v, false); err != nil {
  4306  				return err
  4307  			}
  4308  		}
  4309  	}
  4310  	return nil
  4311  }
  4312  
  4313  func strToBit(
  4314  	ctx context.Context,
  4315  	from vector.FunctionParameterWrapper[types.Varlena],
  4316  	to *vector.FunctionResult[uint64], bitSize int, length int) error {
  4317  	for i := 0; i < length; i++ {
  4318  		v, null := from.GetStrValue(uint64(i))
  4319  		if null {
  4320  			if err := to.AppendBytes(nil, true); err != nil {
  4321  				return err
  4322  			}
  4323  		} else {
  4324  			if len(v) > 8 {
  4325  				return moerr.NewOutOfRange(ctx, fmt.Sprintf("bit(%d)", bitSize), "value %s", string(v))
  4326  			}
  4327  
  4328  			var val uint64
  4329  			for j := range v {
  4330  				val = (val << 8) | uint64(v[j])
  4331  			}
  4332  			if val > uint64(1<<bitSize-1) {
  4333  				return moerr.NewOutOfRange(ctx, fmt.Sprintf("bit(%d)", bitSize), "value %s", string(v))
  4334  			}
  4335  
  4336  			if err := to.Append(val, false); err != nil {
  4337  				return err
  4338  			}
  4339  		}
  4340  	}
  4341  	return nil
  4342  }
  4343  
  4344  func strToArray[T types.RealNumbers](
  4345  	_ context.Context,
  4346  	from vector.FunctionParameterWrapper[types.Varlena],
  4347  	to *vector.FunctionResult[types.Varlena], length int, _ types.Type) error {
  4348  
  4349  	var i uint64
  4350  	var l = uint64(length)
  4351  	for i = 0; i < l; i++ {
  4352  		v, null := from.GetStrValue(i)
  4353  		if null || len(v) == 0 {
  4354  			if err := to.AppendBytes(nil, true); err != nil {
  4355  				return err
  4356  			}
  4357  		} else {
  4358  
  4359  			b, err := types.StringToArrayToBytes[T](convertByteSliceToString(v))
  4360  			if err != nil {
  4361  				return err
  4362  			}
  4363  			if err = to.AppendBytes(b, false); err != nil {
  4364  				return err
  4365  			}
  4366  		}
  4367  	}
  4368  	return nil
  4369  }
  4370  
  4371  func blobToArray[T types.RealNumbers](
  4372  	_ context.Context,
  4373  	from vector.FunctionParameterWrapper[types.Varlena],
  4374  	to *vector.FunctionResult[types.Varlena], length int, _ types.Type) error {
  4375  
  4376  	toType := to.GetType()
  4377  
  4378  	var i uint64
  4379  	var l = uint64(length)
  4380  	for i = 0; i < l; i++ {
  4381  
  4382  		v, null := from.GetStrValue(i)
  4383  		if null || len(v) == 0 {
  4384  			if err := to.AppendBytes(nil, true); err != nil {
  4385  				return err
  4386  			}
  4387  		} else {
  4388  			arr := types.BytesToArray[T](v)
  4389  			if int(toType.Width) != len(arr) {
  4390  				return moerr.NewArrayDefMismatchNoCtx(int(toType.Width), len(arr))
  4391  			}
  4392  
  4393  			if err := to.AppendBytes(v, false); err != nil {
  4394  				return err
  4395  			}
  4396  		}
  4397  	}
  4398  	return nil
  4399  }
  4400  
  4401  func arrayToArray[I types.RealNumbers, O types.RealNumbers](
  4402  	_ context.Context,
  4403  	from vector.FunctionParameterWrapper[types.Varlena],
  4404  	to *vector.FunctionResult[types.Varlena], length int, _ types.Type) error {
  4405  
  4406  	var i uint64
  4407  	var l = uint64(length)
  4408  	for i = 0; i < l; i++ {
  4409  		v, null := from.GetStrValue(i)
  4410  		if null {
  4411  			if err := to.AppendBytes(nil, true); err != nil {
  4412  				return err
  4413  			}
  4414  			continue
  4415  		}
  4416  
  4417  		// NOTE: During ARRAY --> ARRAY conversion, if you do width check
  4418  		// `to.GetType().Width != from.GetType().Width`
  4419  		// cases b/b and b+sqrt(b) fails.
  4420  
  4421  		if from.GetType().Oid == to.GetType().Oid {
  4422  			// Eg:- VECF32(3) --> VECF32(3)
  4423  			if err := to.AppendBytes(v, false); err != nil {
  4424  				return err
  4425  			}
  4426  		} else {
  4427  			// Eg:- VECF32(3) --> VECF64(3)
  4428  			_v := types.BytesToArray[I](v)
  4429  			cast, err := moarray.Cast[I, O](_v)
  4430  			if err != nil {
  4431  				return err
  4432  			}
  4433  			bytes := types.ArrayToBytes[O](cast)
  4434  			if err := to.AppendBytes(bytes, false); err != nil {
  4435  				return err
  4436  			}
  4437  		}
  4438  
  4439  	}
  4440  	return nil
  4441  }
  4442  
  4443  func uuidToStr(
  4444  	ctx context.Context,
  4445  	from vector.FunctionParameterWrapper[types.Uuid],
  4446  	to *vector.FunctionResult[types.Varlena], length int, toType types.Type) error {
  4447  	var i uint64
  4448  	var l = uint64(length)
  4449  	// Here cast using cast(data_type as binary[(n)]).
  4450  	if toType.Oid == types.T_binary && toType.Scale == -1 {
  4451  		for i = 0; i < l; i++ {
  4452  			v, null := from.GetValue(i)
  4453  			v1 := []byte(v.ToString())
  4454  			if err := explicitCastToBinary(toType, v1, null, to); err != nil {
  4455  				return err
  4456  			}
  4457  		}
  4458  		return nil
  4459  	}
  4460  	for i = 0; i < l; i++ {
  4461  		v, null := from.GetValue(i)
  4462  		if null {
  4463  			if err := to.AppendBytes(nil, true); err != nil {
  4464  				return err
  4465  			}
  4466  		} else {
  4467  			result := []byte(v.ToString())
  4468  			if toType.Oid == types.T_binary || toType.Oid == types.T_varbinary {
  4469  				if int32(len(result)) > toType.Width {
  4470  					return moerr.NewDataTruncatedNoCtx("Uuid", "truncated for binary/varbinary")
  4471  				}
  4472  			}
  4473  			if toType.Oid == types.T_binary && len(result) < int(toType.Width) {
  4474  				add0 := int(toType.Width) - len(result)
  4475  				for ; add0 != 0; add0-- {
  4476  					result = append(result, 0)
  4477  				}
  4478  			}
  4479  			if toType.Oid == types.T_char || toType.Oid == types.T_varchar {
  4480  				if int32(len(result)) > toType.Width {
  4481  					return moerr.NewDataTruncatedNoCtx("Uuid", "truncated for char/varchar")
  4482  				}
  4483  			}
  4484  			if len(result) > int(toType.Width) && toType.Oid != types.T_text && toType.Oid != types.T_blob {
  4485  				return formatCastError(ctx, from.GetSourceVector(), toType, fmt.Sprintf(
  4486  					"%v is larger than Dest length %v", v.ToString(), toType.Width))
  4487  			}
  4488  			if err := to.AppendBytes(result, false); err != nil {
  4489  				return err
  4490  			}
  4491  		}
  4492  	}
  4493  	return nil
  4494  }
  4495  
  4496  func jsonToStr(
  4497  	ctx context.Context,
  4498  	from vector.FunctionParameterWrapper[types.Varlena],
  4499  	to *vector.FunctionResult[types.Varlena], length int) error {
  4500  	var i uint64
  4501  	toType := to.GetType()
  4502  	for i = 0; i < uint64(length); i++ {
  4503  		v, null := from.GetStrValue(i)
  4504  		if null {
  4505  			if err := to.AppendBytes(nil, true); err != nil {
  4506  				return err
  4507  			}
  4508  		} else {
  4509  			bj := types.DecodeJson(v)
  4510  			val, err := bj.MarshalJSON()
  4511  			if err != nil {
  4512  				return err
  4513  			}
  4514  			if len(val) > int(toType.Width) && toType.Oid != types.T_text && toType.Oid != types.T_blob {
  4515  				return formatCastError(ctx, from.GetSourceVector(), toType, fmt.Sprintf(
  4516  					"%v is larger than Dest length %v", val, toType.Width))
  4517  			}
  4518  			if err = to.AppendBytes(val, false); err != nil {
  4519  				return err
  4520  			}
  4521  		}
  4522  	}
  4523  	return nil
  4524  }
  4525  
  4526  func enumToUint16(
  4527  	from vector.FunctionParameterWrapper[types.Enum],
  4528  	to *vector.FunctionResult[uint16], length int) error {
  4529  	var i uint64
  4530  	for i = 0; i < uint64(length); i++ {
  4531  		v, null := from.GetValue(i)
  4532  		if null {
  4533  			if err := to.Append(0, true); err != nil {
  4534  				return err
  4535  			}
  4536  		} else {
  4537  			if err := to.Append(uint16(v), false); err != nil {
  4538  				return err
  4539  			}
  4540  		}
  4541  	}
  4542  	return nil
  4543  }
  4544  
  4545  func enumToStr(
  4546  	ctx context.Context,
  4547  	from vector.FunctionParameterWrapper[types.Enum],
  4548  	to *vector.FunctionResult[types.Varlena], length int) error {
  4549  	var i uint64
  4550  	toType := to.GetType()
  4551  	for i = 0; i < uint64(length); i++ {
  4552  		v, null := from.GetValue(i)
  4553  		if null {
  4554  			if err := to.AppendBytes(nil, true); err != nil {
  4555  				return err
  4556  			}
  4557  		} else {
  4558  			result := strconv.FormatUint(uint64(v), 10)
  4559  			if len(result) > int(toType.Width) && toType.Oid != types.T_text && toType.Oid != types.T_blob {
  4560  				return formatCastError(ctx, from.GetSourceVector(), toType, fmt.Sprintf(
  4561  					"%v is larger than Dest length %v", v, toType.Width))
  4562  			}
  4563  			if err := to.AppendBytes([]byte(result), false); err != nil {
  4564  				return err
  4565  			}
  4566  		}
  4567  	}
  4568  	return nil
  4569  }
  4570  
  4571  func OverflowForNumericToNumeric[T1, T2 constraints.Integer | constraints.Float](ctx context.Context,
  4572  	xs []T1, nsp *nulls.Nulls) error {
  4573  	return overflowForNumericToNumeric[T1, T2](ctx, xs, nsp)
  4574  }
  4575  
  4576  func overflowForNumericToNumeric[T1, T2 constraints.Integer | constraints.Float](ctx context.Context,
  4577  	xs []T1, nsp *nulls.Nulls) error {
  4578  	if len(xs) == 0 {
  4579  		return nil
  4580  	}
  4581  
  4582  	var t2 T2
  4583  	var ri interface{} = &t2
  4584  	switch slice := (any(xs)).(type) {
  4585  
  4586  	case []int8:
  4587  		switch ri.(type) {
  4588  		case *uint8, *uint16, *uint32, *uint64:
  4589  			for i, x := range xs {
  4590  				if !nsp.Contains(uint64(i)) && x < 0 {
  4591  					return moerr.NewOutOfRange(ctx, "uint", "value '%v'", x)
  4592  				}
  4593  			}
  4594  		}
  4595  
  4596  	case []int16:
  4597  		switch ri.(type) {
  4598  		case *int8:
  4599  			for i, x := range slice {
  4600  				if !nsp.Contains(uint64(i)) && (x < math.MinInt8 || x > math.MaxInt8) {
  4601  					return moerr.NewOutOfRange(ctx, "int8", "value '%v'", x)
  4602  				}
  4603  			}
  4604  		case *uint8:
  4605  			for i, x := range slice {
  4606  				if !nsp.Contains(uint64(i)) && (x < 0 || x > math.MaxUint8) {
  4607  					return moerr.NewOutOfRange(ctx, "uint8", "value '%v'", x)
  4608  				}
  4609  			}
  4610  		case *uint16, *uint32, *uint64:
  4611  			for i, x := range slice {
  4612  				if !nsp.Contains(uint64(i)) && x < 0 {
  4613  					return moerr.NewOutOfRange(ctx, "uint", "value '%v'", x)
  4614  				}
  4615  			}
  4616  		}
  4617  
  4618  	case []int32:
  4619  		switch ri.(type) {
  4620  		case *int8:
  4621  			for i, x := range slice {
  4622  				if !nsp.Contains(uint64(i)) && (x < math.MinInt8 || x > math.MaxInt8) {
  4623  					return moerr.NewOutOfRange(ctx, "int8", "value '%v'", x)
  4624  				}
  4625  			}
  4626  		case *int16:
  4627  			for i, x := range slice {
  4628  				if !nsp.Contains(uint64(i)) && (x < math.MinInt16 || x > math.MaxInt16) {
  4629  					return moerr.NewOutOfRange(ctx, "int16", "value '%v'", x)
  4630  				}
  4631  			}
  4632  		case *uint8:
  4633  			for i, x := range slice {
  4634  				if !nsp.Contains(uint64(i)) && (x < 0 || x > math.MaxUint8) {
  4635  					return moerr.NewOutOfRange(ctx, "uint8", "value '%v'", x)
  4636  				}
  4637  			}
  4638  		case *uint16:
  4639  			for i, x := range slice {
  4640  				if !nsp.Contains(uint64(i)) && (x < 0 || x > math.MaxUint16) {
  4641  					return moerr.NewOutOfRange(ctx, "uint16", "value '%v'", x)
  4642  				}
  4643  			}
  4644  		case *uint32, *uint64:
  4645  			for i, x := range slice {
  4646  				if !nsp.Contains(uint64(i)) && x < 0 {
  4647  					return moerr.NewOutOfRange(ctx, "uint", "value '%v'", x)
  4648  				}
  4649  			}
  4650  		}
  4651  
  4652  	case []int64:
  4653  		switch ri.(type) {
  4654  		case *int8:
  4655  			for i, x := range slice {
  4656  				if !nsp.Contains(uint64(i)) && (x < math.MinInt8 || x > math.MaxInt8) {
  4657  					return moerr.NewOutOfRange(ctx, "int8", "value '%v'", x)
  4658  				}
  4659  			}
  4660  		case *int16:
  4661  			for i, x := range slice {
  4662  				if !nsp.Contains(uint64(i)) && (x < math.MinInt16 || x > math.MaxInt16) {
  4663  					return moerr.NewOutOfRange(ctx, "int16", "value '%v'", x)
  4664  				}
  4665  			}
  4666  		case *int32:
  4667  			for i, x := range slice {
  4668  				if !nsp.Contains(uint64(i)) && (x < math.MinInt32 || x > math.MaxInt32) {
  4669  					return moerr.NewOutOfRange(ctx, "int32", "value '%v'", x)
  4670  				}
  4671  			}
  4672  		case *uint8:
  4673  			for i, x := range slice {
  4674  				if !nsp.Contains(uint64(i)) && (x < 0 || x > math.MaxUint8) {
  4675  					return moerr.NewOutOfRange(ctx, "uint8", "value '%v'", x)
  4676  				}
  4677  			}
  4678  		case *uint16:
  4679  			for i, x := range slice {
  4680  				if !nsp.Contains(uint64(i)) && (x < 0 || x > math.MaxUint16) {
  4681  					return moerr.NewOutOfRange(ctx, "uint16", "value '%v'", x)
  4682  				}
  4683  			}
  4684  		case *uint32:
  4685  			for i, x := range slice {
  4686  				if !nsp.Contains(uint64(i)) && (x < 0 || x > math.MaxUint32) {
  4687  					return moerr.NewOutOfRange(ctx, "uint32", "value '%v'", x)
  4688  				}
  4689  			}
  4690  		case *uint64:
  4691  			for i, x := range slice {
  4692  				if !nsp.Contains(uint64(i)) && x < 0 {
  4693  					// XXX for adapt to bvt, but i don't know why we hide the wrong value here.
  4694  					return moerr.NewOutOfRange(ctx, "uint64", "value '%v'", x)
  4695  				}
  4696  			}
  4697  		}
  4698  
  4699  	case []uint8:
  4700  		switch ri.(type) {
  4701  		case *int8:
  4702  			for i, x := range slice {
  4703  				if !nsp.Contains(uint64(i)) && x > math.MaxInt8 {
  4704  					return moerr.NewOutOfRange(ctx, "int8", "value '%v'", x)
  4705  				}
  4706  			}
  4707  		}
  4708  
  4709  	case []uint16:
  4710  		switch ri.(type) {
  4711  		case *int8:
  4712  			for i, x := range slice {
  4713  				if !nsp.Contains(uint64(i)) && x > math.MaxInt8 {
  4714  					return moerr.NewOutOfRange(ctx, "int8", "value '%v'", x)
  4715  				}
  4716  			}
  4717  		case *int16:
  4718  			for i, x := range slice {
  4719  				if !nsp.Contains(uint64(i)) && x > math.MaxInt16 {
  4720  					return moerr.NewOutOfRange(ctx, "int16", "value '%v'", x)
  4721  				}
  4722  			}
  4723  		case *uint8:
  4724  			for i, x := range slice {
  4725  				if !nsp.Contains(uint64(i)) && x > math.MaxUint8 {
  4726  					return moerr.NewOutOfRange(ctx, "uint8", "value '%v'", x)
  4727  				}
  4728  			}
  4729  		}
  4730  
  4731  	case []uint32:
  4732  		switch ri.(type) {
  4733  		case *int8:
  4734  			for i, x := range slice {
  4735  				if !nsp.Contains(uint64(i)) && x > math.MaxInt8 {
  4736  					return moerr.NewOutOfRange(ctx, "int8", "value '%v'", x)
  4737  				}
  4738  			}
  4739  		case *int16:
  4740  			for i, x := range slice {
  4741  				if !nsp.Contains(uint64(i)) && x > math.MaxInt16 {
  4742  					return moerr.NewOutOfRange(ctx, "int16", "value '%v'", x)
  4743  				}
  4744  			}
  4745  		case *int32:
  4746  			for i, x := range slice {
  4747  				if !nsp.Contains(uint64(i)) && x > math.MaxInt32 {
  4748  					return moerr.NewOutOfRange(ctx, "int32", "value '%v'", x)
  4749  				}
  4750  			}
  4751  		case *uint8:
  4752  			for i, x := range slice {
  4753  				if !nsp.Contains(uint64(i)) && x > math.MaxUint8 {
  4754  					return moerr.NewOutOfRange(ctx, "uint8", "value '%v'", x)
  4755  				}
  4756  			}
  4757  		case *uint16:
  4758  			for i, x := range slice {
  4759  				if !nsp.Contains(uint64(i)) && x > math.MaxUint16 {
  4760  					return moerr.NewOutOfRange(ctx, "uint16", "value '%v'", x)
  4761  				}
  4762  			}
  4763  		}
  4764  
  4765  	case []uint64:
  4766  		switch ri.(type) {
  4767  		case *int8:
  4768  			for i, x := range slice {
  4769  				if !nsp.Contains(uint64(i)) && x > math.MaxInt8 {
  4770  					return moerr.NewOutOfRange(ctx, "int8", "value '%v'", x)
  4771  				}
  4772  			}
  4773  		case *int16:
  4774  			for i, x := range slice {
  4775  				if !nsp.Contains(uint64(i)) && x > math.MaxInt16 {
  4776  					return moerr.NewOutOfRange(ctx, "int16", "value '%v'", x)
  4777  				}
  4778  			}
  4779  		case *int32:
  4780  			for i, x := range slice {
  4781  				if !nsp.Contains(uint64(i)) && x > math.MaxInt32 {
  4782  					return moerr.NewOutOfRange(ctx, "int32", "value '%v'", x)
  4783  				}
  4784  			}
  4785  		case *int64:
  4786  			for i, x := range slice {
  4787  				if !nsp.Contains(uint64(i)) && x > math.MaxInt64 {
  4788  					return moerr.NewOutOfRange(ctx, "int64", "value '%v'", x)
  4789  				}
  4790  			}
  4791  		case *uint8:
  4792  			for i, x := range slice {
  4793  				if !nsp.Contains(uint64(i)) && x > math.MaxUint8 {
  4794  					return moerr.NewOutOfRange(ctx, "uint8", "value '%v'", x)
  4795  				}
  4796  			}
  4797  		case *uint16:
  4798  			for i, x := range slice {
  4799  				if !nsp.Contains(uint64(i)) && x > math.MaxUint16 {
  4800  					return moerr.NewOutOfRange(ctx, "uint16", "value '%v'", x)
  4801  				}
  4802  			}
  4803  		case *uint32:
  4804  			for i, x := range slice {
  4805  				if !nsp.Contains(uint64(i)) && x > math.MaxUint32 {
  4806  					return moerr.NewOutOfRange(ctx, "uint32", "value '%v'", x)
  4807  				}
  4808  			}
  4809  		}
  4810  
  4811  	case []float32:
  4812  		switch ri.(type) {
  4813  		case *int8:
  4814  			for i, x := range slice {
  4815  				if !nsp.Contains(uint64(i)) && math.Round(float64(x)) > math.MaxInt8 {
  4816  					return moerr.NewOutOfRange(ctx, "int8", "value '%v'", x)
  4817  				}
  4818  			}
  4819  		case *int16:
  4820  			for i, x := range slice {
  4821  				if !nsp.Contains(uint64(i)) && math.Round(float64(x)) > math.MaxInt16 {
  4822  					return moerr.NewOutOfRange(ctx, "int16", "value '%v'", x)
  4823  				}
  4824  			}
  4825  		case *int32:
  4826  			for i, x := range slice {
  4827  				if !nsp.Contains(uint64(i)) && math.Round(float64(x)) > math.MaxInt32 {
  4828  					return moerr.NewOutOfRange(ctx, "int32", "value '%v'", x)
  4829  				}
  4830  			}
  4831  		case *int64:
  4832  			for i, x := range slice {
  4833  				if !nsp.Contains(uint64(i)) && math.Round(float64(x)) > math.MaxInt64 {
  4834  					return moerr.NewOutOfRange(ctx, "int64", "value '%v'", x)
  4835  				}
  4836  			}
  4837  		case *uint8:
  4838  			for i, x := range slice {
  4839  				if !nsp.Contains(uint64(i)) && math.Round(float64(x)) > math.MaxUint8 {
  4840  					return moerr.NewOutOfRange(ctx, "uint8", "value '%v'", x)
  4841  				}
  4842  			}
  4843  		case *uint16:
  4844  			for i, x := range slice {
  4845  				if !nsp.Contains(uint64(i)) && math.Round(float64(x)) > math.MaxUint16 {
  4846  					return moerr.NewOutOfRange(ctx, "uint16", "value '%v'", x)
  4847  				}
  4848  			}
  4849  		case *uint32:
  4850  			for i, x := range slice {
  4851  				if !nsp.Contains(uint64(i)) && math.Round(float64(x)) > math.MaxUint32 {
  4852  					return moerr.NewOutOfRange(ctx, "uint32", "value '%v'", x)
  4853  				}
  4854  			}
  4855  		case *uint64:
  4856  			for i, x := range slice {
  4857  				if !nsp.Contains(uint64(i)) && math.Round(float64(x)) > math.MaxUint64 {
  4858  					return moerr.NewOutOfRange(ctx, "uint64", "value '%v'", x)
  4859  				}
  4860  			}
  4861  		}
  4862  
  4863  	case []float64:
  4864  		switch ri.(type) {
  4865  		case *int8:
  4866  			for i, x := range slice {
  4867  				if !nsp.Contains(uint64(i)) && math.Round(x) > math.MaxInt8 {
  4868  					return moerr.NewOutOfRange(ctx, "int8", "value '%v'", x)
  4869  				}
  4870  			}
  4871  		case *int16:
  4872  			for i, x := range slice {
  4873  				if !nsp.Contains(uint64(i)) && math.Round(x) > math.MaxInt16 {
  4874  					return moerr.NewOutOfRange(ctx, "int16", "value '%v'", x)
  4875  				}
  4876  			}
  4877  		case *int32:
  4878  			for i, x := range slice {
  4879  				if !nsp.Contains(uint64(i)) && math.Round(x) > math.MaxInt32 {
  4880  					return moerr.NewOutOfRange(ctx, "int32", "value '%v'", x)
  4881  				}
  4882  			}
  4883  		case *int64:
  4884  			for i, x := range slice {
  4885  				if !nsp.Contains(uint64(i)) &&
  4886  					(math.Round(x) > math.MaxInt64 || math.Round(x) < math.MinInt64) {
  4887  					return moerr.NewOutOfRange(ctx, "int64", "value '%v'", x)
  4888  				}
  4889  			}
  4890  		case *uint8:
  4891  			for i, x := range slice {
  4892  				if !nsp.Contains(uint64(i)) && math.Round(x) > math.MaxUint8 {
  4893  					return moerr.NewOutOfRange(ctx, "uint8", "value '%v'", x)
  4894  				}
  4895  			}
  4896  		case *uint16:
  4897  			for i, x := range slice {
  4898  				if !nsp.Contains(uint64(i)) && math.Round(x) > math.MaxUint16 {
  4899  					return moerr.NewOutOfRange(ctx, "uint16", "value '%v'", x)
  4900  				}
  4901  			}
  4902  		case *uint32:
  4903  			for i, x := range slice {
  4904  				if !nsp.Contains(uint64(i)) && math.Round(x) > math.MaxUint32 {
  4905  					return moerr.NewOutOfRange(ctx, "uint32", "value '%v'", x)
  4906  				}
  4907  			}
  4908  		case *uint64:
  4909  			for i, x := range slice {
  4910  				if !nsp.Contains(uint64(i)) && math.Round(x) > math.MaxUint64 {
  4911  					return moerr.NewOutOfRange(ctx, "uint64", "value '%v'", x)
  4912  				}
  4913  			}
  4914  		case *float32:
  4915  			for i, x := range slice {
  4916  				if !nsp.Contains(uint64(i)) && x > math.MaxFloat32 {
  4917  					return moerr.NewOutOfRange(ctx, "float32", "value '%v'", x)
  4918  				}
  4919  			}
  4920  		}
  4921  
  4922  	}
  4923  	return nil
  4924  }
  4925  
  4926  func appendNulls[T types.FixedSizeT](result vector.FunctionResultWrapper, length int) error {
  4927  	if r, ok := result.(*vector.FunctionResult[types.Varlena]); ok {
  4928  		var i uint64
  4929  		for i = 0; i < uint64(length); i++ {
  4930  			if err := r.AppendBytes(nil, true); err != nil {
  4931  				return err
  4932  			}
  4933  		}
  4934  		return nil
  4935  	}
  4936  	if r, ok := result.(*vector.FunctionResult[T]); ok {
  4937  		var t T
  4938  		var i uint64
  4939  		for i = 0; i < uint64(length); i++ {
  4940  			if err := r.Append(t, true); err != nil {
  4941  				return err
  4942  			}
  4943  		}
  4944  		return nil
  4945  	}
  4946  	return nil
  4947  }
  4948  
  4949  // convertByteSliceToString is just a temp method.
  4950  func convertByteSliceToString(v []byte) string {
  4951  	return util.UnsafeBytesToString(v)
  4952  	// return string(v)
  4953  }
  4954  
  4955  // shorten the string to the one with no more than 101 characters.
  4956  func shortenValueString(valueStr string) string {
  4957  	utf8Str := []rune(valueStr)
  4958  	l := len(utf8Str)
  4959  	if l > 100 {
  4960  		return string(utf8Str[:100]) + "..."
  4961  	}
  4962  	return valueStr
  4963  }
  4964  
  4965  func FormatCastErrorForInsertValue(ctx context.Context, originStr string, typ types.Type, extraInfo string) error {
  4966  	valueStr := strings.TrimRight(strings.TrimLeft(originStr, "["), "]")
  4967  	shortenValueStr := shortenValueString(valueStr)
  4968  	errStr := fmt.Sprintf("Can't cast '%s' to %v type.", shortenValueStr, typ)
  4969  	return moerr.NewInternalError(ctx, errStr+" "+extraInfo)
  4970  }
  4971  
  4972  func formatCastError(ctx context.Context, vec *vector.Vector, typ types.Type, extraInfo string) error {
  4973  	var errStr string
  4974  	if vec.IsConst() {
  4975  		if vec.IsConstNull() {
  4976  			errStr = fmt.Sprintf("Can't cast 'NULL' as %v type.", typ)
  4977  		} else {
  4978  			valueStr := strings.TrimRight(strings.TrimLeft(fmt.Sprintf("%v", vec), "["), "]")
  4979  			shortenValueStr := shortenValueString(valueStr)
  4980  			errStr = fmt.Sprintf("Can't cast '%s' from %v type to %v type.", shortenValueStr, vec.GetType(), typ)
  4981  		}
  4982  	} else {
  4983  		errStr = fmt.Sprintf("Can't cast column from %v type to %v type because of one or more values in that column.", vec.GetType(), typ)
  4984  	}
  4985  	return moerr.NewInternalError(ctx, errStr+" "+extraInfo)
  4986  }
  4987  
  4988  func explicitCastToBinary(toType types.Type, v []byte, null bool, to *vector.FunctionResult[types.Varlena]) error {
  4989  	if null {
  4990  		if err := to.AppendBytes(nil, true); err != nil {
  4991  			return err
  4992  		}
  4993  		return nil
  4994  	}
  4995  	// cast(data_type as binary)
  4996  	if toType.Width == -1 {
  4997  		if err := to.AppendBytes(v, false); err != nil {
  4998  			return err
  4999  		}
  5000  		return nil
  5001  	}
  5002  	// cast(data_type as binary(n))
  5003  	// truncating
  5004  	if int32(len(v)) > toType.Width {
  5005  		v = v[:toType.Width]
  5006  	}
  5007  	// right-padding.
  5008  	if len(v) < int(toType.Width) {
  5009  		add0 := int(toType.Width) - len(v)
  5010  		for ; add0 != 0; add0-- {
  5011  			v = append(v, 0)
  5012  		}
  5013  	}
  5014  	if err := to.AppendBytes(v, false); err != nil {
  5015  		return err
  5016  	}
  5017  	return nil
  5018  }
  5019  
  5020  func floatToBytes(v float64, bitSize int) []byte {
  5021  	if v >= float64(1e15) || (v < float64(1e-13) && v > 0) || (v < 0 && v > -1e-13) || v <= -1e15 {
  5022  		return []byte(strconv.FormatFloat(float64(v), 'E', -1, bitSize))
  5023  	} else {
  5024  		return []byte(strconv.FormatFloat(float64(v), 'f', -1, bitSize))
  5025  	}
  5026  }