github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/function/agg/any_value.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 agg
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/container/types"
    19  	"github.com/matrixorigin/matrixone/pkg/sql/colexec/aggexec"
    20  )
    21  
    22  func RegisterAnyValue1(id int64) {
    23  	aggexec.RegisterSingleAggFromFixedToFixed(
    24  		aggexec.MakeSingleAgg1RegisteredInfo(
    25  			aggexec.MakeSingleColumnAggInformation(id, types.T_uint8.ToType(), AnyValueReturnType, false, true),
    26  			aggexec.GenerateFlagContextFromFixedToFixed[uint8, uint8],
    27  			aggexec.InitFlagContextFromFixedToFixed[uint8, uint8],
    28  			FillAnyValue1[uint8], nil, FillsAnyValue1[uint8],
    29  			MergeAnyValue1[uint8],
    30  			nil,
    31  		))
    32  
    33  	aggexec.RegisterSingleAggFromFixedToFixed(
    34  		aggexec.MakeSingleAgg1RegisteredInfo(
    35  			aggexec.MakeSingleColumnAggInformation(id, types.T_uint16.ToType(), AnyValueReturnType, false, true),
    36  			aggexec.GenerateFlagContextFromFixedToFixed[uint16, uint16],
    37  			aggexec.InitFlagContextFromFixedToFixed[uint16, uint16],
    38  			FillAnyValue1[uint16], nil, FillsAnyValue1[uint16],
    39  			MergeAnyValue1[uint16],
    40  			nil,
    41  		))
    42  
    43  	aggexec.RegisterSingleAggFromFixedToFixed(
    44  		aggexec.MakeSingleAgg1RegisteredInfo(
    45  			aggexec.MakeSingleColumnAggInformation(id, types.T_uint32.ToType(), AnyValueReturnType, false, true),
    46  			aggexec.GenerateFlagContextFromFixedToFixed[uint32, uint32],
    47  			aggexec.InitFlagContextFromFixedToFixed[uint32, uint32],
    48  			FillAnyValue1[uint32], nil, FillsAnyValue1[uint32],
    49  			MergeAnyValue1[uint32],
    50  			nil,
    51  		))
    52  
    53  	aggexec.RegisterSingleAggFromFixedToFixed(
    54  		aggexec.MakeSingleAgg1RegisteredInfo(
    55  			aggexec.MakeSingleColumnAggInformation(id, types.T_uint64.ToType(), AnyValueReturnType, false, true),
    56  			aggexec.GenerateFlagContextFromFixedToFixed[uint64, uint64],
    57  			aggexec.InitFlagContextFromFixedToFixed[uint64, uint64],
    58  			FillAnyValue1[uint64], nil, FillsAnyValue1[uint64],
    59  			MergeAnyValue1[uint64],
    60  			nil,
    61  		))
    62  
    63  	aggexec.RegisterSingleAggFromFixedToFixed(
    64  		aggexec.MakeSingleAgg1RegisteredInfo(
    65  			aggexec.MakeSingleColumnAggInformation(id, types.T_int8.ToType(), AnyValueReturnType, false, true),
    66  			aggexec.GenerateFlagContextFromFixedToFixed[int8, int8],
    67  			aggexec.InitFlagContextFromFixedToFixed[int8, int8],
    68  			FillAnyValue1[int8], nil, FillsAnyValue1[int8],
    69  			MergeAnyValue1[int8],
    70  			nil,
    71  		))
    72  
    73  	aggexec.RegisterSingleAggFromFixedToFixed(
    74  		aggexec.MakeSingleAgg1RegisteredInfo(
    75  			aggexec.MakeSingleColumnAggInformation(id, types.T_int16.ToType(), AnyValueReturnType, false, true),
    76  			aggexec.GenerateFlagContextFromFixedToFixed[int16, int16],
    77  			aggexec.InitFlagContextFromFixedToFixed[int16, int16],
    78  			FillAnyValue1[int16], nil, FillsAnyValue1[int16],
    79  			MergeAnyValue1[int16],
    80  			nil,
    81  		))
    82  
    83  	aggexec.RegisterSingleAggFromFixedToFixed(
    84  		aggexec.MakeSingleAgg1RegisteredInfo(
    85  			aggexec.MakeSingleColumnAggInformation(id, types.T_int32.ToType(), AnyValueReturnType, false, true),
    86  			aggexec.GenerateFlagContextFromFixedToFixed[int32, int32],
    87  			aggexec.InitFlagContextFromFixedToFixed[int32, int32],
    88  			FillAnyValue1[int32], nil, FillsAnyValue1[int32],
    89  			MergeAnyValue1[int32],
    90  			nil,
    91  		))
    92  
    93  	aggexec.RegisterSingleAggFromFixedToFixed(
    94  		aggexec.MakeSingleAgg1RegisteredInfo(
    95  			aggexec.MakeSingleColumnAggInformation(id, types.T_int64.ToType(), AnyValueReturnType, false, true),
    96  			aggexec.GenerateFlagContextFromFixedToFixed[int64, int64],
    97  			aggexec.InitFlagContextFromFixedToFixed[int64, int64],
    98  			FillAnyValue1[int64], nil, FillsAnyValue1[int64],
    99  			MergeAnyValue1[int64],
   100  			nil,
   101  		))
   102  
   103  	aggexec.RegisterSingleAggFromFixedToFixed(
   104  		aggexec.MakeSingleAgg1RegisteredInfo(
   105  			aggexec.MakeSingleColumnAggInformation(id, types.T_float32.ToType(), AnyValueReturnType, false, true),
   106  			aggexec.GenerateFlagContextFromFixedToFixed[float32, float32],
   107  			aggexec.InitFlagContextFromFixedToFixed[float32, float32],
   108  			FillAnyValue1[float32], nil, FillsAnyValue1[float32],
   109  			MergeAnyValue1[float32],
   110  			nil,
   111  		))
   112  
   113  	aggexec.RegisterSingleAggFromFixedToFixed(
   114  		aggexec.MakeSingleAgg1RegisteredInfo(
   115  			aggexec.MakeSingleColumnAggInformation(id, types.T_float64.ToType(), AnyValueReturnType, false, true),
   116  			aggexec.GenerateFlagContextFromFixedToFixed[float64, float64],
   117  			aggexec.InitFlagContextFromFixedToFixed[float64, float64],
   118  			FillAnyValue1[float64], nil, FillsAnyValue1[float64],
   119  			MergeAnyValue1[float64],
   120  			nil,
   121  		))
   122  
   123  	aggexec.RegisterSingleAggFromFixedToFixed(
   124  		aggexec.MakeSingleAgg1RegisteredInfo(
   125  			aggexec.MakeSingleColumnAggInformation(id, types.T_date.ToType(), AnyValueReturnType, false, true),
   126  			aggexec.GenerateFlagContextFromFixedToFixed[types.Date, types.Date],
   127  			aggexec.InitFlagContextFromFixedToFixed[types.Date, types.Date],
   128  			FillAnyValue1[types.Date], nil, FillsAnyValue1[types.Date],
   129  			MergeAnyValue1[types.Date],
   130  			nil,
   131  		))
   132  
   133  	aggexec.RegisterSingleAggFromFixedToFixed(
   134  		aggexec.MakeSingleAgg1RegisteredInfo(
   135  			aggexec.MakeSingleColumnAggInformation(id, types.T_datetime.ToType(), AnyValueReturnType, false, true),
   136  			aggexec.GenerateFlagContextFromFixedToFixed[types.Datetime, types.Datetime],
   137  			aggexec.InitFlagContextFromFixedToFixed[types.Datetime, types.Datetime],
   138  			FillAnyValue1[types.Datetime], nil, FillsAnyValue1[types.Datetime],
   139  			MergeAnyValue1[types.Datetime],
   140  			nil,
   141  		))
   142  
   143  	aggexec.RegisterSingleAggFromFixedToFixed(
   144  		aggexec.MakeSingleAgg1RegisteredInfo(
   145  			aggexec.MakeSingleColumnAggInformation(id, types.T_timestamp.ToType(), AnyValueReturnType, false, true),
   146  			aggexec.GenerateFlagContextFromFixedToFixed[types.Timestamp, types.Timestamp],
   147  			aggexec.InitFlagContextFromFixedToFixed[types.Timestamp, types.Timestamp],
   148  			FillAnyValue1[types.Timestamp], nil, FillsAnyValue1[types.Timestamp],
   149  			MergeAnyValue1[types.Timestamp],
   150  			nil,
   151  		))
   152  
   153  	aggexec.RegisterSingleAggFromFixedToFixed(
   154  		aggexec.MakeSingleAgg1RegisteredInfo(
   155  			aggexec.MakeSingleColumnAggInformation(id, types.T_time.ToType(), AnyValueReturnType, false, true),
   156  			aggexec.GenerateFlagContextFromFixedToFixed[types.Time, types.Time],
   157  			aggexec.InitFlagContextFromFixedToFixed[types.Time, types.Time],
   158  			FillAnyValue1[types.Time], nil, FillsAnyValue1[types.Time],
   159  			MergeAnyValue1[types.Time],
   160  			nil,
   161  		))
   162  
   163  	aggexec.RegisterSingleAggFromFixedToFixed(
   164  		aggexec.MakeSingleAgg1RegisteredInfo(
   165  			aggexec.MakeSingleColumnAggInformation(id, types.T_decimal64.ToType(), AnyValueReturnType, false, true),
   166  			aggexec.GenerateFlagContextFromFixedToFixed[types.Decimal64, types.Decimal64],
   167  			aggexec.InitFlagContextFromFixedToFixed[types.Decimal64, types.Decimal64],
   168  			FillAnyValue1[types.Decimal64], nil, FillsAnyValue1[types.Decimal64],
   169  			MergeAnyValue1[types.Decimal64],
   170  			nil,
   171  		))
   172  
   173  	aggexec.RegisterSingleAggFromFixedToFixed(
   174  		aggexec.MakeSingleAgg1RegisteredInfo(
   175  			aggexec.MakeSingleColumnAggInformation(id, types.T_decimal128.ToType(), AnyValueReturnType, false, true),
   176  			aggexec.GenerateFlagContextFromFixedToFixed[types.Decimal128, types.Decimal128],
   177  			aggexec.InitFlagContextFromFixedToFixed[types.Decimal128, types.Decimal128],
   178  			FillAnyValue1[types.Decimal128], nil, FillsAnyValue1[types.Decimal128],
   179  			MergeAnyValue1[types.Decimal128],
   180  			nil,
   181  		))
   182  
   183  	aggexec.RegisterSingleAggFromFixedToFixed(
   184  		aggexec.MakeSingleAgg1RegisteredInfo(
   185  			aggexec.MakeSingleColumnAggInformation(id, types.T_bool.ToType(), AnyValueReturnType, false, true),
   186  			aggexec.GenerateFlagContextFromFixedToFixed[bool, bool],
   187  			aggexec.InitFlagContextFromFixedToFixed[bool, bool],
   188  			FillAnyValue1[bool], nil, FillsAnyValue1[bool],
   189  			MergeAnyValue1[bool],
   190  			nil,
   191  		))
   192  
   193  	aggexec.RegisterSingleAggFromFixedToFixed(
   194  		aggexec.MakeSingleAgg1RegisteredInfo(
   195  			aggexec.MakeSingleColumnAggInformation(id, types.T_bit.ToType(), AnyValueReturnType, false, true),
   196  			aggexec.GenerateFlagContextFromFixedToFixed[uint64, uint64],
   197  			aggexec.InitFlagContextFromFixedToFixed[uint64, uint64],
   198  			FillAnyValue1[uint64], nil, FillsAnyValue1[uint64],
   199  			MergeAnyValue1[uint64],
   200  			nil,
   201  		))
   202  
   203  	aggexec.RegisterSingleAggFromFixedToFixed(
   204  		aggexec.MakeSingleAgg1RegisteredInfo(
   205  			aggexec.MakeSingleColumnAggInformation(id, types.T_uuid.ToType(), AnyValueReturnType, false, true),
   206  			aggexec.GenerateFlagContextFromFixedToFixed[types.Uuid, types.Uuid],
   207  			aggexec.InitFlagContextFromFixedToFixed[types.Uuid, types.Uuid],
   208  			FillAnyValue1[types.Uuid], nil, FillsAnyValue1[types.Uuid],
   209  			MergeAnyValue1[types.Uuid],
   210  			nil,
   211  		))
   212  
   213  	aggexec.RegisterSingleAggFromFixedToFixed(
   214  		aggexec.MakeSingleAgg1RegisteredInfo(
   215  			aggexec.MakeSingleColumnAggInformation(id, types.T_Rowid.ToType(), AnyValueReturnType, false, true),
   216  			aggexec.GenerateFlagContextFromFixedToFixed[types.Rowid, types.Rowid],
   217  			aggexec.InitFlagContextFromFixedToFixed[types.Rowid, types.Rowid],
   218  			FillAnyValue1[types.Rowid], nil, FillsAnyValue1[types.Rowid],
   219  			MergeAnyValue1[types.Rowid],
   220  			nil,
   221  		))
   222  
   223  	varlenTs := []types.T{types.T_varchar, types.T_char, types.T_blob, types.T_text, types.T_binary, types.T_varbinary}
   224  	for _, t := range varlenTs {
   225  		aggexec.RegisterSingleAggFromVarToVar(
   226  			aggexec.MakeSingleAgg4RegisteredInfo(
   227  				aggexec.MakeSingleColumnAggInformation(id, t.ToType(), AnyValueReturnType, false, true),
   228  				aggexec.GenerateFlagContextFromVarToVar,
   229  				aggexec.InitFlagContextFromVarToVar,
   230  				FillAnyValue2, nil, FillsAnyValue2,
   231  				MergeAnyValue2,
   232  				nil,
   233  			))
   234  	}
   235  }
   236  
   237  var AnyValueSupportedTypes = []types.T{
   238  	types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   239  	types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   240  	types.T_float32, types.T_float64,
   241  	types.T_date, types.T_datetime,
   242  	types.T_timestamp, types.T_time,
   243  	types.T_decimal64, types.T_decimal128,
   244  	types.T_bool,
   245  	types.T_bit,
   246  	types.T_varchar, types.T_char, types.T_blob, types.T_text,
   247  	types.T_uuid,
   248  	types.T_binary, types.T_varbinary,
   249  	types.T_Rowid,
   250  }
   251  
   252  func AnyValueReturnType(typs []types.Type) types.Type {
   253  	return typs[0]
   254  }
   255  
   256  func FillAnyValue1[from types.FixedSizeTExceptStrType](
   257  	exec aggexec.SingleAggFromFixedRetFixed[from, from], value from, getter aggexec.AggGetter[from], setter aggexec.AggSetter[from]) error {
   258  	a := exec.(*aggexec.ContextWithEmptyFlagOfSingleAggRetFixed[from])
   259  	if a.IsEmpty {
   260  		a.IsEmpty = false
   261  		setter(value)
   262  	}
   263  	return nil
   264  }
   265  func FillsAnyValue1[from types.FixedSizeTExceptStrType](
   266  	exec aggexec.SingleAggFromFixedRetFixed[from, from],
   267  	value from, isNull bool, count int, getter aggexec.AggGetter[from], setter aggexec.AggSetter[from]) error {
   268  	if !isNull {
   269  		a := exec.(*aggexec.ContextWithEmptyFlagOfSingleAggRetFixed[from])
   270  		if a.IsEmpty {
   271  			a.IsEmpty = false
   272  			setter(value)
   273  		}
   274  	}
   275  	return nil
   276  }
   277  func MergeAnyValue1[from types.FixedSizeTExceptStrType](
   278  	exec1, exec2 aggexec.SingleAggFromFixedRetFixed[from, from], getter1, getter2 aggexec.AggGetter[from], setter aggexec.AggSetter[from]) error {
   279  	a1 := exec1.(*aggexec.ContextWithEmptyFlagOfSingleAggRetFixed[from])
   280  	a2 := exec2.(*aggexec.ContextWithEmptyFlagOfSingleAggRetFixed[from])
   281  	if a1.IsEmpty && !a2.IsEmpty {
   282  		a1.IsEmpty = false
   283  		setter(getter2())
   284  	}
   285  	return nil
   286  }
   287  
   288  func FillAnyValue2(
   289  	exec aggexec.SingleAggFromVarRetVar, value []byte, getter aggexec.AggBytesGetter, setter aggexec.AggBytesSetter) error {
   290  	a := exec.(*aggexec.ContextWithEmptyFlagOfSingleAggRetBytes)
   291  	if a.IsEmpty {
   292  		a.IsEmpty = false
   293  		return setter(value)
   294  	}
   295  	return nil
   296  }
   297  func FillsAnyValue2(
   298  	exec aggexec.SingleAggFromVarRetVar,
   299  	value []byte, isNull bool, count int, getter aggexec.AggBytesGetter, setter aggexec.AggBytesSetter) error {
   300  	if !isNull {
   301  		a := exec.(*aggexec.ContextWithEmptyFlagOfSingleAggRetBytes)
   302  		if a.IsEmpty {
   303  			a.IsEmpty = false
   304  			return setter(value)
   305  		}
   306  	}
   307  	return nil
   308  }
   309  func MergeAnyValue2(
   310  	exec1, exec2 aggexec.SingleAggFromVarRetVar, getter1, getter2 aggexec.AggBytesGetter, setter aggexec.AggBytesSetter) error {
   311  	a1 := exec1.(*aggexec.ContextWithEmptyFlagOfSingleAggRetBytes)
   312  	a2 := exec2.(*aggexec.ContextWithEmptyFlagOfSingleAggRetBytes)
   313  	if a1.IsEmpty && !a2.IsEmpty {
   314  		a1.IsEmpty = false
   315  		return setter(getter2())
   316  	}
   317  	return nil
   318  }