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