github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/aggexec/single_distinct.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 "github.com/matrixorigin/matrixone/pkg/container/vector"
    18  
    19  func (exec *singleAggFuncExec1[from, to]) distinctBulkFill(
    20  	groupIndex int, vectors []*vector.Vector, length int) error {
    21  	exec.ret.groupToSet = groupIndex
    22  	getter := exec.ret.aggGet
    23  	setter := exec.ret.aggSet
    24  
    25  	vec := vectors[0]
    26  	if vec.IsConst() {
    27  		if need, err := exec.distinctHash.fill(groupIndex, vectors, 0); err != nil || !need {
    28  			return err
    29  		}
    30  
    31  		if vec.IsConstNull() {
    32  			if exec.receiveNull {
    33  				exec.ret.setGroupNotEmpty(groupIndex)
    34  				return exec.fillNull(exec.groups[groupIndex], getter, setter)
    35  			}
    36  		} else {
    37  			exec.ret.setGroupNotEmpty(groupIndex)
    38  			return exec.fill(exec.groups[groupIndex], vector.MustFixedCol[from](vec)[0], getter, setter)
    39  		}
    40  		return nil
    41  	}
    42  
    43  	exec.arg.prepare(vec)
    44  	needs, err := exec.distinctHash.bulkFill(groupIndex, vectors)
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	mustNotEmpty := false
    50  	if exec.receiveNull {
    51  		for i, j := uint64(0), uint64(length); i < j; i++ {
    52  			if needs[i] {
    53  				v, null := exec.arg.w.GetValue(i)
    54  				mustNotEmpty = true
    55  				if null {
    56  					if err = exec.fillNull(exec.groups[groupIndex], getter, setter); err != nil {
    57  						return err
    58  					}
    59  				} else {
    60  					if err = exec.fill(exec.groups[groupIndex], v, getter, setter); err != nil {
    61  						return err
    62  					}
    63  				}
    64  			}
    65  		}
    66  
    67  	} else {
    68  		for i, j := uint64(0), uint64(length); i < j; i++ {
    69  			if needs[i] {
    70  				v, null := exec.arg.w.GetValue(i)
    71  				if !null {
    72  					mustNotEmpty = true
    73  					if err = exec.fill(exec.groups[groupIndex], v, getter, setter); err != nil {
    74  						return err
    75  					}
    76  				}
    77  			}
    78  		}
    79  	}
    80  	if mustNotEmpty {
    81  		exec.ret.setGroupNotEmpty(groupIndex)
    82  	}
    83  	return nil
    84  }
    85  
    86  func (exec *singleAggFuncExec1[from, to]) distinctBatchFill(
    87  	offset int, groups []uint64, vectors []*vector.Vector) error {
    88  	vec := vectors[0]
    89  	setter := exec.ret.aggSet
    90  	getter := exec.ret.aggGet
    91  
    92  	needs, err := exec.distinctHash.batchFill(vectors, offset, groups)
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	if vec.IsConst() {
    98  		if vec.IsConstNull() {
    99  			if exec.receiveNull {
   100  				for i := 0; i < len(groups); i++ {
   101  					if needs[0] && groups[i] != GroupNotMatched {
   102  						groupIdx := int(groups[i] - 1)
   103  						exec.ret.groupToSet = groupIdx
   104  						exec.ret.setGroupNotEmpty(groupIdx)
   105  						if err = exec.fillNull(exec.groups[groupIdx], getter, setter); err != nil {
   106  							return err
   107  						}
   108  					}
   109  				}
   110  			}
   111  			return nil
   112  		}
   113  
   114  		value := vector.MustFixedCol[from](vec)[0]
   115  		for i := 0; i < len(groups); i++ {
   116  			if needs[i] && groups[i] != GroupNotMatched {
   117  				groupIdx := int(groups[i] - 1)
   118  				exec.ret.groupToSet = groupIdx
   119  				exec.ret.setGroupNotEmpty(groupIdx)
   120  				if err = exec.fill(exec.groups[groupIdx], value, getter, setter); err != nil {
   121  					return err
   122  				}
   123  			}
   124  		}
   125  		return nil
   126  	}
   127  
   128  	exec.arg.prepare(vec)
   129  	if exec.receiveNull {
   130  		for i, j, idx := uint64(offset), uint64(offset+len(groups)), 0; i < j; i++ {
   131  			if needs[idx] && groups[idx] != GroupNotMatched {
   132  				v, null := exec.arg.w.GetValue(i)
   133  				groupIdx := int(groups[idx] - 1)
   134  				exec.ret.groupToSet = groupIdx
   135  				exec.ret.setGroupNotEmpty(groupIdx)
   136  				if null {
   137  					if err = exec.fillNull(exec.groups[groupIdx], getter, setter); err != nil {
   138  						return err
   139  					}
   140  				} else {
   141  					if err = exec.fill(exec.groups[groupIdx], v, getter, setter); err != nil {
   142  						return err
   143  					}
   144  				}
   145  			}
   146  			idx++
   147  		}
   148  
   149  	} else {
   150  		for i, j, idx := uint64(offset), uint64(offset+len(groups)), 0; i < j; i++ {
   151  			if needs[idx] && groups[idx] != GroupNotMatched {
   152  				v, null := exec.arg.w.GetValue(i)
   153  				if !null {
   154  					groupIdx := int(groups[idx] - 1)
   155  					exec.ret.groupToSet = groupIdx
   156  					exec.ret.setGroupNotEmpty(groupIdx)
   157  					if err = exec.fill(exec.groups[groupIdx], v, getter, setter); err != nil {
   158  						return err
   159  					}
   160  				}
   161  			}
   162  			idx++
   163  		}
   164  	}
   165  
   166  	return nil
   167  }
   168  
   169  func (exec *singleAggFuncExec2[from]) distinctBulkFill(
   170  	groupIndex int, vectors []*vector.Vector, length int) error {
   171  	exec.ret.groupToSet = groupIndex
   172  	getter := exec.ret.aggGet
   173  	setter := exec.ret.aggSet
   174  
   175  	vec := vectors[0]
   176  	if vec.IsConst() {
   177  		if need, err := exec.distinctHash.fill(groupIndex, vectors, 0); err != nil || !need {
   178  			return err
   179  		}
   180  
   181  		if vec.IsConstNull() {
   182  			if exec.receiveNull {
   183  				exec.ret.setGroupNotEmpty(groupIndex)
   184  				return exec.fillNull(exec.groups[groupIndex], getter, setter)
   185  			}
   186  		} else {
   187  			exec.ret.setGroupNotEmpty(groupIndex)
   188  			return exec.fill(exec.groups[groupIndex], vector.MustFixedCol[from](vec)[0], getter, setter)
   189  		}
   190  		return nil
   191  	}
   192  
   193  	exec.arg.prepare(vec)
   194  	needs, err := exec.distinctHash.bulkFill(groupIndex, vectors)
   195  	if err != nil {
   196  		return err
   197  	}
   198  
   199  	mustNotEmpty := false
   200  	if exec.receiveNull {
   201  		for i, j := uint64(0), uint64(length); i < j; i++ {
   202  			if needs[i] {
   203  				v, null := exec.arg.w.GetValue(i)
   204  				mustNotEmpty = true
   205  				if null {
   206  					if err = exec.fillNull(exec.groups[groupIndex], getter, setter); err != nil {
   207  						return err
   208  					}
   209  				} else {
   210  					if err = exec.fill(exec.groups[groupIndex], v, getter, setter); err != nil {
   211  						return err
   212  					}
   213  				}
   214  			}
   215  		}
   216  
   217  	} else {
   218  		for i, j := uint64(0), uint64(length); i < j; i++ {
   219  			if needs[i] {
   220  				v, null := exec.arg.w.GetValue(i)
   221  				if !null {
   222  					mustNotEmpty = true
   223  					if err = exec.fill(exec.groups[groupIndex], v, getter, setter); err != nil {
   224  						return err
   225  					}
   226  				}
   227  			}
   228  		}
   229  	}
   230  	if mustNotEmpty {
   231  		exec.ret.setGroupNotEmpty(groupIndex)
   232  	}
   233  	return nil
   234  }
   235  
   236  func (exec *singleAggFuncExec2[from]) distinctBatchFill(
   237  	offset int, groups []uint64, vectors []*vector.Vector) error {
   238  	vec := vectors[0]
   239  	setter := exec.ret.aggSet
   240  	getter := exec.ret.aggGet
   241  
   242  	needs, err := exec.distinctHash.batchFill(vectors, offset, groups)
   243  	if err != nil {
   244  		return err
   245  	}
   246  
   247  	if vec.IsConst() {
   248  		if vec.IsConstNull() {
   249  			if exec.receiveNull {
   250  				for i := 0; i < len(groups); i++ {
   251  					if needs[0] && groups[i] != GroupNotMatched {
   252  						groupIdx := int(groups[i] - 1)
   253  						exec.ret.groupToSet = groupIdx
   254  						exec.ret.setGroupNotEmpty(groupIdx)
   255  						if err = exec.fillNull(exec.groups[groupIdx], getter, setter); err != nil {
   256  							return err
   257  						}
   258  					}
   259  				}
   260  			}
   261  			return nil
   262  		}
   263  
   264  		value := vector.MustFixedCol[from](vec)[0]
   265  		for i := 0; i < len(groups); i++ {
   266  			if needs[i] && groups[i] != GroupNotMatched {
   267  				groupIdx := int(groups[i] - 1)
   268  				exec.ret.groupToSet = groupIdx
   269  				exec.ret.setGroupNotEmpty(groupIdx)
   270  				if err = exec.fill(exec.groups[groupIdx], value, getter, setter); err != nil {
   271  					return err
   272  				}
   273  			}
   274  		}
   275  		return nil
   276  	}
   277  
   278  	exec.arg.prepare(vec)
   279  	if exec.receiveNull {
   280  		for i, j, idx := uint64(offset), uint64(offset+len(groups)), 0; i < j; i++ {
   281  			if needs[idx] && groups[idx] != GroupNotMatched {
   282  				v, null := exec.arg.w.GetValue(i)
   283  				groupIdx := int(groups[idx] - 1)
   284  				exec.ret.groupToSet = groupIdx
   285  				exec.ret.setGroupNotEmpty(groupIdx)
   286  				if null {
   287  					if err = exec.fillNull(exec.groups[groupIdx], getter, setter); err != nil {
   288  						return err
   289  					}
   290  				} else {
   291  					if err = exec.fill(exec.groups[groupIdx], v, getter, setter); err != nil {
   292  						return err
   293  					}
   294  				}
   295  			}
   296  			idx++
   297  		}
   298  
   299  	} else {
   300  		for i, j, idx := uint64(offset), uint64(offset+len(groups)), 0; i < j; i++ {
   301  			if needs[idx] && groups[idx] != GroupNotMatched {
   302  				v, null := exec.arg.w.GetValue(i)
   303  				if !null {
   304  					groupIdx := int(groups[idx] - 1)
   305  					exec.ret.groupToSet = groupIdx
   306  					exec.ret.setGroupNotEmpty(groupIdx)
   307  					if err = exec.fill(exec.groups[groupIdx], v, getter, setter); err != nil {
   308  						return err
   309  					}
   310  				}
   311  			}
   312  			idx++
   313  		}
   314  	}
   315  
   316  	return nil
   317  }
   318  
   319  func (exec *singleAggFuncExec3[to]) distinctBulkFill(
   320  	groupIndex int, vectors []*vector.Vector, length int) error {
   321  	exec.ret.groupToSet = groupIndex
   322  	getter := exec.ret.aggGet
   323  	setter := exec.ret.aggSet
   324  
   325  	vec := vectors[0]
   326  	if vec.IsConst() {
   327  		if need, err := exec.distinctHash.fill(groupIndex, vectors, 0); err != nil || !need {
   328  			return err
   329  		}
   330  
   331  		if vec.IsConstNull() {
   332  			if exec.receiveNull {
   333  				exec.ret.setGroupNotEmpty(groupIndex)
   334  				if err := exec.fillNull(exec.groups[groupIndex], getter, setter); err != nil {
   335  					return err
   336  				}
   337  			}
   338  		} else {
   339  			exec.ret.setGroupNotEmpty(groupIndex)
   340  			if err := exec.fill(exec.groups[groupIndex], vector.MustBytesCol(vec)[0], getter, setter); err != nil {
   341  				return err
   342  			}
   343  		}
   344  		return nil
   345  	}
   346  
   347  	exec.arg.prepare(vec)
   348  	needs, err := exec.distinctHash.bulkFill(groupIndex, vectors)
   349  	if err != nil {
   350  		return err
   351  	}
   352  
   353  	mustNotEmpty := false
   354  	if exec.receiveNull {
   355  		for i, j := uint64(0), uint64(length); i < j; i++ {
   356  			if needs[i] {
   357  				v, null := exec.arg.w.GetStrValue(i)
   358  				mustNotEmpty = true
   359  				if null {
   360  					if err = exec.fillNull(exec.groups[groupIndex], getter, setter); err != nil {
   361  						return err
   362  					}
   363  				} else {
   364  					if err = exec.fill(exec.groups[groupIndex], v, getter, setter); err != nil {
   365  						return err
   366  					}
   367  				}
   368  			}
   369  		}
   370  
   371  	} else {
   372  		for i, j := uint64(0), uint64(length); i < j; i++ {
   373  			if needs[i] {
   374  				v, null := exec.arg.w.GetStrValue(i)
   375  				if !null {
   376  					mustNotEmpty = true
   377  					if err = exec.fill(exec.groups[groupIndex], v, getter, setter); err != nil {
   378  						return err
   379  					}
   380  				}
   381  			}
   382  		}
   383  	}
   384  	if mustNotEmpty {
   385  		exec.ret.setGroupNotEmpty(groupIndex)
   386  	}
   387  	return nil
   388  }
   389  
   390  func (exec *singleAggFuncExec3[to]) distinctBatchFill(
   391  	offset int, groups []uint64, vectors []*vector.Vector) error {
   392  	vec := vectors[0]
   393  	setter := exec.ret.aggSet
   394  	getter := exec.ret.aggGet
   395  
   396  	needs, err := exec.distinctHash.batchFill(vectors, offset, groups)
   397  	if err != nil {
   398  		return err
   399  	}
   400  
   401  	if vec.IsConst() {
   402  		if vec.IsConstNull() {
   403  			if exec.receiveNull {
   404  				for i := 0; i < len(groups); i++ {
   405  					if needs[0] && groups[i] != GroupNotMatched {
   406  						groupIdx := int(groups[i] - 1)
   407  						exec.ret.groupToSet = groupIdx
   408  						exec.ret.setGroupNotEmpty(groupIdx)
   409  						if err = exec.fillNull(exec.groups[groupIdx], getter, setter); err != nil {
   410  							return err
   411  						}
   412  					}
   413  				}
   414  			}
   415  			return nil
   416  		}
   417  
   418  		value := vector.MustBytesCol(vec)[0]
   419  		for i := 0; i < len(groups); i++ {
   420  			if needs[i] && groups[i] != GroupNotMatched {
   421  				groupIdx := int(groups[i] - 1)
   422  				exec.ret.groupToSet = groupIdx
   423  				exec.ret.setGroupNotEmpty(groupIdx)
   424  				if err = exec.fill(exec.groups[groupIdx], value, getter, setter); err != nil {
   425  					return err
   426  				}
   427  			}
   428  		}
   429  		return nil
   430  	}
   431  
   432  	exec.arg.prepare(vec)
   433  	if exec.receiveNull {
   434  		for i, j, idx := uint64(offset), uint64(offset+len(groups)), 0; i < j; i++ {
   435  			if needs[idx] && groups[idx] != GroupNotMatched {
   436  				v, null := exec.arg.w.GetStrValue(i)
   437  				groupIdx := int(groups[idx] - 1)
   438  				exec.ret.groupToSet = groupIdx
   439  				exec.ret.setGroupNotEmpty(groupIdx)
   440  				if null {
   441  					if err = exec.fillNull(exec.groups[groupIdx], getter, setter); err != nil {
   442  						return err
   443  					}
   444  				} else {
   445  					if err = exec.fill(exec.groups[groupIdx], v, getter, setter); err != nil {
   446  						return err
   447  					}
   448  				}
   449  			}
   450  			idx++
   451  		}
   452  
   453  	} else {
   454  		for i, j, idx := uint64(offset), uint64(offset+len(groups)), 0; i < j; i++ {
   455  			if needs[idx] && groups[idx] != GroupNotMatched {
   456  				v, null := exec.arg.w.GetStrValue(i)
   457  				if !null {
   458  					groupIdx := int(groups[idx] - 1)
   459  					exec.ret.groupToSet = groupIdx
   460  					exec.ret.setGroupNotEmpty(groupIdx)
   461  					if err = exec.fill(exec.groups[groupIdx], v, getter, setter); err != nil {
   462  						return err
   463  					}
   464  				}
   465  			}
   466  			idx++
   467  		}
   468  	}
   469  
   470  	return nil
   471  }
   472  
   473  func (exec *singleAggFuncExec4) distinctBulkFill(
   474  	groupIndex int, vectors []*vector.Vector, length int) error {
   475  	exec.ret.groupToSet = groupIndex
   476  	getter := exec.ret.aggGet
   477  	setter := exec.ret.aggSet
   478  
   479  	vec := vectors[0]
   480  	if vec.IsConst() {
   481  		if need, err := exec.distinctHash.fill(groupIndex, vectors, 0); err != nil || !need {
   482  			return err
   483  		}
   484  
   485  		if vec.IsConstNull() {
   486  			if exec.receiveNull {
   487  				exec.ret.setGroupNotEmpty(groupIndex)
   488  				return exec.fillNull(exec.groups[groupIndex], getter, setter)
   489  			}
   490  		} else {
   491  			exec.ret.setGroupNotEmpty(groupIndex)
   492  			return exec.fill(exec.groups[groupIndex], vector.MustBytesCol(vec)[0], getter, setter)
   493  		}
   494  		return nil
   495  	}
   496  
   497  	exec.arg.prepare(vec)
   498  	needs, err := exec.distinctHash.bulkFill(groupIndex, vectors)
   499  	if err != nil {
   500  		return err
   501  	}
   502  
   503  	mustNotEmpty := false
   504  	if exec.receiveNull {
   505  		for i, j := uint64(0), uint64(length); i < j; i++ {
   506  			if needs[i] {
   507  				v, null := exec.arg.w.GetStrValue(i)
   508  				mustNotEmpty = true
   509  				if null {
   510  					if err = exec.fillNull(exec.groups[groupIndex], getter, setter); err != nil {
   511  						return err
   512  					}
   513  				} else {
   514  					if err = exec.fill(exec.groups[groupIndex], v, getter, setter); err != nil {
   515  						return err
   516  					}
   517  				}
   518  			}
   519  		}
   520  
   521  	} else {
   522  		for i, j := uint64(0), uint64(length); i < j; i++ {
   523  			if needs[i] {
   524  				v, null := exec.arg.w.GetStrValue(i)
   525  				if !null {
   526  					mustNotEmpty = true
   527  					if err = exec.fill(exec.groups[groupIndex], v, getter, setter); err != nil {
   528  						return err
   529  					}
   530  				}
   531  			}
   532  		}
   533  	}
   534  	if mustNotEmpty {
   535  		exec.ret.setGroupNotEmpty(groupIndex)
   536  	}
   537  	return nil
   538  }
   539  
   540  func (exec *singleAggFuncExec4) distinctBatchFill(
   541  	offset int, groups []uint64, vectors []*vector.Vector) error {
   542  	vec := vectors[0]
   543  	setter := exec.ret.aggSet
   544  	getter := exec.ret.aggGet
   545  
   546  	needs, err := exec.distinctHash.batchFill(vectors, offset, groups)
   547  	if err != nil {
   548  		return err
   549  	}
   550  
   551  	if vec.IsConst() {
   552  		if vec.IsConstNull() {
   553  			if exec.receiveNull {
   554  				for i := 0; i < len(groups); i++ {
   555  					if needs[0] && groups[i] != GroupNotMatched {
   556  						groupIdx := int(groups[i] - 1)
   557  						exec.ret.groupToSet = groupIdx
   558  						exec.ret.setGroupNotEmpty(groupIdx)
   559  						if err = exec.fillNull(exec.groups[groupIdx], getter, setter); err != nil {
   560  							return err
   561  						}
   562  					}
   563  				}
   564  			}
   565  			return nil
   566  		}
   567  
   568  		value := vector.MustBytesCol(vec)[0]
   569  		for i := 0; i < len(groups); i++ {
   570  			if needs[i] && groups[i] != GroupNotMatched {
   571  				groupIdx := int(groups[i] - 1)
   572  				exec.ret.groupToSet = groupIdx
   573  				exec.ret.setGroupNotEmpty(groupIdx)
   574  				if err = exec.fill(exec.groups[groupIdx], value, getter, setter); err != nil {
   575  					return err
   576  				}
   577  			}
   578  		}
   579  		return nil
   580  	}
   581  
   582  	exec.arg.prepare(vec)
   583  	if exec.receiveNull {
   584  		for i, j, idx := uint64(offset), uint64(offset+len(groups)), 0; i < j; i++ {
   585  			if needs[idx] && groups[idx] != GroupNotMatched {
   586  				v, null := exec.arg.w.GetStrValue(i)
   587  				groupIdx := int(groups[idx] - 1)
   588  				exec.ret.groupToSet = groupIdx
   589  				exec.ret.setGroupNotEmpty(groupIdx)
   590  				if null {
   591  					if err = exec.fillNull(exec.groups[groupIdx], getter, setter); err != nil {
   592  						return err
   593  					}
   594  				} else {
   595  					if err = exec.fill(exec.groups[groupIdx], v, getter, setter); err != nil {
   596  						return err
   597  					}
   598  				}
   599  			}
   600  			idx++
   601  		}
   602  
   603  	} else {
   604  		for i, j, idx := uint64(offset), uint64(offset+len(groups)), 0; i < j; i++ {
   605  			if needs[idx] && groups[idx] != GroupNotMatched {
   606  				v, null := exec.arg.w.GetStrValue(i)
   607  				if !null {
   608  					groupIdx := int(groups[idx] - 1)
   609  					exec.ret.groupToSet = groupIdx
   610  					exec.ret.setGroupNotEmpty(groupIdx)
   611  					if err = exec.fill(exec.groups[groupIdx], v, getter, setter); err != nil {
   612  						return err
   613  					}
   614  				}
   615  			}
   616  			idx++
   617  		}
   618  	}
   619  
   620  	return nil
   621  }