github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/operator/cast2.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 operator
    16  
    17  import (
    18  	"context"
    19  	"encoding/hex"
    20  	"fmt"
    21  	"math"
    22  	"strconv"
    23  	"strings"
    24  	"time"
    25  	"unicode/utf8"
    26  	"unsafe"
    27  
    28  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    29  	"github.com/matrixorigin/matrixone/pkg/container/types"
    30  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    31  	"github.com/matrixorigin/matrixone/pkg/sql/plan/function/builtin/binary"
    32  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    33  	"golang.org/x/exp/constraints"
    34  )
    35  
    36  // XXX need this one to make a pretty function register.
    37  var supportedTypeCast = map[types.T][]types.T{
    38  	types.T_any: {
    39  		types.T_bool,
    40  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
    41  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
    42  		types.T_char, types.T_varchar, types.T_blob, types.T_text, types.T_json,
    43  		types.T_float32, types.T_float64,
    44  		types.T_decimal64, types.T_decimal128,
    45  		types.T_date, types.T_datetime,
    46  		types.T_time, types.T_timestamp,
    47  	},
    48  
    49  	types.T_bool: {
    50  		types.T_bool,
    51  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
    52  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
    53  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
    54  	},
    55  
    56  	types.T_int8: {
    57  		types.T_bool,
    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_float32, types.T_float64,
    61  		types.T_decimal64, types.T_decimal128,
    62  		types.T_time, types.T_timestamp,
    63  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
    64  	},
    65  
    66  	types.T_int16: {
    67  		types.T_bool,
    68  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
    69  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
    70  		types.T_float32, types.T_float64,
    71  		types.T_decimal64, types.T_decimal128,
    72  		types.T_time, types.T_timestamp,
    73  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
    74  	},
    75  
    76  	types.T_int32: {
    77  		types.T_bool,
    78  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
    79  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
    80  		types.T_float32, types.T_float64,
    81  		types.T_decimal64, types.T_decimal128,
    82  		types.T_time, types.T_timestamp,
    83  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
    84  	},
    85  
    86  	types.T_int64: {
    87  		types.T_bool,
    88  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
    89  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
    90  		types.T_float32, types.T_float64,
    91  		types.T_decimal64, types.T_decimal128,
    92  		types.T_time, types.T_timestamp,
    93  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
    94  	},
    95  
    96  	types.T_uint8: {
    97  		types.T_bool,
    98  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
    99  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   100  		types.T_float32, types.T_float64,
   101  		types.T_decimal64, types.T_decimal128,
   102  		types.T_time, types.T_timestamp,
   103  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   104  	},
   105  
   106  	types.T_uint16: {
   107  		types.T_bool,
   108  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   109  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   110  		types.T_float32, types.T_float64,
   111  		types.T_decimal64, types.T_decimal128,
   112  		types.T_time, types.T_timestamp,
   113  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   114  	},
   115  
   116  	types.T_uint32: {
   117  		types.T_bool,
   118  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   119  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   120  		types.T_float32, types.T_float64,
   121  		types.T_decimal64, types.T_decimal128,
   122  		types.T_time, types.T_timestamp,
   123  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   124  	},
   125  
   126  	types.T_uint64: {
   127  		types.T_bool,
   128  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   129  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   130  		types.T_float32, types.T_float64,
   131  		types.T_decimal64, types.T_decimal128,
   132  		types.T_time, types.T_timestamp,
   133  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   134  	},
   135  
   136  	types.T_float32: {
   137  		types.T_bool,
   138  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   139  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   140  		types.T_float32, types.T_float64,
   141  		types.T_decimal64, types.T_decimal128,
   142  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   143  	},
   144  
   145  	types.T_float64: {
   146  		types.T_bool,
   147  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   148  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   149  		types.T_float32, types.T_float64,
   150  		types.T_decimal64, types.T_decimal128,
   151  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   152  	},
   153  
   154  	types.T_date: {
   155  		types.T_int32, types.T_int64,
   156  		types.T_date, types.T_datetime,
   157  		types.T_time, types.T_timestamp,
   158  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   159  	},
   160  
   161  	types.T_datetime: {
   162  		types.T_int32, types.T_int64,
   163  		types.T_date, types.T_datetime,
   164  		types.T_time, types.T_timestamp,
   165  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   166  	},
   167  
   168  	types.T_timestamp: {
   169  		types.T_int32, types.T_int64,
   170  		types.T_date, types.T_datetime,
   171  		types.T_timestamp,
   172  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   173  	},
   174  
   175  	types.T_time: {
   176  		types.T_date, types.T_datetime,
   177  		types.T_time,
   178  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   179  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   180  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   181  		types.T_decimal64, types.T_decimal128,
   182  	},
   183  
   184  	types.T_decimal64: {
   185  		types.T_float32, types.T_float64,
   186  		types.T_int64,
   187  		types.T_uint64,
   188  		types.T_decimal64, types.T_decimal128,
   189  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   190  		types.T_time, types.T_timestamp,
   191  	},
   192  
   193  	types.T_decimal128: {
   194  		types.T_float32, types.T_float64,
   195  		types.T_int32, types.T_int64,
   196  		types.T_uint64,
   197  		types.T_decimal64, types.T_decimal128,
   198  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   199  		types.T_time, types.T_timestamp,
   200  	},
   201  
   202  	types.T_char: {
   203  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   204  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   205  		types.T_float32, types.T_float64,
   206  		types.T_decimal64, types.T_decimal128,
   207  		types.T_bool,
   208  		types.T_json,
   209  		types.T_uuid,
   210  		types.T_date, types.T_datetime,
   211  		types.T_time, types.T_timestamp,
   212  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   213  	},
   214  
   215  	types.T_varchar: {
   216  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   217  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   218  		types.T_float32, types.T_float64,
   219  		types.T_decimal64, types.T_decimal128,
   220  		types.T_bool,
   221  		types.T_json,
   222  		types.T_uuid,
   223  		types.T_date, types.T_datetime,
   224  		types.T_time, types.T_timestamp,
   225  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   226  	},
   227  
   228  	types.T_blob: {
   229  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   230  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   231  		types.T_float32, types.T_float64,
   232  		types.T_decimal64, types.T_decimal128,
   233  		types.T_bool,
   234  		types.T_json,
   235  		types.T_uuid,
   236  		types.T_date, types.T_datetime,
   237  		types.T_time, types.T_timestamp,
   238  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   239  	},
   240  
   241  	types.T_text: {
   242  		types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   243  		types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   244  		types.T_float32, types.T_float64,
   245  		types.T_decimal64, types.T_decimal128,
   246  		types.T_bool,
   247  		types.T_json,
   248  		types.T_uuid,
   249  		types.T_date, types.T_datetime,
   250  		types.T_time, types.T_timestamp,
   251  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   252  	},
   253  
   254  	types.T_json: {
   255  		types.T_char, types.T_varchar, types.T_text,
   256  	},
   257  
   258  	types.T_uuid: {
   259  		types.T_char, types.T_varchar, types.T_blob, types.T_text,
   260  	},
   261  
   262  	types.T_TS: {
   263  		types.T_TS,
   264  	},
   265  
   266  	types.T_Rowid: {
   267  		types.T_Rowid,
   268  	},
   269  }
   270  
   271  func IfTypeCastSupported(sourceType, targetType types.T) bool {
   272  	supportList, ok := supportedTypeCast[sourceType]
   273  	if ok {
   274  		for _, t := range supportList {
   275  			if t == targetType {
   276  				return true
   277  			}
   278  		}
   279  	}
   280  	return false
   281  }
   282  
   283  func NewCast(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   284  	var err error
   285  	// Cast Parameter1 as Type Parameter2
   286  	fromType := parameters[0].GetType()
   287  	toType := parameters[1].GetType()
   288  	from := parameters[0]
   289  	switch fromType.Oid {
   290  	case types.T_any: // scalar null
   291  		err = scalarNullToOthers(proc.Ctx, toType, result, length)
   292  	case types.T_bool:
   293  		s := vector.GenerateFunctionFixedTypeParameter[bool](from)
   294  		err = boolToOthers(proc.Ctx, s, toType, result, length)
   295  	case types.T_int8:
   296  		s := vector.GenerateFunctionFixedTypeParameter[int8](from)
   297  		err = int8ToOthers(proc.Ctx, s, toType, result, length)
   298  	case types.T_int16:
   299  		s := vector.GenerateFunctionFixedTypeParameter[int16](from)
   300  		err = int16ToOthers(proc.Ctx, s, toType, result, length)
   301  	case types.T_int32:
   302  		s := vector.GenerateFunctionFixedTypeParameter[int32](from)
   303  		err = int32ToOthers(proc.Ctx, s, toType, result, length)
   304  	case types.T_int64:
   305  		s := vector.GenerateFunctionFixedTypeParameter[int64](from)
   306  		err = int64ToOthers(proc.Ctx, s, toType, result, length)
   307  	case types.T_uint8:
   308  		s := vector.GenerateFunctionFixedTypeParameter[uint8](from)
   309  		err = uint8ToOthers(proc.Ctx, s, toType, result, length)
   310  	case types.T_uint16:
   311  		s := vector.GenerateFunctionFixedTypeParameter[uint16](from)
   312  		err = uint16ToOthers(proc.Ctx, s, toType, result, length)
   313  	case types.T_uint32:
   314  		s := vector.GenerateFunctionFixedTypeParameter[uint32](from)
   315  		err = uint32ToOthers(proc.Ctx, s, toType, result, length)
   316  	case types.T_uint64:
   317  		s := vector.GenerateFunctionFixedTypeParameter[uint64](from)
   318  		err = uint64ToOthers(proc.Ctx, s, toType, result, length)
   319  	case types.T_float32:
   320  		s := vector.GenerateFunctionFixedTypeParameter[float32](from)
   321  		err = float32ToOthers(proc.Ctx, s, toType, result, length)
   322  	case types.T_float64:
   323  		s := vector.GenerateFunctionFixedTypeParameter[float64](from)
   324  		err = float64ToOthers(proc.Ctx, s, toType, result, length)
   325  	case types.T_decimal64:
   326  		s := vector.GenerateFunctionFixedTypeParameter[types.Decimal64](from)
   327  		err = decimal64ToOthers(proc.Ctx, s, toType, result, length)
   328  	case types.T_decimal128:
   329  		s := vector.GenerateFunctionFixedTypeParameter[types.Decimal128](from)
   330  		err = decimal128ToOthers(proc.Ctx, s, toType, result, length)
   331  	case types.T_date:
   332  		s := vector.GenerateFunctionFixedTypeParameter[types.Date](from)
   333  		err = dateToOthers(proc, s, toType, result, length)
   334  	case types.T_datetime:
   335  		s := vector.GenerateFunctionFixedTypeParameter[types.Datetime](from)
   336  		err = datetimeToOthers(proc, s, toType, result, length)
   337  	case types.T_time:
   338  		s := vector.GenerateFunctionFixedTypeParameter[types.Time](from)
   339  		err = timeToOthers(proc.Ctx, s, toType, result, length)
   340  	case types.T_timestamp:
   341  		s := vector.GenerateFunctionFixedTypeParameter[types.Timestamp](from)
   342  		err = timestampToOthers(proc, s, toType, result, length)
   343  	case types.T_char, types.T_varchar, types.T_blob, types.T_text:
   344  		s := vector.GenerateFunctionStrParameter(from)
   345  		err = strTypeToOthers(proc, s, toType, result, length)
   346  	case types.T_uuid:
   347  		s := vector.GenerateFunctionFixedTypeParameter[types.Uuid](from)
   348  		err = uuidToOthers(proc.Ctx, s, toType, result, length)
   349  	case types.T_TS:
   350  		s := vector.GenerateFunctionFixedTypeParameter[types.TS](from)
   351  		err = tsToOthers(proc.Ctx, s, toType, result, length)
   352  	case types.T_Rowid:
   353  		s := vector.GenerateFunctionFixedTypeParameter[types.Rowid](from)
   354  		err = rowidToOthers(proc.Ctx, s, toType, result, length)
   355  	case types.T_json:
   356  		s := vector.GenerateFunctionStrParameter(from)
   357  		err = jsonToOthers(proc.Ctx, s, toType, result, length)
   358  	default:
   359  		// XXX we set the function here to adapt to the BVT cases.
   360  		err = formatCastError(proc.Ctx, from, toType, "")
   361  	}
   362  	return err
   363  }
   364  
   365  func scalarNullToOthers(ctx context.Context,
   366  	totype types.Type, result vector.FunctionResultWrapper, length int) error {
   367  	switch totype.Oid {
   368  	case types.T_bool:
   369  		return appendNulls[bool](result, length)
   370  	case types.T_int8:
   371  		return appendNulls[int8](result, length)
   372  	case types.T_int16:
   373  		return appendNulls[int16](result, length)
   374  	case types.T_int32:
   375  		return appendNulls[int32](result, length)
   376  	case types.T_int64:
   377  		return appendNulls[int64](result, length)
   378  	case types.T_uint8:
   379  		return appendNulls[uint8](result, length)
   380  	case types.T_uint16:
   381  		return appendNulls[uint16](result, length)
   382  	case types.T_uint32:
   383  		return appendNulls[uint32](result, length)
   384  	case types.T_uint64:
   385  		return appendNulls[uint64](result, length)
   386  	case types.T_char, types.T_varchar, types.T_blob, types.T_text, types.T_json:
   387  		return appendNulls[types.Varlena](result, length)
   388  	case types.T_float32:
   389  		return appendNulls[float32](result, length)
   390  	case types.T_float64:
   391  		return appendNulls[float64](result, length)
   392  	case types.T_decimal64:
   393  		return appendNulls[types.Decimal64](result, length)
   394  	case types.T_decimal128:
   395  		return appendNulls[types.Decimal128](result, length)
   396  	case types.T_date:
   397  		return appendNulls[types.Date](result, length)
   398  	case types.T_datetime:
   399  		return appendNulls[types.Datetime](result, length)
   400  	case types.T_time:
   401  		return appendNulls[types.Time](result, length)
   402  	case types.T_timestamp:
   403  		return appendNulls[types.Timestamp](result, length)
   404  	}
   405  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from NULL to %s", totype))
   406  }
   407  
   408  func boolToOthers(ctx context.Context,
   409  	source vector.FunctionParameterWrapper[bool],
   410  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
   411  	switch toType.Oid {
   412  	case types.T_bool:
   413  		rs := vector.MustFunctionResult[bool](result)
   414  		rs.SetFromParameter(source)
   415  		return nil
   416  	case types.T_char, types.T_varchar, types.T_blob, types.T_text:
   417  		// string type.
   418  		rs := vector.MustFunctionResult[types.Varlena](result)
   419  		return boolToStr(source, rs, length)
   420  	case types.T_int8:
   421  		rs := vector.MustFunctionResult[int8](result)
   422  		return boolToInteger(source, rs, length)
   423  	case types.T_int16:
   424  		rs := vector.MustFunctionResult[int16](result)
   425  		return boolToInteger(source, rs, length)
   426  	case types.T_int32:
   427  		rs := vector.MustFunctionResult[int32](result)
   428  		return boolToInteger(source, rs, length)
   429  	case types.T_int64:
   430  		rs := vector.MustFunctionResult[int64](result)
   431  		return boolToInteger(source, rs, length)
   432  	case types.T_uint8:
   433  		rs := vector.MustFunctionResult[uint8](result)
   434  		return boolToInteger(source, rs, length)
   435  	case types.T_uint16:
   436  		rs := vector.MustFunctionResult[uint16](result)
   437  		return boolToInteger(source, rs, length)
   438  	case types.T_uint32:
   439  		rs := vector.MustFunctionResult[uint32](result)
   440  		return boolToInteger(source, rs, length)
   441  	case types.T_uint64:
   442  		rs := vector.MustFunctionResult[uint64](result)
   443  		return boolToInteger(source, rs, length)
   444  	}
   445  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from bool to %s", toType))
   446  }
   447  
   448  // although we can merge the int8ToOthers / int16ToOthers ... into intToOthers (use the generic).
   449  // but for extensibility, we didn't do that.
   450  // uint and float are the same.
   451  func int8ToOthers(ctx context.Context,
   452  	source vector.FunctionParameterWrapper[int8],
   453  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
   454  	switch toType.Oid {
   455  	case types.T_bool:
   456  		rs := vector.MustFunctionResult[bool](result)
   457  		return numericToBool(source, rs, length)
   458  	case types.T_int8:
   459  		rs := vector.MustFunctionResult[int8](result)
   460  		rs.SetFromParameter(source)
   461  		return nil
   462  	case types.T_int16:
   463  		rs := vector.MustFunctionResult[int16](result)
   464  		return numericToNumeric(ctx, source, rs, length)
   465  	case types.T_int32:
   466  		rs := vector.MustFunctionResult[int32](result)
   467  		return numericToNumeric(ctx, source, rs, length)
   468  	case types.T_int64:
   469  		rs := vector.MustFunctionResult[int64](result)
   470  		return numericToNumeric(ctx, source, rs, length)
   471  	case types.T_uint8:
   472  		rs := vector.MustFunctionResult[uint8](result)
   473  		return numericToNumeric(ctx, source, rs, length)
   474  	case types.T_uint16:
   475  		rs := vector.MustFunctionResult[uint16](result)
   476  		return numericToNumeric(ctx, source, rs, length)
   477  	case types.T_uint32:
   478  		rs := vector.MustFunctionResult[uint32](result)
   479  		return numericToNumeric(ctx, source, rs, length)
   480  	case types.T_uint64:
   481  		rs := vector.MustFunctionResult[uint64](result)
   482  		return numericToNumeric(ctx, source, rs, length)
   483  	case types.T_float32:
   484  		rs := vector.MustFunctionResult[float32](result)
   485  		return numericToNumeric(ctx, source, rs, length)
   486  	case types.T_float64:
   487  		rs := vector.MustFunctionResult[float64](result)
   488  		return numericToNumeric(ctx, source, rs, length)
   489  	case types.T_decimal64:
   490  		rs := vector.MustFunctionResult[types.Decimal64](result)
   491  		return signedToDecimal64(source, rs, length)
   492  	case types.T_decimal128:
   493  		rs := vector.MustFunctionResult[types.Decimal128](result)
   494  		return signedToDecimal128(source, rs, length)
   495  	case types.T_char, types.T_varchar, types.T_blob, types.T_text:
   496  		// string type.
   497  		rs := vector.MustFunctionResult[types.Varlena](result)
   498  		return signedToStr(source, rs, length)
   499  	case types.T_time:
   500  		rs := vector.MustFunctionResult[types.Time](result)
   501  		return integerToTime(ctx, source, rs, length)
   502  	case types.T_timestamp:
   503  		rs := vector.MustFunctionResult[types.Timestamp](result)
   504  		return integerToTimestamp(source, rs, length)
   505  	}
   506  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from int8 to %s", toType))
   507  }
   508  
   509  func int16ToOthers(ctx context.Context,
   510  	source vector.FunctionParameterWrapper[int16],
   511  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
   512  	switch toType.Oid {
   513  	case types.T_bool:
   514  		rs := vector.MustFunctionResult[bool](result)
   515  		return numericToBool(source, rs, length)
   516  	case types.T_int8:
   517  		rs := vector.MustFunctionResult[int8](result)
   518  		return numericToNumeric(ctx, source, rs, length)
   519  	case types.T_int16:
   520  		rs := vector.MustFunctionResult[int16](result)
   521  		rs.SetFromParameter(source)
   522  		return nil
   523  	case types.T_int32:
   524  		rs := vector.MustFunctionResult[int32](result)
   525  		return numericToNumeric(ctx, source, rs, length)
   526  	case types.T_int64:
   527  		rs := vector.MustFunctionResult[int64](result)
   528  		return numericToNumeric(ctx, source, rs, length)
   529  	case types.T_uint8:
   530  		rs := vector.MustFunctionResult[uint8](result)
   531  		return numericToNumeric(ctx, source, rs, length)
   532  	case types.T_uint16:
   533  		rs := vector.MustFunctionResult[uint16](result)
   534  		return numericToNumeric(ctx, source, rs, length)
   535  	case types.T_uint32:
   536  		rs := vector.MustFunctionResult[uint32](result)
   537  		return numericToNumeric(ctx, source, rs, length)
   538  	case types.T_uint64:
   539  		rs := vector.MustFunctionResult[uint64](result)
   540  		return numericToNumeric(ctx, source, rs, length)
   541  	case types.T_float32:
   542  		rs := vector.MustFunctionResult[float32](result)
   543  		return numericToNumeric(ctx, source, rs, length)
   544  	case types.T_float64:
   545  		rs := vector.MustFunctionResult[float64](result)
   546  		return numericToNumeric(ctx, source, rs, length)
   547  	case types.T_decimal64:
   548  		rs := vector.MustFunctionResult[types.Decimal64](result)
   549  		return signedToDecimal64(source, rs, length)
   550  	case types.T_decimal128:
   551  		rs := vector.MustFunctionResult[types.Decimal128](result)
   552  		return signedToDecimal128(source, rs, length)
   553  	case types.T_char, types.T_varchar, types.T_blob, types.T_text:
   554  		// string type.
   555  		rs := vector.MustFunctionResult[types.Varlena](result)
   556  		return signedToStr(source, rs, length)
   557  	case types.T_time:
   558  		rs := vector.MustFunctionResult[types.Time](result)
   559  		return integerToTime(ctx, source, rs, length)
   560  	case types.T_timestamp:
   561  		rs := vector.MustFunctionResult[types.Timestamp](result)
   562  		return integerToTimestamp(source, rs, length)
   563  	}
   564  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from int16 to %s", toType))
   565  }
   566  
   567  func int32ToOthers(ctx context.Context,
   568  	source vector.FunctionParameterWrapper[int32],
   569  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
   570  	switch toType.Oid {
   571  	case types.T_bool:
   572  		rs := vector.MustFunctionResult[bool](result)
   573  		return numericToBool(source, rs, length)
   574  	case types.T_int8:
   575  		rs := vector.MustFunctionResult[int8](result)
   576  		return numericToNumeric(ctx, source, rs, length)
   577  	case types.T_int16:
   578  		rs := vector.MustFunctionResult[int16](result)
   579  		return numericToNumeric(ctx, source, rs, length)
   580  	case types.T_int32:
   581  		rs := vector.MustFunctionResult[int32](result)
   582  		rs.SetFromParameter(source)
   583  		return nil
   584  	case types.T_int64:
   585  		rs := vector.MustFunctionResult[int64](result)
   586  		return numericToNumeric(ctx, source, rs, length)
   587  	case types.T_uint8:
   588  		rs := vector.MustFunctionResult[uint8](result)
   589  		return numericToNumeric(ctx, source, rs, length)
   590  	case types.T_uint16:
   591  		rs := vector.MustFunctionResult[uint16](result)
   592  		return numericToNumeric(ctx, source, rs, length)
   593  	case types.T_uint32:
   594  		rs := vector.MustFunctionResult[uint32](result)
   595  		return numericToNumeric(ctx, source, rs, length)
   596  	case types.T_uint64:
   597  		rs := vector.MustFunctionResult[uint64](result)
   598  		return numericToNumeric(ctx, source, rs, length)
   599  	case types.T_float32:
   600  		rs := vector.MustFunctionResult[float32](result)
   601  		return numericToNumeric(ctx, source, rs, length)
   602  	case types.T_float64:
   603  		rs := vector.MustFunctionResult[float64](result)
   604  		return numericToNumeric(ctx, source, rs, length)
   605  	case types.T_decimal64:
   606  		rs := vector.MustFunctionResult[types.Decimal64](result)
   607  		return signedToDecimal64(source, rs, length)
   608  	case types.T_decimal128:
   609  		rs := vector.MustFunctionResult[types.Decimal128](result)
   610  		return signedToDecimal128(source, rs, length)
   611  	case types.T_char, types.T_varchar, types.T_blob, types.T_text:
   612  		// string type.
   613  		rs := vector.MustFunctionResult[types.Varlena](result)
   614  		return signedToStr(source, rs, length)
   615  	case types.T_time:
   616  		rs := vector.MustFunctionResult[types.Time](result)
   617  		return integerToTime(ctx, source, rs, length)
   618  	case types.T_timestamp:
   619  		rs := vector.MustFunctionResult[types.Timestamp](result)
   620  		return integerToTimestamp(source, rs, length)
   621  	}
   622  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from int32 to %s", toType))
   623  }
   624  
   625  func int64ToOthers(ctx context.Context,
   626  	source vector.FunctionParameterWrapper[int64],
   627  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
   628  	switch toType.Oid {
   629  	case types.T_bool:
   630  		rs := vector.MustFunctionResult[bool](result)
   631  		return numericToBool(source, rs, length)
   632  	case types.T_int8:
   633  		rs := vector.MustFunctionResult[int8](result)
   634  		return numericToNumeric(ctx, source, rs, length)
   635  	case types.T_int16:
   636  		rs := vector.MustFunctionResult[int16](result)
   637  		return numericToNumeric(ctx, source, rs, length)
   638  	case types.T_int32:
   639  		rs := vector.MustFunctionResult[int32](result)
   640  		return numericToNumeric(ctx, source, rs, length)
   641  	case types.T_int64:
   642  		rs := vector.MustFunctionResult[int64](result)
   643  		rs.SetFromParameter(source)
   644  		return nil
   645  	case types.T_uint8:
   646  		rs := vector.MustFunctionResult[uint8](result)
   647  		return numericToNumeric(ctx, source, rs, length)
   648  	case types.T_uint16:
   649  		rs := vector.MustFunctionResult[uint16](result)
   650  		return numericToNumeric(ctx, source, rs, length)
   651  	case types.T_uint32:
   652  		rs := vector.MustFunctionResult[uint32](result)
   653  		return numericToNumeric(ctx, source, rs, length)
   654  	case types.T_uint64:
   655  		rs := vector.MustFunctionResult[uint64](result)
   656  		return numericToNumeric(ctx, source, rs, length)
   657  	case types.T_float32:
   658  		rs := vector.MustFunctionResult[float32](result)
   659  		return numericToNumeric(ctx, source, rs, length)
   660  	case types.T_float64:
   661  		rs := vector.MustFunctionResult[float64](result)
   662  		return numericToNumeric(ctx, source, rs, length)
   663  	case types.T_decimal64:
   664  		rs := vector.MustFunctionResult[types.Decimal64](result)
   665  		return signedToDecimal64(source, rs, length)
   666  	case types.T_decimal128:
   667  		rs := vector.MustFunctionResult[types.Decimal128](result)
   668  		return signedToDecimal128(source, rs, length)
   669  	case types.T_char, types.T_varchar, types.T_blob, types.T_text:
   670  		// string type.
   671  		rs := vector.MustFunctionResult[types.Varlena](result)
   672  		return signedToStr(source, rs, length)
   673  	case types.T_time:
   674  		rs := vector.MustFunctionResult[types.Time](result)
   675  		return integerToTime(ctx, source, rs, length)
   676  	case types.T_timestamp:
   677  		rs := vector.MustFunctionResult[types.Timestamp](result)
   678  		return integerToTimestamp(source, rs, length)
   679  	}
   680  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from int64 to %s", toType))
   681  }
   682  
   683  func uint8ToOthers(ctx context.Context,
   684  	source vector.FunctionParameterWrapper[uint8],
   685  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
   686  	switch toType.Oid {
   687  	case types.T_bool:
   688  		rs := vector.MustFunctionResult[bool](result)
   689  		return numericToBool(source, rs, length)
   690  	case types.T_int8:
   691  		rs := vector.MustFunctionResult[int8](result)
   692  		return numericToNumeric(ctx, source, rs, length)
   693  	case types.T_int16:
   694  		rs := vector.MustFunctionResult[int16](result)
   695  		return numericToNumeric(ctx, source, rs, length)
   696  	case types.T_int32:
   697  		rs := vector.MustFunctionResult[int32](result)
   698  		return numericToNumeric(ctx, source, rs, length)
   699  	case types.T_int64:
   700  		rs := vector.MustFunctionResult[int64](result)
   701  		return numericToNumeric(ctx, source, rs, length)
   702  	case types.T_uint8:
   703  		rs := vector.MustFunctionResult[uint8](result)
   704  		rs.SetFromParameter(source)
   705  		return nil
   706  	case types.T_uint16:
   707  		rs := vector.MustFunctionResult[uint16](result)
   708  		return numericToNumeric(ctx, source, rs, length)
   709  	case types.T_uint32:
   710  		rs := vector.MustFunctionResult[uint32](result)
   711  		return numericToNumeric(ctx, source, rs, length)
   712  	case types.T_uint64:
   713  		rs := vector.MustFunctionResult[uint64](result)
   714  		return numericToNumeric(ctx, source, rs, length)
   715  	case types.T_float32:
   716  		rs := vector.MustFunctionResult[float32](result)
   717  		return numericToNumeric(ctx, source, rs, length)
   718  	case types.T_float64:
   719  		rs := vector.MustFunctionResult[float64](result)
   720  		return numericToNumeric(ctx, source, rs, length)
   721  	case types.T_decimal64:
   722  		rs := vector.MustFunctionResult[types.Decimal64](result)
   723  		return unsignedToDecimal64(source, rs, length)
   724  	case types.T_decimal128:
   725  		rs := vector.MustFunctionResult[types.Decimal128](result)
   726  		return unsignedToDecimal128(source, rs, length)
   727  	case types.T_char, types.T_varchar, types.T_blob, types.T_text:
   728  		rs := vector.MustFunctionResult[types.Varlena](result)
   729  		return unsignedToStr(source, rs, length)
   730  	case types.T_time:
   731  		rs := vector.MustFunctionResult[types.Time](result)
   732  		return integerToTime(ctx, source, rs, length)
   733  	case types.T_timestamp:
   734  		rs := vector.MustFunctionResult[types.Timestamp](result)
   735  		return integerToTimestamp(source, rs, length)
   736  	}
   737  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from uint8 to %s", toType))
   738  }
   739  
   740  func uint16ToOthers(ctx context.Context,
   741  	source vector.FunctionParameterWrapper[uint16],
   742  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
   743  	switch toType.Oid {
   744  	case types.T_bool:
   745  		rs := vector.MustFunctionResult[bool](result)
   746  		return numericToBool(source, rs, length)
   747  	case types.T_int8:
   748  		rs := vector.MustFunctionResult[int8](result)
   749  		return numericToNumeric(ctx, source, rs, length)
   750  	case types.T_int16:
   751  		rs := vector.MustFunctionResult[int16](result)
   752  		return numericToNumeric(ctx, source, rs, length)
   753  	case types.T_int32:
   754  		rs := vector.MustFunctionResult[int32](result)
   755  		return numericToNumeric(ctx, source, rs, length)
   756  	case types.T_int64:
   757  		rs := vector.MustFunctionResult[int64](result)
   758  		return numericToNumeric(ctx, source, rs, length)
   759  	case types.T_uint8:
   760  		rs := vector.MustFunctionResult[uint8](result)
   761  		return numericToNumeric(ctx, source, rs, length)
   762  	case types.T_uint16:
   763  		rs := vector.MustFunctionResult[uint16](result)
   764  		rs.SetFromParameter(source)
   765  		return nil
   766  	case types.T_uint32:
   767  		rs := vector.MustFunctionResult[uint32](result)
   768  		return numericToNumeric(ctx, source, rs, length)
   769  	case types.T_uint64:
   770  		rs := vector.MustFunctionResult[uint64](result)
   771  		return numericToNumeric(ctx, source, rs, length)
   772  	case types.T_float32:
   773  		rs := vector.MustFunctionResult[float32](result)
   774  		return numericToNumeric(ctx, source, rs, length)
   775  	case types.T_float64:
   776  		rs := vector.MustFunctionResult[float64](result)
   777  		return numericToNumeric(ctx, source, rs, length)
   778  	case types.T_decimal64:
   779  		rs := vector.MustFunctionResult[types.Decimal64](result)
   780  		return unsignedToDecimal64(source, rs, length)
   781  	case types.T_decimal128:
   782  		rs := vector.MustFunctionResult[types.Decimal128](result)
   783  		return unsignedToDecimal128(source, rs, length)
   784  	case types.T_char, types.T_varchar, types.T_blob, types.T_text:
   785  		rs := vector.MustFunctionResult[types.Varlena](result)
   786  		return unsignedToStr(source, rs, length)
   787  	case types.T_time:
   788  		rs := vector.MustFunctionResult[types.Time](result)
   789  		return integerToTime(ctx, source, rs, length)
   790  	case types.T_timestamp:
   791  		rs := vector.MustFunctionResult[types.Timestamp](result)
   792  		return integerToTimestamp(source, rs, length)
   793  	}
   794  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from uint16 to %s", toType))
   795  }
   796  
   797  func uint32ToOthers(ctx context.Context,
   798  	source vector.FunctionParameterWrapper[uint32],
   799  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
   800  	switch toType.Oid {
   801  	case types.T_bool:
   802  		rs := vector.MustFunctionResult[bool](result)
   803  		return numericToBool(source, rs, length)
   804  	case types.T_int8:
   805  		rs := vector.MustFunctionResult[int8](result)
   806  		return numericToNumeric(ctx, source, rs, length)
   807  	case types.T_int16:
   808  		rs := vector.MustFunctionResult[int16](result)
   809  		return numericToNumeric(ctx, source, rs, length)
   810  	case types.T_int32:
   811  		rs := vector.MustFunctionResult[int32](result)
   812  		return numericToNumeric(ctx, source, rs, length)
   813  	case types.T_int64:
   814  		rs := vector.MustFunctionResult[int64](result)
   815  		return numericToNumeric(ctx, source, rs, length)
   816  	case types.T_uint8:
   817  		rs := vector.MustFunctionResult[uint8](result)
   818  		return numericToNumeric(ctx, source, rs, length)
   819  	case types.T_uint16:
   820  		rs := vector.MustFunctionResult[uint16](result)
   821  		return numericToNumeric(ctx, source, rs, length)
   822  	case types.T_uint32:
   823  		rs := vector.MustFunctionResult[uint32](result)
   824  		rs.SetFromParameter(source)
   825  		return nil
   826  	case types.T_uint64:
   827  		rs := vector.MustFunctionResult[uint64](result)
   828  		return numericToNumeric(ctx, source, rs, length)
   829  	case types.T_float32:
   830  		rs := vector.MustFunctionResult[float32](result)
   831  		return numericToNumeric(ctx, source, rs, length)
   832  	case types.T_float64:
   833  		rs := vector.MustFunctionResult[float64](result)
   834  		return numericToNumeric(ctx, source, rs, length)
   835  	case types.T_decimal64:
   836  		rs := vector.MustFunctionResult[types.Decimal64](result)
   837  		return unsignedToDecimal64(source, rs, length)
   838  	case types.T_decimal128:
   839  		rs := vector.MustFunctionResult[types.Decimal128](result)
   840  		return unsignedToDecimal128(source, rs, length)
   841  	case types.T_char, types.T_varchar, types.T_blob, types.T_text:
   842  		rs := vector.MustFunctionResult[types.Varlena](result)
   843  		return unsignedToStr(source, rs, length)
   844  	case types.T_time:
   845  		rs := vector.MustFunctionResult[types.Time](result)
   846  		return integerToTime(ctx, source, rs, length)
   847  	case types.T_timestamp:
   848  		rs := vector.MustFunctionResult[types.Timestamp](result)
   849  		return integerToTimestamp(source, rs, length)
   850  	}
   851  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from uint32 to %s", toType))
   852  }
   853  
   854  func uint64ToOthers(ctx context.Context,
   855  	source vector.FunctionParameterWrapper[uint64],
   856  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
   857  	switch toType.Oid {
   858  	case types.T_bool:
   859  		rs := vector.MustFunctionResult[bool](result)
   860  		return numericToBool(source, rs, length)
   861  	case types.T_int8:
   862  		rs := vector.MustFunctionResult[int8](result)
   863  		return numericToNumeric(ctx, source, rs, length)
   864  	case types.T_int16:
   865  		rs := vector.MustFunctionResult[int16](result)
   866  		return numericToNumeric(ctx, source, rs, length)
   867  	case types.T_int32:
   868  		rs := vector.MustFunctionResult[int32](result)
   869  		return numericToNumeric(ctx, source, rs, length)
   870  	case types.T_int64:
   871  		rs := vector.MustFunctionResult[int64](result)
   872  		return numericToNumeric(ctx, source, rs, length)
   873  	case types.T_uint8:
   874  		rs := vector.MustFunctionResult[uint8](result)
   875  		return numericToNumeric(ctx, source, rs, length)
   876  	case types.T_uint16:
   877  		rs := vector.MustFunctionResult[uint16](result)
   878  		return numericToNumeric(ctx, source, rs, length)
   879  	case types.T_uint32:
   880  		rs := vector.MustFunctionResult[uint32](result)
   881  		return numericToNumeric(ctx, source, rs, length)
   882  	case types.T_uint64:
   883  		rs := vector.MustFunctionResult[uint64](result)
   884  		rs.SetFromParameter(source)
   885  		return nil
   886  	case types.T_float32:
   887  		rs := vector.MustFunctionResult[float32](result)
   888  		return numericToNumeric(ctx, source, rs, length)
   889  	case types.T_float64:
   890  		rs := vector.MustFunctionResult[float64](result)
   891  		return numericToNumeric(ctx, source, rs, length)
   892  	case types.T_decimal64:
   893  		rs := vector.MustFunctionResult[types.Decimal64](result)
   894  		return unsignedToDecimal64(source, rs, length)
   895  	case types.T_decimal128:
   896  		rs := vector.MustFunctionResult[types.Decimal128](result)
   897  		return unsignedToDecimal128(source, rs, length)
   898  	case types.T_char, types.T_varchar, types.T_blob, types.T_text:
   899  		rs := vector.MustFunctionResult[types.Varlena](result)
   900  		return unsignedToStr(source, rs, length)
   901  	case types.T_time:
   902  		rs := vector.MustFunctionResult[types.Time](result)
   903  		return integerToTime(ctx, source, rs, length)
   904  	case types.T_timestamp:
   905  		rs := vector.MustFunctionResult[types.Timestamp](result)
   906  		return integerToTimestamp(source, rs, length)
   907  	}
   908  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from uint64 to %s", toType))
   909  }
   910  
   911  func float32ToOthers(ctx context.Context,
   912  	source vector.FunctionParameterWrapper[float32],
   913  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
   914  	switch toType.Oid {
   915  	case types.T_bool:
   916  		rs := vector.MustFunctionResult[bool](result)
   917  		return numericToBool(source, rs, length)
   918  	case types.T_int8:
   919  		rs := vector.MustFunctionResult[int8](result)
   920  		return floatToInteger(ctx, source, rs, length)
   921  	case types.T_int16:
   922  		rs := vector.MustFunctionResult[int16](result)
   923  		return floatToInteger(ctx, source, rs, length)
   924  	case types.T_int32:
   925  		rs := vector.MustFunctionResult[int32](result)
   926  		return floatToInteger(ctx, source, rs, length)
   927  	case types.T_int64:
   928  		rs := vector.MustFunctionResult[int64](result)
   929  		return floatToInteger(ctx, source, rs, length)
   930  	case types.T_uint8:
   931  		rs := vector.MustFunctionResult[uint8](result)
   932  		return floatToInteger(ctx, source, rs, length)
   933  	case types.T_uint16:
   934  		rs := vector.MustFunctionResult[uint16](result)
   935  		return floatToInteger(ctx, source, rs, length)
   936  	case types.T_uint32:
   937  		rs := vector.MustFunctionResult[uint32](result)
   938  		return floatToInteger(ctx, source, rs, length)
   939  	case types.T_uint64:
   940  		rs := vector.MustFunctionResult[uint64](result)
   941  		return floatToInteger(ctx, source, rs, length)
   942  	case types.T_float32:
   943  		rs := vector.MustFunctionResult[float32](result)
   944  		rs.SetFromParameter(source)
   945  		return nil
   946  	case types.T_float64:
   947  		rs := vector.MustFunctionResult[float64](result)
   948  		return numericToNumeric(ctx, source, rs, length)
   949  	case types.T_decimal64:
   950  		rs := vector.MustFunctionResult[types.Decimal64](result)
   951  		return floatToDecimal64(source, rs, length)
   952  	case types.T_decimal128:
   953  		rs := vector.MustFunctionResult[types.Decimal128](result)
   954  		return floatToDecimal128(source, rs, length)
   955  	case types.T_char, types.T_varchar, types.T_blob, types.T_text:
   956  		rs := vector.MustFunctionResult[types.Varlena](result)
   957  		return floatToStr(source, rs, length)
   958  	}
   959  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from float32 to %s", toType))
   960  }
   961  
   962  func float64ToOthers(ctx context.Context,
   963  	source vector.FunctionParameterWrapper[float64],
   964  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
   965  	switch toType.Oid {
   966  	case types.T_bool:
   967  		rs := vector.MustFunctionResult[bool](result)
   968  		return numericToBool(source, rs, length)
   969  	case types.T_int8:
   970  		rs := vector.MustFunctionResult[int8](result)
   971  		return floatToInteger(ctx, source, rs, length)
   972  	case types.T_int16:
   973  		rs := vector.MustFunctionResult[int16](result)
   974  		return floatToInteger(ctx, source, rs, length)
   975  	case types.T_int32:
   976  		rs := vector.MustFunctionResult[int32](result)
   977  		return floatToInteger(ctx, source, rs, length)
   978  	case types.T_int64:
   979  		rs := vector.MustFunctionResult[int64](result)
   980  		return floatToInteger(ctx, source, rs, length)
   981  	case types.T_uint8:
   982  		rs := vector.MustFunctionResult[uint8](result)
   983  		return floatToInteger(ctx, source, rs, length)
   984  	case types.T_uint16:
   985  		rs := vector.MustFunctionResult[uint16](result)
   986  		return floatToInteger(ctx, source, rs, length)
   987  	case types.T_uint32:
   988  		rs := vector.MustFunctionResult[uint32](result)
   989  		return floatToInteger(ctx, source, rs, length)
   990  	case types.T_uint64:
   991  		rs := vector.MustFunctionResult[uint64](result)
   992  		return floatToInteger(ctx, source, rs, length)
   993  	case types.T_float32:
   994  		rs := vector.MustFunctionResult[float32](result)
   995  		return numericToNumeric(ctx, source, rs, length)
   996  	case types.T_float64:
   997  		rs := vector.MustFunctionResult[float64](result)
   998  		rs.SetFromParameter(source)
   999  		return nil
  1000  	case types.T_decimal64:
  1001  		rs := vector.MustFunctionResult[types.Decimal64](result)
  1002  		return floatToDecimal64(source, rs, length)
  1003  	case types.T_decimal128:
  1004  		rs := vector.MustFunctionResult[types.Decimal128](result)
  1005  		return floatToDecimal128(source, rs, length)
  1006  	case types.T_char, types.T_varchar, types.T_blob, types.T_text:
  1007  		rs := vector.MustFunctionResult[types.Varlena](result)
  1008  		return floatToStr(source, rs, length)
  1009  	}
  1010  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from float64 to %s", toType))
  1011  }
  1012  
  1013  func dateToOthers(proc *process.Process,
  1014  	source vector.FunctionParameterWrapper[types.Date],
  1015  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1016  	switch toType.Oid {
  1017  	case types.T_int32:
  1018  		rs := vector.MustFunctionResult[int32](result)
  1019  		return dateToSigned(source, rs, length)
  1020  	case types.T_int64:
  1021  		rs := vector.MustFunctionResult[int64](result)
  1022  		return dateToSigned(source, rs, length)
  1023  	case types.T_date:
  1024  		rs := vector.MustFunctionResult[types.Date](result)
  1025  		rs.SetFromParameter(source)
  1026  		return nil
  1027  	case types.T_time:
  1028  		rs := vector.MustFunctionResult[types.Time](result)
  1029  		return dateToTime(source, rs, length)
  1030  	case types.T_timestamp:
  1031  		zone := time.Local
  1032  		if proc != nil {
  1033  			zone = proc.SessionInfo.TimeZone
  1034  		}
  1035  		rs := vector.MustFunctionResult[types.Timestamp](result)
  1036  		return dateToTimestamp(source, rs, length, zone)
  1037  	case types.T_datetime:
  1038  		rs := vector.MustFunctionResult[types.Datetime](result)
  1039  		return dateToDatetime(source, rs, length)
  1040  	case types.T_char, types.T_varchar, types.T_blob, types.T_text:
  1041  		rs := vector.MustFunctionResult[types.Varlena](result)
  1042  		return dateToStr(source, rs, length)
  1043  	}
  1044  	return moerr.NewInternalError(proc.Ctx, fmt.Sprintf("unsupported cast from date to %s", toType))
  1045  }
  1046  
  1047  func datetimeToOthers(proc *process.Process,
  1048  	source vector.FunctionParameterWrapper[types.Datetime],
  1049  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1050  	switch toType.Oid {
  1051  	case types.T_int32:
  1052  		rs := vector.MustFunctionResult[int32](result)
  1053  		return datetimeToInt32(proc.Ctx, source, rs, length)
  1054  	case types.T_int64:
  1055  		rs := vector.MustFunctionResult[int64](result)
  1056  		return datetimeToInt64(source, rs, length)
  1057  	case types.T_timestamp:
  1058  		zone := time.Local
  1059  		if proc != nil {
  1060  			zone = proc.SessionInfo.TimeZone
  1061  		}
  1062  		rs := vector.MustFunctionResult[types.Timestamp](result)
  1063  		return datetimeToTimestamp(source, rs, length, zone)
  1064  	case types.T_date:
  1065  		rs := vector.MustFunctionResult[types.Date](result)
  1066  		return datetimeToDate(source, rs, length)
  1067  	case types.T_datetime:
  1068  		rs := vector.MustFunctionResult[types.Datetime](result)
  1069  		v := source.GetSourceVector()
  1070  		v.Typ = toType
  1071  		rs.SetFromParameter(source)
  1072  		return nil
  1073  	case types.T_time:
  1074  		rs := vector.MustFunctionResult[types.Time](result)
  1075  		return datetimeToTime(source, rs, length)
  1076  	case types.T_char, types.T_varchar, types.T_blob, types.T_text:
  1077  		rs := vector.MustFunctionResult[types.Varlena](result)
  1078  		return datetimeToStr(source, rs, length)
  1079  	}
  1080  	return moerr.NewInternalError(proc.Ctx, fmt.Sprintf("unsupported cast from datetime to %s", toType))
  1081  }
  1082  
  1083  func timestampToOthers(proc *process.Process,
  1084  	source vector.FunctionParameterWrapper[types.Timestamp],
  1085  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1086  	zone := time.Local
  1087  	if proc != nil {
  1088  		zone = proc.SessionInfo.TimeZone
  1089  	}
  1090  
  1091  	switch toType.Oid {
  1092  	case types.T_int32:
  1093  		rs := vector.MustFunctionResult[int32](result)
  1094  		return timestampToInt32(proc.Ctx, source, rs, length)
  1095  	case types.T_int64:
  1096  		rs := vector.MustFunctionResult[int64](result)
  1097  		return timestampToInt64(source, rs, length)
  1098  	case types.T_date:
  1099  		rs := vector.MustFunctionResult[types.Date](result)
  1100  		return timestampToDate(proc.Ctx, source, rs, length, zone)
  1101  	case types.T_datetime:
  1102  		rs := vector.MustFunctionResult[types.Datetime](result)
  1103  		return timestampToDatetime(proc.Ctx, source, rs, length, zone)
  1104  	case types.T_timestamp:
  1105  		rs := vector.MustFunctionResult[types.Timestamp](result)
  1106  		v := source.GetSourceVector()
  1107  		v.Typ = toType
  1108  		rs.SetFromParameter(source)
  1109  		return nil
  1110  	case types.T_char, types.T_varchar, types.T_blob, types.T_text:
  1111  		rs := vector.MustFunctionResult[types.Varlena](result)
  1112  		return timestampToStr(source, rs, length, zone)
  1113  	}
  1114  	return moerr.NewInternalError(proc.Ctx, fmt.Sprintf("unsupported cast from timestamp to %s", toType))
  1115  }
  1116  
  1117  func timeToOthers(ctx context.Context,
  1118  	source vector.FunctionParameterWrapper[types.Time],
  1119  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1120  	switch toType.Oid {
  1121  	case types.T_int8:
  1122  		rs := vector.MustFunctionResult[int8](result)
  1123  		return timeToInteger(ctx, source, rs, length)
  1124  	case types.T_int16:
  1125  		rs := vector.MustFunctionResult[int16](result)
  1126  		return timeToInteger(ctx, source, rs, length)
  1127  	case types.T_int32:
  1128  		rs := vector.MustFunctionResult[int32](result)
  1129  		return timeToInteger(ctx, source, rs, length)
  1130  	case types.T_int64:
  1131  		rs := vector.MustFunctionResult[int64](result)
  1132  		return timeToInteger(ctx, source, rs, length)
  1133  	case types.T_uint8:
  1134  		rs := vector.MustFunctionResult[uint8](result)
  1135  		return timeToInteger(ctx, source, rs, length)
  1136  	case types.T_uint16:
  1137  		rs := vector.MustFunctionResult[uint16](result)
  1138  		return timeToInteger(ctx, source, rs, length)
  1139  	case types.T_uint32:
  1140  		rs := vector.MustFunctionResult[uint32](result)
  1141  		return timeToInteger(ctx, source, rs, length)
  1142  	case types.T_uint64:
  1143  		rs := vector.MustFunctionResult[uint64](result)
  1144  		return timeToInteger(ctx, source, rs, length)
  1145  	case types.T_date:
  1146  		rs := vector.MustFunctionResult[types.Date](result)
  1147  		return timeToDate(source, rs, length)
  1148  	case types.T_datetime:
  1149  		rs := vector.MustFunctionResult[types.Datetime](result)
  1150  		return timeToDatetime(source, rs, length)
  1151  	case types.T_time:
  1152  		rs := vector.MustFunctionResult[types.Time](result)
  1153  		v := source.GetSourceVector()
  1154  		v.Typ = toType
  1155  		rs.SetFromParameter(source)
  1156  		return nil
  1157  	case types.T_char, types.T_varchar, types.T_blob, types.T_text:
  1158  		rs := vector.MustFunctionResult[types.Varlena](result)
  1159  		return timeToStr(source, rs, length)
  1160  	case types.T_decimal64:
  1161  		rs := vector.MustFunctionResult[types.Decimal64](result)
  1162  		return timeToDecimal64(ctx, source, rs, length)
  1163  	case types.T_decimal128:
  1164  		rs := vector.MustFunctionResult[types.Decimal128](result)
  1165  		return timeToDecimal128(ctx, source, rs, length)
  1166  	}
  1167  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from time to %s", toType))
  1168  }
  1169  
  1170  func decimal64ToOthers(ctx context.Context,
  1171  	source vector.FunctionParameterWrapper[types.Decimal64],
  1172  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1173  	switch toType.Oid {
  1174  	case types.T_float32:
  1175  		rs := vector.MustFunctionResult[float32](result)
  1176  		return decimal64ToFloat(ctx, source, rs, length, 32)
  1177  	case types.T_float64:
  1178  		rs := vector.MustFunctionResult[float64](result)
  1179  		return decimal64ToFloat(ctx, source, rs, length, 64)
  1180  	case types.T_int64:
  1181  		rs := vector.MustFunctionResult[int64](result)
  1182  		return decimal64ToInt64(ctx, source, rs, length)
  1183  	case types.T_uint64:
  1184  		rs := vector.MustFunctionResult[uint64](result)
  1185  		return decimal64ToUnsigned(ctx, source, rs, 64, length)
  1186  	case types.T_decimal64:
  1187  		rs := vector.MustFunctionResult[types.Decimal64](result)
  1188  		v := source.GetSourceVector()
  1189  		v.Typ = toType
  1190  		rs.SetFromParameter(source)
  1191  		return nil
  1192  	case types.T_decimal128:
  1193  		rs := vector.MustFunctionResult[types.Decimal128](result)
  1194  		return decimal64ToDecimal128(source, rs, length)
  1195  	case types.T_timestamp:
  1196  		rs := vector.MustFunctionResult[types.Timestamp](result)
  1197  		return decimal64ToTimestamp(source, rs, length)
  1198  	case types.T_time:
  1199  		rs := vector.MustFunctionResult[types.Time](result)
  1200  		return decimal64ToTime(source, rs, length)
  1201  	case types.T_char, types.T_varchar, types.T_blob, types.T_text:
  1202  		rs := vector.MustFunctionResult[types.Varlena](result)
  1203  		return decimal64ToStr(source, rs, length)
  1204  	}
  1205  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from decimal64 to %s", toType))
  1206  }
  1207  
  1208  func decimal128ToOthers(ctx context.Context,
  1209  	source vector.FunctionParameterWrapper[types.Decimal128],
  1210  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1211  	switch toType.Oid {
  1212  	case types.T_int32:
  1213  		rs := vector.MustFunctionResult[int32](result)
  1214  		return decimal128ToSigned(ctx, source, rs, 32, length)
  1215  	case types.T_int64:
  1216  		rs := vector.MustFunctionResult[int64](result)
  1217  		return decimal128ToSigned(ctx, source, rs, 64, length)
  1218  	case types.T_uint64:
  1219  		rs := vector.MustFunctionResult[uint64](result)
  1220  		return decimal128ToUnsigned(ctx, source, rs, 64, length)
  1221  	case types.T_decimal64:
  1222  		rs := vector.MustFunctionResult[types.Decimal64](result)
  1223  		return decimal128ToDecimal64(ctx, source, rs, length)
  1224  	case types.T_decimal128:
  1225  		rs := vector.MustFunctionResult[types.Decimal128](result)
  1226  		v := source.GetSourceVector()
  1227  		v.Typ = toType
  1228  		rs.SetFromParameter(source)
  1229  		return nil
  1230  	case types.T_float32:
  1231  		rs := vector.MustFunctionResult[float32](result)
  1232  		return decimal128ToFloat(ctx, source, rs, length, 32)
  1233  	case types.T_float64:
  1234  		rs := vector.MustFunctionResult[float64](result)
  1235  		return decimal128ToFloat(ctx, source, rs, length, 64)
  1236  	case types.T_time:
  1237  		rs := vector.MustFunctionResult[types.Time](result)
  1238  		return decimal128ToTime(source, rs, length)
  1239  	case types.T_timestamp:
  1240  		rs := vector.MustFunctionResult[types.Timestamp](result)
  1241  		return decimal128ToTimestamp(source, rs, length)
  1242  	case types.T_char, types.T_varchar, types.T_blob, types.T_text:
  1243  		rs := vector.MustFunctionResult[types.Varlena](result)
  1244  		return decimal128ToStr(source, rs, length)
  1245  	}
  1246  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from decimal128 to %s", toType))
  1247  }
  1248  
  1249  func strTypeToOthers(proc *process.Process,
  1250  	source vector.FunctionParameterWrapper[types.Varlena],
  1251  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1252  	ctx := proc.Ctx
  1253  	switch toType.Oid {
  1254  	case types.T_int8:
  1255  		rs := vector.MustFunctionResult[int8](result)
  1256  		return strToSigned(ctx, source, rs, 8, length)
  1257  	case types.T_int16:
  1258  		rs := vector.MustFunctionResult[int16](result)
  1259  		return strToSigned(ctx, source, rs, 16, length)
  1260  	case types.T_int32:
  1261  		rs := vector.MustFunctionResult[int32](result)
  1262  		return strToSigned(ctx, source, rs, 32, length)
  1263  	case types.T_int64:
  1264  		rs := vector.MustFunctionResult[int64](result)
  1265  		return strToSigned(ctx, source, rs, 64, length)
  1266  	case types.T_uint8:
  1267  		rs := vector.MustFunctionResult[uint8](result)
  1268  		return strToUnsigned(ctx, source, rs, 8, length)
  1269  	case types.T_uint16:
  1270  		rs := vector.MustFunctionResult[uint16](result)
  1271  		return strToUnsigned(ctx, source, rs, 16, length)
  1272  	case types.T_uint32:
  1273  		rs := vector.MustFunctionResult[uint32](result)
  1274  		return strToUnsigned(ctx, source, rs, 32, length)
  1275  	case types.T_uint64:
  1276  		rs := vector.MustFunctionResult[uint64](result)
  1277  		return strToUnsigned(ctx, source, rs, 64, length)
  1278  	case types.T_float32:
  1279  		rs := vector.MustFunctionResult[float32](result)
  1280  		return strToFloat(ctx, source, rs, 32, length)
  1281  	case types.T_float64:
  1282  		rs := vector.MustFunctionResult[float64](result)
  1283  		return strToFloat(ctx, source, rs, 64, length)
  1284  	case types.T_decimal64:
  1285  		rs := vector.MustFunctionResult[types.Decimal64](result)
  1286  		return strToDecimal64(source, rs, length)
  1287  	case types.T_decimal128:
  1288  		rs := vector.MustFunctionResult[types.Decimal128](result)
  1289  		return strToDecimal128(source, rs, length)
  1290  	case types.T_bool:
  1291  		rs := vector.MustFunctionResult[bool](result)
  1292  		return strToBool(source, rs, length)
  1293  	case types.T_json:
  1294  		rs := vector.MustFunctionResult[types.Varlena](result)
  1295  		return strToJson(source, rs, length)
  1296  	case types.T_uuid:
  1297  		rs := vector.MustFunctionResult[types.Uuid](result)
  1298  		return strToUuid(source, rs, length)
  1299  	case types.T_date:
  1300  		rs := vector.MustFunctionResult[types.Date](result)
  1301  		return strToDate(source, rs, length)
  1302  	case types.T_datetime:
  1303  		rs := vector.MustFunctionResult[types.Datetime](result)
  1304  		return strToDatetime(source, rs, length)
  1305  	case types.T_time:
  1306  		rs := vector.MustFunctionResult[types.Time](result)
  1307  		return strToTime(source, rs, length)
  1308  	case types.T_timestamp:
  1309  		rs := vector.MustFunctionResult[types.Timestamp](result)
  1310  		zone := time.Local
  1311  		if proc != nil {
  1312  			zone = proc.SessionInfo.TimeZone
  1313  		}
  1314  		return strToTimestamp(source, rs, zone, length)
  1315  	case types.T_char, types.T_varchar, types.T_text, types.T_blob:
  1316  		rs := vector.MustFunctionResult[types.Varlena](result)
  1317  		return strToStr(proc.Ctx, source, rs, length)
  1318  	}
  1319  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from %s to %s", source.GetType(), toType))
  1320  }
  1321  
  1322  func uuidToOthers(ctx context.Context,
  1323  	source vector.FunctionParameterWrapper[types.Uuid],
  1324  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1325  	switch toType.Oid {
  1326  	case types.T_char, types.T_varchar, types.T_blob, types.T_text:
  1327  		rs := vector.MustFunctionResult[types.Varlena](result)
  1328  		return uuidToStr(source, rs, length)
  1329  	}
  1330  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from uuid to %s", toType))
  1331  }
  1332  
  1333  func tsToOthers(ctx context.Context,
  1334  	source vector.FunctionParameterWrapper[types.TS],
  1335  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1336  	if toType.Oid == types.T_TS {
  1337  		rs := vector.MustFunctionResult[types.TS](result)
  1338  		rs.SetFromParameter(source)
  1339  		return nil
  1340  	}
  1341  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from ts to %s", toType))
  1342  }
  1343  
  1344  func rowidToOthers(ctx context.Context,
  1345  	source vector.FunctionParameterWrapper[types.Rowid],
  1346  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1347  	if toType.Oid == types.T_Rowid {
  1348  		rs := vector.MustFunctionResult[types.Rowid](result)
  1349  		rs.SetFromParameter(source)
  1350  		return nil
  1351  	}
  1352  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from rowid to %s", toType))
  1353  }
  1354  
  1355  func jsonToOthers(ctx context.Context,
  1356  	source vector.FunctionParameterWrapper[types.Varlena],
  1357  	toType types.Type, result vector.FunctionResultWrapper, length int) error {
  1358  	switch toType.Oid {
  1359  	case types.T_char, types.T_varchar, types.T_text:
  1360  		rs := vector.MustFunctionResult[types.Varlena](result)
  1361  		return jsonToStr(source, rs, length)
  1362  	}
  1363  	return moerr.NewInternalError(ctx, fmt.Sprintf("unsupported cast from json to %s", toType))
  1364  }
  1365  
  1366  // XXX do not use it to cast float to integer, please use floatToInteger
  1367  func numericToNumeric[T1, T2 constraints.Integer | constraints.Float](
  1368  	ctx context.Context,
  1369  	from vector.FunctionParameterWrapper[T1], to *vector.FunctionResult[T2], length int) error {
  1370  	var i uint64
  1371  	var dftValue T2
  1372  	times := uint64(length)
  1373  	if err := overflowForNumericToNumeric[T1, T2](ctx, from.UnSafeGetAllValue()); err != nil {
  1374  		return err
  1375  	}
  1376  	for i = 0; i < times; i++ {
  1377  		v, isnull := from.GetValue(i)
  1378  		if isnull {
  1379  			if err := to.Append(dftValue, true); err != nil {
  1380  				return err
  1381  			}
  1382  		} else {
  1383  			if err := to.Append(T2(v), false); err != nil {
  1384  				return err
  1385  			}
  1386  		}
  1387  	}
  1388  	return nil
  1389  }
  1390  
  1391  // XXX do not use it to cast float to integer, please use floatToInteger
  1392  func floatToInteger[T1 constraints.Float, T2 constraints.Integer](
  1393  	ctx context.Context,
  1394  	from vector.FunctionParameterWrapper[T1], to *vector.FunctionResult[T2],
  1395  	length int) error {
  1396  	var i uint64
  1397  	var dftValue T2
  1398  	times := uint64(length)
  1399  	if err := overflowForNumericToNumeric[T1, T2](ctx, from.UnSafeGetAllValue()); err != nil {
  1400  		return err
  1401  	}
  1402  	for i = 0; i < times; i++ {
  1403  		v, isnull := from.GetValue(i)
  1404  		if isnull {
  1405  			if err := to.Append(dftValue, true); err != nil {
  1406  				return err
  1407  			}
  1408  		} else {
  1409  			if err := to.Append(T2(math.Round(float64(v))), false); err != nil {
  1410  				return err
  1411  			}
  1412  		}
  1413  	}
  1414  	return nil
  1415  }
  1416  
  1417  func numericToBool[T constraints.Integer | constraints.Float](
  1418  	from vector.FunctionParameterWrapper[T],
  1419  	to *vector.FunctionResult[bool], length int) error {
  1420  	var i uint64
  1421  	l := uint64(length)
  1422  	for i = 0; i < l; i++ {
  1423  		v, null := from.GetValue(i)
  1424  		err := to.Append(v != 0, null)
  1425  		if err != nil {
  1426  			return err
  1427  		}
  1428  	}
  1429  	return nil
  1430  }
  1431  
  1432  func boolToStr(
  1433  	from vector.FunctionParameterWrapper[bool],
  1434  	to *vector.FunctionResult[types.Varlena], length int) error {
  1435  	var i uint64
  1436  	l := uint64(length)
  1437  	for i = 0; i < l; i++ {
  1438  		v, null := from.GetValue(i)
  1439  		if null {
  1440  			if err := to.AppendStr(nil, true); err != nil {
  1441  				return err
  1442  			}
  1443  		} else {
  1444  			if v {
  1445  				if err := to.AppendStr([]byte("1"), false); err != nil {
  1446  					return err
  1447  				}
  1448  			} else {
  1449  				if err := to.AppendStr([]byte("0"), false); err != nil {
  1450  					return err
  1451  				}
  1452  			}
  1453  		}
  1454  	}
  1455  	return nil
  1456  }
  1457  
  1458  func boolToInteger[T constraints.Integer](
  1459  	from vector.FunctionParameterWrapper[bool],
  1460  	to *vector.FunctionResult[T], length int) error {
  1461  	var i uint64
  1462  	l := uint64(length)
  1463  	var dft T
  1464  	for i = 0; i < l; i++ {
  1465  		v, null := from.GetValue(i)
  1466  		if null {
  1467  			if err := to.Append(dft, true); err != nil {
  1468  				return err
  1469  			}
  1470  		} else {
  1471  			if v {
  1472  				if err := to.Append(1, false); err != nil {
  1473  					return err
  1474  				}
  1475  			} else {
  1476  				if err := to.Append(0, false); err != nil {
  1477  					return err
  1478  				}
  1479  			}
  1480  		}
  1481  	}
  1482  	return nil
  1483  }
  1484  
  1485  func signedToDecimal64[T1 constraints.Signed](
  1486  	from vector.FunctionParameterWrapper[T1],
  1487  	to *vector.FunctionResult[types.Decimal64], length int) error {
  1488  	var i uint64
  1489  	l := uint64(length)
  1490  	var dft types.Decimal64
  1491  	toType := to.GetType()
  1492  
  1493  	for i = 0; i < l; i++ {
  1494  		v, null := from.GetValue(i)
  1495  		if null {
  1496  			if err := to.Append(dft, true); err != nil {
  1497  				return err
  1498  			}
  1499  		} else {
  1500  			result, err := types.Decimal64_FromInt64(int64(v), toType.Width, toType.Scale)
  1501  			if err != nil {
  1502  				return err
  1503  			}
  1504  			if err = to.Append(result, false); err != nil {
  1505  				return err
  1506  			}
  1507  		}
  1508  	}
  1509  	return nil
  1510  }
  1511  
  1512  func signedToDecimal128[T1 constraints.Signed](
  1513  	from vector.FunctionParameterWrapper[T1],
  1514  	to *vector.FunctionResult[types.Decimal128], length int) error {
  1515  	var i uint64
  1516  	l := uint64(length)
  1517  	var dft types.Decimal128
  1518  	toType := to.GetType()
  1519  
  1520  	for i = 0; i < l; i++ {
  1521  		v, null := from.GetValue(i)
  1522  		if null {
  1523  			if err := to.Append(dft, true); err != nil {
  1524  				return err
  1525  			}
  1526  		} else {
  1527  			result, err := types.Decimal128_FromInt64(int64(v), toType.Width, toType.Scale)
  1528  			if err != nil {
  1529  				return err
  1530  			}
  1531  			if err = to.Append(result, false); err != nil {
  1532  				return err
  1533  			}
  1534  		}
  1535  	}
  1536  	return nil
  1537  }
  1538  
  1539  func unsignedToDecimal64[T1 constraints.Unsigned](
  1540  	from vector.FunctionParameterWrapper[T1],
  1541  	to *vector.FunctionResult[types.Decimal64], length int) error {
  1542  	var i uint64
  1543  	l := uint64(length)
  1544  	var dft types.Decimal64
  1545  	toType := to.GetType()
  1546  
  1547  	for i = 0; i < l; i++ {
  1548  		v, null := from.GetValue(i)
  1549  		if null {
  1550  			if err := to.Append(dft, true); err != nil {
  1551  				return err
  1552  			}
  1553  		} else {
  1554  			result, err := types.Decimal64_FromUint64(uint64(v), toType.Width, toType.Scale)
  1555  			if err != nil {
  1556  				return err
  1557  			}
  1558  			if err = to.Append(result, false); err != nil {
  1559  				return err
  1560  			}
  1561  		}
  1562  	}
  1563  	return nil
  1564  }
  1565  
  1566  func unsignedToDecimal128[T1 constraints.Unsigned](
  1567  	from vector.FunctionParameterWrapper[T1],
  1568  	to *vector.FunctionResult[types.Decimal128], length int) error {
  1569  	var i uint64
  1570  	l := uint64(length)
  1571  	var dft types.Decimal128
  1572  	toType := to.GetType()
  1573  
  1574  	for i = 0; i < l; i++ {
  1575  		v, null := from.GetValue(i)
  1576  		if null {
  1577  			if err := to.Append(dft, true); err != nil {
  1578  				return err
  1579  			}
  1580  		} else {
  1581  			result, err := types.Decimal128_FromUint64(uint64(v), toType.Width, toType.Scale)
  1582  			if err != nil {
  1583  				return err
  1584  			}
  1585  			if err = to.Append(result, false); err != nil {
  1586  				return err
  1587  			}
  1588  		}
  1589  	}
  1590  	return nil
  1591  }
  1592  
  1593  func floatToDecimal64[T constraints.Float](
  1594  	from vector.FunctionParameterWrapper[T],
  1595  	to *vector.FunctionResult[types.Decimal64], length int) error {
  1596  	var i uint64
  1597  	l := uint64(length)
  1598  	var dft types.Decimal64
  1599  	toType := to.GetType()
  1600  
  1601  	for i = 0; i < l; i++ {
  1602  		v, null := from.GetValue(i)
  1603  		if null {
  1604  			if err := to.Append(dft, true); err != nil {
  1605  				return err
  1606  			}
  1607  		} else {
  1608  			result64, err := types.Decimal64_FromFloat64(float64(v), toType.Width, toType.Scale)
  1609  			if err != nil {
  1610  				return err
  1611  			}
  1612  			if err = to.Append(result64, false); err != nil {
  1613  				return err
  1614  			}
  1615  		}
  1616  	}
  1617  	return nil
  1618  }
  1619  
  1620  func floatToDecimal128[T constraints.Float](
  1621  	from vector.FunctionParameterWrapper[T],
  1622  	to *vector.FunctionResult[types.Decimal128], length int) error {
  1623  	var i uint64
  1624  	l := uint64(length)
  1625  	var dft types.Decimal128
  1626  	toType := to.GetType()
  1627  
  1628  	for i = 0; i < l; i++ {
  1629  		v, null := from.GetValue(i)
  1630  		if null {
  1631  			if err := to.Append(dft, true); err != nil {
  1632  				return err
  1633  			}
  1634  		} else {
  1635  			result128, err := types.Decimal128_FromFloat64(float64(v), toType.Width, toType.Scale)
  1636  			if err != nil {
  1637  				return err
  1638  			}
  1639  			if err = to.Append(result128, false); err != nil {
  1640  				return err
  1641  			}
  1642  		}
  1643  	}
  1644  	return nil
  1645  }
  1646  
  1647  func signedToStr[T constraints.Integer](
  1648  	from vector.FunctionParameterWrapper[T],
  1649  	to *vector.FunctionResult[types.Varlena], length int) error {
  1650  	var i uint64
  1651  	l := uint64(length)
  1652  	for i = 0; i < l; i++ {
  1653  		v, null := from.GetValue(i)
  1654  		if null {
  1655  			if err := to.AppendStr(nil, true); err != nil {
  1656  				return err
  1657  			}
  1658  		} else {
  1659  			result := []byte(strconv.FormatInt(int64(v), 10))
  1660  			if err := to.AppendStr(result, false); err != nil {
  1661  				return err
  1662  			}
  1663  		}
  1664  	}
  1665  	return nil
  1666  }
  1667  
  1668  func unsignedToStr[T constraints.Unsigned](
  1669  	from vector.FunctionParameterWrapper[T],
  1670  	to *vector.FunctionResult[types.Varlena], length int) error {
  1671  	var i uint64
  1672  	l := uint64(length)
  1673  	for i = 0; i < l; i++ {
  1674  		v, null := from.GetValue(i)
  1675  		if null {
  1676  			if err := to.AppendStr(nil, true); err != nil {
  1677  				return err
  1678  			}
  1679  		} else {
  1680  			result := []byte(strconv.FormatUint(uint64(v), 10))
  1681  			if err := to.AppendStr(result, false); err != nil {
  1682  				return err
  1683  			}
  1684  		}
  1685  	}
  1686  	return nil
  1687  }
  1688  
  1689  func floatToStr[T constraints.Float](
  1690  	from vector.FunctionParameterWrapper[T],
  1691  	to *vector.FunctionResult[types.Varlena], length int) error {
  1692  	var i uint64
  1693  	l := uint64(length)
  1694  	bitSize := int(unsafe.Sizeof(T(0)) * 8)
  1695  	for i = 0; i < l; i++ {
  1696  		v, null := from.GetValue(i)
  1697  		if null {
  1698  			if err := to.AppendStr(nil, true); err != nil {
  1699  				return err
  1700  			}
  1701  		} else {
  1702  			result := []byte(strconv.FormatFloat(float64(v), 'G', -1, bitSize))
  1703  			if err := to.AppendStr(result, false); err != nil {
  1704  				return err
  1705  			}
  1706  		}
  1707  	}
  1708  	return nil
  1709  }
  1710  
  1711  func integerToTimestamp[T constraints.Integer](
  1712  	from vector.FunctionParameterWrapper[T],
  1713  	to *vector.FunctionResult[types.Timestamp], length int) error {
  1714  	var i uint64
  1715  	l := uint64(length)
  1716  	var dft types.Timestamp
  1717  	// XXX what is the 32536771199
  1718  	for i = 0; i < l; i++ {
  1719  		v, null := from.GetValue(i)
  1720  		if null || v < 0 || uint64(v) > 32536771199 {
  1721  			if err := to.Append(dft, true); err != nil {
  1722  				return err
  1723  			}
  1724  		} else {
  1725  			result := types.UnixToTimestamp(int64(v))
  1726  			if err := to.Append(result, false); err != nil {
  1727  				return err
  1728  			}
  1729  		}
  1730  	}
  1731  	return nil
  1732  }
  1733  
  1734  func integerToTime[T constraints.Integer](
  1735  	ctx context.Context,
  1736  	from vector.FunctionParameterWrapper[T],
  1737  	to *vector.FunctionResult[types.Time], length int) error {
  1738  	var i uint64
  1739  	l := uint64(length)
  1740  	var dft types.Time
  1741  	toType := to.GetType()
  1742  	for i = 0; i < l; i++ {
  1743  		v, null := from.GetValue(i)
  1744  		vI64 := int64(v)
  1745  		if null {
  1746  			if err := to.Append(dft, true); err != nil {
  1747  				return err
  1748  			}
  1749  		} else {
  1750  			if vI64 < types.MinInputIntTime || vI64 > types.MaxInputIntTime {
  1751  				return moerr.NewOutOfRange(ctx, "time", "value %d", v)
  1752  			}
  1753  			result, err := types.ParseInt64ToTime(vI64, toType.Precision)
  1754  			if err != nil {
  1755  				return err
  1756  			}
  1757  			if err = to.Append(result, false); err != nil {
  1758  				return err
  1759  			}
  1760  		}
  1761  	}
  1762  	return nil
  1763  }
  1764  
  1765  func dateToSigned[T int32 | int64](
  1766  	from vector.FunctionParameterWrapper[types.Date],
  1767  	to *vector.FunctionResult[T], length int) error {
  1768  	var i uint64
  1769  	for i = 0; i < uint64(length); i++ {
  1770  		v, null := from.GetValue(i)
  1771  		if null {
  1772  			if err := to.Append(0, true); err != nil {
  1773  				return err
  1774  			}
  1775  		} else {
  1776  			val := v.DaysSinceUnixEpoch()
  1777  			if err := to.Append(T(val), false); err != nil {
  1778  				return err
  1779  			}
  1780  		}
  1781  	}
  1782  	return nil
  1783  }
  1784  
  1785  func dateToTime(
  1786  	from vector.FunctionParameterWrapper[types.Date],
  1787  	to *vector.FunctionResult[types.Time], length int) error {
  1788  	var i uint64
  1789  	l := uint64(length)
  1790  	for i = 0; i < l; i++ {
  1791  		v, null := from.GetValue(i)
  1792  		if null {
  1793  			if err := to.Append(0, true); err != nil {
  1794  				return err
  1795  			}
  1796  		} else {
  1797  			if err := to.Append(v.ToTime(), false); err != nil {
  1798  				return err
  1799  			}
  1800  		}
  1801  	}
  1802  	return nil
  1803  }
  1804  
  1805  func datetimeToTime(
  1806  	from vector.FunctionParameterWrapper[types.Datetime],
  1807  	to *vector.FunctionResult[types.Time], length int) error {
  1808  	var i uint64
  1809  	l := uint64(length)
  1810  	totype := to.GetType()
  1811  	for i = 0; i < l; i++ {
  1812  		v, null := from.GetValue(i)
  1813  		if null {
  1814  			if err := to.Append(0, true); err != nil {
  1815  				return err
  1816  			}
  1817  		} else {
  1818  			if err := to.Append(v.ToTime(totype.Precision), false); err != nil {
  1819  				return err
  1820  			}
  1821  		}
  1822  	}
  1823  	return nil
  1824  }
  1825  
  1826  func dateToTimestamp(
  1827  	from vector.FunctionParameterWrapper[types.Date],
  1828  	to *vector.FunctionResult[types.Timestamp], length int,
  1829  	zone *time.Location) error {
  1830  	var i uint64
  1831  	l := uint64(length)
  1832  	for i = 0; i < l; i++ {
  1833  		v, null := from.GetValue(i)
  1834  		if null {
  1835  			if err := to.Append(0, true); err != nil {
  1836  				return err
  1837  			}
  1838  		} else {
  1839  			if err := to.Append(v.ToTimestamp(zone), false); err != nil {
  1840  				return err
  1841  			}
  1842  		}
  1843  	}
  1844  	return nil
  1845  }
  1846  
  1847  func datetimeToInt32(
  1848  	ctx context.Context,
  1849  	from vector.FunctionParameterWrapper[types.Datetime],
  1850  	to *vector.FunctionResult[int32], length int) error {
  1851  	var i uint64
  1852  	l := uint64(length)
  1853  	for i = 0; i < l; i++ {
  1854  		v, null := from.GetValue(i)
  1855  		if null {
  1856  			if err := to.Append(0, true); err != nil {
  1857  				return err
  1858  			}
  1859  		} else {
  1860  			val := v.SecsSinceUnixEpoch()
  1861  			if val < math.MinInt32 || val > math.MaxInt32 {
  1862  				return moerr.NewOutOfRange(ctx, "int32", "value '%v'", val)
  1863  			}
  1864  			if err := to.Append(int32(val), false); err != nil {
  1865  				return err
  1866  			}
  1867  		}
  1868  	}
  1869  	return nil
  1870  }
  1871  
  1872  func datetimeToInt64(
  1873  	from vector.FunctionParameterWrapper[types.Datetime],
  1874  	to *vector.FunctionResult[int64], length int) error {
  1875  	var i uint64
  1876  	l := uint64(length)
  1877  	for i = 0; i < l; i++ {
  1878  		v, null := from.GetValue(i)
  1879  		if null {
  1880  			if err := to.Append(0, true); err != nil {
  1881  				return err
  1882  			}
  1883  		} else {
  1884  			val := v.SecsSinceUnixEpoch()
  1885  			if err := to.Append(val, false); err != nil {
  1886  				return err
  1887  			}
  1888  		}
  1889  	}
  1890  	return nil
  1891  }
  1892  
  1893  func datetimeToTimestamp(
  1894  	from vector.FunctionParameterWrapper[types.Datetime],
  1895  	to *vector.FunctionResult[types.Timestamp], length int,
  1896  	zone *time.Location) error {
  1897  	var i uint64
  1898  	l := uint64(length)
  1899  	for i = 0; i < l; i++ {
  1900  		v, null := from.GetValue(i)
  1901  		if null {
  1902  			if err := to.Append(0, true); err != nil {
  1903  				return err
  1904  			}
  1905  		} else {
  1906  			if err := to.Append(v.ToTimestamp(zone), false); err != nil {
  1907  				return err
  1908  			}
  1909  		}
  1910  	}
  1911  	return nil
  1912  }
  1913  
  1914  func dateToDatetime(
  1915  	from vector.FunctionParameterWrapper[types.Date],
  1916  	to *vector.FunctionResult[types.Datetime], length int) error {
  1917  	var i uint64
  1918  	l := uint64(length)
  1919  	for i = 0; i < l; i++ {
  1920  		v, null := from.GetValue(i)
  1921  		if null {
  1922  			if err := to.Append(0, true); err != nil {
  1923  				return err
  1924  			}
  1925  		} else {
  1926  			if err := to.Append(v.ToDatetime(), false); err != nil {
  1927  				return err
  1928  			}
  1929  		}
  1930  	}
  1931  	return nil
  1932  }
  1933  
  1934  func timestampToDatetime(
  1935  	ctx context.Context,
  1936  	from vector.FunctionParameterWrapper[types.Timestamp],
  1937  	to *vector.FunctionResult[types.Datetime], length int,
  1938  	zone *time.Location) error {
  1939  	var i uint64
  1940  	l := uint64(length)
  1941  	tempR := make([]types.Datetime, 1)
  1942  	tempT := make([]types.Timestamp, 1)
  1943  	for i = 0; i < l; i++ {
  1944  		v, null := from.GetValue(i)
  1945  		if null {
  1946  			if err := to.Append(0, true); err != nil {
  1947  				return err
  1948  			}
  1949  		} else {
  1950  			tempT[0] = v
  1951  			result, err := binary.TimestampToDatetime(ctx, zone, tempT, tempR)
  1952  			if err != nil {
  1953  				return err
  1954  			}
  1955  			if err = to.Append(result[0], false); err != nil {
  1956  				return err
  1957  			}
  1958  		}
  1959  	}
  1960  	return nil
  1961  }
  1962  
  1963  func timeToDatetime(
  1964  	from vector.FunctionParameterWrapper[types.Time],
  1965  	to *vector.FunctionResult[types.Datetime], length int) error {
  1966  	var i uint64
  1967  	l := uint64(length)
  1968  	totype := to.GetType()
  1969  	for i = 0; i < l; i++ {
  1970  		v, null := from.GetValue(i)
  1971  		if null {
  1972  			if err := to.Append(0, true); err != nil {
  1973  				return err
  1974  			}
  1975  		} else {
  1976  			if err := to.Append(v.ToDatetime(totype.Precision), false); err != nil {
  1977  				return err
  1978  			}
  1979  		}
  1980  	}
  1981  	return nil
  1982  }
  1983  
  1984  func datetimeToDate(
  1985  	from vector.FunctionParameterWrapper[types.Datetime],
  1986  	to *vector.FunctionResult[types.Date], length int) error {
  1987  	var i uint64
  1988  	l := uint64(length)
  1989  	for i = 0; i < l; i++ {
  1990  		v, null := from.GetValue(i)
  1991  		if null {
  1992  			if err := to.Append(0, true); err != nil {
  1993  				return err
  1994  			}
  1995  		} else {
  1996  			if err := to.Append(v.ToDate(), false); err != nil {
  1997  				return err
  1998  			}
  1999  		}
  2000  	}
  2001  	return nil
  2002  }
  2003  
  2004  func timestampToInt32(
  2005  	ctx context.Context,
  2006  	from vector.FunctionParameterWrapper[types.Timestamp],
  2007  	to *vector.FunctionResult[int32], length int) error {
  2008  	var i uint64
  2009  	for i = 0; i < uint64(length); i++ {
  2010  		v, null := from.GetValue(i)
  2011  		if null {
  2012  			if err := to.Append(0, true); err != nil {
  2013  				return err
  2014  			}
  2015  		} else {
  2016  			val := v.Unix()
  2017  			if val < math.MinInt32 || val > math.MaxInt32 {
  2018  				return moerr.NewOutOfRange(ctx, "int32", "value '%v'", val)
  2019  			}
  2020  			if err := to.Append(int32(val), false); err != nil {
  2021  				return err
  2022  			}
  2023  		}
  2024  	}
  2025  	return nil
  2026  }
  2027  
  2028  func timestampToInt64(
  2029  	from vector.FunctionParameterWrapper[types.Timestamp],
  2030  	to *vector.FunctionResult[int64], length int) error {
  2031  	var i uint64
  2032  	for i = 0; i < uint64(length); i++ {
  2033  		v, null := from.GetValue(i)
  2034  		if null {
  2035  			if err := to.Append(0, true); err != nil {
  2036  				return err
  2037  			}
  2038  		} else {
  2039  			val := v.Unix()
  2040  			if err := to.Append(val, false); err != nil {
  2041  				return err
  2042  			}
  2043  		}
  2044  	}
  2045  	return nil
  2046  }
  2047  
  2048  func timestampToDate(
  2049  	ctx context.Context,
  2050  	from vector.FunctionParameterWrapper[types.Timestamp],
  2051  	to *vector.FunctionResult[types.Date], length int,
  2052  	zone *time.Location) error {
  2053  	var i uint64
  2054  	l := uint64(length)
  2055  	tempR := make([]types.Datetime, 1)
  2056  	tempT := make([]types.Timestamp, 1)
  2057  	for i = 0; i < l; i++ {
  2058  		v, null := from.GetValue(i)
  2059  		if null {
  2060  			if err := to.Append(0, true); err != nil {
  2061  				return err
  2062  			}
  2063  		} else {
  2064  			tempT[0] = v
  2065  			// XXX I'm not sure if it's a good way to convert it to datetime first.
  2066  			// but I just follow the old logic of old code.
  2067  			result, err := binary.TimestampToDatetime(ctx, zone, tempT, tempR)
  2068  			if err != nil {
  2069  				return err
  2070  			}
  2071  			if err = to.Append(result[0].ToDate(), false); err != nil {
  2072  				return err
  2073  			}
  2074  		}
  2075  	}
  2076  	return nil
  2077  }
  2078  
  2079  func timeToInteger[T constraints.Integer](
  2080  	ctx context.Context,
  2081  	from vector.FunctionParameterWrapper[types.Time],
  2082  	to *vector.FunctionResult[T], length int) error {
  2083  	var i uint64
  2084  	l := uint64(length)
  2085  	var dft T
  2086  	for i = 0; i < l; i++ {
  2087  		v, null := from.GetValue(i)
  2088  		if null {
  2089  			if err := to.Append(dft, true); err != nil {
  2090  				return err
  2091  			}
  2092  		} else {
  2093  			r := v.ToInt64()
  2094  			// XXX we may need an elegant method to do overflow check.
  2095  			if err := overflowForNumericToNumeric[int64, T](ctx, []int64{r}); err != nil {
  2096  				return err
  2097  			}
  2098  			if err := to.Append(T(r), false); err != nil {
  2099  				return err
  2100  			}
  2101  		}
  2102  	}
  2103  	return nil
  2104  }
  2105  
  2106  func timeToDate(
  2107  	from vector.FunctionParameterWrapper[types.Time],
  2108  	to *vector.FunctionResult[types.Date], length int) error {
  2109  	var i uint64
  2110  	l := uint64(length)
  2111  	for i = 0; i < l; i++ {
  2112  		v, null := from.GetValue(i)
  2113  		if null {
  2114  			if err := to.Append(0, true); err != nil {
  2115  				return err
  2116  			}
  2117  		} else {
  2118  			if err := to.Append(v.ToDate(), false); err != nil {
  2119  				return err
  2120  			}
  2121  		}
  2122  	}
  2123  	return nil
  2124  }
  2125  
  2126  func dateToStr(
  2127  	from vector.FunctionParameterWrapper[types.Date],
  2128  	to *vector.FunctionResult[types.Varlena], length int) error {
  2129  	var i uint64
  2130  	l := uint64(length)
  2131  	for i = 0; i < l; i++ {
  2132  		v, null := from.GetValue(i)
  2133  		if null {
  2134  			if err := to.AppendStr(nil, true); err != nil {
  2135  				return err
  2136  			}
  2137  		} else {
  2138  			if err := to.AppendStr([]byte(v.String()), false); err != nil {
  2139  				return err
  2140  			}
  2141  		}
  2142  	}
  2143  	return nil
  2144  }
  2145  
  2146  func datetimeToStr(
  2147  	from vector.FunctionParameterWrapper[types.Datetime],
  2148  	to *vector.FunctionResult[types.Varlena], length int) error {
  2149  	var i uint64
  2150  	l := uint64(length)
  2151  	fromType := from.GetType()
  2152  	for i = 0; i < l; i++ {
  2153  		v, null := from.GetValue(i)
  2154  		if null {
  2155  			if err := to.AppendStr(nil, true); err != nil {
  2156  				return err
  2157  			}
  2158  		} else {
  2159  			if err := to.AppendStr([]byte(v.String2(fromType.Precision)), false); err != nil {
  2160  				return err
  2161  			}
  2162  		}
  2163  	}
  2164  	return nil
  2165  }
  2166  
  2167  func timestampToStr(
  2168  	from vector.FunctionParameterWrapper[types.Timestamp],
  2169  	to *vector.FunctionResult[types.Varlena], length int,
  2170  	zone *time.Location) error {
  2171  	var i uint64
  2172  	l := uint64(length)
  2173  	fromType := from.GetType()
  2174  	for i = 0; i < l; i++ {
  2175  		v, null := from.GetValue(i)
  2176  		if null {
  2177  			if err := to.AppendStr(nil, true); err != nil {
  2178  				return err
  2179  			}
  2180  		} else {
  2181  			if err := to.AppendStr([]byte(v.String2(zone, fromType.Precision)), false); err != nil {
  2182  				return err
  2183  			}
  2184  		}
  2185  	}
  2186  	return nil
  2187  }
  2188  
  2189  func timeToStr(
  2190  	from vector.FunctionParameterWrapper[types.Time],
  2191  	to *vector.FunctionResult[types.Varlena], length int) error {
  2192  	var i uint64
  2193  	l := uint64(length)
  2194  	fromType := from.GetType()
  2195  	for i = 0; i < l; i++ {
  2196  		v, null := from.GetValue(i)
  2197  		if null {
  2198  			if err := to.AppendStr(nil, true); err != nil {
  2199  				return err
  2200  			}
  2201  		} else {
  2202  			if err := to.AppendStr([]byte(v.String2(fromType.Precision)), false); err != nil {
  2203  				return err
  2204  			}
  2205  		}
  2206  	}
  2207  	return nil
  2208  }
  2209  
  2210  func timeToDecimal64(
  2211  	ctx context.Context,
  2212  	from vector.FunctionParameterWrapper[types.Time],
  2213  	to *vector.FunctionResult[types.Decimal64], length int) error {
  2214  	var i uint64
  2215  	l := uint64(length)
  2216  	var dft types.Decimal64
  2217  	fromType := from.GetType()
  2218  	totype := to.GetType()
  2219  	for ; i < l; i++ {
  2220  		v, null := from.GetValue(i)
  2221  		if null {
  2222  			if err := to.Append(dft, true); err != nil {
  2223  				return err
  2224  			}
  2225  		} else {
  2226  			result, err := v.ToDecimal64(ctx, totype.Width, fromType.Precision)
  2227  			if err != nil {
  2228  				return err
  2229  			}
  2230  			if err = to.Append(result, false); err != nil {
  2231  				return err
  2232  			}
  2233  		}
  2234  	}
  2235  	return nil
  2236  }
  2237  
  2238  func timeToDecimal128(
  2239  	ctx context.Context,
  2240  	from vector.FunctionParameterWrapper[types.Time],
  2241  	to *vector.FunctionResult[types.Decimal128], length int) error {
  2242  	var i uint64
  2243  	l := uint64(length)
  2244  	var dft types.Decimal128
  2245  	fromType := from.GetType()
  2246  	totype := to.GetType()
  2247  	for ; i < l; i++ {
  2248  		v, null := from.GetValue(i)
  2249  		if null {
  2250  			if err := to.Append(dft, true); err != nil {
  2251  				return err
  2252  			}
  2253  		} else {
  2254  			result, err := v.ToDecimal128(ctx, totype.Width, fromType.Precision)
  2255  			if err != nil {
  2256  				return err
  2257  			}
  2258  			if err = to.Append(result, false); err != nil {
  2259  				return err
  2260  			}
  2261  		}
  2262  	}
  2263  	return nil
  2264  }
  2265  
  2266  func decimal64ToInt64(
  2267  	ctx context.Context,
  2268  	from vector.FunctionParameterWrapper[types.Decimal64],
  2269  	to *vector.FunctionResult[int64], length int) error {
  2270  	var i uint64
  2271  	l := uint64(length)
  2272  	fromTyp := from.GetType()
  2273  	for i = 0; i < l; i++ {
  2274  		v, null := from.GetValue(i)
  2275  		if null {
  2276  			if err := to.Append(0, true); err != nil {
  2277  				return err
  2278  			}
  2279  		} else {
  2280  			xStr := v.ToStringWithScale(fromTyp.Scale)
  2281  			floatRepresentation, err := strconv.ParseFloat(xStr, 64)
  2282  			if err != nil {
  2283  				return err
  2284  			}
  2285  			if floatRepresentation > math.MaxInt64 || floatRepresentation < math.MinInt64 {
  2286  				return moerr.NewOutOfRange(ctx, "int64", "value '%v'", xStr)
  2287  			}
  2288  			err = to.Append(int64(math.Round(floatRepresentation)), false)
  2289  			if err != nil {
  2290  				return err
  2291  			}
  2292  		}
  2293  	}
  2294  	return nil
  2295  }
  2296  
  2297  func decimal128ToSigned[T constraints.Signed](
  2298  	ctx context.Context,
  2299  	from vector.FunctionParameterWrapper[types.Decimal128],
  2300  	to *vector.FunctionResult[T], bitSize int, length int) error {
  2301  	var i uint64
  2302  	l := uint64(length)
  2303  	for i = 0; i < l; i++ {
  2304  		v, null := from.GetValue(i)
  2305  		if null {
  2306  			if err := to.Append(0, true); err != nil {
  2307  				return err
  2308  			}
  2309  		} else {
  2310  			xStr := v.ToStringWithScale(0)
  2311  			result, err := strconv.ParseInt(xStr, 10, bitSize)
  2312  			if err != nil {
  2313  				return moerr.NewOutOfRange(ctx,
  2314  					fmt.Sprintf("int%d", bitSize),
  2315  					"value '%v'", xStr)
  2316  			}
  2317  			err = to.Append(T(result), false)
  2318  			if err != nil {
  2319  				return err
  2320  			}
  2321  		}
  2322  	}
  2323  	return nil
  2324  }
  2325  
  2326  func decimal64ToUnsigned[T constraints.Unsigned](
  2327  	ctx context.Context,
  2328  	from vector.FunctionParameterWrapper[types.Decimal64],
  2329  	to *vector.FunctionResult[T], bitSize int,
  2330  	length int) error {
  2331  	var i uint64
  2332  	l := uint64(length)
  2333  	fromType := from.GetType()
  2334  	for i = 0; i < l; i++ {
  2335  		v, null := from.GetValue(i)
  2336  		if null {
  2337  			if err := to.Append(0, true); err != nil {
  2338  				return err
  2339  			}
  2340  		} else {
  2341  			xStr := v.ToStringWithScale(fromType.Scale)
  2342  			xStr = strings.Split(xStr, ".")[0]
  2343  			result, err := strconv.ParseUint(xStr, 10, bitSize)
  2344  			if err != nil {
  2345  				return moerr.NewOutOfRange(ctx,
  2346  					fmt.Sprintf("uint%d", bitSize),
  2347  					"value '%v'", xStr)
  2348  			}
  2349  			err = to.Append(T(result), false)
  2350  			if err != nil {
  2351  				return err
  2352  			}
  2353  		}
  2354  	}
  2355  	return nil
  2356  }
  2357  
  2358  func decimal128ToUnsigned[T constraints.Unsigned](
  2359  	ctx context.Context,
  2360  	from vector.FunctionParameterWrapper[types.Decimal128],
  2361  	to *vector.FunctionResult[T], bitSize int,
  2362  	length int) error {
  2363  	var i uint64
  2364  	l := uint64(length)
  2365  	fromType := from.GetType()
  2366  	for i = 0; i < l; i++ {
  2367  		v, null := from.GetValue(i)
  2368  		if null {
  2369  			if err := to.Append(0, true); err != nil {
  2370  				return err
  2371  			}
  2372  		} else {
  2373  			xStr := v.ToStringWithScale(fromType.Scale)
  2374  			xStr = strings.Split(xStr, ".")[0]
  2375  			result, err := strconv.ParseUint(xStr, 10, bitSize)
  2376  			if err != nil {
  2377  				return moerr.NewOutOfRange(ctx,
  2378  					fmt.Sprintf("uint%d", bitSize),
  2379  					"value '%v'", xStr)
  2380  			}
  2381  			err = to.Append(T(result), false)
  2382  			if err != nil {
  2383  				return err
  2384  			}
  2385  		}
  2386  	}
  2387  	return nil
  2388  }
  2389  
  2390  func decimal64ToTime(
  2391  	from vector.FunctionParameterWrapper[types.Decimal64],
  2392  	to *vector.FunctionResult[types.Time], length int) error {
  2393  	var i uint64
  2394  	l := uint64(length)
  2395  	totype := to.GetType()
  2396  	for i = 0; i < l; i++ {
  2397  		v, null := from.GetValue(i)
  2398  		if null {
  2399  			if err := to.Append(0, true); err != nil {
  2400  				return err
  2401  			}
  2402  		} else {
  2403  			result, err := types.ParseDecimal64lToTime(v, totype.Precision)
  2404  			if err != nil {
  2405  				return err
  2406  			}
  2407  			if err = to.Append(result, false); err != nil {
  2408  				return err
  2409  			}
  2410  		}
  2411  	}
  2412  	return nil
  2413  }
  2414  
  2415  func decimal128ToTime(
  2416  	from vector.FunctionParameterWrapper[types.Decimal128],
  2417  	to *vector.FunctionResult[types.Time], length int) error {
  2418  	var i uint64
  2419  	l := uint64(length)
  2420  	totype := to.GetType()
  2421  	for i = 0; i < l; i++ {
  2422  		v, null := from.GetValue(i)
  2423  		if null {
  2424  			if err := to.Append(0, true); err != nil {
  2425  				return err
  2426  			}
  2427  		} else {
  2428  			result, err := types.ParseDecimal128lToTime(v, totype.Precision)
  2429  			if err != nil {
  2430  				return err
  2431  			}
  2432  			if err = to.Append(result, false); err != nil {
  2433  				return err
  2434  			}
  2435  		}
  2436  	}
  2437  	return nil
  2438  }
  2439  
  2440  func decimal64ToTimestamp(
  2441  	from vector.FunctionParameterWrapper[types.Decimal64],
  2442  	to *vector.FunctionResult[types.Timestamp], length int) error {
  2443  	var i uint64
  2444  	l := uint64(length)
  2445  	for i = 0; i < l; i++ {
  2446  		v, null := from.GetValue(i)
  2447  		if null {
  2448  			if err := to.Append(0, true); err != nil {
  2449  				return err
  2450  			}
  2451  		} else {
  2452  			ts := types.Timestamp(v.ToInt64())
  2453  			if err := to.Append(ts, false); err != nil {
  2454  				return err
  2455  			}
  2456  		}
  2457  	}
  2458  	return nil
  2459  }
  2460  
  2461  func decimal128ToTimestamp(
  2462  	from vector.FunctionParameterWrapper[types.Decimal128],
  2463  	to *vector.FunctionResult[types.Timestamp], length int) error {
  2464  	var i uint64
  2465  	l := uint64(length)
  2466  	for i = 0; i < l; i++ {
  2467  		v, null := from.GetValue(i)
  2468  		if null {
  2469  			if err := to.Append(0, true); err != nil {
  2470  				return err
  2471  			}
  2472  		} else {
  2473  			ts := types.Timestamp(v.ToInt64())
  2474  			if err := to.Append(ts, false); err != nil {
  2475  				return err
  2476  			}
  2477  		}
  2478  	}
  2479  	return nil
  2480  }
  2481  
  2482  func decimal64ToFloat[T constraints.Float](
  2483  	ctx context.Context,
  2484  	from vector.FunctionParameterWrapper[types.Decimal64],
  2485  	to *vector.FunctionResult[T], length int, bitSize int) error {
  2486  	// IF float32, then bitSize should be 32. IF float64, then 64
  2487  	var i uint64
  2488  	l := uint64(length)
  2489  	fromType := from.GetType()
  2490  	for i = 0; i < l; i++ {
  2491  		v, null := from.GetValue(i)
  2492  		if null {
  2493  			if err := to.Append(0, true); err != nil {
  2494  				return err
  2495  			}
  2496  		} else {
  2497  			xStr := v.ToStringWithScale(fromType.Scale)
  2498  			result, err := strconv.ParseFloat(xStr, bitSize)
  2499  			if err != nil {
  2500  				return moerr.NewOutOfRange(ctx, "float32", "value '%v'", xStr)
  2501  			}
  2502  			if err = to.Append(T(result), false); err != nil {
  2503  				return err
  2504  			}
  2505  		}
  2506  	}
  2507  	return nil
  2508  }
  2509  
  2510  func decimal128ToFloat[T constraints.Float](
  2511  	ctx context.Context,
  2512  	from vector.FunctionParameterWrapper[types.Decimal128],
  2513  	to *vector.FunctionResult[T], length int, bitSize int) error {
  2514  	// IF float32, then bitSize should be 32. IF float64, then 64
  2515  	var i uint64
  2516  	l := uint64(length)
  2517  	fromType := from.GetType()
  2518  	for i = 0; i < l; i++ {
  2519  		v, null := from.GetValue(i)
  2520  		if null {
  2521  			if err := to.Append(0, true); err != nil {
  2522  				return err
  2523  			}
  2524  		} else {
  2525  			xStr := v.ToStringWithScale(fromType.Scale)
  2526  			result, err := strconv.ParseFloat(xStr, bitSize)
  2527  			if err != nil {
  2528  				return moerr.NewOutOfRange(ctx, "float32", "value '%v'", xStr)
  2529  			}
  2530  			if err = to.Append(T(result), false); err != nil {
  2531  				return err
  2532  			}
  2533  		}
  2534  	}
  2535  	return nil
  2536  }
  2537  
  2538  func decimal64ToDecimal128(
  2539  	from vector.FunctionParameterWrapper[types.Decimal64],
  2540  	to *vector.FunctionResult[types.Decimal128], length int) error {
  2541  	var i uint64
  2542  	l := uint64(length)
  2543  	var dft types.Decimal128
  2544  	totype := to.GetType()
  2545  	{
  2546  		v := to.GetResultVector()
  2547  		v.Typ.Scale = from.GetType().Scale
  2548  	}
  2549  	for i = 0; i < l; i++ {
  2550  		v, null := from.GetValue(i)
  2551  		if null {
  2552  			if err := to.Append(dft, true); err != nil {
  2553  				return err
  2554  			}
  2555  		} else {
  2556  			result, err := types.Decimal128_FromDecimal64WithScale(
  2557  				v, totype.Width, totype.Scale)
  2558  			if err != nil {
  2559  				return err
  2560  			}
  2561  			if err = to.Append(result, false); err != nil {
  2562  				return err
  2563  			}
  2564  		}
  2565  	}
  2566  	return nil
  2567  }
  2568  
  2569  // the scale of decimal128 is guaranteed to be less than 18
  2570  // this cast function is too slow, and therefore only temporary, rewrite needed
  2571  func decimal128ToDecimal64(
  2572  	ctx context.Context,
  2573  	from vector.FunctionParameterWrapper[types.Decimal128],
  2574  	to *vector.FunctionResult[types.Decimal64], length int) error {
  2575  	var i uint64
  2576  	l := uint64(length)
  2577  	var dft types.Decimal64
  2578  	totype := to.GetType()
  2579  	for i = 0; i < l; i++ {
  2580  		v, null := from.GetValue(i)
  2581  		if null {
  2582  			if err := to.Append(dft, true); err != nil {
  2583  				return err
  2584  			}
  2585  		} else {
  2586  			result, err := v.ToDecimal64(totype.Width, totype.Scale)
  2587  			if err != nil {
  2588  				// XXX so ...
  2589  				return moerr.NewOutOfRange(ctx, "dec64", "value '%v'", v)
  2590  			}
  2591  			if err = to.Append(result, false); err != nil {
  2592  				return err
  2593  			}
  2594  		}
  2595  	}
  2596  	return nil
  2597  }
  2598  
  2599  func decimal64ToStr(
  2600  	from vector.FunctionParameterWrapper[types.Decimal64],
  2601  	to *vector.FunctionResult[types.Varlena], length int) error {
  2602  	var i uint64
  2603  	l := uint64(length)
  2604  	fromType := from.GetType()
  2605  	for i = 0; i < l; i++ {
  2606  		v, null := from.GetValue(i)
  2607  		if null {
  2608  			if err := to.AppendStr(nil, true); err != nil {
  2609  				return err
  2610  			}
  2611  		} else {
  2612  			result := []byte(v.ToStringWithScale(fromType.Scale))
  2613  			if err := to.AppendStr(result, false); err != nil {
  2614  				return err
  2615  			}
  2616  		}
  2617  	}
  2618  	return nil
  2619  }
  2620  
  2621  func decimal128ToStr(
  2622  	from vector.FunctionParameterWrapper[types.Decimal128],
  2623  	to *vector.FunctionResult[types.Varlena], length int) error {
  2624  	var i uint64
  2625  	l := uint64(length)
  2626  	fromType := from.GetType()
  2627  	for i = 0; i < l; i++ {
  2628  		v, null := from.GetValue(i)
  2629  		if null {
  2630  			if err := to.AppendStr(nil, true); err != nil {
  2631  				return err
  2632  			}
  2633  		} else {
  2634  			result := []byte(v.ToStringWithScale(fromType.Scale))
  2635  			if err := to.AppendStr(result, false); err != nil {
  2636  				return err
  2637  			}
  2638  		}
  2639  	}
  2640  	return nil
  2641  }
  2642  
  2643  func strToSigned[T constraints.Signed](
  2644  	ctx context.Context,
  2645  	from vector.FunctionParameterWrapper[types.Varlena],
  2646  	to *vector.FunctionResult[T], bitSize int,
  2647  	length int) error {
  2648  	var i uint64
  2649  	var l = uint64(length)
  2650  	isBinary := from.GetSourceVector().GetIsBin()
  2651  
  2652  	var result T
  2653  	for i = 0; i < l; i++ {
  2654  		v, null := from.GetStrValue(i)
  2655  		if null {
  2656  			if err := to.Append(0, true); err != nil {
  2657  				return err
  2658  			}
  2659  		} else {
  2660  			if isBinary {
  2661  				r, err := strconv.ParseInt(
  2662  					hex.EncodeToString(v), 16, 64)
  2663  				if err != nil {
  2664  					if strings.Contains(err.Error(), "value out of range") {
  2665  						// the string maybe non-visible,don't print it
  2666  						return moerr.NewOutOfRange(ctx, "int", "")
  2667  					}
  2668  					return moerr.NewInvalidArg(ctx, "cast to int", r)
  2669  				}
  2670  				result = T(r)
  2671  			} else {
  2672  				s := convertByteSliceToString(v)
  2673  				r, err := strconv.ParseInt(
  2674  					strings.TrimSpace(s), 10, bitSize)
  2675  				if err != nil {
  2676  					// XXX I'm not sure if we should return the int8 / int16 / int64 info. or
  2677  					// just return the int. the old code just return the int. too much bvt result needs to update.
  2678  					if strings.Contains(err.Error(), "value out of range") {
  2679  						return moerr.NewOutOfRange(ctx, "int", "value '%s'", s)
  2680  					}
  2681  					return moerr.NewInvalidArg(ctx, "cast to int", s)
  2682  				}
  2683  				result = T(r)
  2684  			}
  2685  			if err := to.Append(result, false); err != nil {
  2686  				return err
  2687  			}
  2688  		}
  2689  	}
  2690  	return nil
  2691  }
  2692  
  2693  func strToUnsigned[T constraints.Unsigned](
  2694  	ctx context.Context,
  2695  	from vector.FunctionParameterWrapper[types.Varlena],
  2696  	to *vector.FunctionResult[T], bitSize int,
  2697  	length int) error {
  2698  	var i uint64
  2699  	var l = uint64(length)
  2700  	isBinary := from.GetSourceVector().GetIsBin()
  2701  
  2702  	var val uint64
  2703  	var tErr error
  2704  	for i = 0; i < l; i++ {
  2705  		v, null := from.GetStrValue(i)
  2706  		if null {
  2707  			if err := to.Append(0, true); err != nil {
  2708  				return err
  2709  			}
  2710  		} else {
  2711  			var res *string
  2712  			if isBinary {
  2713  				s := hex.EncodeToString(v)
  2714  				res = &s
  2715  				val, tErr = strconv.ParseUint(s, 16, 64)
  2716  			} else {
  2717  				s := convertByteSliceToString(v)
  2718  				res = &s
  2719  				val, tErr = strconv.ParseUint(strings.TrimSpace(s), 10, bitSize)
  2720  			}
  2721  			if tErr != nil {
  2722  				if strings.Contains(tErr.Error(), "value out of range") {
  2723  					return moerr.NewOutOfRange(ctx, fmt.Sprintf("uint%d", bitSize), "value '%s'", *res)
  2724  				}
  2725  				return moerr.NewInvalidArg(ctx, fmt.Sprintf("cast to uint%d", bitSize), *res)
  2726  			}
  2727  			if err := to.Append(T(val), false); err != nil {
  2728  				return err
  2729  			}
  2730  		}
  2731  	}
  2732  	return nil
  2733  }
  2734  
  2735  func strToFloat[T constraints.Float](
  2736  	ctx context.Context,
  2737  	from vector.FunctionParameterWrapper[types.Varlena],
  2738  	to *vector.FunctionResult[T], bitSize int,
  2739  	length int) error {
  2740  	var i uint64
  2741  	var l = uint64(length)
  2742  	isBinary := from.GetSourceVector().GetIsBin()
  2743  
  2744  	var result T
  2745  	var tErr error
  2746  	var r1 uint64
  2747  	var r2 float64
  2748  	for i = 0; i < l; i++ {
  2749  		v, null := from.GetStrValue(i)
  2750  		if null {
  2751  			if err := to.Append(0, true); err != nil {
  2752  				return err
  2753  			}
  2754  		} else {
  2755  			if isBinary {
  2756  				s := hex.EncodeToString(v)
  2757  				r1, tErr = strconv.ParseUint(s, 16, 64)
  2758  				if tErr != nil {
  2759  					if strings.Contains(tErr.Error(), "value out of range") {
  2760  						return moerr.NewOutOfRange(ctx, "float", "value '%s'", s)
  2761  					}
  2762  					return moerr.NewInvalidArg(ctx, "cast to float", s)
  2763  				}
  2764  				result = T(r1)
  2765  			} else {
  2766  				s := convertByteSliceToString(v)
  2767  				r2, tErr = strconv.ParseFloat(s, bitSize)
  2768  				if tErr != nil {
  2769  					return tErr
  2770  				}
  2771  				result = T(r2)
  2772  			}
  2773  
  2774  			if err := to.Append(result, false); err != nil {
  2775  				return err
  2776  			}
  2777  		}
  2778  	}
  2779  	return nil
  2780  }
  2781  
  2782  func strToDecimal64(
  2783  	from vector.FunctionParameterWrapper[types.Varlena],
  2784  	to *vector.FunctionResult[types.Decimal64], length int,
  2785  ) error {
  2786  	var i uint64
  2787  	var l = uint64(length)
  2788  	var dft types.Decimal64
  2789  	totype := to.GetType()
  2790  	isb := from.GetSourceVector().GetIsBin()
  2791  	for i = 0; i < l; i++ {
  2792  		v, null := from.GetStrValue(i)
  2793  		if null {
  2794  			if err := to.Append(dft, true); err != nil {
  2795  				return err
  2796  			}
  2797  		} else {
  2798  			s := convertByteSliceToString(v)
  2799  			result, err := types.ParseStringToDecimal64(
  2800  				s, totype.Width, totype.Scale, isb)
  2801  			if err != nil {
  2802  				return err
  2803  			}
  2804  			if err = to.Append(result, false); err != nil {
  2805  				return err
  2806  			}
  2807  		}
  2808  	}
  2809  	return nil
  2810  }
  2811  
  2812  func strToDecimal128(
  2813  	from vector.FunctionParameterWrapper[types.Varlena],
  2814  	to *vector.FunctionResult[types.Decimal128], length int,
  2815  ) error {
  2816  	var i uint64
  2817  	var l = uint64(length)
  2818  	var dft types.Decimal128
  2819  	totype := to.GetType()
  2820  	isb := from.GetSourceVector().GetIsBin()
  2821  	for i = 0; i < l; i++ {
  2822  		v, null := from.GetStrValue(i)
  2823  		if null {
  2824  			if err := to.Append(dft, true); err != nil {
  2825  				return err
  2826  			}
  2827  		} else {
  2828  			s := convertByteSliceToString(v)
  2829  			result, err := types.ParseStringToDecimal128(
  2830  				s, totype.Width, totype.Scale, isb)
  2831  			if err != nil {
  2832  				return err
  2833  			}
  2834  			if err = to.Append(result, false); err != nil {
  2835  				return err
  2836  			}
  2837  		}
  2838  	}
  2839  	return nil
  2840  }
  2841  
  2842  func strToBool(
  2843  	from vector.FunctionParameterWrapper[types.Varlena],
  2844  	to *vector.FunctionResult[bool], length int) error {
  2845  	var i uint64
  2846  	var l = uint64(length)
  2847  	for i = 0; i < l; i++ {
  2848  		v, null := from.GetStrValue(i)
  2849  		if null {
  2850  			if err := to.Append(false, true); err != nil {
  2851  				return err
  2852  			}
  2853  		} else {
  2854  			s := convertByteSliceToString(v)
  2855  			val, err := types.ParseBool(s)
  2856  			if err != nil {
  2857  				return err
  2858  			}
  2859  			if err = to.Append(val, false); err != nil {
  2860  				return err
  2861  			}
  2862  		}
  2863  	}
  2864  	return nil
  2865  }
  2866  
  2867  func strToUuid(
  2868  	from vector.FunctionParameterWrapper[types.Varlena],
  2869  	to *vector.FunctionResult[types.Uuid], length int) error {
  2870  	var i uint64
  2871  	var l = uint64(length)
  2872  	var dft types.Uuid
  2873  	for i = 0; i < l; i++ {
  2874  		v, null := from.GetStrValue(i)
  2875  		if null {
  2876  			if err := to.Append(dft, true); err != nil {
  2877  				return err
  2878  			}
  2879  		} else {
  2880  			s := convertByteSliceToString(v)
  2881  			val, err := types.ParseUuid(s)
  2882  			if err != nil {
  2883  				return err
  2884  			}
  2885  			if err = to.Append(val, false); err != nil {
  2886  				return err
  2887  			}
  2888  		}
  2889  	}
  2890  	return nil
  2891  }
  2892  
  2893  func strToJson(
  2894  	from vector.FunctionParameterWrapper[types.Varlena],
  2895  	to *vector.FunctionResult[types.Varlena], length int) error {
  2896  	var i uint64
  2897  	var l = uint64(length)
  2898  	for i = 0; i < l; i++ {
  2899  		v, null := from.GetStrValue(i)
  2900  		if null {
  2901  			if err := to.AppendStr(nil, true); err != nil {
  2902  				return err
  2903  			}
  2904  		} else {
  2905  			s := convertByteSliceToString(v)
  2906  			json, err := types.ParseStringToByteJson(s)
  2907  			if err != nil {
  2908  				return err
  2909  			}
  2910  			val, err := types.EncodeJson(json)
  2911  			if err != nil {
  2912  				return err
  2913  			}
  2914  			if err = to.AppendStr(val, false); err != nil {
  2915  				return err
  2916  			}
  2917  		}
  2918  	}
  2919  	return nil
  2920  }
  2921  
  2922  func strToDate(
  2923  	from vector.FunctionParameterWrapper[types.Varlena],
  2924  	to *vector.FunctionResult[types.Date], length int) error {
  2925  	var i uint64
  2926  	var l = uint64(length)
  2927  	var dft types.Date
  2928  	for i = 0; i < l; i++ {
  2929  		v, null := from.GetStrValue(i)
  2930  		if null || len(v) == 0 {
  2931  			if err := to.Append(dft, true); err != nil {
  2932  				return err
  2933  			}
  2934  		} else {
  2935  			s := convertByteSliceToString(v)
  2936  			val, err := types.ParseDateCast(s)
  2937  			if err != nil {
  2938  				return err
  2939  			}
  2940  			if err = to.Append(val, false); err != nil {
  2941  				return err
  2942  			}
  2943  		}
  2944  	}
  2945  	return nil
  2946  }
  2947  
  2948  func strToTime(
  2949  	from vector.FunctionParameterWrapper[types.Varlena],
  2950  	to *vector.FunctionResult[types.Time], length int) error {
  2951  	var i uint64
  2952  	var l = uint64(length)
  2953  	var dft types.Time
  2954  	totype := to.GetType()
  2955  	for i = 0; i < l; i++ {
  2956  		v, null := from.GetStrValue(i)
  2957  		if null || len(v) == 0 {
  2958  			if err := to.Append(dft, true); err != nil {
  2959  				return err
  2960  			}
  2961  		} else {
  2962  			s := convertByteSliceToString(v)
  2963  			val, err := types.ParseTime(s, totype.Precision)
  2964  			if err != nil {
  2965  				return err
  2966  			}
  2967  			if err = to.Append(val, false); err != nil {
  2968  				return err
  2969  			}
  2970  		}
  2971  	}
  2972  	return nil
  2973  }
  2974  
  2975  func strToDatetime(
  2976  	from vector.FunctionParameterWrapper[types.Varlena],
  2977  	to *vector.FunctionResult[types.Datetime], length int) error {
  2978  	var i uint64
  2979  	var l = uint64(length)
  2980  	var dft types.Datetime
  2981  	totype := to.GetType()
  2982  	for i = 0; i < l; i++ {
  2983  		v, null := from.GetStrValue(i)
  2984  		if null || len(v) == 0 {
  2985  			if err := to.Append(dft, true); err != nil {
  2986  				return err
  2987  			}
  2988  		} else {
  2989  			s := convertByteSliceToString(v)
  2990  			val, err := types.ParseDatetime(s, totype.Precision)
  2991  			if err != nil {
  2992  				return err
  2993  			}
  2994  			if err = to.Append(val, false); err != nil {
  2995  				return err
  2996  			}
  2997  		}
  2998  	}
  2999  	return nil
  3000  }
  3001  
  3002  func strToTimestamp(
  3003  	from vector.FunctionParameterWrapper[types.Varlena],
  3004  	to *vector.FunctionResult[types.Timestamp],
  3005  	zone *time.Location, length int) error {
  3006  	var i uint64
  3007  	var l = uint64(length)
  3008  	var dft types.Timestamp
  3009  	totype := to.GetType()
  3010  	for i = 0; i < l; i++ {
  3011  		v, null := from.GetStrValue(i)
  3012  		if null || len(v) == 0 {
  3013  			if err := to.Append(dft, true); err != nil {
  3014  				return err
  3015  			}
  3016  		} else {
  3017  			s := convertByteSliceToString(v)
  3018  			val, err := types.ParseTimestamp(zone, s, totype.Precision)
  3019  			if err != nil {
  3020  				return err
  3021  			}
  3022  			if err = to.Append(val, false); err != nil {
  3023  				return err
  3024  			}
  3025  		}
  3026  	}
  3027  	return nil
  3028  }
  3029  
  3030  func strToStr(
  3031  	ctx context.Context,
  3032  	from vector.FunctionParameterWrapper[types.Varlena],
  3033  	to *vector.FunctionResult[types.Varlena], length int) error {
  3034  	totype := to.GetType()
  3035  	destLen := int(totype.Width)
  3036  	var i uint64
  3037  	var l = uint64(length)
  3038  	if totype.Oid != types.T_text && destLen != 0 {
  3039  		for i = 0; i < l; i++ {
  3040  			v, null := from.GetStrValue(i)
  3041  			if null {
  3042  				if err := to.AppendStr(nil, true); err != nil {
  3043  					return err
  3044  				}
  3045  				continue
  3046  			}
  3047  			// check the length.
  3048  			s := convertByteSliceToString(v)
  3049  			if utf8.RuneCountInString(s) > destLen {
  3050  				return formatCastError(ctx, from.GetSourceVector(), totype, fmt.Sprintf(
  3051  					"Src length %v is larger than Dest length %v", len(s), destLen))
  3052  			}
  3053  			if err := to.AppendStr(v, false); err != nil {
  3054  				return err
  3055  			}
  3056  		}
  3057  	} else {
  3058  		for i = 0; i < l; i++ {
  3059  			v, null := from.GetStrValue(i)
  3060  			if null {
  3061  				if err := to.AppendStr(nil, true); err != nil {
  3062  					return err
  3063  				}
  3064  				continue
  3065  			}
  3066  			// check the length.
  3067  			if err := to.AppendStr(v, false); err != nil {
  3068  				return err
  3069  			}
  3070  		}
  3071  	}
  3072  	return nil
  3073  }
  3074  
  3075  func uuidToStr(
  3076  	from vector.FunctionParameterWrapper[types.Uuid],
  3077  	to *vector.FunctionResult[types.Varlena], length int) error {
  3078  	var i uint64
  3079  	var l = uint64(length)
  3080  	for i = 0; i < l; i++ {
  3081  		v, null := from.GetValue(i)
  3082  		if null {
  3083  			if err := to.AppendStr(nil, true); err != nil {
  3084  				return err
  3085  			}
  3086  		} else {
  3087  			result := v.ToString()
  3088  			if err := to.AppendStr([]byte(result), false); err != nil {
  3089  				return err
  3090  			}
  3091  		}
  3092  	}
  3093  	return nil
  3094  }
  3095  
  3096  func jsonToStr(
  3097  	from vector.FunctionParameterWrapper[types.Varlena],
  3098  	to *vector.FunctionResult[types.Varlena], length int) error {
  3099  	var i uint64
  3100  	for i = 0; i < uint64(length); i++ {
  3101  		v, null := from.GetStrValue(i)
  3102  		if null {
  3103  			if err := to.AppendStr(nil, true); err != nil {
  3104  				return err
  3105  			}
  3106  		} else {
  3107  			bj := types.DecodeJson(v)
  3108  			val, err := bj.MarshalJSON()
  3109  			if err != nil {
  3110  				return err
  3111  			}
  3112  			if err = to.AppendStr(val, false); err != nil {
  3113  				return err
  3114  			}
  3115  		}
  3116  	}
  3117  	return nil
  3118  }
  3119  
  3120  func overflowForNumericToNumeric[T1, T2 constraints.Integer | constraints.Float](ctx context.Context, xs []T1) error {
  3121  	if len(xs) == 0 {
  3122  		return nil
  3123  	}
  3124  
  3125  	var t1 T1
  3126  	var t2 T2
  3127  	var li interface{} = &t1
  3128  	var ri interface{} = &t2
  3129  	switch li.(type) {
  3130  	case *int8:
  3131  		switch ri.(type) {
  3132  		case *uint8, *uint16, *uint32, *uint64:
  3133  			for _, x := range xs {
  3134  				if x < 0 {
  3135  					return moerr.NewOutOfRange(ctx, "uint", "value '%v'", x)
  3136  				}
  3137  			}
  3138  		}
  3139  	case *int16:
  3140  		nxs := unsafe.Slice((*int16)(unsafe.Pointer(&xs[0])), len(xs))
  3141  		switch ri.(type) {
  3142  		case *int8:
  3143  			for _, x := range nxs {
  3144  				if x < math.MinInt8 || x > math.MaxInt8 {
  3145  					return moerr.NewOutOfRange(ctx, "int8", "value '%v'", x)
  3146  				}
  3147  			}
  3148  		case *uint8:
  3149  			for _, x := range nxs {
  3150  				if x < 0 || x > math.MaxUint8 {
  3151  					return moerr.NewOutOfRange(ctx, "uint8", "value '%v'", x)
  3152  				}
  3153  			}
  3154  		case *uint16, *uint32, *uint64:
  3155  			for _, x := range nxs {
  3156  				if x < 0 {
  3157  					return moerr.NewOutOfRange(ctx, "uint", "value '%v'", x)
  3158  				}
  3159  			}
  3160  		}
  3161  	case *int32:
  3162  		nxs := unsafe.Slice((*int32)(unsafe.Pointer(&xs[0])), len(xs))
  3163  		switch ri.(type) {
  3164  		case *int8:
  3165  			for _, x := range nxs {
  3166  				if x < math.MinInt8 || x > math.MaxInt8 {
  3167  					return moerr.NewOutOfRange(ctx, "int8", "value '%v'", x)
  3168  				}
  3169  			}
  3170  		case *int16:
  3171  			for _, x := range nxs {
  3172  				if x < math.MinInt16 || x > math.MaxInt16 {
  3173  					return moerr.NewOutOfRange(ctx, "int16", "value '%v'", x)
  3174  				}
  3175  			}
  3176  		case *uint8:
  3177  			for _, x := range nxs {
  3178  				if x < 0 || x > math.MaxUint8 {
  3179  					return moerr.NewOutOfRange(ctx, "uint8", "value '%v'", x)
  3180  				}
  3181  			}
  3182  		case *uint16:
  3183  			for _, x := range nxs {
  3184  				if x < 0 || x > math.MaxUint16 {
  3185  					return moerr.NewOutOfRange(ctx, "uint16", "value '%v'", x)
  3186  				}
  3187  			}
  3188  		case *uint32, *uint64:
  3189  			for _, x := range nxs {
  3190  				if x < 0 {
  3191  					return moerr.NewOutOfRange(ctx, "uint", "value '%v'", x)
  3192  				}
  3193  			}
  3194  		}
  3195  	case *int64:
  3196  		nxs := unsafe.Slice((*int64)(unsafe.Pointer(&xs[0])), len(xs))
  3197  		switch ri.(type) {
  3198  		case *int8:
  3199  			for _, x := range nxs {
  3200  				if x < math.MinInt8 || x > math.MaxInt8 {
  3201  					return moerr.NewOutOfRange(ctx, "int8", "value '%v'", x)
  3202  				}
  3203  			}
  3204  		case *int16:
  3205  			for _, x := range nxs {
  3206  				if x < math.MinInt16 || x > math.MaxInt16 {
  3207  					return moerr.NewOutOfRange(ctx, "int16", "value '%v'", x)
  3208  				}
  3209  			}
  3210  		case *int32:
  3211  			for _, x := range nxs {
  3212  				if x < math.MinInt32 || x > math.MaxInt32 {
  3213  					return moerr.NewOutOfRange(ctx, "int32", "value '%v'", x)
  3214  				}
  3215  			}
  3216  		case *uint8:
  3217  			for _, x := range nxs {
  3218  				if x < 0 || x > math.MaxUint8 {
  3219  					return moerr.NewOutOfRange(ctx, "uint8", "value '%v'", x)
  3220  				}
  3221  			}
  3222  		case *uint16:
  3223  			for _, x := range nxs {
  3224  				if x < 0 || x > math.MaxUint16 {
  3225  					return moerr.NewOutOfRange(ctx, "uint16", "value '%v'", x)
  3226  				}
  3227  			}
  3228  		case *uint32:
  3229  			for _, x := range nxs {
  3230  				if x < 0 || x > math.MaxUint32 {
  3231  					return moerr.NewOutOfRange(ctx, "uint32", "value '%v'", x)
  3232  				}
  3233  			}
  3234  		case *uint64:
  3235  			for _, x := range nxs {
  3236  				if x < 0 {
  3237  					// XXX for adapt to bvt, but i don't know why we hide the wrong value here.
  3238  					return moerr.NewOutOfRange(ctx, "uint64", "")
  3239  				}
  3240  			}
  3241  		}
  3242  	case *uint8:
  3243  		nxs := unsafe.Slice((*uint8)(unsafe.Pointer(&xs[0])), len(xs))
  3244  		switch ri.(type) {
  3245  		case *int8:
  3246  			for _, x := range nxs {
  3247  				if x > math.MaxInt8 {
  3248  					return moerr.NewOutOfRange(ctx, "int8", "value '%v'", x)
  3249  				}
  3250  			}
  3251  		}
  3252  	case *uint16:
  3253  		nxs := unsafe.Slice((*uint16)(unsafe.Pointer(&xs[0])), len(xs))
  3254  		switch ri.(type) {
  3255  		case *int8:
  3256  			for _, x := range nxs {
  3257  				if x > math.MaxInt8 {
  3258  					return moerr.NewOutOfRange(ctx, "int8", "value '%v'", x)
  3259  				}
  3260  			}
  3261  		case *int16:
  3262  			for _, x := range nxs {
  3263  				if x > math.MaxInt16 {
  3264  					return moerr.NewOutOfRange(ctx, "int16", "value '%v'", x)
  3265  				}
  3266  			}
  3267  		case *uint8:
  3268  			for _, x := range nxs {
  3269  				if x > math.MaxUint8 {
  3270  					return moerr.NewOutOfRange(ctx, "uint8", "value '%v'", x)
  3271  				}
  3272  			}
  3273  		}
  3274  	case *uint32:
  3275  		nxs := unsafe.Slice((*uint32)(unsafe.Pointer(&xs[0])), len(xs))
  3276  		switch ri.(type) {
  3277  		case *int8:
  3278  			for _, x := range nxs {
  3279  				if x > math.MaxInt8 {
  3280  					return moerr.NewOutOfRange(ctx, "int8", "value '%v'", x)
  3281  				}
  3282  			}
  3283  		case *int16:
  3284  			for _, x := range nxs {
  3285  				if x > math.MaxInt16 {
  3286  					return moerr.NewOutOfRange(ctx, "int16", "value '%v'", x)
  3287  				}
  3288  			}
  3289  		case *int32:
  3290  			for _, x := range nxs {
  3291  				if x > math.MaxInt32 {
  3292  					return moerr.NewOutOfRange(ctx, "int32", "value '%v'", x)
  3293  				}
  3294  			}
  3295  		case *uint8:
  3296  			for _, x := range nxs {
  3297  				if x > math.MaxUint8 {
  3298  					return moerr.NewOutOfRange(ctx, "uint8", "value '%v'", x)
  3299  				}
  3300  			}
  3301  		case *uint16:
  3302  			for _, x := range nxs {
  3303  				if x > math.MaxUint16 {
  3304  					return moerr.NewOutOfRange(ctx, "uint16", "value '%v'", x)
  3305  				}
  3306  			}
  3307  		}
  3308  	case *uint64:
  3309  		nxs := unsafe.Slice((*uint64)(unsafe.Pointer(&xs[0])), len(xs))
  3310  		switch ri.(type) {
  3311  		case *int8:
  3312  			for _, x := range nxs {
  3313  				if x > math.MaxInt8 {
  3314  					return moerr.NewOutOfRange(ctx, "int8", "value '%v'", x)
  3315  				}
  3316  			}
  3317  		case *int16:
  3318  			for _, x := range nxs {
  3319  				if x > math.MaxInt16 {
  3320  					return moerr.NewOutOfRange(ctx, "int16", "value '%v'", x)
  3321  				}
  3322  			}
  3323  		case *int32:
  3324  			for _, x := range nxs {
  3325  				if x > math.MaxInt32 {
  3326  					return moerr.NewOutOfRange(ctx, "int32", "value '%v'", x)
  3327  				}
  3328  			}
  3329  		case *int64:
  3330  			for _, x := range nxs {
  3331  				if x > math.MaxInt64 {
  3332  					return moerr.NewOutOfRangeNoCtx("int64", "")
  3333  				}
  3334  			}
  3335  		case *uint8:
  3336  			for _, x := range nxs {
  3337  				if x > math.MaxUint8 {
  3338  					return moerr.NewOutOfRange(ctx, "uint8", "value '%v'", x)
  3339  				}
  3340  			}
  3341  		case *uint16:
  3342  			for _, x := range nxs {
  3343  				if x > math.MaxUint16 {
  3344  					return moerr.NewOutOfRange(ctx, "uint16", "value '%v'", x)
  3345  				}
  3346  			}
  3347  		case *uint32:
  3348  			for _, x := range nxs {
  3349  				if x > math.MaxUint32 {
  3350  					return moerr.NewOutOfRange(ctx, "uint32", "value '%v'", x)
  3351  				}
  3352  			}
  3353  		}
  3354  	case *float32:
  3355  		nxs := unsafe.Slice((*float32)(unsafe.Pointer(&xs[0])), len(xs))
  3356  		switch ri.(type) {
  3357  		case *int8:
  3358  			for _, x := range nxs {
  3359  				if math.Round(float64(x)) > math.MaxInt8 {
  3360  					return moerr.NewOutOfRange(ctx, "int8", "value '%v'", x)
  3361  				}
  3362  			}
  3363  		case *int16:
  3364  			for _, x := range nxs {
  3365  				if math.Round(float64(x)) > math.MaxInt16 {
  3366  					return moerr.NewOutOfRange(ctx, "int16", "value '%v'", x)
  3367  				}
  3368  			}
  3369  		case *int32:
  3370  			for _, x := range nxs {
  3371  				if math.Round(float64(x)) > math.MaxInt32 {
  3372  					return moerr.NewOutOfRange(ctx, "int32", "value '%v'", x)
  3373  				}
  3374  			}
  3375  		case *int64:
  3376  			for _, x := range nxs {
  3377  				if math.Round(float64(x)) > math.MaxInt64 {
  3378  					return moerr.NewOutOfRange(ctx, "int64", "value '%v'", x)
  3379  				}
  3380  			}
  3381  		case *uint8:
  3382  			for _, x := range nxs {
  3383  				if math.Round(float64(x)) > math.MaxUint8 {
  3384  					return moerr.NewOutOfRange(ctx, "uint8", "value '%v'", x)
  3385  				}
  3386  			}
  3387  		case *uint16:
  3388  			for _, x := range nxs {
  3389  				if math.Round(float64(x)) > math.MaxUint16 {
  3390  					return moerr.NewOutOfRange(ctx, "uint16", "value '%v'", x)
  3391  				}
  3392  			}
  3393  		case *uint32:
  3394  			for _, x := range nxs {
  3395  				if math.Round(float64(x)) > math.MaxUint32 {
  3396  					return moerr.NewOutOfRange(ctx, "uint32", "value '%v'", x)
  3397  				}
  3398  			}
  3399  		case *uint64:
  3400  			for _, x := range nxs {
  3401  				if math.Round(float64(x)) > math.MaxUint64 {
  3402  					return moerr.NewOutOfRange(ctx, "uint64", "value '%v'", x)
  3403  				}
  3404  			}
  3405  		}
  3406  	case *float64:
  3407  		nxs := unsafe.Slice((*float64)(unsafe.Pointer(&xs[0])), len(xs))
  3408  		switch ri.(type) {
  3409  		case *int8:
  3410  			for _, x := range nxs {
  3411  				if math.Round(x) > math.MaxInt8 {
  3412  					return moerr.NewOutOfRange(ctx, "int8", "value '%v'", x)
  3413  				}
  3414  			}
  3415  		case *int16:
  3416  			for _, x := range nxs {
  3417  				if math.Round(x) > math.MaxInt16 {
  3418  					return moerr.NewOutOfRange(ctx, "int16", "value '%v'", x)
  3419  				}
  3420  			}
  3421  		case *int32:
  3422  			for _, x := range nxs {
  3423  				if math.Round(x) > math.MaxInt32 {
  3424  					return moerr.NewOutOfRange(ctx, "int32", "value '%v'", x)
  3425  				}
  3426  			}
  3427  		case *int64:
  3428  			for _, x := range nxs {
  3429  				if math.Round(x) > math.MaxInt64 || math.Round(x) < math.MinInt64 {
  3430  					return moerr.NewOutOfRange(ctx, "int64", "value '%v'", x)
  3431  				}
  3432  			}
  3433  		case *uint8:
  3434  			for _, x := range nxs {
  3435  				if math.Round(x) > math.MaxUint8 {
  3436  					return moerr.NewOutOfRange(ctx, "uint8", "value '%v'", x)
  3437  				}
  3438  			}
  3439  		case *uint16:
  3440  			for _, x := range nxs {
  3441  				if math.Round(x) > math.MaxUint16 {
  3442  					return moerr.NewOutOfRange(ctx, "uint16", "value '%v'", x)
  3443  				}
  3444  			}
  3445  		case *uint32:
  3446  			for _, x := range nxs {
  3447  				if math.Round(x) > math.MaxUint32 {
  3448  					return moerr.NewOutOfRange(ctx, "uint32", "value '%v'", x)
  3449  				}
  3450  			}
  3451  		case *uint64:
  3452  			for _, x := range nxs {
  3453  				if math.Round(x) > math.MaxUint64 {
  3454  					return moerr.NewOutOfRange(ctx, "uint64", "value '%v'", x)
  3455  				}
  3456  			}
  3457  		case *float32:
  3458  			for _, x := range nxs {
  3459  				if x > math.MaxFloat32 {
  3460  					return moerr.NewOutOfRange(ctx, "float32", "value '%v'", x)
  3461  				}
  3462  			}
  3463  		}
  3464  	}
  3465  	return nil
  3466  }
  3467  
  3468  func appendNulls[T types.FixedSizeT](result vector.FunctionResultWrapper, length int) error {
  3469  	if r, ok := result.(*vector.FunctionResult[types.Varlena]); ok {
  3470  		var i uint64
  3471  		for i = 0; i < uint64(length); i++ {
  3472  			if err := r.AppendStr(nil, true); err != nil {
  3473  				return err
  3474  			}
  3475  		}
  3476  		return nil
  3477  	}
  3478  	if r, ok := result.(*vector.FunctionResult[T]); ok {
  3479  		var t T
  3480  		var i uint64
  3481  		for i = 0; i < uint64(length); i++ {
  3482  			if err := r.Append(t, true); err != nil {
  3483  				return err
  3484  			}
  3485  		}
  3486  		return nil
  3487  	}
  3488  	return nil
  3489  }
  3490  
  3491  // convertByteSliceToString is just a temp method.
  3492  func convertByteSliceToString(v []byte) string {
  3493  	// s := *(*string)(unsafe.Pointer(&v))
  3494  	return string(v)
  3495  }