github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/aggexec/single.go (about)

     1  // Copyright 2024 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 aggexec
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    22  )
    23  
    24  // singleAggInfo contains the basic information of single column agg.
    25  type singleAggInfo struct {
    26  	aggID    int64
    27  	distinct bool
    28  	argType  types.Type
    29  	retType  types.Type
    30  
    31  	// emptyNull indicates that whether we should return null for a group without any input value.
    32  	emptyNull bool
    33  }
    34  
    35  func (info singleAggInfo) eq(other singleAggInfo) bool {
    36  	return info.aggID == other.aggID &&
    37  		info.distinct == other.distinct &&
    38  		info.argType.Eq(other.argType) &&
    39  		info.retType.Eq(other.retType) &&
    40  		info.emptyNull == other.emptyNull
    41  }
    42  
    43  func (info singleAggInfo) String() string {
    44  	return fmt.Sprintf("{aggID: %d, argType: %s, retType: %s}", info.aggID, info.argType.String(), info.retType.String())
    45  }
    46  
    47  func (info singleAggInfo) AggID() int64 {
    48  	return info.aggID
    49  }
    50  
    51  func (info singleAggInfo) IsDistinct() bool {
    52  	return info.distinct
    53  }
    54  
    55  func (info singleAggInfo) TypesInfo() ([]types.Type, types.Type) {
    56  	return []types.Type{info.argType}, info.retType
    57  }
    58  
    59  func (info singleAggInfo) getEncoded() *EncodedBasicInfo {
    60  	return &EncodedBasicInfo{
    61  		Id:         info.aggID,
    62  		IsDistinct: info.distinct,
    63  		Args:       []types.Type{info.argType},
    64  		Ret:        info.retType,
    65  	}
    66  }
    67  
    68  type singleAggOptimizedInfo struct {
    69  	// modify it to `acceptNull` later.
    70  	receiveNull bool
    71  }
    72  
    73  type singleAggExecExtraInformation struct {
    74  	partialGroup  int
    75  	partialResult any
    76  }
    77  
    78  func (optimized *singleAggExecExtraInformation) SetExtraInformation(partialResult any, groupIndex int) error {
    79  	optimized.partialGroup = groupIndex
    80  	optimized.partialResult = partialResult
    81  	return nil
    82  }
    83  
    84  // the executors of single column agg.
    85  // singleAggFuncExec1 receives a fixed size type except string and returns a fixed size type except string.
    86  // singleAggFuncExec2 receives a fixed size type except string and returns a byte type.
    87  // singleAggFuncExec3 receives a byte type and returns a fixed size type except string.
    88  // singleAggFuncExec4 receives a byte type and returns a byte type.
    89  type singleAggFuncExec1[from, to types.FixedSizeTExceptStrType] struct {
    90  	singleAggInfo
    91  	singleAggOptimizedInfo
    92  	singleAggExecExtraInformation
    93  	distinctHash
    94  
    95  	arg    sFixedArg[from]
    96  	ret    aggFuncResult[to]
    97  	groups []SingleAggFromFixedRetFixed[from, to]
    98  
    99  	initGroup SingleAggInit1[from, to]
   100  	fill      SingleAggFill1[from, to]
   101  	fillNull  SingleAggFillNull1[from, to]
   102  	fills     SingleAggFills1[from, to]
   103  	merge     SingleAggMerge1[from, to]
   104  	flush     SingleAggFlush1[from, to]
   105  
   106  	// method to new the private structure for group growing.
   107  	gGroup func() SingleAggFromFixedRetFixed[from, to]
   108  }
   109  type singleAggFuncExec2[from types.FixedSizeTExceptStrType] struct {
   110  	singleAggInfo
   111  	singleAggOptimizedInfo
   112  	singleAggExecExtraInformation
   113  	distinctHash
   114  
   115  	arg    sFixedArg[from]
   116  	ret    aggFuncBytesResult
   117  	groups []SingleAggFromFixedRetVar[from]
   118  
   119  	initGroup SingleAggInit2[from]
   120  	fill      SingleAggFill2[from]
   121  	fillNull  SingleAggFillNull2[from]
   122  	fills     SingleAggFills2[from]
   123  	merge     SingleAggMerge2[from]
   124  	flush     SingleAggFlush2[from]
   125  
   126  	// method to new the private structure for group growing.
   127  	gGroup func() SingleAggFromFixedRetVar[from]
   128  }
   129  type singleAggFuncExec3[to types.FixedSizeTExceptStrType] struct {
   130  	singleAggInfo
   131  	singleAggOptimizedInfo
   132  	singleAggExecExtraInformation
   133  	distinctHash
   134  
   135  	arg    sBytesArg
   136  	ret    aggFuncResult[to]
   137  	groups []SingleAggFromVarRetFixed[to]
   138  
   139  	initGroup SingleAggInit3[to]
   140  	fill      SingleAggFill3[to]
   141  	fillNull  SingleAggFillNull3[to]
   142  	fills     SingleAggFills3[to]
   143  	merge     SingleAggMerge3[to]
   144  	flush     SingleAggFlush3[to]
   145  
   146  	// method to new the private structure for group growing.
   147  	gGroup func() SingleAggFromVarRetFixed[to]
   148  }
   149  type singleAggFuncExec4 struct {
   150  	singleAggInfo
   151  	singleAggOptimizedInfo
   152  	singleAggExecExtraInformation
   153  	distinctHash
   154  
   155  	arg    sBytesArg
   156  	ret    aggFuncBytesResult
   157  	groups []SingleAggFromVarRetVar
   158  
   159  	initGroup SingleAggInit4
   160  	fill      SingleAggFill4
   161  	fillNull  SingleAggFillNull4
   162  	fills     SingleAggFills4
   163  	merge     SingleAggMerge4
   164  	flush     SingleAggFlush4
   165  
   166  	// method to new the private structure for group growing.
   167  	gGroup func() SingleAggFromVarRetVar
   168  }
   169  
   170  func (exec *singleAggFuncExec1[from, to]) init(
   171  	mg AggMemoryManager,
   172  	info singleAggInfo,
   173  	opt singleAggOptimizedInfo,
   174  	impl aggImplementation) {
   175  
   176  	exec.singleAggInfo = info
   177  	exec.singleAggOptimizedInfo = opt
   178  	exec.singleAggExecExtraInformation = emptyExtraInfo
   179  	exec.ret = initFixedAggFuncResult[to](mg, info.retType, info.emptyNull)
   180  	exec.groups = make([]SingleAggFromFixedRetFixed[from, to], 0, 1)
   181  	exec.gGroup = impl.generator.(func() SingleAggFromFixedRetFixed[from, to])
   182  	exec.fills = impl.fills.(SingleAggFills1[from, to])
   183  	exec.fill = impl.fill.(SingleAggFill1[from, to])
   184  	exec.merge = impl.merge.(SingleAggMerge1[from, to])
   185  	if impl.fillNull != nil {
   186  		exec.fillNull = impl.fillNull.(SingleAggFillNull1[from, to])
   187  	}
   188  	if impl.flush != nil {
   189  		exec.flush = impl.flush.(SingleAggFlush1[from, to])
   190  	}
   191  	if impl.init != nil {
   192  		exec.initGroup = impl.init.(SingleAggInit1[from, to])
   193  	}
   194  
   195  	if info.distinct {
   196  		exec.distinctHash = newDistinctHash(mg.Mp(), opt.receiveNull)
   197  	}
   198  }
   199  
   200  func (exec *singleAggFuncExec1[from, to]) GroupGrow(more int) error {
   201  	if exec.IsDistinct() {
   202  		if err := exec.distinctHash.grows(more); err != nil {
   203  			return err
   204  		}
   205  	}
   206  
   207  	if err := exec.ret.grows(more); err != nil {
   208  		return err
   209  	}
   210  
   211  	setter := exec.ret.aggSet
   212  	oldLength := len(exec.groups)
   213  	if cap(exec.groups) >= oldLength+more {
   214  		exec.groups = exec.groups[:oldLength+more]
   215  	} else {
   216  		exec.groups = append(exec.groups, make([]SingleAggFromFixedRetFixed[from, to], more)...)
   217  	}
   218  	for i, j := oldLength, len(exec.groups); i < j; i++ {
   219  		exec.groups[i] = exec.gGroup()
   220  	}
   221  
   222  	if exec.initGroup != nil {
   223  		for i, j := oldLength, len(exec.groups); i < j; i++ {
   224  			exec.ret.groupToSet = i
   225  			if err := exec.initGroup(exec.groups[i], setter, exec.singleAggInfo.argType, exec.singleAggInfo.retType); err != nil {
   226  				return err
   227  			}
   228  		}
   229  	}
   230  	return nil
   231  }
   232  
   233  func (exec *singleAggFuncExec1[from, to]) PreAllocateGroups(more int) error {
   234  	// todo: only expand the memory of the result now.
   235  	//  should expand the groups, distinctHash as well next day.
   236  	if len(exec.groups) == 0 {
   237  		exec.groups = make([]SingleAggFromFixedRetFixed[from, to], 0, more)
   238  	} else {
   239  		oldLength := len(exec.groups)
   240  		exec.groups = append(exec.groups, make([]SingleAggFromFixedRetFixed[from, to], more)...)
   241  		exec.groups = exec.groups[:oldLength]
   242  	}
   243  
   244  	return exec.ret.preAllocate(more)
   245  }
   246  
   247  func (exec *singleAggFuncExec1[from, to]) Fill(groupIndex int, row int, vectors []*vector.Vector) error {
   248  	vec := vectors[0]
   249  	if vec.IsConst() {
   250  		row = 0
   251  	}
   252  
   253  	if exec.IsDistinct() {
   254  		if need, err := exec.distinctHash.fill(groupIndex, vectors, row); !need || err != nil {
   255  			return err
   256  		}
   257  	}
   258  
   259  	exec.ret.groupToSet = groupIndex
   260  	getter := exec.ret.aggGet
   261  	setter := exec.ret.aggSet
   262  
   263  	if vec.IsConstNull() || vec.GetNulls().Contains(uint64(row)) {
   264  		if exec.receiveNull {
   265  			exec.ret.setGroupNotEmpty(groupIndex)
   266  			return exec.fillNull(exec.groups[groupIndex], getter, setter)
   267  		}
   268  		return nil
   269  	}
   270  
   271  	exec.ret.setGroupNotEmpty(groupIndex)
   272  	return exec.fill(exec.groups[groupIndex], vector.MustFixedCol[from](vec)[row], getter, setter)
   273  }
   274  
   275  func (exec *singleAggFuncExec1[from, to]) BulkFill(groupIndex int, vectors []*vector.Vector) error {
   276  	length := vectors[0].Length()
   277  	if length == 0 {
   278  		return nil
   279  	}
   280  
   281  	if exec.IsDistinct() {
   282  		return exec.distinctBulkFill(groupIndex, vectors, length)
   283  	}
   284  
   285  	vec := vectors[0]
   286  	exec.ret.groupToSet = groupIndex
   287  	getter := exec.ret.aggGet
   288  	setter := exec.ret.aggSet
   289  
   290  	if vec.IsConst() {
   291  		if vec.IsConstNull() {
   292  			var value from
   293  			if exec.receiveNull {
   294  				exec.ret.setGroupNotEmpty(groupIndex)
   295  				return exec.fills(exec.groups[groupIndex], value, true, length, getter, setter)
   296  			}
   297  		} else {
   298  			exec.ret.setGroupNotEmpty(groupIndex)
   299  			return exec.fills(exec.groups[groupIndex], vector.MustFixedCol[from](vec)[0], false, length, getter, setter)
   300  		}
   301  		return nil
   302  	}
   303  
   304  	exec.arg.prepare(vec)
   305  	if exec.arg.w.WithAnyNullValue() {
   306  		if exec.receiveNull {
   307  			exec.ret.setGroupNotEmpty(groupIndex)
   308  			for i, j := uint64(0), uint64(length); i < j; i++ {
   309  				v, null := exec.arg.w.GetValue(i)
   310  				if null {
   311  					if err := exec.fillNull(exec.groups[groupIndex], getter, setter); err != nil {
   312  						return err
   313  					}
   314  				} else {
   315  					if err := exec.fill(exec.groups[groupIndex], v, getter, setter); err != nil {
   316  						return err
   317  					}
   318  				}
   319  			}
   320  		} else {
   321  			mustNotEmpty := false
   322  			for i, j := uint64(0), uint64(length); i < j; i++ {
   323  				v, null := exec.arg.w.GetValue(i)
   324  				if !null {
   325  					mustNotEmpty = true
   326  					if err := exec.fill(exec.groups[groupIndex], v, getter, setter); err != nil {
   327  						return err
   328  					}
   329  				}
   330  			}
   331  			if mustNotEmpty {
   332  				exec.ret.setGroupNotEmpty(groupIndex)
   333  			}
   334  		}
   335  		return nil
   336  	}
   337  
   338  	exec.ret.setGroupNotEmpty(groupIndex)
   339  	for i, j := uint64(0), uint64(length); i < j; i++ {
   340  		v, _ := exec.arg.w.GetValue(i)
   341  		if err := exec.fill(exec.groups[groupIndex], v, getter, setter); err != nil {
   342  			return err
   343  		}
   344  	}
   345  	return nil
   346  }
   347  
   348  func (exec *singleAggFuncExec1[from, to]) BatchFill(offset int, groups []uint64, vectors []*vector.Vector) error {
   349  	if exec.IsDistinct() {
   350  		return exec.distinctBatchFill(offset, groups, vectors)
   351  	}
   352  
   353  	vec := vectors[0]
   354  	setter := exec.ret.aggSet
   355  	getter := exec.ret.aggGet
   356  
   357  	if vec.IsConst() {
   358  		if vec.IsConstNull() {
   359  			if exec.receiveNull {
   360  				for i := 0; i < len(groups); i++ {
   361  					if groups[i] != GroupNotMatched {
   362  						groupIdx := int(groups[i] - 1)
   363  						exec.ret.groupToSet = groupIdx
   364  						exec.ret.setGroupNotEmpty(groupIdx)
   365  						if err := exec.fillNull(exec.groups[groupIdx], getter, setter); err != nil {
   366  							return nil
   367  						}
   368  					}
   369  				}
   370  			}
   371  			return nil
   372  		}
   373  
   374  		value := vector.MustFixedCol[from](vec)[0]
   375  		for i := 0; i < len(groups); i++ {
   376  			if groups[i] != GroupNotMatched {
   377  				groupIdx := int(groups[i] - 1)
   378  				exec.ret.groupToSet = groupIdx
   379  				exec.ret.setGroupNotEmpty(groupIdx)
   380  				if err := exec.fill(exec.groups[groupIdx], value, getter, setter); err != nil {
   381  					return nil
   382  				}
   383  			}
   384  		}
   385  		return nil
   386  	}
   387  
   388  	exec.arg.prepare(vec)
   389  	if exec.arg.w.WithAnyNullValue() {
   390  		if exec.receiveNull {
   391  			for i, j, idx := uint64(offset), uint64(offset+len(groups)), 0; i < j; i++ {
   392  				if groups[idx] != GroupNotMatched {
   393  					v, null := exec.arg.w.GetValue(i)
   394  					groupIdx := int(groups[idx] - 1)
   395  					exec.ret.groupToSet = groupIdx
   396  					exec.ret.setGroupNotEmpty(groupIdx)
   397  					if null {
   398  						if err := exec.fillNull(exec.groups[groupIdx], getter, setter); err != nil {
   399  							return err
   400  						}
   401  					} else {
   402  						if err := exec.fill(exec.groups[groupIdx], v, getter, setter); err != nil {
   403  							return err
   404  						}
   405  					}
   406  				}
   407  				idx++
   408  			}
   409  			return nil
   410  		}
   411  
   412  		for i, j, idx := uint64(offset), uint64(offset+len(groups)), 0; i < j; i++ {
   413  			if groups[idx] != GroupNotMatched {
   414  				v, null := exec.arg.w.GetValue(i)
   415  				if !null {
   416  					groupIdx := int(groups[idx] - 1)
   417  					exec.ret.groupToSet = groupIdx
   418  					exec.ret.setGroupNotEmpty(groupIdx)
   419  					if err := exec.fill(exec.groups[groupIdx], v, getter, setter); err != nil {
   420  						return err
   421  					}
   422  				}
   423  			}
   424  			idx++
   425  		}
   426  		return nil
   427  	}
   428  
   429  	for i, j, idx := uint64(offset), uint64(offset+len(groups)), 0; i < j; i++ {
   430  		if groups[idx] != GroupNotMatched {
   431  			v, _ := exec.arg.w.GetValue(i)
   432  			groupIdx := int(groups[idx] - 1)
   433  			exec.ret.groupToSet = groupIdx
   434  			exec.ret.setGroupNotEmpty(groupIdx)
   435  			if err := exec.fill(exec.groups[groupIdx], v, getter, setter); err != nil {
   436  				return err
   437  			}
   438  		}
   439  		idx++
   440  	}
   441  	return nil
   442  }
   443  
   444  func (exec *singleAggFuncExec1[from, to]) Merge(next AggFuncExec, groupIdx1, groupIdx2 int) error {
   445  	other := next.(*singleAggFuncExec1[from, to])
   446  	exec.ret.groupToSet = groupIdx1
   447  	other.ret.groupToSet = groupIdx2
   448  
   449  	exec.ret.mergeEmpty(other.ret.basicResult, groupIdx1, groupIdx2)
   450  	if err := exec.merge(
   451  		exec.groups[groupIdx1],
   452  		other.groups[groupIdx2],
   453  		exec.ret.aggGet, other.ret.aggGet,
   454  		exec.ret.aggSet); err != nil {
   455  		return err
   456  	}
   457  	return exec.distinctHash.merge(&other.distinctHash)
   458  }
   459  
   460  func (exec *singleAggFuncExec1[from, to]) BatchMerge(next AggFuncExec, offset int, groups []uint64) error {
   461  	other := next.(*singleAggFuncExec1[from, to])
   462  	setter := exec.ret.aggSet
   463  	getter1, getter2 := exec.ret.aggGet, other.ret.aggGet
   464  
   465  	for i := range groups {
   466  		if groups[i] == GroupNotMatched {
   467  			continue
   468  		}
   469  		groupIdx1, groupIdx2 := int(groups[i]-1), i+offset
   470  		exec.ret.groupToSet = groupIdx1
   471  		other.ret.groupToSet = groupIdx2
   472  
   473  		exec.ret.mergeEmpty(other.ret.basicResult, groupIdx1, groupIdx2)
   474  		if err := exec.merge(
   475  			exec.groups[groupIdx1],
   476  			other.groups[groupIdx2],
   477  			getter1, getter2,
   478  			setter); err != nil {
   479  			return err
   480  		}
   481  	}
   482  	return exec.distinctHash.merge(&other.distinctHash)
   483  }
   484  
   485  func (exec *singleAggFuncExec1[from, to]) Flush() (*vector.Vector, error) {
   486  	if exec.partialResult != nil {
   487  		exec.ret.groupToSet = exec.partialGroup
   488  		if vs, ok := exec.partialResult.(from); ok {
   489  			exec.ret.setGroupNotEmpty(exec.partialGroup)
   490  			if err := exec.fill(exec.groups[exec.partialGroup], vs, exec.ret.aggGet, exec.ret.aggSet); err != nil {
   491  				return nil, err
   492  			}
   493  		}
   494  	}
   495  
   496  	if exec.flush == nil {
   497  		return exec.ret.flush(), nil
   498  	}
   499  
   500  	setter := exec.ret.aggSet
   501  	getter := exec.ret.aggGet
   502  
   503  	if exec.ret.emptyBeNull {
   504  		for i, group := range exec.groups {
   505  			if exec.ret.groupIsEmpty(i) {
   506  				continue
   507  			}
   508  			exec.ret.groupToSet = i
   509  			if err := exec.flush(group, getter, setter); err != nil {
   510  				return nil, err
   511  			}
   512  		}
   513  	} else {
   514  		for i, group := range exec.groups {
   515  			exec.ret.groupToSet = i
   516  			if err := exec.flush(group, getter, setter); err != nil {
   517  				return nil, err
   518  			}
   519  		}
   520  	}
   521  
   522  	return exec.ret.flush(), nil
   523  }
   524  
   525  func (exec *singleAggFuncExec1[from, to]) Free() {
   526  	exec.ret.free()
   527  	exec.distinctHash.free()
   528  }
   529  
   530  func (exec *singleAggFuncExec2[from]) init(
   531  	mg AggMemoryManager,
   532  	info singleAggInfo,
   533  	opt singleAggOptimizedInfo,
   534  	agg aggImplementation) {
   535  
   536  	exec.singleAggInfo = info
   537  	exec.singleAggOptimizedInfo = opt
   538  	exec.singleAggExecExtraInformation = emptyExtraInfo
   539  	exec.ret = initBytesAggFuncResult(mg, info.retType, info.emptyNull)
   540  	exec.groups = make([]SingleAggFromFixedRetVar[from], 0, 1)
   541  	exec.gGroup = agg.generator.(func() SingleAggFromFixedRetVar[from])
   542  
   543  	exec.fills = agg.fills.(SingleAggFills2[from])
   544  	exec.fill = agg.fill.(SingleAggFill2[from])
   545  	exec.merge = agg.merge.(SingleAggMerge2[from])
   546  	if agg.fillNull != nil {
   547  		exec.fillNull = agg.fillNull.(SingleAggFillNull2[from])
   548  	}
   549  	if agg.flush != nil {
   550  		exec.flush = agg.flush.(SingleAggFlush2[from])
   551  	}
   552  	if agg.init != nil {
   553  		exec.initGroup = agg.init.(SingleAggInit2[from])
   554  	}
   555  
   556  	if info.distinct {
   557  		exec.distinctHash = newDistinctHash(mg.Mp(), opt.receiveNull)
   558  	}
   559  }
   560  
   561  func (exec *singleAggFuncExec2[from]) GroupGrow(more int) error {
   562  	if exec.IsDistinct() {
   563  		if err := exec.distinctHash.grows(more); err != nil {
   564  			return err
   565  		}
   566  	}
   567  
   568  	if err := exec.ret.grows(more); err != nil {
   569  		return err
   570  	}
   571  
   572  	setter := exec.ret.aggSet
   573  	oldLength := len(exec.groups)
   574  	if cap(exec.groups) >= oldLength+more {
   575  		exec.groups = exec.groups[:oldLength+more]
   576  	} else {
   577  		exec.groups = append(exec.groups, make([]SingleAggFromFixedRetVar[from], more)...)
   578  	}
   579  	for i, j := oldLength, len(exec.groups); i < j; i++ {
   580  		exec.groups[i] = exec.gGroup()
   581  	}
   582  	if exec.initGroup != nil {
   583  		for i, j := oldLength, len(exec.groups); i < j; i++ {
   584  			exec.ret.groupToSet = i
   585  			if err := exec.initGroup(exec.groups[i], setter, exec.singleAggInfo.argType, exec.singleAggInfo.retType); err != nil {
   586  				return err
   587  			}
   588  		}
   589  	}
   590  	return nil
   591  }
   592  
   593  func (exec *singleAggFuncExec2[from]) PreAllocateGroups(more int) error {
   594  	if len(exec.groups) == 0 {
   595  		exec.groups = make([]SingleAggFromFixedRetVar[from], 0, more)
   596  	} else {
   597  		oldLength := len(exec.groups)
   598  		exec.groups = append(exec.groups, make([]SingleAggFromFixedRetVar[from], more)...)
   599  		exec.groups = exec.groups[:oldLength]
   600  	}
   601  
   602  	return exec.ret.preAllocate(more)
   603  }
   604  
   605  func (exec *singleAggFuncExec2[from]) Fill(groupIndex int, row int, vectors []*vector.Vector) error {
   606  	vec := vectors[0]
   607  	if vec.IsConst() {
   608  		row = 0
   609  	}
   610  
   611  	if exec.IsDistinct() {
   612  		if need, err := exec.distinctHash.fill(groupIndex, vectors, row); !need || err != nil {
   613  			return err
   614  		}
   615  	}
   616  
   617  	exec.ret.groupToSet = groupIndex
   618  	setter := exec.ret.aggSet
   619  	getter := exec.ret.aggGet
   620  
   621  	if vec.IsConstNull() || vec.GetNulls().Contains(uint64(row)) {
   622  		if exec.receiveNull {
   623  			exec.ret.setGroupNotEmpty(groupIndex)
   624  			return exec.fillNull(exec.groups[groupIndex], getter, setter)
   625  		}
   626  		return nil
   627  	}
   628  
   629  	exec.ret.setGroupNotEmpty(groupIndex)
   630  	return exec.fill(exec.groups[groupIndex], vector.MustFixedCol[from](vec)[row], getter, setter)
   631  }
   632  
   633  func (exec *singleAggFuncExec2[from]) BulkFill(groupIndex int, vectors []*vector.Vector) error {
   634  	vec := vectors[0]
   635  	length := vec.Length()
   636  	if length == 0 {
   637  		return nil
   638  	}
   639  
   640  	if exec.IsDistinct() {
   641  		return exec.distinctBulkFill(groupIndex, vectors, length)
   642  	}
   643  
   644  	exec.ret.groupToSet = groupIndex
   645  	setter := exec.ret.aggSet
   646  	getter := exec.ret.aggGet
   647  
   648  	if vec.IsConst() {
   649  		if vec.IsConstNull() {
   650  			var value from
   651  			if exec.receiveNull {
   652  				exec.ret.setGroupNotEmpty(groupIndex)
   653  				return exec.fills(exec.groups[groupIndex], value, true, length, getter, setter)
   654  			}
   655  		} else {
   656  			exec.ret.setGroupNotEmpty(groupIndex)
   657  			return exec.fills(exec.groups[groupIndex], vector.MustFixedCol[from](vec)[0], false, length, getter, setter)
   658  		}
   659  		return nil
   660  	}
   661  
   662  	exec.arg.prepare(vec)
   663  	if exec.arg.w.WithAnyNullValue() {
   664  		if exec.receiveNull {
   665  			exec.ret.setGroupNotEmpty(groupIndex)
   666  			for i, j := uint64(0), uint64(length); i < j; i++ {
   667  				v, null := exec.arg.w.GetValue(i)
   668  				if null {
   669  					if err := exec.fillNull(exec.groups[groupIndex], getter, setter); err != nil {
   670  						return err
   671  					}
   672  				} else {
   673  					if err := exec.fill(exec.groups[groupIndex], v, getter, setter); err != nil {
   674  						return err
   675  					}
   676  				}
   677  			}
   678  		} else {
   679  			mustNotEmpty := false
   680  			for i, j := uint64(0), uint64(length); i < j; i++ {
   681  				v, null := exec.arg.w.GetValue(i)
   682  				if !null {
   683  					mustNotEmpty = true
   684  					if err := exec.fill(exec.groups[groupIndex], v, getter, setter); err != nil {
   685  						return err
   686  					}
   687  				}
   688  			}
   689  			if mustNotEmpty {
   690  				exec.ret.setGroupNotEmpty(groupIndex)
   691  			}
   692  		}
   693  		return nil
   694  	}
   695  
   696  	exec.ret.setGroupNotEmpty(groupIndex)
   697  	for i, j := uint64(0), uint64(length); i < j; i++ {
   698  		v, _ := exec.arg.w.GetValue(i)
   699  		if err := exec.fill(exec.groups[groupIndex], v, getter, setter); err != nil {
   700  			return err
   701  		}
   702  	}
   703  	return nil
   704  }
   705  
   706  func (exec *singleAggFuncExec2[from]) BatchFill(offset int, groups []uint64, vectors []*vector.Vector) error {
   707  	if exec.IsDistinct() {
   708  		return exec.distinctBatchFill(offset, groups, vectors)
   709  	}
   710  
   711  	vec := vectors[0]
   712  	setter := exec.ret.aggSet
   713  	getter := exec.ret.aggGet
   714  
   715  	if vec.IsConst() {
   716  		if vec.IsConstNull() {
   717  			if exec.receiveNull {
   718  				for i := 0; i < len(groups); i++ {
   719  					if groups[i] != GroupNotMatched {
   720  						groupIdx := int(groups[i] - 1)
   721  						exec.ret.groupToSet = groupIdx
   722  						exec.ret.setGroupNotEmpty(groupIdx)
   723  						if err := exec.fillNull(exec.groups[groupIdx], getter, setter); err != nil {
   724  							return nil
   725  						}
   726  					}
   727  				}
   728  			}
   729  			return nil
   730  		}
   731  
   732  		value := vector.MustFixedCol[from](vec)[0]
   733  		for i := 0; i < len(groups); i++ {
   734  			if groups[i] != GroupNotMatched {
   735  				groupIdx := int(groups[i] - 1)
   736  				exec.ret.groupToSet = groupIdx
   737  				exec.ret.setGroupNotEmpty(groupIdx)
   738  				if err := exec.fill(exec.groups[groupIdx], value, getter, setter); err != nil {
   739  					return nil
   740  				}
   741  			}
   742  		}
   743  		return nil
   744  	}
   745  
   746  	exec.arg.prepare(vec)
   747  	if exec.arg.w.WithAnyNullValue() {
   748  		if exec.receiveNull {
   749  			for i, j, idx := uint64(offset), uint64(offset+len(groups)), 0; i < j; i++ {
   750  				if groups[idx] != GroupNotMatched {
   751  					v, null := exec.arg.w.GetValue(i)
   752  					groupIdx := int(groups[idx] - 1)
   753  					exec.ret.groupToSet = groupIdx
   754  					exec.ret.setGroupNotEmpty(groupIdx)
   755  					if null {
   756  						if err := exec.fillNull(exec.groups[groupIdx], getter, setter); err != nil {
   757  							return err
   758  						}
   759  					} else {
   760  						if err := exec.fill(exec.groups[groupIdx], v, getter, setter); err != nil {
   761  							return err
   762  						}
   763  					}
   764  				}
   765  				idx++
   766  			}
   767  			return nil
   768  		}
   769  
   770  		for i, j, idx := uint64(offset), uint64(offset+len(groups)), 0; i < j; i++ {
   771  			if groups[idx] != GroupNotMatched {
   772  				v, null := exec.arg.w.GetValue(i)
   773  				if !null {
   774  					groupIdx := int(groups[idx] - 1)
   775  					exec.ret.groupToSet = groupIdx
   776  					exec.ret.setGroupNotEmpty(groupIdx)
   777  					if err := exec.fill(exec.groups[groupIdx], v, getter, setter); err != nil {
   778  						return err
   779  					}
   780  				}
   781  			}
   782  			idx++
   783  		}
   784  		return nil
   785  	}
   786  
   787  	for i, j, idx := uint64(offset), uint64(offset+len(groups)), 0; i < j; i++ {
   788  		if groups[idx] != GroupNotMatched {
   789  			v, _ := exec.arg.w.GetValue(i)
   790  			groupIdx := int(groups[idx] - 1)
   791  			exec.ret.groupToSet = groupIdx
   792  			exec.ret.setGroupNotEmpty(groupIdx)
   793  			if err := exec.fill(exec.groups[groupIdx], v, getter, setter); err != nil {
   794  				return err
   795  			}
   796  		}
   797  		idx++
   798  	}
   799  	return nil
   800  }
   801  
   802  func (exec *singleAggFuncExec2[from]) Merge(next AggFuncExec, groupIdx1, groupIdx2 int) error {
   803  	other := next.(*singleAggFuncExec2[from])
   804  	exec.ret.groupToSet = groupIdx1
   805  	other.ret.groupToSet = groupIdx2
   806  
   807  	exec.ret.mergeEmpty(other.ret.basicResult, groupIdx1, groupIdx2)
   808  	if err := exec.merge(
   809  		exec.groups[groupIdx1],
   810  		other.groups[groupIdx2],
   811  		exec.ret.aggGet, other.ret.aggGet,
   812  		exec.ret.aggSet); err != nil {
   813  		return err
   814  	}
   815  	return exec.distinctHash.merge(&other.distinctHash)
   816  }
   817  
   818  func (exec *singleAggFuncExec2[from]) BatchMerge(next AggFuncExec, offset int, groups []uint64) error {
   819  	other := next.(*singleAggFuncExec2[from])
   820  	setter := exec.ret.aggSet
   821  	getter1, getter2 := exec.ret.aggGet, other.ret.aggGet
   822  
   823  	for i := range groups {
   824  		if groups[i] == GroupNotMatched {
   825  			continue
   826  		}
   827  		groupIdx1, groupIdx2 := int(groups[i]-1), i+offset
   828  		exec.ret.groupToSet = groupIdx1
   829  		other.ret.groupToSet = groupIdx2
   830  
   831  		exec.ret.mergeEmpty(other.ret.basicResult, groupIdx1, groupIdx2)
   832  		if err := exec.merge(
   833  			exec.groups[groupIdx1],
   834  			other.groups[groupIdx2],
   835  			getter1, getter2,
   836  			setter); err != nil {
   837  			return err
   838  		}
   839  	}
   840  	return exec.distinctHash.merge(&other.distinctHash)
   841  }
   842  
   843  func (exec *singleAggFuncExec2[from]) Flush() (*vector.Vector, error) {
   844  	if exec.partialResult != nil {
   845  		exec.ret.groupToSet = exec.partialGroup
   846  		if vs, ok := exec.partialResult.(from); ok {
   847  			exec.ret.setGroupNotEmpty(exec.partialGroup)
   848  			if err := exec.fill(exec.groups[exec.partialGroup], vs, exec.ret.aggGet, exec.ret.aggSet); err != nil {
   849  				return nil, err
   850  			}
   851  		}
   852  	}
   853  
   854  	if exec.flush == nil {
   855  		return exec.ret.flush(), nil
   856  	}
   857  
   858  	setter := exec.ret.aggSet
   859  	getter := exec.ret.aggGet
   860  	if exec.ret.emptyBeNull {
   861  		for i, group := range exec.groups {
   862  			if exec.ret.groupIsEmpty(i) {
   863  				continue
   864  			}
   865  			exec.ret.groupToSet = i
   866  			if err := exec.flush(group, getter, setter); err != nil {
   867  				return nil, err
   868  			}
   869  		}
   870  	} else {
   871  		for i, group := range exec.groups {
   872  			exec.ret.groupToSet = i
   873  			if err := exec.flush(group, getter, setter); err != nil {
   874  				return nil, err
   875  			}
   876  		}
   877  	}
   878  	return exec.ret.flush(), nil
   879  }
   880  
   881  func (exec *singleAggFuncExec2[from]) Free() {
   882  	exec.ret.free()
   883  	exec.distinctHash.free()
   884  }
   885  
   886  func (exec *singleAggFuncExec3[to]) init(
   887  	mg AggMemoryManager,
   888  	info singleAggInfo,
   889  	opt singleAggOptimizedInfo,
   890  	impl aggImplementation) {
   891  
   892  	exec.singleAggInfo = info
   893  	exec.singleAggOptimizedInfo = opt
   894  	exec.singleAggExecExtraInformation = emptyExtraInfo
   895  	exec.ret = initFixedAggFuncResult[to](mg, info.retType, info.emptyNull)
   896  	exec.groups = make([]SingleAggFromVarRetFixed[to], 0, 1)
   897  	exec.gGroup = impl.generator.(func() SingleAggFromVarRetFixed[to])
   898  
   899  	exec.fills = impl.fills.(SingleAggFills3[to])
   900  	exec.fill = impl.fill.(SingleAggFill3[to])
   901  	exec.merge = impl.merge.(SingleAggMerge3[to])
   902  	if impl.fillNull != nil {
   903  		exec.fillNull = impl.fillNull.(SingleAggFillNull3[to])
   904  	}
   905  	if impl.flush != nil {
   906  		exec.flush = impl.flush.(SingleAggFlush3[to])
   907  	}
   908  	if impl.init != nil {
   909  		exec.initGroup = impl.init.(SingleAggInit3[to])
   910  	}
   911  
   912  	if info.distinct {
   913  		exec.distinctHash = newDistinctHash(mg.Mp(), opt.receiveNull)
   914  	}
   915  }
   916  
   917  func (exec *singleAggFuncExec3[to]) GroupGrow(more int) error {
   918  	if exec.IsDistinct() {
   919  		if err := exec.distinctHash.grows(more); err != nil {
   920  			return err
   921  		}
   922  	}
   923  
   924  	if err := exec.ret.grows(more); err != nil {
   925  		return err
   926  	}
   927  
   928  	setter := exec.ret.aggSet
   929  	oldLength := len(exec.groups)
   930  	if cap(exec.groups) >= oldLength+more {
   931  		exec.groups = exec.groups[:oldLength+more]
   932  	} else {
   933  		exec.groups = append(exec.groups, make([]SingleAggFromVarRetFixed[to], more)...)
   934  	}
   935  	for i, j := oldLength, len(exec.groups); i < j; i++ {
   936  		exec.groups[i] = exec.gGroup()
   937  	}
   938  
   939  	if exec.initGroup != nil {
   940  		for i, j := oldLength, len(exec.groups); i < j; i++ {
   941  			exec.ret.groupToSet = i
   942  			if err := exec.initGroup(exec.groups[i], setter, exec.singleAggInfo.argType, exec.singleAggInfo.retType); err != nil {
   943  				return err
   944  			}
   945  		}
   946  	}
   947  	return nil
   948  }
   949  
   950  func (exec *singleAggFuncExec3[to]) PreAllocateGroups(more int) error {
   951  	if len(exec.groups) == 0 {
   952  		exec.groups = make([]SingleAggFromVarRetFixed[to], 0, more)
   953  	} else {
   954  		oldLength := len(exec.groups)
   955  		exec.groups = append(exec.groups, make([]SingleAggFromVarRetFixed[to], more)...)
   956  		exec.groups = exec.groups[:oldLength]
   957  	}
   958  
   959  	return exec.ret.preAllocate(more)
   960  }
   961  
   962  func (exec *singleAggFuncExec3[to]) Fill(groupIndex int, row int, vectors []*vector.Vector) error {
   963  	vec := vectors[0]
   964  	if vec.IsConst() {
   965  		row = 0
   966  	}
   967  
   968  	if exec.IsDistinct() {
   969  		if need, err := exec.distinctHash.fill(groupIndex, vectors, row); !need || err != nil {
   970  			return err
   971  		}
   972  	}
   973  
   974  	exec.ret.groupToSet = groupIndex
   975  	setter := exec.ret.aggSet
   976  	getter := exec.ret.aggGet
   977  
   978  	if vec.IsConstNull() || vec.GetNulls().Contains(uint64(row)) {
   979  		if exec.receiveNull {
   980  			exec.ret.setGroupNotEmpty(groupIndex)
   981  			return exec.fillNull(exec.groups[groupIndex], getter, setter)
   982  		}
   983  		return nil
   984  	}
   985  
   986  	exec.ret.setGroupNotEmpty(groupIndex)
   987  	return exec.fill(exec.groups[groupIndex], vec.GetBytesAt(row), getter, setter)
   988  }
   989  
   990  func (exec *singleAggFuncExec3[to]) BulkFill(groupIndex int, vectors []*vector.Vector) error {
   991  	vec := vectors[0]
   992  	length := vec.Length()
   993  	if length == 0 {
   994  		return nil
   995  	}
   996  
   997  	if exec.IsDistinct() {
   998  		return exec.distinctBulkFill(groupIndex, vectors, length)
   999  	}
  1000  
  1001  	exec.ret.groupToSet = groupIndex
  1002  	setter := exec.ret.aggSet
  1003  	getter := exec.ret.aggGet
  1004  
  1005  	if vec.IsConst() {
  1006  		if vec.IsConstNull() {
  1007  			if exec.receiveNull {
  1008  				exec.ret.setGroupNotEmpty(groupIndex)
  1009  				return exec.fills(exec.groups[groupIndex], nil, true, length, getter, setter)
  1010  			}
  1011  		} else {
  1012  			exec.ret.setGroupNotEmpty(groupIndex)
  1013  			return exec.fills(exec.groups[groupIndex], vec.GetBytesAt(0), false, length, getter, setter)
  1014  		}
  1015  		return nil
  1016  	}
  1017  
  1018  	exec.arg.prepare(vec)
  1019  	if exec.arg.w.WithAnyNullValue() {
  1020  		if exec.receiveNull {
  1021  			exec.ret.setGroupNotEmpty(groupIndex)
  1022  			for i, j := uint64(0), uint64(length); i < j; i++ {
  1023  				v, null := exec.arg.w.GetStrValue(i)
  1024  				if null {
  1025  					if err := exec.fillNull(exec.groups[groupIndex], getter, setter); err != nil {
  1026  						return err
  1027  					}
  1028  				} else {
  1029  					if err := exec.fill(exec.groups[groupIndex], v, getter, setter); err != nil {
  1030  						return err
  1031  					}
  1032  				}
  1033  			}
  1034  		} else {
  1035  			mustNotEmpty := false
  1036  			for i, j := uint64(0), uint64(length); i < j; i++ {
  1037  				v, null := exec.arg.w.GetStrValue(i)
  1038  				if !null {
  1039  					mustNotEmpty = true
  1040  					if err := exec.fill(exec.groups[groupIndex], v, getter, setter); err != nil {
  1041  						return err
  1042  					}
  1043  				}
  1044  			}
  1045  			if mustNotEmpty {
  1046  				exec.ret.setGroupNotEmpty(groupIndex)
  1047  			}
  1048  		}
  1049  		return nil
  1050  	}
  1051  
  1052  	exec.ret.setGroupNotEmpty(groupIndex)
  1053  	for i, j := uint64(0), uint64(length); i < j; i++ {
  1054  		v, _ := exec.arg.w.GetStrValue(i)
  1055  		if err := exec.fill(exec.groups[groupIndex], v, getter, setter); err != nil {
  1056  			return err
  1057  		}
  1058  	}
  1059  	return nil
  1060  }
  1061  
  1062  func (exec *singleAggFuncExec3[to]) BatchFill(offset int, groups []uint64, vectors []*vector.Vector) error {
  1063  	if exec.IsDistinct() {
  1064  		return exec.distinctBatchFill(offset, groups, vectors)
  1065  	}
  1066  
  1067  	vec := vectors[0]
  1068  	setter := exec.ret.aggSet
  1069  	getter := exec.ret.aggGet
  1070  
  1071  	if vec.IsConst() {
  1072  		if vec.IsConstNull() {
  1073  			if exec.receiveNull {
  1074  				for i := 0; i < len(groups); i++ {
  1075  					if groups[i] != GroupNotMatched {
  1076  						groupIdx := int(groups[i] - 1)
  1077  						exec.ret.groupToSet = groupIdx
  1078  						exec.ret.setGroupNotEmpty(groupIdx)
  1079  						if err := exec.fillNull(exec.groups[groupIdx], getter, setter); err != nil {
  1080  							return nil
  1081  						}
  1082  					}
  1083  				}
  1084  			}
  1085  			return nil
  1086  		}
  1087  
  1088  		value := vec.GetBytesAt(0)
  1089  		for i := 0; i < len(groups); i++ {
  1090  			if groups[i] != GroupNotMatched {
  1091  				groupIdx := int(groups[i] - 1)
  1092  				exec.ret.groupToSet = groupIdx
  1093  				exec.ret.setGroupNotEmpty(groupIdx)
  1094  				if err := exec.fill(exec.groups[groupIdx], value, getter, setter); err != nil {
  1095  					return nil
  1096  				}
  1097  			}
  1098  		}
  1099  		return nil
  1100  	}
  1101  
  1102  	exec.arg.prepare(vec)
  1103  	if exec.arg.w.WithAnyNullValue() {
  1104  		if exec.receiveNull {
  1105  			for i, j, idx := uint64(offset), uint64(offset+len(groups)), 0; i < j; i++ {
  1106  				if groups[idx] != GroupNotMatched {
  1107  					v, null := exec.arg.w.GetStrValue(i)
  1108  					groupIdx := int(groups[idx] - 1)
  1109  					exec.ret.groupToSet = groupIdx
  1110  					exec.ret.setGroupNotEmpty(groupIdx)
  1111  					if null {
  1112  						if err := exec.fillNull(exec.groups[groupIdx], getter, setter); err != nil {
  1113  							return err
  1114  						}
  1115  					} else {
  1116  						if err := exec.fill(exec.groups[groupIdx], v, getter, setter); err != nil {
  1117  							return err
  1118  						}
  1119  					}
  1120  				}
  1121  				idx++
  1122  			}
  1123  			return nil
  1124  		}
  1125  
  1126  		for i, j, idx := uint64(offset), uint64(offset+len(groups)), 0; i < j; i++ {
  1127  			if groups[idx] != GroupNotMatched {
  1128  				v, null := exec.arg.w.GetStrValue(i)
  1129  				if !null {
  1130  					groupIdx := int(groups[idx] - 1)
  1131  					exec.ret.groupToSet = groupIdx
  1132  					exec.ret.setGroupNotEmpty(groupIdx)
  1133  					if err := exec.fill(exec.groups[groupIdx], v, getter, setter); err != nil {
  1134  						return err
  1135  					}
  1136  				}
  1137  			}
  1138  			idx++
  1139  		}
  1140  		return nil
  1141  	}
  1142  
  1143  	for i, j, idx := uint64(offset), uint64(offset+len(groups)), 0; i < j; i++ {
  1144  		if groups[idx] != GroupNotMatched {
  1145  			v, _ := exec.arg.w.GetStrValue(i)
  1146  			groupIdx := int(groups[idx] - 1)
  1147  			exec.ret.groupToSet = groupIdx
  1148  			exec.ret.setGroupNotEmpty(groupIdx)
  1149  			if err := exec.fill(exec.groups[groupIdx], v, getter, setter); err != nil {
  1150  				return err
  1151  			}
  1152  		}
  1153  		idx++
  1154  	}
  1155  	return nil
  1156  }
  1157  
  1158  func (exec *singleAggFuncExec3[to]) Merge(next AggFuncExec, groupIdx1, groupIdx2 int) error {
  1159  	other := next.(*singleAggFuncExec3[to])
  1160  	exec.ret.groupToSet = groupIdx1
  1161  	other.ret.groupToSet = groupIdx2
  1162  
  1163  	exec.ret.mergeEmpty(other.ret.basicResult, groupIdx1, groupIdx2)
  1164  	if err := exec.merge(
  1165  		exec.groups[groupIdx1],
  1166  		other.groups[groupIdx2],
  1167  		exec.ret.aggGet, other.ret.aggGet,
  1168  		exec.ret.aggSet); err != nil {
  1169  		return err
  1170  	}
  1171  	return exec.distinctHash.merge(&other.distinctHash)
  1172  }
  1173  
  1174  func (exec *singleAggFuncExec3[to]) BatchMerge(next AggFuncExec, offset int, groups []uint64) error {
  1175  	other := next.(*singleAggFuncExec3[to])
  1176  	setter := exec.ret.aggSet
  1177  	getter1, getter2 := exec.ret.aggGet, other.ret.aggGet
  1178  
  1179  	for i := range groups {
  1180  		if groups[i] == GroupNotMatched {
  1181  			continue
  1182  		}
  1183  		groupIdx1, groupIdx2 := int(groups[i]-1), i+offset
  1184  		exec.ret.groupToSet = groupIdx1
  1185  		other.ret.groupToSet = groupIdx2
  1186  
  1187  		exec.ret.mergeEmpty(other.ret.basicResult, groupIdx1, groupIdx2)
  1188  		if err := exec.merge(
  1189  			exec.groups[groupIdx1],
  1190  			other.groups[groupIdx2],
  1191  			getter1, getter2,
  1192  			setter); err != nil {
  1193  			return err
  1194  		}
  1195  	}
  1196  	return exec.distinctHash.merge(&other.distinctHash)
  1197  }
  1198  
  1199  func (exec *singleAggFuncExec3[to]) Flush() (*vector.Vector, error) {
  1200  	if exec.partialResult != nil {
  1201  		exec.ret.groupToSet = exec.partialGroup
  1202  		if vs, ok := exec.partialResult.([]byte); ok {
  1203  			exec.ret.setGroupNotEmpty(exec.partialGroup)
  1204  			if err := exec.fill(exec.groups[exec.partialGroup], vs, exec.ret.aggGet, exec.ret.aggSet); err != nil {
  1205  				return nil, err
  1206  			}
  1207  		}
  1208  	}
  1209  
  1210  	if exec.flush == nil {
  1211  		return exec.ret.flush(), nil
  1212  	}
  1213  
  1214  	setter := exec.ret.aggSet
  1215  	getter := exec.ret.aggGet
  1216  	if exec.ret.emptyBeNull {
  1217  		for i, group := range exec.groups {
  1218  			if exec.ret.groupIsEmpty(i) {
  1219  				continue
  1220  			}
  1221  			exec.ret.groupToSet = i
  1222  			if err := exec.flush(group, getter, setter); err != nil {
  1223  				return nil, err
  1224  			}
  1225  		}
  1226  	} else {
  1227  		for i, group := range exec.groups {
  1228  			exec.ret.groupToSet = i
  1229  			if err := exec.flush(group, getter, setter); err != nil {
  1230  				return nil, err
  1231  			}
  1232  		}
  1233  	}
  1234  
  1235  	return exec.ret.flush(), nil
  1236  }
  1237  
  1238  func (exec *singleAggFuncExec3[to]) Free() {
  1239  	exec.ret.free()
  1240  	exec.distinctHash.free()
  1241  }
  1242  
  1243  func (exec *singleAggFuncExec4) init(
  1244  	mg AggMemoryManager,
  1245  	info singleAggInfo,
  1246  	opt singleAggOptimizedInfo,
  1247  	impl aggImplementation) {
  1248  
  1249  	exec.singleAggInfo = info
  1250  	exec.singleAggOptimizedInfo = opt
  1251  	exec.singleAggExecExtraInformation = emptyExtraInfo
  1252  	exec.ret = initBytesAggFuncResult(mg, info.retType, info.emptyNull)
  1253  	exec.groups = make([]SingleAggFromVarRetVar, 0, 1)
  1254  	exec.gGroup = impl.generator.(func() SingleAggFromVarRetVar)
  1255  	exec.fills = impl.fills.(SingleAggFills4)
  1256  	exec.fill = impl.fill.(SingleAggFill4)
  1257  	exec.merge = impl.merge.(SingleAggMerge4)
  1258  	if impl.fillNull != nil {
  1259  		exec.fillNull = impl.fillNull.(SingleAggFillNull4)
  1260  	}
  1261  	if impl.flush != nil {
  1262  		exec.flush = impl.flush.(SingleAggFlush4)
  1263  	}
  1264  	if impl.init != nil {
  1265  		exec.initGroup = impl.init.(SingleAggInit4)
  1266  	}
  1267  
  1268  	if info.distinct {
  1269  		exec.distinctHash = newDistinctHash(mg.Mp(), opt.receiveNull)
  1270  	}
  1271  }
  1272  
  1273  func (exec *singleAggFuncExec4) GroupGrow(more int) error {
  1274  	if exec.IsDistinct() {
  1275  		if err := exec.distinctHash.grows(more); err != nil {
  1276  			return err
  1277  		}
  1278  	}
  1279  
  1280  	if err := exec.ret.grows(more); err != nil {
  1281  		return err
  1282  	}
  1283  
  1284  	setter := exec.ret.aggSet
  1285  	oldLength := len(exec.groups)
  1286  	if cap(exec.groups) >= oldLength+more {
  1287  		exec.groups = exec.groups[:oldLength+more]
  1288  	} else {
  1289  		exec.groups = append(exec.groups, make([]SingleAggFromVarRetVar, more)...)
  1290  	}
  1291  	for i, j := oldLength, len(exec.groups); i < j; i++ {
  1292  		exec.groups[i] = exec.gGroup()
  1293  	}
  1294  
  1295  	if exec.initGroup != nil {
  1296  		for i, j := oldLength, len(exec.groups); i < j; i++ {
  1297  			exec.ret.groupToSet = i
  1298  			if err := exec.initGroup(exec.groups[i], setter, exec.singleAggInfo.argType, exec.singleAggInfo.retType); err != nil {
  1299  				return err
  1300  			}
  1301  		}
  1302  	}
  1303  	return nil
  1304  }
  1305  
  1306  func (exec *singleAggFuncExec4) PreAllocateGroups(more int) error {
  1307  	if len(exec.groups) == 0 {
  1308  		exec.groups = make([]SingleAggFromVarRetVar, 0, more)
  1309  	} else {
  1310  		oldLength := len(exec.groups)
  1311  		exec.groups = append(exec.groups, make([]SingleAggFromVarRetVar, more)...)
  1312  		exec.groups = exec.groups[:oldLength]
  1313  	}
  1314  
  1315  	return exec.ret.preAllocate(more)
  1316  }
  1317  
  1318  func (exec *singleAggFuncExec4) Fill(groupIndex int, row int, vectors []*vector.Vector) error {
  1319  	vec := vectors[0]
  1320  	if vec.IsConst() {
  1321  		row = 0
  1322  	}
  1323  
  1324  	if exec.IsDistinct() {
  1325  		if need, err := exec.distinctHash.fill(groupIndex, vectors, row); !need || err != nil {
  1326  			return err
  1327  		}
  1328  	}
  1329  
  1330  	exec.ret.groupToSet = groupIndex
  1331  	setter := exec.ret.aggSet
  1332  	getter := exec.ret.aggGet
  1333  
  1334  	if vec.IsConstNull() || vec.GetNulls().Contains(uint64(row)) {
  1335  		if exec.receiveNull {
  1336  			exec.ret.setGroupNotEmpty(groupIndex)
  1337  			return exec.fillNull(exec.groups[groupIndex], getter, setter)
  1338  		}
  1339  		return nil
  1340  	}
  1341  
  1342  	exec.ret.setGroupNotEmpty(groupIndex)
  1343  	return exec.fill(exec.groups[groupIndex], vec.GetBytesAt(row), getter, setter)
  1344  }
  1345  
  1346  func (exec *singleAggFuncExec4) BulkFill(groupIndex int, vectors []*vector.Vector) error {
  1347  	vec := vectors[0]
  1348  	length := vec.Length()
  1349  	if length == 0 {
  1350  		return nil
  1351  	}
  1352  
  1353  	if exec.IsDistinct() {
  1354  		return exec.distinctBulkFill(groupIndex, vectors, length)
  1355  	}
  1356  
  1357  	exec.ret.groupToSet = groupIndex
  1358  	setter := exec.ret.aggSet
  1359  	getter := exec.ret.aggGet
  1360  
  1361  	if vec.IsConst() {
  1362  		if vec.IsConstNull() {
  1363  			if exec.receiveNull {
  1364  				exec.ret.setGroupNotEmpty(groupIndex)
  1365  				return exec.fills(exec.groups[groupIndex], nil, true, length, getter, setter)
  1366  			}
  1367  		} else {
  1368  			exec.ret.setGroupNotEmpty(groupIndex)
  1369  			return exec.fills(exec.groups[groupIndex], vec.GetBytesAt(0), false, length, getter, setter)
  1370  		}
  1371  		return nil
  1372  	}
  1373  
  1374  	exec.arg.prepare(vec)
  1375  	if exec.arg.w.WithAnyNullValue() {
  1376  		if exec.receiveNull {
  1377  			exec.ret.setGroupNotEmpty(groupIndex)
  1378  			for i, j := uint64(0), uint64(length); i < j; i++ {
  1379  				v, null := exec.arg.w.GetStrValue(i)
  1380  				if null {
  1381  					if err := exec.fillNull(exec.groups[groupIndex], getter, setter); err != nil {
  1382  						return err
  1383  					}
  1384  				} else {
  1385  					if err := exec.fill(exec.groups[groupIndex], v, getter, setter); err != nil {
  1386  						return err
  1387  					}
  1388  				}
  1389  			}
  1390  		} else {
  1391  			mustNotEmpty := false
  1392  			for i, j := uint64(0), uint64(length); i < j; i++ {
  1393  				v, null := exec.arg.w.GetStrValue(i)
  1394  				if !null {
  1395  					mustNotEmpty = true
  1396  					if err := exec.fill(exec.groups[groupIndex], v, getter, setter); err != nil {
  1397  						return err
  1398  					}
  1399  				}
  1400  			}
  1401  			if mustNotEmpty {
  1402  				exec.ret.setGroupNotEmpty(groupIndex)
  1403  			}
  1404  		}
  1405  		return nil
  1406  	}
  1407  
  1408  	exec.ret.setGroupNotEmpty(groupIndex)
  1409  	for i, j := uint64(0), uint64(length); i < j; i++ {
  1410  		v, _ := exec.arg.w.GetStrValue(i)
  1411  		if err := exec.fill(exec.groups[groupIndex], v, getter, setter); err != nil {
  1412  			return err
  1413  		}
  1414  	}
  1415  	return nil
  1416  }
  1417  
  1418  func (exec *singleAggFuncExec4) BatchFill(offset int, groups []uint64, vectors []*vector.Vector) error {
  1419  	if exec.IsDistinct() {
  1420  		return exec.distinctBatchFill(offset, groups, vectors)
  1421  	}
  1422  
  1423  	vec := vectors[0]
  1424  	setter := exec.ret.aggSet
  1425  	getter := exec.ret.aggGet
  1426  
  1427  	if vec.IsConst() {
  1428  		if vec.IsConstNull() {
  1429  			if exec.receiveNull {
  1430  				for i := 0; i < len(groups); i++ {
  1431  					if groups[i] != GroupNotMatched {
  1432  						groupIdx := int(groups[i] - 1)
  1433  						exec.ret.groupToSet = groupIdx
  1434  						exec.ret.setGroupNotEmpty(groupIdx)
  1435  						if err := exec.fillNull(exec.groups[groupIdx], getter, setter); err != nil {
  1436  							return err
  1437  						}
  1438  					}
  1439  				}
  1440  			}
  1441  			return nil
  1442  		}
  1443  
  1444  		value := vec.GetBytesAt(0)
  1445  		for i := 0; i < len(groups); i++ {
  1446  			if groups[i] != GroupNotMatched {
  1447  				groupIdx := int(groups[i] - 1)
  1448  				exec.ret.groupToSet = groupIdx
  1449  				exec.ret.setGroupNotEmpty(groupIdx)
  1450  				if err := exec.fill(exec.groups[groupIdx], value, getter, setter); err != nil {
  1451  					return err
  1452  				}
  1453  			}
  1454  		}
  1455  		return nil
  1456  	}
  1457  
  1458  	exec.arg.prepare(vec)
  1459  	if exec.arg.w.WithAnyNullValue() {
  1460  		if exec.receiveNull {
  1461  			for i, j, idx := uint64(offset), uint64(offset+len(groups)), 0; i < j; i++ {
  1462  				if groups[idx] != GroupNotMatched {
  1463  					v, null := exec.arg.w.GetStrValue(i)
  1464  					groupIdx := int(groups[idx] - 1)
  1465  					exec.ret.groupToSet = groupIdx
  1466  					exec.ret.setGroupNotEmpty(groupIdx)
  1467  					if null {
  1468  						if err := exec.fillNull(exec.groups[groupIdx], getter, setter); err != nil {
  1469  							return err
  1470  						}
  1471  					} else {
  1472  						if err := exec.fill(exec.groups[groupIdx], v, getter, setter); err != nil {
  1473  							return err
  1474  						}
  1475  					}
  1476  				}
  1477  				idx++
  1478  			}
  1479  			return nil
  1480  		}
  1481  
  1482  		for i, j, idx := uint64(offset), uint64(offset+len(groups)), 0; i < j; i++ {
  1483  			if groups[idx] != GroupNotMatched {
  1484  				v, null := exec.arg.w.GetStrValue(i)
  1485  				if !null {
  1486  					groupIdx := int(groups[idx] - 1)
  1487  					exec.ret.groupToSet = groupIdx
  1488  					exec.ret.setGroupNotEmpty(groupIdx)
  1489  					if err := exec.fill(exec.groups[groupIdx], v, getter, setter); err != nil {
  1490  						return err
  1491  					}
  1492  				}
  1493  			}
  1494  			idx++
  1495  		}
  1496  		return nil
  1497  	}
  1498  
  1499  	for i, j, idx := uint64(offset), uint64(offset+len(groups)), 0; i < j; i++ {
  1500  		if groups[idx] != GroupNotMatched {
  1501  			v, _ := exec.arg.w.GetStrValue(i)
  1502  			groupIdx := int(groups[idx] - 1)
  1503  			exec.ret.groupToSet = groupIdx
  1504  			exec.ret.setGroupNotEmpty(groupIdx)
  1505  			if err := exec.fill(exec.groups[groupIdx], v, getter, setter); err != nil {
  1506  				return err
  1507  			}
  1508  		}
  1509  		idx++
  1510  	}
  1511  	return nil
  1512  }
  1513  
  1514  func (exec *singleAggFuncExec4) Merge(next AggFuncExec, groupIdx1, groupIdx2 int) error {
  1515  	other := next.(*singleAggFuncExec4)
  1516  	exec.ret.groupToSet = groupIdx1
  1517  	other.ret.groupToSet = groupIdx2
  1518  
  1519  	exec.ret.mergeEmpty(other.ret.basicResult, groupIdx1, groupIdx2)
  1520  	if err := exec.merge(
  1521  		exec.groups[groupIdx1], other.groups[groupIdx2],
  1522  		exec.ret.aggGet, other.ret.aggGet,
  1523  		exec.ret.aggSet); err != nil {
  1524  		return err
  1525  	}
  1526  	return exec.distinctHash.merge(&other.distinctHash)
  1527  }
  1528  
  1529  func (exec *singleAggFuncExec4) BatchMerge(next AggFuncExec, offset int, groups []uint64) error {
  1530  	other := next.(*singleAggFuncExec4)
  1531  	setter := exec.ret.aggSet
  1532  	getter1, getter2 := exec.ret.aggGet, other.ret.aggGet
  1533  
  1534  	for i := range groups {
  1535  		if groups[i] == GroupNotMatched {
  1536  			continue
  1537  		}
  1538  		groupIdx1, groupIdx2 := int(groups[i]-1), i+offset
  1539  		exec.ret.groupToSet = groupIdx1
  1540  		other.ret.groupToSet = groupIdx2
  1541  
  1542  		exec.ret.mergeEmpty(other.ret.basicResult, groupIdx1, groupIdx2)
  1543  		if err := exec.merge(
  1544  			exec.groups[groupIdx1], other.groups[groupIdx2],
  1545  			getter1, getter2,
  1546  			setter); err != nil {
  1547  			return err
  1548  		}
  1549  	}
  1550  	return exec.distinctHash.merge(&other.distinctHash)
  1551  }
  1552  
  1553  func (exec *singleAggFuncExec4) Flush() (*vector.Vector, error) {
  1554  	if exec.partialResult != nil {
  1555  		exec.ret.groupToSet = exec.partialGroup
  1556  		if vs, ok := exec.partialResult.([]byte); ok {
  1557  			exec.ret.setGroupNotEmpty(exec.partialGroup)
  1558  			if err := exec.fill(exec.groups[exec.partialGroup], vs, exec.ret.aggGet, exec.ret.aggSet); err != nil {
  1559  				return nil, err
  1560  			}
  1561  		}
  1562  	}
  1563  
  1564  	if exec.flush == nil {
  1565  		return exec.ret.flush(), nil
  1566  	}
  1567  
  1568  	setter := exec.ret.aggSet
  1569  	getter := exec.ret.aggGet
  1570  	if exec.ret.emptyBeNull {
  1571  		for i, group := range exec.groups {
  1572  			if exec.ret.groupIsEmpty(i) {
  1573  				continue
  1574  			}
  1575  			exec.ret.groupToSet = i
  1576  			if err := exec.flush(group, getter, setter); err != nil {
  1577  				return nil, err
  1578  			}
  1579  		}
  1580  	} else {
  1581  		for i, group := range exec.groups {
  1582  			exec.ret.groupToSet = i
  1583  			if err := exec.flush(group, getter, setter); err != nil {
  1584  				return nil, err
  1585  			}
  1586  		}
  1587  	}
  1588  
  1589  	return exec.ret.flush(), nil
  1590  }
  1591  
  1592  func (exec *singleAggFuncExec4) Free() {
  1593  	exec.ret.free()
  1594  	exec.distinctHash.free()
  1595  }