github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/function/agg/max.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  	"bytes"
    19  	"github.com/matrixorigin/matrixone/pkg/container/types"
    20  	"github.com/matrixorigin/matrixone/pkg/sql/colexec/aggexec"
    21  	"math"
    22  )
    23  
    24  func RegisterMax1(id int64) {
    25  	aggexec.RegisterSingleAggFromFixedToFixed(
    26  		aggexec.MakeSingleAgg1RegisteredInfo(
    27  			aggexec.MakeSingleColumnAggInformation(id, types.T_uint8.ToType(), MaxReturnType, false, true),
    28  			newAggMax[uint8],
    29  			InitAggMax1[uint8],
    30  			FillAggMax1[uint8], nil, FillsAggMax1[uint8],
    31  			MergeAggMax1[uint8],
    32  			nil,
    33  		))
    34  
    35  	aggexec.RegisterSingleAggFromFixedToFixed(
    36  		aggexec.MakeSingleAgg1RegisteredInfo(
    37  			aggexec.MakeSingleColumnAggInformation(id, types.T_uint16.ToType(), MaxReturnType, false, true),
    38  			newAggMax[uint16],
    39  			InitAggMax1[uint16],
    40  			FillAggMax1[uint16], nil, FillsAggMax1[uint16],
    41  			MergeAggMax1[uint16],
    42  			nil,
    43  		))
    44  
    45  	aggexec.RegisterSingleAggFromFixedToFixed(
    46  		aggexec.MakeSingleAgg1RegisteredInfo(
    47  			aggexec.MakeSingleColumnAggInformation(id, types.T_uint32.ToType(), MaxReturnType, false, true),
    48  			newAggMax[uint32],
    49  			InitAggMax1[uint32],
    50  			FillAggMax1[uint32], nil, FillsAggMax1[uint32],
    51  			MergeAggMax1[uint32],
    52  			nil,
    53  		))
    54  
    55  	aggexec.RegisterSingleAggFromFixedToFixed(
    56  		aggexec.MakeSingleAgg1RegisteredInfo(
    57  			aggexec.MakeSingleColumnAggInformation(id, types.T_uint64.ToType(), MaxReturnType, false, true),
    58  			newAggMax[uint64],
    59  			InitAggMax1[uint64],
    60  			FillAggMax1[uint64], nil, FillsAggMax1[uint64],
    61  			MergeAggMax1[uint64],
    62  			nil,
    63  		))
    64  
    65  	aggexec.RegisterSingleAggFromFixedToFixed(
    66  		aggexec.MakeSingleAgg1RegisteredInfo(
    67  			aggexec.MakeSingleColumnAggInformation(id, types.T_int8.ToType(), MaxReturnType, false, true),
    68  			newAggMax[int8],
    69  			InitAggMax1[int8],
    70  			FillAggMax1[int8], nil, FillsAggMax1[int8],
    71  			MergeAggMax1[int8],
    72  			nil,
    73  		))
    74  
    75  	aggexec.RegisterSingleAggFromFixedToFixed(
    76  		aggexec.MakeSingleAgg1RegisteredInfo(
    77  			aggexec.MakeSingleColumnAggInformation(id, types.T_int16.ToType(), MaxReturnType, false, true),
    78  			newAggMax[int16],
    79  			InitAggMax1[int16],
    80  			FillAggMax1[int16], nil, FillsAggMax1[int16],
    81  			MergeAggMax1[int16],
    82  			nil,
    83  		))
    84  
    85  	aggexec.RegisterSingleAggFromFixedToFixed(
    86  		aggexec.MakeSingleAgg1RegisteredInfo(
    87  			aggexec.MakeSingleColumnAggInformation(id, types.T_int32.ToType(), MaxReturnType, false, true),
    88  			newAggMax[int32],
    89  			InitAggMax1[int32],
    90  			FillAggMax1[int32], nil, FillsAggMax1[int32],
    91  			MergeAggMax1[int32],
    92  			nil,
    93  		))
    94  
    95  	aggexec.RegisterSingleAggFromFixedToFixed(
    96  		aggexec.MakeSingleAgg1RegisteredInfo(
    97  			aggexec.MakeSingleColumnAggInformation(id, types.T_int64.ToType(), MaxReturnType, false, true),
    98  			newAggMax[int64],
    99  			InitAggMax1[int64],
   100  			FillAggMax1[int64], nil, FillsAggMax1[int64],
   101  			MergeAggMax1[int64],
   102  			nil,
   103  		))
   104  
   105  	aggexec.RegisterSingleAggFromFixedToFixed(
   106  		aggexec.MakeSingleAgg1RegisteredInfo(
   107  			aggexec.MakeSingleColumnAggInformation(id, types.T_float32.ToType(), MaxReturnType, false, true),
   108  			newAggMax[float32],
   109  			InitAggMax1[float32],
   110  			FillAggMax1[float32], nil, FillsAggMax1[float32],
   111  			MergeAggMax1[float32],
   112  			nil,
   113  		))
   114  
   115  	aggexec.RegisterSingleAggFromFixedToFixed(
   116  		aggexec.MakeSingleAgg1RegisteredInfo(
   117  			aggexec.MakeSingleColumnAggInformation(id, types.T_float64.ToType(), MaxReturnType, false, true),
   118  			newAggMax[float64],
   119  			InitAggMax1[float64],
   120  			FillAggMax1[float64], nil, FillsAggMax1[float64],
   121  			MergeAggMax1[float64],
   122  			nil,
   123  		))
   124  
   125  	aggexec.RegisterSingleAggFromFixedToFixed(
   126  		aggexec.MakeSingleAgg1RegisteredInfo(
   127  			aggexec.MakeSingleColumnAggInformation(id, types.T_date.ToType(), MaxReturnType, false, true),
   128  			newAggMax[types.Date],
   129  			InitAggMax1[types.Date],
   130  			FillAggMax1[types.Date], nil, FillsAggMax1[types.Date],
   131  			MergeAggMax1[types.Date],
   132  			nil,
   133  		))
   134  
   135  	aggexec.RegisterSingleAggFromFixedToFixed(
   136  		aggexec.MakeSingleAgg1RegisteredInfo(
   137  			aggexec.MakeSingleColumnAggInformation(id, types.T_datetime.ToType(), MaxReturnType, false, true),
   138  			newAggMax[types.Datetime],
   139  			InitAggMax1[types.Datetime],
   140  			FillAggMax1[types.Datetime], nil, FillsAggMax1[types.Datetime],
   141  			MergeAggMax1[types.Datetime],
   142  			nil,
   143  		))
   144  
   145  	aggexec.RegisterSingleAggFromFixedToFixed(
   146  		aggexec.MakeSingleAgg1RegisteredInfo(
   147  			aggexec.MakeSingleColumnAggInformation(id, types.T_timestamp.ToType(), MaxReturnType, false, true),
   148  			newAggMax[types.Timestamp],
   149  			InitAggMax1[types.Timestamp],
   150  			FillAggMax1[types.Timestamp], nil, FillsAggMax1[types.Timestamp],
   151  			MergeAggMax1[types.Timestamp],
   152  			nil,
   153  		))
   154  
   155  	aggexec.RegisterSingleAggFromFixedToFixed(
   156  		aggexec.MakeSingleAgg1RegisteredInfo(
   157  			aggexec.MakeSingleColumnAggInformation(id, types.T_time.ToType(), MaxReturnType, false, true),
   158  			newAggMax[types.Time],
   159  			InitAggMax1[types.Time],
   160  			FillAggMax1[types.Time], nil, FillsAggMax1[types.Time],
   161  			MergeAggMax1[types.Time],
   162  			nil,
   163  		))
   164  
   165  	aggexec.RegisterSingleAggFromFixedToFixed(
   166  		aggexec.MakeSingleAgg1RegisteredInfo(
   167  			aggexec.MakeSingleColumnAggInformation(id, types.T_bit.ToType(), MaxReturnType, false, true),
   168  			newAggMax[uint64],
   169  			InitAggMax1[uint64],
   170  			FillAggMax1[uint64], nil, FillsAggMax1[uint64],
   171  			MergeAggMax1[uint64],
   172  			nil,
   173  		))
   174  
   175  	aggexec.RegisterSingleAggFromFixedToFixed(
   176  		aggexec.MakeSingleAgg1RegisteredInfo(
   177  			aggexec.MakeSingleColumnAggInformation(id, types.T_bool.ToType(), MaxReturnType, false, true),
   178  			aggexec.GenerateEmptyContextFromFixedToFixed[bool, bool],
   179  			InitAggMaxBool,
   180  			FillAggMaxBool, nil, FillsAggMaxBool,
   181  			MergeAggMaxBool,
   182  			nil,
   183  		))
   184  
   185  	aggexec.RegisterSingleAggFromFixedToFixed(
   186  		aggexec.MakeSingleAgg1RegisteredInfo(
   187  			aggexec.MakeSingleColumnAggInformation(id, types.T_uuid.ToType(), MaxReturnType, false, true),
   188  			aggexec.GenerateFlagContextFromFixedToFixed[types.Uuid, types.Uuid],
   189  			aggexec.InitFlagContextFromFixedToFixed[types.Uuid, types.Uuid],
   190  			FillAggMaxUuid, nil, FillsAggMaxUuid,
   191  			MergeAggMaxUuid,
   192  			nil,
   193  		))
   194  
   195  	aggexec.RegisterSingleAggFromFixedToFixed(
   196  		aggexec.MakeSingleAgg1RegisteredInfo(
   197  			aggexec.MakeSingleColumnAggInformation(id, types.T_decimal64.ToType(), MaxReturnType, false, true),
   198  			newAggMaxDecimal64,
   199  			InitAggMaxDecimal64,
   200  			FillAggMaxDecimal64, nil, FillsAggMaxDecimal64,
   201  			MergeAggMaxDecimal64,
   202  			nil,
   203  		))
   204  
   205  	aggexec.RegisterSingleAggFromFixedToFixed(
   206  		aggexec.MakeSingleAgg1RegisteredInfo(
   207  			aggexec.MakeSingleColumnAggInformation(id, types.T_decimal128.ToType(), MaxReturnType, false, true),
   208  			newAggMaxDecimal128,
   209  			InitAggMaxDecimal128,
   210  			FillAggMaxDecimal128, nil, FillsAggMaxDecimal128,
   211  			MergeAggMaxDecimal128,
   212  			nil,
   213  		))
   214  
   215  	varlenList := []types.T{types.T_varchar, types.T_char, types.T_blob, types.T_text, types.T_binary, types.T_varbinary}
   216  	for _, t := range varlenList {
   217  		aggexec.RegisterSingleAggFromVarToVar(
   218  			aggexec.MakeSingleAgg4RegisteredInfo(
   219  				aggexec.MakeSingleColumnAggInformation(id, t.ToType(), MaxReturnType, false, true),
   220  				aggexec.GenerateFlagContextFromVarToVar,
   221  				aggexec.InitFlagContextFromVarToVar,
   222  				FillAggMaxBytes, nil, FillsAggMaxBytes,
   223  				MergeAggMaxBytes,
   224  				nil,
   225  			))
   226  	}
   227  }
   228  
   229  var MaxSupportedTypes = []types.T{
   230  	types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   231  	types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   232  	types.T_float32, types.T_float64,
   233  	types.T_date, types.T_datetime,
   234  	types.T_timestamp, types.T_time,
   235  	types.T_decimal64, types.T_decimal128,
   236  	types.T_bool,
   237  	types.T_bit,
   238  	types.T_varchar, types.T_char, types.T_blob, types.T_text,
   239  	types.T_uuid,
   240  	types.T_binary, types.T_varbinary,
   241  }
   242  
   243  func MaxReturnType(typs []types.Type) types.Type {
   244  	return typs[0]
   245  }
   246  
   247  type aggMax[from canCompare] struct{}
   248  
   249  func newAggMax[from canCompare]() aggexec.SingleAggFromFixedRetFixed[from, from] {
   250  	return aggMax[from]{}
   251  }
   252  func (a aggMax[from]) Marshal() []byte  { return nil }
   253  func (a aggMax[from]) Unmarshal([]byte) {}
   254  
   255  var fromTypeIDtoMinValue = map[types.T]interface{}{
   256  	types.T_bit:       uint64(0),
   257  	types.T_uint8:     uint8(0),
   258  	types.T_uint16:    uint16(0),
   259  	types.T_uint32:    uint32(0),
   260  	types.T_uint64:    uint64(0),
   261  	types.T_int8:      int8(math.MinInt8),
   262  	types.T_int16:     int16(math.MinInt16),
   263  	types.T_int32:     int32(math.MinInt32),
   264  	types.T_int64:     int64(math.MinInt64),
   265  	types.T_float32:   float32(-math.MaxFloat32),
   266  	types.T_float64:   -math.MaxFloat64,
   267  	types.T_date:      types.Date(0),
   268  	types.T_datetime:  types.Datetime(0),
   269  	types.T_timestamp: types.Timestamp(0),
   270  	types.T_time:      types.Time(0),
   271  }
   272  
   273  func InitAggMax1[from canCompare](
   274  	exec aggexec.SingleAggFromFixedRetFixed[from, from], setter aggexec.AggSetter[from], arg, ret types.Type) error {
   275  	setter(fromTypeIDtoMinValue[arg.Oid].(from))
   276  	return nil
   277  }
   278  func FillAggMax1[from canCompare](
   279  	exec aggexec.SingleAggFromFixedRetFixed[from, from],
   280  	value from, getter aggexec.AggGetter[from], setter aggexec.AggSetter[from]) error {
   281  	if value > getter() {
   282  		setter(value)
   283  	}
   284  	return nil
   285  }
   286  func FillsAggMax1[from canCompare](
   287  	exec aggexec.SingleAggFromFixedRetFixed[from, from],
   288  	value from, isNull bool, count int, getter aggexec.AggGetter[from], setter aggexec.AggSetter[from]) error {
   289  	if isNull {
   290  		return nil
   291  	}
   292  	return FillAggMax1(exec, value, getter, setter)
   293  }
   294  func MergeAggMax1[from canCompare](
   295  	exec1, exec2 aggexec.SingleAggFromFixedRetFixed[from, from],
   296  	getter1, getter2 aggexec.AggGetter[from], setter1 aggexec.AggSetter[from]) error {
   297  	if getter2() > getter1() {
   298  		setter1(getter2())
   299  	}
   300  	return nil
   301  }
   302  
   303  func InitAggMaxBool(exec aggexec.SingleAggFromFixedRetFixed[bool, bool], setter aggexec.AggSetter[bool], arg, ret types.Type) error {
   304  	setter(false)
   305  	return nil
   306  }
   307  func FillAggMaxBool(
   308  	exec aggexec.SingleAggFromFixedRetFixed[bool, bool],
   309  	value bool, getter aggexec.AggGetter[bool], setter aggexec.AggSetter[bool]) error {
   310  	if value {
   311  		setter(true)
   312  	}
   313  	return nil
   314  }
   315  func FillsAggMaxBool(
   316  	exec aggexec.SingleAggFromFixedRetFixed[bool, bool],
   317  	value bool, isNull bool, count int, getter aggexec.AggGetter[bool], setter aggexec.AggSetter[bool]) error {
   318  	if isNull {
   319  		return nil
   320  	}
   321  	return FillAggMaxBool(exec, value, getter, setter)
   322  }
   323  func MergeAggMaxBool(
   324  	exec1, exec2 aggexec.SingleAggFromFixedRetFixed[bool, bool],
   325  	getter1, getter2 aggexec.AggGetter[bool], setter1 aggexec.AggSetter[bool]) error {
   326  	if getter2() {
   327  		setter1(true)
   328  	}
   329  	return nil
   330  }
   331  
   332  func FillAggMaxUuid(
   333  	exec aggexec.SingleAggFromFixedRetFixed[types.Uuid, types.Uuid],
   334  	value types.Uuid, getter aggexec.AggGetter[types.Uuid], setter aggexec.AggSetter[types.Uuid]) error {
   335  	a := exec.(*aggexec.ContextWithEmptyFlagOfSingleAggRetFixed[types.Uuid])
   336  	if a.IsEmpty {
   337  		a.IsEmpty = false
   338  		setter(value)
   339  	} else {
   340  		if value.Compare(getter()) > 0 {
   341  			setter(value)
   342  		}
   343  	}
   344  	return nil
   345  }
   346  func FillsAggMaxUuid(
   347  	exec aggexec.SingleAggFromFixedRetFixed[types.Uuid, types.Uuid],
   348  	value types.Uuid, isNull bool, count int, getter aggexec.AggGetter[types.Uuid], setter aggexec.AggSetter[types.Uuid]) error {
   349  	if isNull {
   350  		return nil
   351  	}
   352  	return FillAggMaxUuid(exec, value, getter, setter)
   353  }
   354  func MergeAggMaxUuid(
   355  	exec1, exec2 aggexec.SingleAggFromFixedRetFixed[types.Uuid, types.Uuid],
   356  	getter1, getter2 aggexec.AggGetter[types.Uuid], setter1 aggexec.AggSetter[types.Uuid]) error {
   357  	a := exec1.(*aggexec.ContextWithEmptyFlagOfSingleAggRetFixed[types.Uuid])
   358  	b := exec2.(*aggexec.ContextWithEmptyFlagOfSingleAggRetFixed[types.Uuid])
   359  	if a.IsEmpty && !b.IsEmpty {
   360  		a.IsEmpty = false
   361  		setter1(getter2())
   362  	} else if !a.IsEmpty && !b.IsEmpty {
   363  		if getter1().Compare(getter2()) < 0 {
   364  			setter1(getter2())
   365  		}
   366  	}
   367  	return nil
   368  }
   369  
   370  func FillAggMaxBytes(
   371  	exec aggexec.SingleAggFromVarRetVar,
   372  	value []byte, getter aggexec.AggBytesGetter, setter aggexec.AggBytesSetter) error {
   373  	a := exec.(*aggexec.ContextWithEmptyFlagOfSingleAggRetBytes)
   374  	if a.IsEmpty {
   375  		a.IsEmpty = false
   376  		return setter(value)
   377  	}
   378  	if bytes.Compare(value, getter()) > 0 {
   379  		return setter(value)
   380  	}
   381  	return nil
   382  }
   383  func FillsAggMaxBytes(
   384  	exec aggexec.SingleAggFromVarRetVar,
   385  	value []byte, isNull bool, count int, getter aggexec.AggBytesGetter, setter aggexec.AggBytesSetter) error {
   386  	if isNull {
   387  		return nil
   388  	}
   389  	return FillAggMaxBytes(exec, value, getter, setter)
   390  }
   391  func MergeAggMaxBytes(
   392  	exec1, exec2 aggexec.SingleAggFromVarRetVar,
   393  	getter1, getter2 aggexec.AggBytesGetter, setter1 aggexec.AggBytesSetter) error {
   394  	a := exec1.(*aggexec.ContextWithEmptyFlagOfSingleAggRetBytes)
   395  	b := exec2.(*aggexec.ContextWithEmptyFlagOfSingleAggRetBytes)
   396  	if a.IsEmpty && !b.IsEmpty {
   397  		a.IsEmpty = false
   398  		return setter1(getter2())
   399  	} else if !a.IsEmpty && !b.IsEmpty {
   400  		if bytes.Compare(getter1(), getter2()) < 0 {
   401  			return setter1(getter2())
   402  		}
   403  	}
   404  	return nil
   405  }
   406  
   407  type aggMaxDecimal64 struct{}
   408  
   409  func newAggMaxDecimal64() aggexec.SingleAggFromFixedRetFixed[types.Decimal64, types.Decimal64] {
   410  	return aggMaxDecimal64{}
   411  }
   412  
   413  func (a aggMaxDecimal64) Marshal() []byte  { return nil }
   414  func (a aggMaxDecimal64) Unmarshal([]byte) {}
   415  
   416  func InitAggMaxDecimal64(
   417  	exec aggexec.SingleAggFromFixedRetFixed[types.Decimal64, types.Decimal64], setter aggexec.AggSetter[types.Decimal64], arg, ret types.Type) error {
   418  	setter(types.Decimal64Min)
   419  	return nil
   420  }
   421  func FillAggMaxDecimal64(
   422  	exec aggexec.SingleAggFromFixedRetFixed[types.Decimal64, types.Decimal64],
   423  	value types.Decimal64, getter aggexec.AggGetter[types.Decimal64], setter aggexec.AggSetter[types.Decimal64]) error {
   424  	if value.Compare(getter()) > 0 {
   425  		setter(value)
   426  	}
   427  	return nil
   428  }
   429  func FillsAggMaxDecimal64(
   430  	exec aggexec.SingleAggFromFixedRetFixed[types.Decimal64, types.Decimal64],
   431  	value types.Decimal64, isNull bool, count int, getter aggexec.AggGetter[types.Decimal64], setter aggexec.AggSetter[types.Decimal64]) error {
   432  	if isNull {
   433  		return nil
   434  	}
   435  	return FillAggMaxDecimal64(exec, value, getter, setter)
   436  }
   437  func MergeAggMaxDecimal64(
   438  	exec1, exec2 aggexec.SingleAggFromFixedRetFixed[types.Decimal64, types.Decimal64],
   439  	getter1, getter2 aggexec.AggGetter[types.Decimal64], setter1 aggexec.AggSetter[types.Decimal64]) error {
   440  	if getter2().Compare(getter1()) > 0 {
   441  		setter1(getter2())
   442  	}
   443  	return nil
   444  }
   445  
   446  type aggMaxDecimal128 struct{}
   447  
   448  func newAggMaxDecimal128() aggexec.SingleAggFromFixedRetFixed[types.Decimal128, types.Decimal128] {
   449  	return aggMaxDecimal128{}
   450  }
   451  
   452  func (a aggMaxDecimal128) Marshal() []byte  { return nil }
   453  func (a aggMaxDecimal128) Unmarshal([]byte) {}
   454  
   455  func InitAggMaxDecimal128(
   456  	exec aggexec.SingleAggFromFixedRetFixed[types.Decimal128, types.Decimal128], setter aggexec.AggSetter[types.Decimal128], arg, ret types.Type) error {
   457  	setter(types.Decimal128Min)
   458  	return nil
   459  }
   460  func FillAggMaxDecimal128(
   461  	exec aggexec.SingleAggFromFixedRetFixed[types.Decimal128, types.Decimal128],
   462  	value types.Decimal128, getter aggexec.AggGetter[types.Decimal128], setter aggexec.AggSetter[types.Decimal128]) error {
   463  	if value.Compare(getter()) > 0 {
   464  		setter(value)
   465  	}
   466  	return nil
   467  }
   468  func FillsAggMaxDecimal128(
   469  	exec aggexec.SingleAggFromFixedRetFixed[types.Decimal128, types.Decimal128],
   470  	value types.Decimal128, isNull bool, count int, getter aggexec.AggGetter[types.Decimal128], setter aggexec.AggSetter[types.Decimal128]) error {
   471  	if isNull {
   472  		return nil
   473  	}
   474  	return FillAggMaxDecimal128(exec, value, getter, setter)
   475  }
   476  func MergeAggMaxDecimal128(
   477  	exec1, exec2 aggexec.SingleAggFromFixedRetFixed[types.Decimal128, types.Decimal128],
   478  	getter1, getter2 aggexec.AggGetter[types.Decimal128], setter1 aggexec.AggSetter[types.Decimal128]) error {
   479  	if getter2().Compare(getter1()) > 0 {
   480  		setter1(getter2())
   481  	}
   482  	return nil
   483  }