github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/containers/utils.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 containers
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    21  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    22  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    23  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    24  	"github.com/matrixorigin/matrixone/pkg/container/types"
    25  	movec "github.com/matrixorigin/matrixone/pkg/container/vector"
    26  )
    27  
    28  // ### Shallow copy Functions
    29  
    30  func ToCNBatch(tnBat *Batch) *batch.Batch {
    31  	cnBat := batch.New(true, tnBat.Attrs)
    32  	for i, vec := range tnBat.Vecs {
    33  		cnBat.Vecs[i] = vec.GetDownstreamVector()
    34  	}
    35  	return cnBat
    36  }
    37  
    38  func ToTNBatch(cnBat *batch.Batch, mp *mpool.MPool) *Batch {
    39  	tnBat := NewEmptyBatch()
    40  	for i, vec := range cnBat.Vecs {
    41  		v := ToTNVector(vec, mp)
    42  		tnBat.AddVector(cnBat.Attrs[i], v)
    43  	}
    44  	return tnBat
    45  }
    46  
    47  func ToTNVector(v *movec.Vector, mp *mpool.MPool) Vector {
    48  	vec := MakeVector(*v.GetType(), mp)
    49  	vec.setDownstreamVector(v)
    50  	return vec
    51  }
    52  
    53  func CloneVector(src *movec.Vector, mp *mpool.MPool, vp *VectorPool) (Vector, error) {
    54  	var vec Vector
    55  	if vp != nil {
    56  		vec = vp.GetVector(src.GetType())
    57  		mp = vp.GetMPool()
    58  		if err := src.CloneWindowTo(
    59  			vec.GetDownstreamVector(), 0, src.Length(), mp,
    60  		); err != nil {
    61  			vec.Close()
    62  			return nil, err
    63  		}
    64  	} else {
    65  		vec = MakeVector(*src.GetType(), mp)
    66  		if v, err := src.CloneWindow(0, src.Length(), mp); err != nil {
    67  			vec.Close()
    68  			return nil, err
    69  		} else {
    70  			vec.setDownstreamVector(v)
    71  		}
    72  	}
    73  	return vec, nil
    74  }
    75  
    76  // ### Get Functions
    77  
    78  // getNonNullValue Please don't merge it with GetValue(). Used in Vector for getting NonNullValue.
    79  func getNonNullValue(col *movec.Vector, row uint32) any {
    80  
    81  	switch col.GetType().Oid {
    82  	case types.T_bool:
    83  		return movec.GetFixedAt[bool](col, int(row))
    84  	case types.T_bit:
    85  		return movec.GetFixedAt[uint64](col, int(row))
    86  	case types.T_int8:
    87  		return movec.GetFixedAt[int8](col, int(row))
    88  	case types.T_int16:
    89  		return movec.GetFixedAt[int16](col, int(row))
    90  	case types.T_int32:
    91  		return movec.GetFixedAt[int32](col, int(row))
    92  	case types.T_int64:
    93  		return movec.GetFixedAt[int64](col, int(row))
    94  	case types.T_uint8:
    95  		return movec.GetFixedAt[uint8](col, int(row))
    96  	case types.T_uint16:
    97  		return movec.GetFixedAt[uint16](col, int(row))
    98  	case types.T_uint32:
    99  		return movec.GetFixedAt[uint32](col, int(row))
   100  	case types.T_uint64:
   101  		return movec.GetFixedAt[uint64](col, int(row))
   102  	case types.T_decimal64:
   103  		return movec.GetFixedAt[types.Decimal64](col, int(row))
   104  	case types.T_decimal128:
   105  		return movec.GetFixedAt[types.Decimal128](col, int(row))
   106  	case types.T_uuid:
   107  		return movec.GetFixedAt[types.Uuid](col, int(row))
   108  	case types.T_float32:
   109  		return movec.GetFixedAt[float32](col, int(row))
   110  	case types.T_float64:
   111  		return movec.GetFixedAt[float64](col, int(row))
   112  	case types.T_date:
   113  		return movec.GetFixedAt[types.Date](col, int(row))
   114  	case types.T_time:
   115  		return movec.GetFixedAt[types.Time](col, int(row))
   116  	case types.T_datetime:
   117  		return movec.GetFixedAt[types.Datetime](col, int(row))
   118  	case types.T_timestamp:
   119  		return movec.GetFixedAt[types.Timestamp](col, int(row))
   120  	case types.T_enum:
   121  		return movec.GetFixedAt[types.Enum](col, int(row))
   122  	case types.T_TS:
   123  		return movec.GetFixedAt[types.TS](col, int(row))
   124  	case types.T_Rowid:
   125  		return movec.GetFixedAt[types.Rowid](col, int(row))
   126  	case types.T_Blockid:
   127  		return movec.GetFixedAt[types.Blockid](col, int(row))
   128  	case types.T_char, types.T_varchar, types.T_binary, types.T_varbinary, types.T_json, types.T_blob, types.T_text,
   129  		types.T_array_float32, types.T_array_float64:
   130  		return col.GetBytesAt(int(row))
   131  	default:
   132  		//return vector.ErrVecTypeNotSupport
   133  		panic(any("No Support"))
   134  	}
   135  }
   136  
   137  // ### Update Function
   138  
   139  func GenericUpdateFixedValue[T types.FixedSizeT](
   140  	vec *movec.Vector, row uint32, v any, isNull bool, _ *mpool.MPool,
   141  ) {
   142  	if isNull {
   143  		nulls.Add(vec.GetNulls(), uint64(row))
   144  	} else {
   145  		err := movec.SetFixedAt(vec, int(row), v.(T))
   146  		if err != nil {
   147  			panic(err)
   148  		}
   149  		if vec.GetNulls().Contains(uint64(row)) {
   150  			vec.GetNulls().Unset(uint64(row))
   151  		}
   152  	}
   153  }
   154  
   155  func GenericUpdateBytes(
   156  	vec *movec.Vector, row uint32, v any, isNull bool, mp *mpool.MPool,
   157  ) {
   158  	if isNull {
   159  		nulls.Add(vec.GetNulls(), uint64(row))
   160  	} else {
   161  		err := movec.SetBytesAt(vec, int(row), v.([]byte), mp)
   162  		if err != nil {
   163  			panic(err)
   164  		}
   165  		if vec.GetNulls().Contains(uint64(row)) {
   166  			vec.GetNulls().Unset(uint64(row))
   167  		}
   168  	}
   169  }
   170  
   171  func UpdateValue(col *movec.Vector, row uint32, val any, isNull bool, mp *mpool.MPool) {
   172  	switch col.GetType().Oid {
   173  	case types.T_bool:
   174  		GenericUpdateFixedValue[bool](col, row, val, isNull, mp)
   175  	case types.T_bit:
   176  		GenericUpdateFixedValue[uint64](col, row, val, isNull, mp)
   177  	case types.T_int8:
   178  		GenericUpdateFixedValue[int8](col, row, val, isNull, mp)
   179  	case types.T_int16:
   180  		GenericUpdateFixedValue[int16](col, row, val, isNull, mp)
   181  	case types.T_int32:
   182  		GenericUpdateFixedValue[int32](col, row, val, isNull, mp)
   183  	case types.T_int64:
   184  		GenericUpdateFixedValue[int64](col, row, val, isNull, mp)
   185  	case types.T_uint8:
   186  		GenericUpdateFixedValue[uint8](col, row, val, isNull, mp)
   187  	case types.T_uint16:
   188  		GenericUpdateFixedValue[uint16](col, row, val, isNull, mp)
   189  	case types.T_uint32:
   190  		GenericUpdateFixedValue[uint32](col, row, val, isNull, mp)
   191  	case types.T_uint64:
   192  		GenericUpdateFixedValue[uint64](col, row, val, isNull, mp)
   193  	case types.T_decimal64:
   194  		GenericUpdateFixedValue[types.Decimal64](col, row, val, isNull, mp)
   195  	case types.T_decimal128:
   196  		GenericUpdateFixedValue[types.Decimal128](col, row, val, isNull, mp)
   197  	case types.T_float32:
   198  		GenericUpdateFixedValue[float32](col, row, val, isNull, mp)
   199  	case types.T_float64:
   200  		GenericUpdateFixedValue[float64](col, row, val, isNull, mp)
   201  	case types.T_date:
   202  		GenericUpdateFixedValue[types.Date](col, row, val, isNull, mp)
   203  	case types.T_time:
   204  		GenericUpdateFixedValue[types.Time](col, row, val, isNull, mp)
   205  	case types.T_datetime:
   206  		GenericUpdateFixedValue[types.Datetime](col, row, val, isNull, mp)
   207  	case types.T_timestamp:
   208  		GenericUpdateFixedValue[types.Timestamp](col, row, val, isNull, mp)
   209  	case types.T_enum:
   210  		GenericUpdateFixedValue[types.Enum](col, row, val, isNull, mp)
   211  	case types.T_uuid:
   212  		GenericUpdateFixedValue[types.Uuid](col, row, val, isNull, mp)
   213  	case types.T_TS:
   214  		GenericUpdateFixedValue[types.TS](col, row, val, isNull, mp)
   215  	case types.T_Rowid:
   216  		GenericUpdateFixedValue[types.Rowid](col, row, val, isNull, mp)
   217  	case types.T_Blockid:
   218  		GenericUpdateFixedValue[types.Blockid](col, row, val, isNull, mp)
   219  	case types.T_varchar, types.T_char, types.T_json,
   220  		types.T_binary, types.T_varbinary, types.T_blob, types.T_text,
   221  		types.T_array_float32, types.T_array_float64:
   222  		GenericUpdateBytes(col, row, val, isNull, mp)
   223  	default:
   224  		panic(moerr.NewInternalErrorNoCtx("%v not supported", col.GetType()))
   225  	}
   226  }
   227  
   228  // ### Only used in testcases
   229  
   230  func SplitBatch(bat *batch.Batch, cnt int) []*batch.Batch {
   231  	if cnt == 1 {
   232  		return []*batch.Batch{bat}
   233  	}
   234  	length := bat.Vecs[0].Length()
   235  	rows := length / cnt
   236  	if length%cnt == 0 {
   237  		bats := make([]*batch.Batch, 0, cnt)
   238  		for i := 0; i < cnt; i++ {
   239  			newBat := batch.New(true, bat.Attrs)
   240  			for j := 0; j < len(bat.Vecs); j++ {
   241  				window, _ := bat.Vecs[j].CloneWindow(i*rows, (i+1)*rows, nil)
   242  				newBat.Vecs[j] = window
   243  			}
   244  			bats = append(bats, newBat)
   245  		}
   246  		return bats
   247  	}
   248  	rowArray := make([]int, 0)
   249  	if length/cnt == 0 {
   250  		for i := 0; i < length; i++ {
   251  			rowArray = append(rowArray, 1)
   252  		}
   253  	} else {
   254  		left := length
   255  		for i := 0; i < cnt; i++ {
   256  			if left >= rows && i < cnt-1 {
   257  				rowArray = append(rowArray, rows)
   258  			} else {
   259  				rowArray = append(rowArray, left)
   260  			}
   261  			left -= rows
   262  		}
   263  	}
   264  	start := 0
   265  	bats := make([]*batch.Batch, 0, cnt)
   266  	for _, row := range rowArray {
   267  		newBat := batch.New(true, bat.Attrs)
   268  		for j := 0; j < len(bat.Vecs); j++ {
   269  			window, _ := bat.Vecs[j].CloneWindow(start, start+row, nil)
   270  			newBat.Vecs[j] = window
   271  		}
   272  		start += row
   273  		bats = append(bats, newBat)
   274  	}
   275  	return bats
   276  }
   277  
   278  func NewNonNullBatchWithSharedMemory(b *batch.Batch, mp *mpool.MPool) *Batch {
   279  	bat := NewBatch()
   280  	for i, attr := range b.Attrs {
   281  		v := ToTNVector(b.Vecs[i], mp)
   282  		bat.AddVector(attr, v)
   283  	}
   284  	return bat
   285  }
   286  
   287  func ForeachVector(vec Vector, op any, sel *nulls.Bitmap) (err error) {
   288  	return ForeachVectorWindow(vec, 0, vec.Length(), op, nil, sel)
   289  }
   290  
   291  func ForeachVectorWindow(
   292  	vec Vector,
   293  	start, length int,
   294  	op1 any,
   295  	op2 ItOp,
   296  	sel *nulls.Bitmap,
   297  ) (err error) {
   298  	typ := vec.GetType()
   299  	col := vec.GetDownstreamVector()
   300  	if typ.IsVarlen() {
   301  		var op func([]byte, bool, int) error
   302  		if op1 != nil {
   303  			op = op1.(func([]byte, bool, int) error)
   304  		}
   305  		return ForeachWindowVarlen(
   306  			col,
   307  			start,
   308  			length,
   309  			op,
   310  			op2,
   311  			sel)
   312  	}
   313  	switch typ.Oid {
   314  	case types.T_bool:
   315  		var op func(bool, bool, int) error
   316  		if op1 != nil {
   317  			op = op1.(func(bool, bool, int) error)
   318  		}
   319  		return ForeachWindowFixed(
   320  			col,
   321  			start,
   322  			length,
   323  			op,
   324  			op2,
   325  			sel)
   326  	case types.T_bit:
   327  		var op func(uint64, bool, int) error
   328  		if op1 != nil {
   329  			op = op1.(func(uint64, bool, int) error)
   330  		}
   331  		return ForeachWindowFixed(
   332  			col,
   333  			start,
   334  			length,
   335  			op,
   336  			op2,
   337  			sel)
   338  	case types.T_int8:
   339  		var op func(int8, bool, int) error
   340  		if op1 != nil {
   341  			op = op1.(func(int8, bool, int) error)
   342  		}
   343  		return ForeachWindowFixed(
   344  			col,
   345  			start,
   346  			length,
   347  			op,
   348  			op2,
   349  			sel)
   350  	case types.T_int16:
   351  		var op func(int16, bool, int) error
   352  		if op1 != nil {
   353  			op = op1.(func(int16, bool, int) error)
   354  		}
   355  		return ForeachWindowFixed(
   356  			col,
   357  			start,
   358  			length,
   359  			op,
   360  			op2,
   361  			sel)
   362  	case types.T_int32:
   363  		var op func(int32, bool, int) error
   364  		if op1 != nil {
   365  			op = op1.(func(int32, bool, int) error)
   366  		}
   367  		return ForeachWindowFixed(
   368  			col,
   369  			start,
   370  			length,
   371  			op,
   372  			op2,
   373  			sel)
   374  	case types.T_int64:
   375  		var op func(int64, bool, int) error
   376  		if op1 != nil {
   377  			op = op1.(func(int64, bool, int) error)
   378  		}
   379  		return ForeachWindowFixed(
   380  			col,
   381  			start,
   382  			length,
   383  			op,
   384  			op2,
   385  			sel)
   386  	case types.T_uint8:
   387  		var op func(uint8, bool, int) error
   388  		if op1 != nil {
   389  			op = op1.(func(uint8, bool, int) error)
   390  		}
   391  		return ForeachWindowFixed(
   392  			col,
   393  			start,
   394  			length,
   395  			op,
   396  			op2,
   397  			sel)
   398  	case types.T_uint16:
   399  		var op func(uint16, bool, int) error
   400  		if op1 != nil {
   401  			op = op1.(func(uint16, bool, int) error)
   402  		}
   403  		return ForeachWindowFixed(
   404  			col,
   405  			start,
   406  			length,
   407  			op,
   408  			op2,
   409  			sel)
   410  	case types.T_uint32:
   411  		var op func(uint32, bool, int) error
   412  		if op1 != nil {
   413  			op = op1.(func(uint32, bool, int) error)
   414  		}
   415  		return ForeachWindowFixed(
   416  			col,
   417  			start,
   418  			length,
   419  			op,
   420  			op2,
   421  			sel)
   422  	case types.T_uint64:
   423  		var op func(uint64, bool, int) error
   424  		if op1 != nil {
   425  			op = op1.(func(uint64, bool, int) error)
   426  		}
   427  		return ForeachWindowFixed(
   428  			col,
   429  			start,
   430  			length,
   431  			op,
   432  			op2,
   433  			sel)
   434  	case types.T_decimal64:
   435  		var op func(types.Decimal64, bool, int) error
   436  		if op1 != nil {
   437  			op = op1.(func(types.Decimal64, bool, int) error)
   438  		}
   439  		return ForeachWindowFixed(
   440  			col,
   441  			start,
   442  			length,
   443  			op,
   444  			op2,
   445  			sel)
   446  	case types.T_decimal128:
   447  		var op func(types.Decimal128, bool, int) error
   448  		if op1 != nil {
   449  			op = op1.(func(types.Decimal128, bool, int) error)
   450  		}
   451  		return ForeachWindowFixed(
   452  			col,
   453  			start,
   454  			length,
   455  			op,
   456  			op2,
   457  			sel)
   458  	case types.T_decimal256:
   459  		var op func(types.Decimal256, bool, int) error
   460  		if op1 != nil {
   461  			op = op1.(func(types.Decimal256, bool, int) error)
   462  		}
   463  		return ForeachWindowFixed(
   464  			col,
   465  			start,
   466  			length,
   467  			op,
   468  			op2,
   469  			sel)
   470  	case types.T_float32:
   471  		var op func(float32, bool, int) error
   472  		if op1 != nil {
   473  			op = op1.(func(float32, bool, int) error)
   474  		}
   475  		return ForeachWindowFixed(
   476  			col,
   477  			start,
   478  			length,
   479  			op,
   480  			op2,
   481  			sel)
   482  	case types.T_float64:
   483  		var op func(float64, bool, int) error
   484  		if op1 != nil {
   485  			op = op1.(func(float64, bool, int) error)
   486  		}
   487  		return ForeachWindowFixed(
   488  			col,
   489  			start,
   490  			length,
   491  			op,
   492  			op2,
   493  			sel)
   494  	case types.T_timestamp:
   495  		var op func(types.Timestamp, bool, int) error
   496  		if op1 != nil {
   497  			op = op1.(func(types.Timestamp, bool, int) error)
   498  		}
   499  		return ForeachWindowFixed(
   500  			col,
   501  			start,
   502  			length,
   503  			op,
   504  			op2,
   505  			sel)
   506  	case types.T_date:
   507  		var op func(types.Date, bool, int) error
   508  		if op1 != nil {
   509  			op = op1.(func(types.Date, bool, int) error)
   510  		}
   511  		return ForeachWindowFixed(
   512  			col,
   513  			start,
   514  			length,
   515  			op,
   516  			op2,
   517  			sel)
   518  	case types.T_time:
   519  		var op func(types.Time, bool, int) error
   520  		if op1 != nil {
   521  			op = op1.(func(types.Time, bool, int) error)
   522  		}
   523  		return ForeachWindowFixed(
   524  			col,
   525  			start,
   526  			length,
   527  			op,
   528  			op2,
   529  			sel)
   530  	case types.T_datetime:
   531  		var op func(types.Datetime, bool, int) error
   532  		if op1 != nil {
   533  			op = op1.(func(types.Datetime, bool, int) error)
   534  		}
   535  		return ForeachWindowFixed(
   536  			col,
   537  			start,
   538  			length,
   539  			op,
   540  			op2,
   541  			sel)
   542  	case types.T_enum:
   543  		var op func(types.Enum, bool, int) error
   544  		if op1 != nil {
   545  			op = op1.(func(types.Enum, bool, int) error)
   546  		}
   547  		return ForeachWindowFixed(
   548  			col,
   549  			start,
   550  			length,
   551  			op,
   552  			op2,
   553  			sel)
   554  	case types.T_TS:
   555  		var op func(types.TS, bool, int) error
   556  		if op1 != nil {
   557  			op = op1.(func(types.TS, bool, int) error)
   558  		}
   559  		return ForeachWindowFixed(
   560  			col,
   561  			start,
   562  			length,
   563  			op,
   564  			op2,
   565  			sel)
   566  	case types.T_Blockid:
   567  		var op func(types.Blockid, bool, int) error
   568  		if op1 != nil {
   569  			op = op1.(func(types.Blockid, bool, int) error)
   570  		}
   571  		return ForeachWindowFixed(
   572  			col,
   573  			start,
   574  			length,
   575  			op,
   576  			op2,
   577  			sel)
   578  	case types.T_uuid:
   579  		var op func(types.Uuid, bool, int) error
   580  		if op1 != nil {
   581  			op = op1.(func(types.Uuid, bool, int) error)
   582  		}
   583  		return ForeachWindowFixed(
   584  			col,
   585  			start,
   586  			length,
   587  			op,
   588  			op2,
   589  			sel)
   590  	case types.T_Rowid:
   591  		var op func(types.Rowid, bool, int) error
   592  		if op1 != nil {
   593  			op = op1.(func(types.Rowid, bool, int) error)
   594  		}
   595  		return ForeachWindowFixed(
   596  			col,
   597  			start,
   598  			length,
   599  			op,
   600  			op2,
   601  			sel)
   602  	default:
   603  		panic(fmt.Sprintf("unsupported type: %s", typ.String()))
   604  	}
   605  }
   606  
   607  func ForeachWindowBytes(
   608  	vec *movec.Vector,
   609  	start, length int,
   610  	op ItOpT[[]byte],
   611  	sels *nulls.Bitmap,
   612  ) (err error) {
   613  	typ := vec.GetType()
   614  	if typ.IsVarlen() {
   615  		return ForeachWindowVarlen(vec, start, length, op, nil, sels)
   616  	}
   617  	tsize := typ.TypeSize()
   618  	data := vec.UnsafeGetRawData()[start*tsize : (start+length)*tsize]
   619  	if sels.IsEmpty() {
   620  		for i := 0; i < length; i++ {
   621  			if err = op(data[i*tsize:(i+1)*tsize], vec.IsNull(uint64(i+start)), i+start); err != nil {
   622  				break
   623  			}
   624  		}
   625  	} else {
   626  		end := start + length
   627  		it := sels.GetBitmap().Iterator()
   628  		for it.HasNext() {
   629  			idx := uint32(it.Next())
   630  			if int(idx) < start {
   631  				continue
   632  			} else if int(idx) >= end {
   633  				break
   634  			}
   635  			i := int(idx)
   636  			if err = op(data[i*tsize:(i+1)*tsize], vec.IsNull(uint64(i)), i); err != nil {
   637  				break
   638  			}
   639  		}
   640  
   641  	}
   642  	return
   643  }
   644  
   645  func ForeachWindowFixed[T any](
   646  	vec *movec.Vector,
   647  	start, length int,
   648  	op ItOpT[T],
   649  	opAny ItOp,
   650  	sels *nulls.Bitmap,
   651  ) (err error) {
   652  	if vec.IsConst() {
   653  		var v T
   654  		isnull := false
   655  		if vec.IsConstNull() {
   656  			isnull = true
   657  		} else {
   658  			v = movec.GetFixedAt[T](vec, 0)
   659  		}
   660  		if sels.IsEmpty() {
   661  			for i := 0; i < length; i++ {
   662  				if op != nil {
   663  					if err = op(v, isnull, i+start); err != nil {
   664  						break
   665  					}
   666  				}
   667  				if opAny != nil {
   668  					if err = opAny(v, isnull, i+start); err != nil {
   669  						break
   670  					}
   671  				}
   672  			}
   673  		} else {
   674  			end := start + length
   675  			it := sels.GetBitmap().Iterator()
   676  			for it.HasNext() {
   677  				idx := uint32(it.Next())
   678  				if int(idx) < start {
   679  					continue
   680  				} else if int(idx) >= end {
   681  					break
   682  				}
   683  				if op != nil {
   684  					if err = op(v, isnull, int(idx)); err != nil {
   685  						break
   686  					}
   687  				}
   688  				if opAny != nil {
   689  					if err = opAny(v, isnull, int(idx)); err != nil {
   690  						break
   691  					}
   692  				}
   693  			}
   694  		}
   695  
   696  		return
   697  	}
   698  	slice := movec.MustFixedCol[T](vec)[start : start+length]
   699  	if sels.IsEmpty() {
   700  		for i, v := range slice {
   701  			if op != nil {
   702  				if err = op(v, vec.IsNull(uint64(i+start)), i+start); err != nil {
   703  					break
   704  				}
   705  			}
   706  			if opAny != nil {
   707  				if err = opAny(v, vec.IsNull(uint64(i+start)), i+start); err != nil {
   708  					break
   709  				}
   710  			}
   711  		}
   712  	} else {
   713  		end := start + length
   714  		it := sels.GetBitmap().Iterator()
   715  		for it.HasNext() {
   716  			idx := uint32(it.Next())
   717  			if int(idx) < start {
   718  				continue
   719  			} else if int(idx) >= end {
   720  				break
   721  			}
   722  			v := slice[int(idx)-start]
   723  			if op != nil {
   724  				if err = op(v, vec.IsNull(uint64(idx)), int(idx)); err != nil {
   725  					break
   726  				}
   727  			}
   728  			if opAny != nil {
   729  				if err = opAny(v, vec.IsNull(uint64(idx)), int(idx)); err != nil {
   730  					break
   731  				}
   732  			}
   733  		}
   734  	}
   735  	return
   736  }
   737  
   738  func ForeachWindowVarlen(
   739  	vec *movec.Vector,
   740  	start, length int,
   741  	op ItOpT[[]byte],
   742  	opAny ItOp,
   743  	sels *nulls.Bitmap,
   744  ) (err error) {
   745  	if vec.IsConst() {
   746  		var v []byte
   747  		isnull := false
   748  		if vec.IsConstNull() {
   749  			isnull = true
   750  		} else {
   751  			v = vec.GetBytesAt(0)
   752  		}
   753  		for i := 0; i < length; i++ {
   754  			if op != nil {
   755  				if err = op(v, isnull, i+start); err != nil {
   756  					break
   757  				}
   758  			}
   759  			if opAny != nil {
   760  				if err = opAny(v, isnull, i+start); err != nil {
   761  					break
   762  				}
   763  			}
   764  		}
   765  		return
   766  	}
   767  	slice, area := movec.MustVarlenaRawData(vec)
   768  	slice = slice[start : start+length]
   769  	if sels.IsEmpty() {
   770  		for i, v := range slice {
   771  			if op != nil {
   772  				if err = op(v.GetByteSlice(area), vec.IsNull(uint64(i+start)), i+start); err != nil {
   773  					break
   774  				}
   775  			}
   776  			if opAny != nil {
   777  				if err = opAny(v.GetByteSlice(area), vec.IsNull(uint64(i+start)), i+start); err != nil {
   778  					break
   779  				}
   780  			}
   781  		}
   782  	} else {
   783  		end := start + length
   784  		it := sels.GetBitmap().Iterator()
   785  		for it.HasNext() {
   786  			idx := uint32(it.Next())
   787  			if int(idx) < start {
   788  				continue
   789  			} else if int(idx) >= end {
   790  				break
   791  			}
   792  			v := slice[int(idx)-start]
   793  			if op != nil {
   794  				if err = op(v.GetByteSlice(area), vec.IsNull(uint64(idx)), int(idx)); err != nil {
   795  					break
   796  				}
   797  			}
   798  			if opAny != nil {
   799  				if err = opAny(v.GetByteSlice(area), vec.IsNull(uint64(idx)), int(idx)); err != nil {
   800  					break
   801  				}
   802  			}
   803  		}
   804  	}
   805  	return
   806  }
   807  
   808  func MakeForeachVectorOp(t types.T, overloads map[types.T]any, args ...any) any {
   809  	if t.FixedLength() < 0 {
   810  		overload := overloads[t].(func(...any) func([]byte, bool, int) error)
   811  		return overload(args...)
   812  	}
   813  	switch t {
   814  	case types.T_bool:
   815  		overload := overloads[t].(func(...any) func(bool, bool, int) error)
   816  		return overload(args...)
   817  	case types.T_int8:
   818  		overload := overloads[t].(func(...any) func(int8, bool, int) error)
   819  		return overload(args...)
   820  	case types.T_int16:
   821  		overload := overloads[t].(func(...any) func(int16, bool, int) error)
   822  		return overload(args...)
   823  	case types.T_int32:
   824  		overload := overloads[t].(func(...any) func(int32, bool, int) error)
   825  		return overload(args...)
   826  	case types.T_int64:
   827  		overload := overloads[t].(func(...any) func(int64, bool, int) error)
   828  		return overload(args...)
   829  	case types.T_uint8:
   830  		overload := overloads[t].(func(...any) func(uint8, bool, int) error)
   831  		return overload(args...)
   832  	case types.T_uint16:
   833  		overload := overloads[t].(func(...any) func(uint16, bool, int) error)
   834  		return overload(args...)
   835  	case types.T_uint32:
   836  		overload := overloads[t].(func(...any) func(uint32, bool, int) error)
   837  		return overload(args...)
   838  	case types.T_uint64:
   839  		overload := overloads[t].(func(...any) func(uint64, bool, int) error)
   840  		return overload(args...)
   841  	case types.T_float32:
   842  		overload := overloads[t].(func(...any) func(float32, bool, int) error)
   843  		return overload(args...)
   844  	case types.T_float64:
   845  		overload := overloads[t].(func(...any) func(float64, bool, int) error)
   846  		return overload(args...)
   847  	case types.T_decimal64:
   848  		overload := overloads[t].(func(...any) func(types.Decimal64, bool, int) error)
   849  		return overload(args...)
   850  	case types.T_decimal128:
   851  		overload := overloads[t].(func(...any) func(types.Decimal128, bool, int) error)
   852  		return overload(args...)
   853  	case types.T_decimal256:
   854  		overload := overloads[t].(func(...any) func(types.Decimal256, bool, int) error)
   855  		return overload(args...)
   856  	case types.T_timestamp:
   857  		overload := overloads[t].(func(...any) func(types.Timestamp, bool, int) error)
   858  		return overload(args...)
   859  	case types.T_time:
   860  		overload := overloads[t].(func(...any) func(types.Time, bool, int) error)
   861  		return overload(args...)
   862  	case types.T_date:
   863  		overload := overloads[t].(func(...any) func(types.Date, bool, int) error)
   864  		return overload(args...)
   865  	case types.T_datetime:
   866  		overload := overloads[t].(func(...any) func(types.Datetime, bool, int) error)
   867  		return overload(args...)
   868  	case types.T_enum:
   869  		overload := overloads[t].(func(...any) func(types.Enum, bool, int) error)
   870  		return overload(args...)
   871  	case types.T_TS:
   872  		overload := overloads[t].(func(...any) func(types.TS, bool, int) error)
   873  		return overload(args...)
   874  	case types.T_Rowid:
   875  		overload := overloads[t].(func(...any) func(types.Rowid, bool, int) error)
   876  		return overload(args...)
   877  	case types.T_Blockid:
   878  		overload := overloads[t].(func(...any) func(types.Blockid, bool, int) error)
   879  		return overload(args...)
   880  	case types.T_uuid:
   881  		overload := overloads[t].(func(...any) func(types.Uuid, bool, int) error)
   882  		return overload(args...)
   883  	}
   884  	panic(fmt.Sprintf("unsupported type: %s", t.String()))
   885  }