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

     1  // Copyright 2021 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package util
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/google/uuid"
    21  	"github.com/matrixorigin/matrixone/pkg/catalog"
    22  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    23  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    24  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    25  	"github.com/matrixorigin/matrixone/pkg/container/types"
    26  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    27  	"github.com/matrixorigin/matrixone/pkg/sql/plan/function"
    28  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    29  )
    30  
    31  var SerialWithCompacted = serialWithCompacted
    32  var SerialWithoutCompacted = serialWithoutCompacted
    33  var CompactSingleIndexCol = compactSingleIndexCol
    34  var CompactPrimaryCol = compactPrimaryCol
    35  
    36  type PackerList struct {
    37  	ps []*types.Packer
    38  }
    39  
    40  func (list *PackerList) Free() {
    41  	for _, p := range list.ps {
    42  		if p != nil {
    43  			p.FreeMem()
    44  		}
    45  	}
    46  }
    47  
    48  func BuildIndexTableName(ctx context.Context, unique bool) (string, error) {
    49  	var name string
    50  	if unique {
    51  		name = catalog.UniqueIndexTableNamePrefix
    52  	} else {
    53  		name = catalog.SecondaryIndexTableNamePrefix
    54  	}
    55  	id, err := uuid.NewV7()
    56  	if err != nil {
    57  		return "", moerr.NewInternalError(ctx, "newuuid failed")
    58  	}
    59  	name += id.String()
    60  	return name, nil
    61  }
    62  
    63  // BuildUniqueKeyBatch used in test to validate
    64  // serialWithCompacted(), compactSingleIndexCol() and compactPrimaryCol()
    65  func BuildUniqueKeyBatch(vecs []*vector.Vector, attrs []string, parts []string, originTablePrimaryKey string, proc *process.Process, packers *PackerList) (*batch.Batch, int, error) {
    66  	var b *batch.Batch
    67  	var err error
    68  
    69  	if originTablePrimaryKey == "" {
    70  		b = &batch.Batch{
    71  			Attrs: make([]string, 1),
    72  			Vecs:  make([]*vector.Vector, 1),
    73  			Cnt:   1,
    74  		}
    75  		b.Attrs[0] = catalog.IndexTableIndexColName
    76  	} else {
    77  		b = &batch.Batch{
    78  			Attrs: make([]string, 2),
    79  			Vecs:  make([]*vector.Vector, 2),
    80  			Cnt:   1,
    81  		}
    82  		b.Attrs[0] = catalog.IndexTableIndexColName
    83  		b.Attrs[1] = catalog.IndexTablePrimaryColName
    84  	}
    85  	isCompoundIndex := false
    86  	if len(parts) > 1 {
    87  		isCompoundIndex = true
    88  	}
    89  	//bitMap := new(nulls.Nulls)
    90  	var bitMap *nulls.Nulls
    91  	if isCompoundIndex {
    92  		cIndexVecMap := make(map[string]*vector.Vector)
    93  		for num, attrName := range attrs {
    94  			for _, name := range parts {
    95  				if attrName == name {
    96  					cIndexVecMap[name] = vecs[num]
    97  				}
    98  			}
    99  		}
   100  		vs := make([]*vector.Vector, 0)
   101  		for _, part := range parts {
   102  			v := cIndexVecMap[part]
   103  			vs = append(vs, v)
   104  		}
   105  		b.Vecs[0], bitMap, err = serialWithCompacted(vs, proc, packers)
   106  	} else {
   107  		var vec *vector.Vector
   108  		for i, name := range attrs {
   109  			if parts[0] == name {
   110  				vec = vecs[i]
   111  				break
   112  			}
   113  		}
   114  		b.Vecs[0], bitMap, err = compactSingleIndexCol(vec, proc)
   115  	}
   116  
   117  	if len(b.Attrs) > 1 {
   118  		var vec *vector.Vector
   119  		for i, name := range attrs {
   120  			if originTablePrimaryKey == name {
   121  				vec = vecs[i]
   122  			}
   123  		}
   124  		b.Vecs[1], err = compactPrimaryCol(vec, bitMap, proc)
   125  	}
   126  
   127  	if err != nil {
   128  		b.Clean(proc.Mp())
   129  		return nil, -1, err
   130  	}
   131  	b.SetRowCount(b.Vecs[0].Length())
   132  	return b, b.RowCount(), nil
   133  }
   134  
   135  // SerialWithCompacted have a similar function named Serial
   136  // SerialWithCompacted function is used by BuildUniqueKeyBatch
   137  // when vs have null value, the function will ignore the row in
   138  // the vs
   139  // for example:
   140  // input vec is [[1, 1, 1], [2, 2, null], [3, 3, 3]]
   141  // result vec is [serial(1, 2, 3), serial(1, 2, 3)]
   142  // result bitmap is [2]
   143  func serialWithCompacted(vs []*vector.Vector, proc *process.Process, packers *PackerList) (*vector.Vector, *nulls.Nulls, error) {
   144  	// resolve vs
   145  	length := vs[0].Length()
   146  	vct := types.T_varchar.ToType()
   147  	val := make([][]byte, 0, length)
   148  	if length > cap(packers.ps) {
   149  		for _, p := range packers.ps {
   150  			if p != nil {
   151  				p.FreeMem()
   152  			}
   153  		}
   154  		packers.ps = types.NewPackerArray(length, proc.Mp())
   155  	}
   156  	defer func() {
   157  		for i := 0; i < length; i++ {
   158  			packers.ps[i].Reset()
   159  		}
   160  	}()
   161  	bitMap := new(nulls.Nulls)
   162  
   163  	ps := packers.ps
   164  	for _, v := range vs {
   165  		vNull := v.GetNulls()
   166  		hasNull := v.HasNull()
   167  		switch v.GetType().Oid {
   168  		case types.T_bool:
   169  			s := vector.MustFixedCol[bool](v)
   170  			if hasNull {
   171  				for i, b := range s {
   172  					if nulls.Contains(vNull, uint64(i)) {
   173  						nulls.Add(bitMap, uint64(i))
   174  					} else {
   175  						ps[i].EncodeBool(b)
   176  					}
   177  				}
   178  			} else {
   179  				for i, b := range s {
   180  					ps[i].EncodeBool(b)
   181  				}
   182  			}
   183  		case types.T_bit:
   184  			s := vector.MustFixedCol[uint64](v)
   185  			for i, b := range s {
   186  				if nulls.Contains(v.GetNulls(), uint64(i)) {
   187  					nulls.Add(bitMap, uint64(i))
   188  				} else {
   189  					ps[i].EncodeUint64(b)
   190  				}
   191  			}
   192  		case types.T_int8:
   193  			s := vector.MustFixedCol[int8](v)
   194  			if hasNull {
   195  				for i, b := range s {
   196  					if nulls.Contains(vNull, uint64(i)) {
   197  						nulls.Add(bitMap, uint64(i))
   198  					} else {
   199  						ps[i].EncodeInt8(b)
   200  					}
   201  				}
   202  			} else {
   203  				for i, b := range s {
   204  					ps[i].EncodeInt8(b)
   205  				}
   206  			}
   207  		case types.T_int16:
   208  			s := vector.MustFixedCol[int16](v)
   209  			if hasNull {
   210  				for i, b := range s {
   211  					if nulls.Contains(vNull, uint64(i)) {
   212  						nulls.Add(bitMap, uint64(i))
   213  					} else {
   214  						ps[i].EncodeInt16(b)
   215  					}
   216  				}
   217  			} else {
   218  				for i, b := range s {
   219  					ps[i].EncodeInt16(b)
   220  				}
   221  			}
   222  		case types.T_int32:
   223  			s := vector.MustFixedCol[int32](v)
   224  			if hasNull {
   225  				for i, b := range s {
   226  					if nulls.Contains(vNull, uint64(i)) {
   227  						nulls.Add(bitMap, uint64(i))
   228  					} else {
   229  						ps[i].EncodeInt32(b)
   230  					}
   231  				}
   232  			} else {
   233  				for i, b := range s {
   234  					ps[i].EncodeInt32(b)
   235  				}
   236  			}
   237  		case types.T_int64:
   238  			s := vector.MustFixedCol[int64](v)
   239  			if hasNull {
   240  				for i, b := range s {
   241  					if nulls.Contains(vNull, uint64(i)) {
   242  						nulls.Add(bitMap, uint64(i))
   243  					} else {
   244  						ps[i].EncodeInt64(b)
   245  					}
   246  				}
   247  			} else {
   248  				for i, b := range s {
   249  					ps[i].EncodeInt64(b)
   250  				}
   251  			}
   252  		case types.T_uint8:
   253  			s := vector.MustFixedCol[uint8](v)
   254  			if hasNull {
   255  				for i, b := range s {
   256  					if nulls.Contains(vNull, uint64(i)) {
   257  						nulls.Add(bitMap, uint64(i))
   258  					} else {
   259  						ps[i].EncodeUint8(b)
   260  					}
   261  				}
   262  			} else {
   263  				for i, b := range s {
   264  					ps[i].EncodeUint8(b)
   265  				}
   266  			}
   267  		case types.T_uint16:
   268  			s := vector.MustFixedCol[uint16](v)
   269  			if hasNull {
   270  				for i, b := range s {
   271  					if nulls.Contains(vNull, uint64(i)) {
   272  						nulls.Add(bitMap, uint64(i))
   273  					} else {
   274  						ps[i].EncodeUint16(b)
   275  					}
   276  				}
   277  			} else {
   278  				for i, b := range s {
   279  					ps[i].EncodeUint16(b)
   280  				}
   281  			}
   282  		case types.T_uint32:
   283  			s := vector.MustFixedCol[uint32](v)
   284  			if hasNull {
   285  				for i, b := range s {
   286  					if nulls.Contains(vNull, uint64(i)) {
   287  						nulls.Add(bitMap, uint64(i))
   288  					} else {
   289  						ps[i].EncodeUint32(b)
   290  					}
   291  				}
   292  			} else {
   293  				for i, b := range s {
   294  					ps[i].EncodeUint32(b)
   295  				}
   296  			}
   297  		case types.T_uint64:
   298  			s := vector.MustFixedCol[uint64](v)
   299  			if hasNull {
   300  				for i, b := range s {
   301  					if nulls.Contains(vNull, uint64(i)) {
   302  						nulls.Add(bitMap, uint64(i))
   303  					} else {
   304  						ps[i].EncodeUint64(b)
   305  					}
   306  				}
   307  			} else {
   308  				for i, b := range s {
   309  					ps[i].EncodeUint64(b)
   310  				}
   311  			}
   312  		case types.T_float32:
   313  			s := vector.MustFixedCol[float32](v)
   314  			if hasNull {
   315  				for i, b := range s {
   316  					if nulls.Contains(vNull, uint64(i)) {
   317  						nulls.Add(bitMap, uint64(i))
   318  					} else {
   319  						ps[i].EncodeFloat32(b)
   320  					}
   321  				}
   322  			} else {
   323  				for i, b := range s {
   324  					ps[i].EncodeFloat32(b)
   325  				}
   326  			}
   327  		case types.T_float64:
   328  			s := vector.MustFixedCol[float64](v)
   329  			if hasNull {
   330  				for i, b := range s {
   331  					if nulls.Contains(vNull, uint64(i)) {
   332  						nulls.Add(bitMap, uint64(i))
   333  					} else {
   334  						ps[i].EncodeFloat64(b)
   335  					}
   336  				}
   337  			} else {
   338  				for i, b := range s {
   339  					ps[i].EncodeFloat64(b)
   340  				}
   341  			}
   342  		case types.T_date:
   343  			s := vector.MustFixedCol[types.Date](v)
   344  			if hasNull {
   345  				for i, b := range s {
   346  					if nulls.Contains(vNull, uint64(i)) {
   347  						nulls.Add(bitMap, uint64(i))
   348  					} else {
   349  						ps[i].EncodeDate(b)
   350  					}
   351  				}
   352  			} else {
   353  				for i, b := range s {
   354  					ps[i].EncodeDate(b)
   355  				}
   356  			}
   357  		case types.T_time:
   358  			s := vector.MustFixedCol[types.Time](v)
   359  			if hasNull {
   360  				for i, b := range s {
   361  					if nulls.Contains(vNull, uint64(i)) {
   362  						nulls.Add(bitMap, uint64(i))
   363  					} else {
   364  						ps[i].EncodeTime(b)
   365  					}
   366  				}
   367  			} else {
   368  				for i, b := range s {
   369  					ps[i].EncodeTime(b)
   370  				}
   371  			}
   372  		case types.T_datetime:
   373  			s := vector.MustFixedCol[types.Datetime](v)
   374  			if hasNull {
   375  				for i, b := range s {
   376  					if nulls.Contains(vNull, uint64(i)) {
   377  						nulls.Add(bitMap, uint64(i))
   378  					} else {
   379  						ps[i].EncodeDatetime(b)
   380  					}
   381  				}
   382  			} else {
   383  				for i, b := range s {
   384  					ps[i].EncodeDatetime(b)
   385  				}
   386  			}
   387  		case types.T_timestamp:
   388  			s := vector.MustFixedCol[types.Timestamp](v)
   389  			if hasNull {
   390  				for i, b := range s {
   391  					if nulls.Contains(vNull, uint64(i)) {
   392  						nulls.Add(bitMap, uint64(i))
   393  					} else {
   394  						ps[i].EncodeTimestamp(b)
   395  					}
   396  				}
   397  			} else {
   398  				for i, b := range s {
   399  					ps[i].EncodeTimestamp(b)
   400  				}
   401  			}
   402  		case types.T_enum:
   403  			s := vector.MustFixedCol[types.Enum](v)
   404  			if hasNull {
   405  				for i, b := range s {
   406  					if nulls.Contains(vNull, uint64(i)) {
   407  						nulls.Add(bitMap, uint64(i))
   408  					} else {
   409  						ps[i].EncodeEnum(b)
   410  					}
   411  				}
   412  			} else {
   413  				for i, b := range s {
   414  					ps[i].EncodeEnum(b)
   415  				}
   416  			}
   417  		case types.T_decimal64:
   418  			s := vector.MustFixedCol[types.Decimal64](v)
   419  			if hasNull {
   420  				for i, b := range s {
   421  					if nulls.Contains(vNull, uint64(i)) {
   422  						nulls.Add(bitMap, uint64(i))
   423  					} else {
   424  						ps[i].EncodeDecimal64(b)
   425  					}
   426  				}
   427  			} else {
   428  				for i, b := range s {
   429  					ps[i].EncodeDecimal64(b)
   430  				}
   431  			}
   432  		case types.T_decimal128:
   433  			s := vector.MustFixedCol[types.Decimal128](v)
   434  			if hasNull {
   435  				for i, b := range s {
   436  					if nulls.Contains(vNull, uint64(i)) {
   437  						nulls.Add(bitMap, uint64(i))
   438  					} else {
   439  						ps[i].EncodeDecimal128(b)
   440  					}
   441  				}
   442  			} else {
   443  				for i, b := range s {
   444  					ps[i].EncodeDecimal128(b)
   445  				}
   446  			}
   447  		case types.T_json, types.T_char, types.T_varchar, types.T_binary, types.T_varbinary, types.T_blob, types.T_text,
   448  			types.T_array_float32, types.T_array_float64:
   449  			// NOTE 1: We will consider T_array as bytes here just like JSON, VARBINARY and BLOB.
   450  			// If not, we need to define arrayType in types/tuple.go as arrayF32TypeCode, arrayF64TypeCode etc
   451  			// NOTE 2: vs is []string and not []byte. vs[i] is not of form "[1,2,3]". It is binary string of []float32{1,2,3}
   452  			// NOTE 3: This class is mainly used by PreInsertUnique which gets triggered before inserting into column having
   453  			// Unique Key or Primary Key constraint. Vector cannot be UK or PK.
   454  			vs := vector.MustStrCol(v)
   455  			if hasNull {
   456  				for i := range vs {
   457  					if nulls.Contains(vNull, uint64(i)) {
   458  						nulls.Add(bitMap, uint64(i))
   459  					} else {
   460  						ps[i].EncodeStringType([]byte(vs[i]))
   461  					}
   462  				}
   463  			} else {
   464  				for i := range vs {
   465  					ps[i].EncodeStringType([]byte(vs[i]))
   466  				}
   467  			}
   468  		}
   469  	}
   470  
   471  	for i := 0; i < length; i++ {
   472  		if !nulls.Contains(bitMap, uint64(i)) {
   473  			val = append(val, ps[i].GetBuf())
   474  		}
   475  	}
   476  
   477  	vec := proc.GetVector(vct)
   478  	err := vector.AppendBytesList(vec, val, nil, proc.Mp())
   479  
   480  	return vec, bitMap, err
   481  }
   482  
   483  // serialWithoutCompacted is similar to serialWithCompacted and builtInSerial
   484  // serialWithoutCompacted function is used by Secondary Index to support rows containing null entries
   485  // for example:
   486  // input vec is [[1, 1, 1], [2, 2, null], [3, 3, 3]]
   487  // result vec is [serial(1, 2, 3), serial(1, 2, null), serial(1, 2, 3)]
   488  // result bitmap is [] (empty)
   489  // Here we are keeping the same function signature of serialWithCompacted so that we can duplicate the same code of
   490  // `preinsertunique` in `preinsertsecondaryindex`
   491  func serialWithoutCompacted(vs []*vector.Vector, proc *process.Process, packers *PackerList) (*vector.Vector, *nulls.Nulls, error) {
   492  	if len(vs) == 0 {
   493  		// return empty vector and empty bitmap
   494  		return proc.GetVector(types.T_varchar.ToType()), new(nulls.Nulls), nil
   495  	}
   496  
   497  	rowCount := vs[0].Length()
   498  	if rowCount > cap(packers.ps) {
   499  		for _, p := range packers.ps {
   500  			if p != nil {
   501  				p.FreeMem()
   502  			}
   503  		}
   504  		packers.ps = types.NewPackerArray(rowCount, proc.Mp())
   505  	}
   506  	defer func() {
   507  		for i := 0; i < rowCount; i++ {
   508  			packers.ps[i].Reset()
   509  		}
   510  	}()
   511  
   512  	ps := packers.ps
   513  	for _, v := range vs {
   514  		if v.IsConstNull() {
   515  			for i := 0; i < v.Length(); i++ {
   516  				ps[i].EncodeNull()
   517  			}
   518  			continue
   519  		}
   520  		function.SerialHelper(v, nil, ps, true)
   521  	}
   522  
   523  	vec := proc.GetVector(types.T_varchar.ToType())
   524  	for i := 0; i < rowCount; i++ {
   525  		if err := vector.AppendBytes(vec, ps[i].GetBuf(), false, proc.Mp()); err != nil {
   526  			proc.PutVector(vec)
   527  			return nil, nil, err
   528  		}
   529  	}
   530  
   531  	return vec, new(nulls.Nulls), nil
   532  }
   533  
   534  func compactSingleIndexCol(v *vector.Vector, proc *process.Process) (*vector.Vector, *nulls.Nulls, error) {
   535  	vec := proc.GetVector(*v.GetType())
   536  	var err error
   537  	defer func() {
   538  		if err != nil {
   539  			vec.Free(proc.GetMPool())
   540  		}
   541  	}()
   542  
   543  	hasNull := v.HasNull()
   544  	if !hasNull {
   545  		err = vector.GetUnionAllFunction(*v.GetType(), proc.GetMPool())(vec, v)
   546  		return vec, v.GetNulls(), err
   547  	}
   548  	length := v.Length()
   549  	switch v.GetType().Oid {
   550  	case types.T_bool:
   551  		s := vector.MustFixedCol[bool](v)
   552  		ns := make([]bool, 0, length)
   553  		for i, b := range s {
   554  			if !nulls.Contains(v.GetNulls(), uint64(i)) {
   555  				ns = append(ns, b)
   556  			}
   557  		}
   558  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   559  	case types.T_bit:
   560  		s := vector.MustFixedCol[uint64](v)
   561  		ns := make([]uint64, 0, len(s))
   562  		for i, b := range s {
   563  			if !nulls.Contains(v.GetNulls(), uint64(i)) {
   564  				ns = append(ns, b)
   565  			}
   566  		}
   567  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   568  	case types.T_int8:
   569  		s := vector.MustFixedCol[int8](v)
   570  		ns := make([]int8, 0, len(s))
   571  		for i, b := range s {
   572  			if !nulls.Contains(v.GetNulls(), uint64(i)) {
   573  				ns = append(ns, b)
   574  			}
   575  		}
   576  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   577  	case types.T_int16:
   578  		s := vector.MustFixedCol[int16](v)
   579  		ns := make([]int16, 0, len(s))
   580  		for i, b := range s {
   581  			if !nulls.Contains(v.GetNulls(), uint64(i)) {
   582  				ns = append(ns, b)
   583  			}
   584  		}
   585  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   586  	case types.T_int32:
   587  		s := vector.MustFixedCol[int32](v)
   588  		ns := make([]int32, 0, len(s))
   589  		for i, b := range s {
   590  			if !nulls.Contains(v.GetNulls(), uint64(i)) {
   591  				ns = append(ns, b)
   592  			}
   593  		}
   594  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   595  	case types.T_int64:
   596  		s := vector.MustFixedCol[int64](v)
   597  		ns := make([]int64, 0, len(s))
   598  		for i, b := range s {
   599  			if !nulls.Contains(v.GetNulls(), uint64(i)) {
   600  				ns = append(ns, b)
   601  			}
   602  		}
   603  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   604  	case types.T_uint8:
   605  		s := vector.MustFixedCol[uint8](v)
   606  		ns := make([]uint8, 0, len(s))
   607  		for i, b := range s {
   608  			if !nulls.Contains(v.GetNulls(), uint64(i)) {
   609  				ns = append(ns, b)
   610  			}
   611  		}
   612  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   613  	case types.T_uint16:
   614  		s := vector.MustFixedCol[uint16](v)
   615  		ns := make([]uint16, 0, len(s))
   616  		for i, b := range s {
   617  			if !nulls.Contains(v.GetNulls(), uint64(i)) {
   618  				ns = append(ns, b)
   619  			}
   620  		}
   621  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   622  	case types.T_uint32:
   623  		s := vector.MustFixedCol[uint32](v)
   624  		ns := make([]uint32, 0, len(s))
   625  		for i, b := range s {
   626  			if !nulls.Contains(v.GetNulls(), uint64(i)) {
   627  				ns = append(ns, b)
   628  			}
   629  		}
   630  		vec = vector.NewVec(*v.GetType())
   631  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   632  	case types.T_uint64:
   633  		s := vector.MustFixedCol[uint64](v)
   634  		ns := make([]uint64, 0, len(s))
   635  		for i, b := range s {
   636  			if !nulls.Contains(v.GetNulls(), uint64(i)) {
   637  				ns = append(ns, b)
   638  			}
   639  		}
   640  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   641  	case types.T_float32:
   642  		s := vector.MustFixedCol[float32](v)
   643  		ns := make([]float32, 0, len(s))
   644  		for i, b := range s {
   645  			if !nulls.Contains(v.GetNulls(), uint64(i)) {
   646  				ns = append(ns, b)
   647  			}
   648  		}
   649  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   650  	case types.T_float64:
   651  		s := vector.MustFixedCol[float64](v)
   652  		ns := make([]float64, 0, len(s))
   653  		for i, b := range s {
   654  			if !nulls.Contains(v.GetNulls(), uint64(i)) {
   655  				ns = append(ns, b)
   656  			}
   657  		}
   658  		vec = vector.NewVec(*v.GetType())
   659  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   660  	case types.T_date:
   661  		s := vector.MustFixedCol[types.Date](v)
   662  		ns := make([]types.Date, 0, len(s))
   663  		for i, b := range s {
   664  			if !nulls.Contains(v.GetNulls(), uint64(i)) {
   665  				ns = append(ns, b)
   666  			}
   667  		}
   668  		vec = vector.NewVec(*v.GetType())
   669  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   670  	case types.T_time:
   671  		s := vector.MustFixedCol[types.Time](v)
   672  		ns := make([]types.Time, 0, len(s))
   673  		for i, b := range s {
   674  			if !nulls.Contains(v.GetNulls(), uint64(i)) {
   675  				ns = append(ns, b)
   676  			}
   677  		}
   678  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   679  	case types.T_datetime:
   680  		s := vector.MustFixedCol[types.Datetime](v)
   681  		ns := make([]types.Datetime, 0, len(s))
   682  		for i, b := range s {
   683  			if !nulls.Contains(v.GetNulls(), uint64(i)) {
   684  				ns = append(ns, b)
   685  			}
   686  		}
   687  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   688  	case types.T_timestamp:
   689  		s := vector.MustFixedCol[types.Timestamp](v)
   690  		ns := make([]types.Timestamp, 0, len(s))
   691  		for i, b := range s {
   692  			if !nulls.Contains(v.GetNulls(), uint64(i)) {
   693  				ns = append(ns, b)
   694  			}
   695  		}
   696  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   697  	case types.T_enum:
   698  		s := vector.MustFixedCol[types.Enum](v)
   699  		ns := make([]types.Enum, 0, len(s))
   700  		for i, b := range s {
   701  			if !nulls.Contains(v.GetNulls(), uint64(i)) {
   702  				ns = append(ns, b)
   703  			}
   704  		}
   705  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   706  	case types.T_decimal64:
   707  		s := vector.MustFixedCol[types.Decimal64](v)
   708  		ns := make([]types.Decimal64, 0, len(s))
   709  		for i, b := range s {
   710  			if !nulls.Contains(v.GetNulls(), uint64(i)) {
   711  				ns = append(ns, b)
   712  			}
   713  		}
   714  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   715  	case types.T_decimal128:
   716  		s := vector.MustFixedCol[types.Decimal128](v)
   717  		ns := make([]types.Decimal128, 0, len(s))
   718  		for i, b := range s {
   719  			if !nulls.Contains(v.GetNulls(), uint64(i)) {
   720  				ns = append(ns, b)
   721  			}
   722  		}
   723  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   724  	case types.T_json, types.T_char, types.T_varchar, types.T_binary, types.T_varbinary, types.T_blob,
   725  		types.T_array_float32, types.T_array_float64:
   726  		s := vector.MustBytesCol(v)
   727  		ns := make([][]byte, 0, len(s))
   728  		for i, b := range s {
   729  			if !nulls.Contains(v.GetNulls(), uint64(i)) {
   730  				ns = append(ns, b)
   731  			}
   732  		}
   733  		err = vector.AppendBytesList(vec, ns, nil, proc.Mp())
   734  	}
   735  	return vec, v.GetNulls(), err
   736  }
   737  
   738  func compactPrimaryCol(v *vector.Vector, bitMap *nulls.Nulls, proc *process.Process) (*vector.Vector, error) {
   739  	vec := proc.GetVector(*v.GetType())
   740  	var err error
   741  	defer func() {
   742  		if err != nil {
   743  			vec.Free(proc.GetMPool())
   744  		}
   745  	}()
   746  
   747  	if bitMap.IsEmpty() {
   748  		err = vector.GetUnionAllFunction(*v.GetType(), proc.GetMPool())(vec, v)
   749  		return vec, err
   750  	}
   751  	length := v.Length()
   752  	switch v.GetType().Oid {
   753  	case types.T_bool:
   754  		s := vector.MustFixedCol[bool](v)
   755  		ns := make([]bool, 0, length)
   756  		for i, b := range s {
   757  			if !nulls.Contains(bitMap, uint64(i)) {
   758  				ns = append(ns, b)
   759  			}
   760  		}
   761  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   762  	case types.T_bit:
   763  		s := vector.MustFixedCol[uint64](v)
   764  		ns := make([]uint64, 0)
   765  		for i, b := range s {
   766  			if !nulls.Contains(bitMap, uint64(i)) {
   767  				ns = append(ns, b)
   768  			}
   769  		}
   770  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   771  	case types.T_int8:
   772  		s := vector.MustFixedCol[int8](v)
   773  		ns := make([]int8, 0)
   774  		for i, b := range s {
   775  			if !nulls.Contains(bitMap, uint64(i)) {
   776  				ns = append(ns, b)
   777  			}
   778  		}
   779  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   780  	case types.T_int16:
   781  		s := vector.MustFixedCol[int16](v)
   782  		ns := make([]int16, 0)
   783  		for i, b := range s {
   784  			if !nulls.Contains(bitMap, uint64(i)) {
   785  				ns = append(ns, b)
   786  			}
   787  		}
   788  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   789  	case types.T_int32:
   790  		s := vector.MustFixedCol[int32](v)
   791  		ns := make([]int32, 0)
   792  		for i, b := range s {
   793  			if !nulls.Contains(bitMap, uint64(i)) {
   794  				ns = append(ns, b)
   795  			}
   796  		}
   797  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   798  	case types.T_int64:
   799  		s := vector.MustFixedCol[int64](v)
   800  		ns := make([]int64, 0)
   801  		for i, b := range s {
   802  			if !nulls.Contains(bitMap, uint64(i)) {
   803  				ns = append(ns, b)
   804  			}
   805  		}
   806  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   807  	case types.T_uint8:
   808  		s := vector.MustFixedCol[uint8](v)
   809  		ns := make([]uint8, 0)
   810  		for i, b := range s {
   811  			if !nulls.Contains(bitMap, uint64(i)) {
   812  				ns = append(ns, b)
   813  			}
   814  		}
   815  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   816  	case types.T_uint16:
   817  		s := vector.MustFixedCol[uint16](v)
   818  		ns := make([]uint16, 0)
   819  		for i, b := range s {
   820  			if !nulls.Contains(bitMap, uint64(i)) {
   821  				ns = append(ns, b)
   822  			}
   823  		}
   824  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   825  	case types.T_uint32:
   826  		s := vector.MustFixedCol[uint32](v)
   827  		ns := make([]uint32, 0)
   828  		for i, b := range s {
   829  			if !nulls.Contains(bitMap, uint64(i)) {
   830  				ns = append(ns, b)
   831  			}
   832  		}
   833  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   834  	case types.T_uint64:
   835  		s := vector.MustFixedCol[uint64](v)
   836  		ns := make([]uint64, 0)
   837  		for i, b := range s {
   838  			if !nulls.Contains(bitMap, uint64(i)) {
   839  				ns = append(ns, b)
   840  			}
   841  		}
   842  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   843  	case types.T_float32:
   844  		s := vector.MustFixedCol[float32](v)
   845  		ns := make([]float32, 0)
   846  		for i, b := range s {
   847  			if !nulls.Contains(bitMap, uint64(i)) {
   848  				ns = append(ns, b)
   849  			}
   850  		}
   851  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   852  	case types.T_float64:
   853  		s := vector.MustFixedCol[float64](v)
   854  		ns := make([]float64, 0)
   855  		for i, b := range s {
   856  			if !nulls.Contains(bitMap, uint64(i)) {
   857  				ns = append(ns, b)
   858  			}
   859  		}
   860  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   861  	case types.T_date:
   862  		s := vector.MustFixedCol[types.Date](v)
   863  		ns := make([]types.Date, 0)
   864  		for i, b := range s {
   865  			if !nulls.Contains(bitMap, uint64(i)) {
   866  				ns = append(ns, b)
   867  			}
   868  		}
   869  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   870  	case types.T_time:
   871  		s := vector.MustFixedCol[types.Time](v)
   872  		ns := make([]types.Time, 0)
   873  		for i, b := range s {
   874  			if !nulls.Contains(bitMap, uint64(i)) {
   875  				ns = append(ns, b)
   876  			}
   877  		}
   878  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   879  	case types.T_datetime:
   880  		s := vector.MustFixedCol[types.Datetime](v)
   881  		ns := make([]types.Datetime, 0)
   882  		for i, b := range s {
   883  			if !nulls.Contains(bitMap, uint64(i)) {
   884  				ns = append(ns, b)
   885  			}
   886  		}
   887  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   888  	case types.T_timestamp:
   889  		s := vector.MustFixedCol[types.Timestamp](v)
   890  		ns := make([]types.Timestamp, 0)
   891  		for i, b := range s {
   892  			if !nulls.Contains(bitMap, uint64(i)) {
   893  				ns = append(ns, b)
   894  			}
   895  		}
   896  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   897  	case types.T_enum:
   898  		s := vector.MustFixedCol[types.Enum](v)
   899  		ns := make([]types.Enum, 0)
   900  		for i, b := range s {
   901  			if !nulls.Contains(bitMap, uint64(i)) {
   902  				ns = append(ns, b)
   903  			}
   904  		}
   905  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   906  	case types.T_decimal64:
   907  		s := vector.MustFixedCol[types.Decimal64](v)
   908  		ns := make([]types.Decimal64, 0)
   909  		for i, b := range s {
   910  			if !nulls.Contains(bitMap, uint64(i)) {
   911  				ns = append(ns, b)
   912  			}
   913  		}
   914  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   915  	case types.T_decimal128:
   916  		s := vector.MustFixedCol[types.Decimal128](v)
   917  		ns := make([]types.Decimal128, 0)
   918  		for i, b := range s {
   919  			if !nulls.Contains(bitMap, uint64(i)) {
   920  				ns = append(ns, b)
   921  			}
   922  		}
   923  		err = vector.AppendFixedList(vec, ns, nil, proc.Mp())
   924  	case types.T_json, types.T_char, types.T_varchar, types.T_binary, types.T_varbinary, types.T_blob,
   925  		types.T_array_float32, types.T_array_float64:
   926  		s := vector.MustBytesCol(v)
   927  		ns := make([][]byte, 0)
   928  		for i, b := range s {
   929  			if !nulls.Contains(bitMap, uint64(i)) {
   930  				ns = append(ns, b)
   931  			}
   932  		}
   933  		err = vector.AppendBytesList(vec, ns, nil, proc.Mp())
   934  	}
   935  	return vec, err
   936  }