github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/window/window.go (about) 1 // Copyright 2021 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 window 16 17 import ( 18 "bytes" 19 "github.com/matrixorigin/matrixone/pkg/sql/colexec/aggexec" 20 "github.com/matrixorigin/matrixone/pkg/sql/colexec/group" 21 "time" 22 23 "github.com/matrixorigin/matrixone/pkg/common/moerr" 24 "github.com/matrixorigin/matrixone/pkg/container/batch" 25 "github.com/matrixorigin/matrixone/pkg/container/types" 26 "github.com/matrixorigin/matrixone/pkg/container/vector" 27 "github.com/matrixorigin/matrixone/pkg/partition" 28 "github.com/matrixorigin/matrixone/pkg/pb/plan" 29 "github.com/matrixorigin/matrixone/pkg/sort" 30 "github.com/matrixorigin/matrixone/pkg/sql/plan/function" 31 "github.com/matrixorigin/matrixone/pkg/vm" 32 "github.com/matrixorigin/matrixone/pkg/vm/process" 33 ) 34 35 const argName = "window" 36 37 func (arg *Argument) String(buf *bytes.Buffer) { 38 buf.WriteString(argName) 39 buf.WriteString(": window") 40 } 41 42 func (arg *Argument) Prepare(proc *process.Process) (err error) { 43 ap := arg 44 ap.ctr = new(container) 45 ap.ctr.InitReceiver(proc, true) 46 47 ctr := ap.ctr 48 ctr.aggVecs = make([]group.ExprEvalVector, len(ap.Aggs)) 49 for i, ag := range ap.Aggs { 50 expressions := ag.GetArgExpressions() 51 if ctr.aggVecs[i], err = group.MakeEvalVector(proc, expressions); err != nil { 52 return err 53 } 54 } 55 w := ap.WinSpecList[0].Expr.(*plan.Expr_W).W 56 if len(w.PartitionBy) == 0 { 57 ctr.status = receiveAll 58 } 59 60 return nil 61 } 62 63 func (arg *Argument) Call(proc *process.Process) (vm.CallResult, error) { 64 if err, isCancel := vm.CancelCheck(proc); isCancel { 65 return vm.CancelResult, err 66 } 67 68 var err error 69 var end bool 70 ap := arg 71 ctr := ap.ctr 72 anal := proc.GetAnalyze(arg.GetIdx(), arg.GetParallelIdx(), arg.GetParallelMajor()) 73 anal.Start() 74 defer anal.Stop() 75 result := vm.NewCallResult() 76 var bat *batch.Batch 77 78 for { 79 switch ctr.status { 80 case receiveAll: 81 for { 82 bat, end, err = ctr.ReceiveFromAllRegs(anal) 83 if err != nil { 84 return result, err 85 } 86 87 if end { 88 ctr.status = eval 89 break 90 } 91 if ctr.bat == nil { 92 ctr.bat = bat 93 continue 94 } 95 anal.Input(bat, arg.GetIsFirst()) 96 for i := range bat.Vecs { 97 n := bat.Vecs[i].Length() 98 err = ctr.bat.Vecs[i].UnionBatch(bat.Vecs[i], 0, n, makeFlagsOne(n), proc.Mp()) 99 if err != nil { 100 return result, err 101 } 102 } 103 ctr.bat.AddRowCount(bat.RowCount()) 104 } 105 case receive: 106 ctr.bat, end, err = ctr.ReceiveFromAllRegs(anal) 107 if err != nil { 108 return result, err 109 } 110 if end { 111 ctr.status = done 112 } else { 113 ctr.status = eval 114 } 115 case eval: 116 if err = ctr.evalAggVector(ctr.bat, proc); err != nil { 117 return result, err 118 } 119 120 ctr.bat.Aggs = make([]aggexec.AggFuncExec, len(ap.Aggs)) 121 for i, ag := range ap.Aggs { 122 ctr.bat.Aggs[i] = aggexec.MakeAgg(proc, ag.GetAggID(), ag.IsDistinct(), ap.Types[i]) 123 if config := ag.GetExtraConfig(); config != nil { 124 if err = ctr.bat.Aggs[i].SetExtraInformation(config, 0); err != nil { 125 return result, err 126 } 127 } 128 if err = ctr.bat.Aggs[i].GroupGrow(ctr.bat.RowCount()); err != nil { 129 return result, err 130 } 131 } 132 // calculate 133 for i, w := range ap.WinSpecList { 134 // sort and partitions 135 if ap.Fs = makeOrderBy(w); ap.Fs != nil { 136 ctr.orderVecs = make([]group.ExprEvalVector, len(ap.Fs)) 137 for j := range ctr.orderVecs { 138 ctr.orderVecs[j], err = group.MakeEvalVector(proc, []*plan.Expr{ap.Fs[j].Expr}) 139 if err != nil { 140 return result, err 141 } 142 } 143 _, err = ctr.processOrder(i, ap, ctr.bat, proc) 144 if err != nil { 145 return result, err 146 } 147 } 148 // evaluate func 149 if err = ctr.processFunc(i, ap, proc, anal); err != nil { 150 return result, err 151 } 152 153 // clean 154 ctr.cleanOrderVectors() 155 } 156 157 anal.Output(ctr.bat, arg.GetIsLast()) 158 if len(ap.WinSpecList[0].Expr.(*plan.Expr_W).W.PartitionBy) == 0 { 159 ctr.status = done 160 } else { 161 ctr.status = receive 162 } 163 164 result.Batch = ctr.bat 165 result.Status = vm.ExecNext 166 return result, nil 167 case done: 168 result.Status = vm.ExecStop 169 return result, nil 170 } 171 } 172 } 173 174 func (ctr *container) processFunc(idx int, ap *Argument, proc *process.Process, anal process.Analyze) error { 175 var err error 176 n := ctr.bat.Vecs[0].Length() 177 isWinOrder := function.GetFunctionIsWinOrderFunByName(ap.WinSpecList[idx].Expr.(*plan.Expr_W).W.Name) 178 if isWinOrder { 179 if ctr.ps == nil { 180 ctr.ps = append(ctr.ps, 0) 181 } 182 if ctr.os == nil { 183 ctr.os = append(ctr.os, 0) 184 } 185 ctr.ps = append(ctr.ps, int64(n)) 186 ctr.os = append(ctr.os, int64(n)) 187 if len(ctr.os) < len(ctr.ps) { 188 ctr.os = ctr.ps 189 } 190 191 vec := proc.GetVector(types.T_int64.ToType()) 192 defer vec.Free(proc.Mp()) 193 if err = vector.AppendFixedList(vec, ctr.os, nil, proc.Mp()); err != nil { 194 return err 195 } 196 197 o := 0 198 for p := 1; p < len(ctr.ps); p++ { 199 for ; o < len(ctr.os); o++ { 200 201 if ctr.os[o] <= ctr.ps[p] { 202 203 if err = ctr.bat.Aggs[idx].Fill(p-1, o, []*vector.Vector{vec}); err != nil { 204 return err 205 } 206 207 } else { 208 o-- 209 break 210 } 211 212 } 213 } 214 } else { 215 //nullVec := vector.NewConstNull(*ctr.aggVecs[idx].Vec[0].GetType(), 1, proc.Mp()) 216 //defer nullVec.Free(proc.Mp()) 217 218 // plan.Function_AGG, plan.Function_WIN_VALUE 219 for j := 0; j < n; j++ { 220 221 start, end := 0, n 222 223 if ctr.ps != nil { 224 start, end = buildPartitionInterval(ctr.ps, j, n) 225 } 226 227 left, right, err := ctr.buildInterval(j, start, end, ap.WinSpecList[idx].Expr.(*plan.Expr_W).W.Frame) 228 if err != nil { 229 return err 230 } 231 232 if right < start || left > end || left >= right { 233 // todo: I commented this out because it was a waste of time to fill a null value. 234 //if err = ctr.bat.Aggs[idx].Fill(j, 0, []*vector.Vector{nullVec}); err != nil { 235 // return err 236 //} 237 continue 238 } 239 240 if left < start { 241 left = start 242 } 243 if right > end { 244 right = end 245 } 246 247 for k := left; k < right; k++ { 248 if err = ctr.bat.Aggs[idx].Fill(j, k, ctr.aggVecs[idx].Vec); err != nil { 249 return err 250 } 251 } 252 253 } 254 } 255 256 vec, err := ctr.bat.Aggs[idx].Flush() 257 if err != nil { 258 return err 259 } 260 if isWinOrder { 261 vec.SetNulls(nil) 262 } 263 ctr.bat.Vecs = append(ctr.bat.Vecs, vec) 264 if vec != nil { 265 anal.Alloc(int64(vec.Size())) 266 } 267 ctr.os = nil 268 ctr.ps = nil 269 return nil 270 } 271 272 func (ctr *container) buildInterval(rowIdx, start, end int, frame *plan.FrameClause) (int, int, error) { 273 // FrameClause_ROWS 274 if frame.Type == plan.FrameClause_ROWS { 275 start, end = ctr.buildRowsInterval(rowIdx, start, end, frame) 276 return start, end, nil 277 } 278 279 if len(ctr.orderVecs) == 0 { 280 return start, end, nil 281 } 282 283 // FrameClause_Range 284 return ctr.buildRangeInterval(rowIdx, start, end, frame) 285 } 286 287 func (ctr *container) buildRowsInterval(rowIdx int, start, end int, frame *plan.FrameClause) (int, int) { 288 switch frame.Start.Type { 289 case plan.FrameBound_CURRENT_ROW: 290 start = rowIdx 291 case plan.FrameBound_PRECEDING: 292 if !frame.Start.UnBounded { 293 pre := frame.Start.Val.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_U64Val).U64Val 294 start = rowIdx - int(pre) 295 } 296 case plan.FrameBound_FOLLOWING: 297 fol := frame.Start.Val.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_U64Val).U64Val 298 start = rowIdx + int(fol) 299 } 300 301 switch frame.End.Type { 302 case plan.FrameBound_CURRENT_ROW: 303 end = rowIdx + 1 304 case plan.FrameBound_PRECEDING: 305 pre := frame.End.Val.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_U64Val).U64Val 306 end = rowIdx - int(pre) + 1 307 case plan.FrameBound_FOLLOWING: 308 if !frame.End.UnBounded { 309 fol := frame.End.Val.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_U64Val).U64Val 310 end = rowIdx + int(fol) + 1 311 } 312 } 313 return start, end 314 } 315 316 func (ctr *container) buildRangeInterval(rowIdx int, start, end int, frame *plan.FrameClause) (int, int, error) { 317 var err error 318 switch frame.Start.Type { 319 case plan.FrameBound_CURRENT_ROW: 320 start, err = searchLeft(start, end, rowIdx, ctr.orderVecs[len(ctr.orderVecs)-1].Vec[0], nil, false) 321 if err != nil { 322 return start, end, err 323 } 324 case plan.FrameBound_PRECEDING: 325 if !frame.Start.UnBounded { 326 start, err = searchLeft(start, end, rowIdx, ctr.orderVecs[len(ctr.orderVecs)-1].Vec[0], frame.Start.Val, false) 327 if err != nil { 328 return start, end, err 329 } 330 } 331 case plan.FrameBound_FOLLOWING: 332 start, err = searchLeft(start, end, rowIdx, ctr.orderVecs[len(ctr.orderVecs)-1].Vec[0], frame.Start.Val, true) 333 if err != nil { 334 return start, end, err 335 } 336 } 337 338 switch frame.End.Type { 339 case plan.FrameBound_CURRENT_ROW: 340 end, err = searchRight(start, end, rowIdx, ctr.orderVecs[len(ctr.orderVecs)-1].Vec[0], nil, false) 341 if err != nil { 342 return start, end, err 343 } 344 case plan.FrameBound_PRECEDING: 345 end, err = searchRight(start, end, rowIdx, ctr.orderVecs[len(ctr.orderVecs)-1].Vec[0], frame.End.Val, true) 346 if err != nil { 347 return start, end, err 348 } 349 case plan.FrameBound_FOLLOWING: 350 if !frame.End.UnBounded { 351 end, err = searchRight(start, end, rowIdx, ctr.orderVecs[len(ctr.orderVecs)-1].Vec[0], frame.End.Val, false) 352 if err != nil { 353 return start, end, err 354 } 355 } 356 } 357 return start, end, nil 358 } 359 360 func buildPartitionInterval(ps []int64, j int, l int) (int, int) { 361 left, right := 0, 0 362 for i, p := range ps { 363 if p > int64(j) { 364 right = int(p) 365 if i == 0 { 366 left = 0 367 } else { 368 left = int(ps[i-1]) 369 } 370 break 371 } 372 } 373 if right == 0 { 374 return int(ps[len(ps)-1]), l 375 } 376 return left, right 377 } 378 379 func (ctr *container) evalAggVector(bat *batch.Batch, proc *process.Process) (err error) { 380 input := []*batch.Batch{bat} 381 382 for i := range ctr.aggVecs { 383 for j := range ctr.aggVecs[i].Executor { 384 ctr.aggVecs[i].Vec[j], err = ctr.aggVecs[i].Executor[j].Eval(proc, input) 385 if err != nil { 386 return err 387 } 388 } 389 } 390 return nil 391 } 392 393 func makeArgFs(arg *Argument) { 394 arg.ctr.desc = make([]bool, len(arg.Fs)) 395 arg.ctr.nullsLast = make([]bool, len(arg.Fs)) 396 for i, f := range arg.Fs { 397 arg.ctr.desc[i] = f.Flag&plan.OrderBySpec_DESC != 0 398 if f.Flag&plan.OrderBySpec_NULLS_FIRST != 0 { 399 arg.ctr.nullsLast[i] = false 400 } else if f.Flag&plan.OrderBySpec_NULLS_LAST != 0 { 401 arg.ctr.nullsLast[i] = true 402 } else { 403 arg.ctr.nullsLast[i] = arg.ctr.desc[i] 404 } 405 } 406 } 407 408 func makeOrderBy(expr *plan.Expr) []*plan.OrderBySpec { 409 w := expr.Expr.(*plan.Expr_W).W 410 if len(w.PartitionBy) == 0 && len(w.OrderBy) == 0 { 411 return nil 412 } 413 return w.OrderBy 414 } 415 416 func makeFlagsOne(n int) []uint8 { 417 t := make([]uint8, n) 418 for i := range t { 419 t[i]++ 420 } 421 return t 422 } 423 424 func (ctr *container) processOrder(idx int, ap *Argument, bat *batch.Batch, proc *process.Process) (bool, error) { 425 makeArgFs(ap) 426 427 var err error 428 input := []*batch.Batch{bat} 429 for i := range ctr.orderVecs { 430 for j := range ctr.orderVecs[i].Executor { 431 ctr.orderVecs[i].Vec[j], err = ctr.orderVecs[i].Executor[j].Eval(proc, input) 432 if err != nil { 433 return false, err 434 } 435 } 436 } 437 if bat.RowCount() < 2 { 438 return false, nil 439 } 440 441 ovec := ctr.orderVecs[0].Vec[0] 442 var strCol []string 443 444 rowCount := bat.RowCount() 445 //if ctr.sels == nil { 446 // ctr.sels = make([]int64, rowCount) 447 //} 448 ctr.sels = make([]int64, rowCount) 449 for i := 0; i < rowCount; i++ { 450 ctr.sels[i] = int64(i) 451 } 452 453 // skip sort for const vector 454 if !ovec.IsConst() { 455 nullCnt := ovec.GetNulls().Count() 456 if nullCnt < ovec.Length() { 457 if ovec.GetType().IsVarlen() { 458 strCol = vector.MustStrCol(ovec) 459 } else { 460 strCol = nil 461 } 462 sort.Sort(ctr.desc[0], ctr.nullsLast[0], nullCnt > 0, ctr.sels, ovec, strCol) 463 } 464 } 465 466 ps := make([]int64, 0, 16) 467 ds := make([]bool, len(ctr.sels)) 468 469 w := ap.WinSpecList[idx].Expr.(*plan.Expr_W).W 470 n := len(w.PartitionBy) 471 472 i, j := 1, len(ctr.orderVecs) 473 for ; i < j; i++ { 474 desc := ctr.desc[i] 475 nullsLast := ctr.nullsLast[i] 476 ps = partition.Partition(ctr.sels, ds, ps, ovec) 477 vec := ctr.orderVecs[i].Vec[0] 478 // skip sort for const vector 479 if !vec.IsConst() { 480 nullCnt := vec.GetNulls().Count() 481 if nullCnt < vec.Length() { 482 if vec.GetType().IsVarlen() { 483 strCol = vector.MustStrCol(vec) 484 } else { 485 strCol = nil 486 } 487 for i, j := 0, len(ps); i < j; i++ { 488 if i == j-1 { 489 sort.Sort(desc, nullsLast, nullCnt > 0, ctr.sels[ps[i]:], vec, strCol) 490 } else { 491 sort.Sort(desc, nullsLast, nullCnt > 0, ctr.sels[ps[i]:ps[i+1]], vec, strCol) 492 } 493 } 494 } 495 } 496 ovec = vec 497 if n == i { 498 ctr.ps = make([]int64, len(ps)) 499 copy(ctr.ps, ps) 500 } 501 } 502 503 if n == i { 504 ps = partition.Partition(ctr.sels, ds, ps, ovec) 505 ctr.ps = make([]int64, len(ps)) 506 copy(ctr.ps, ps) 507 } else if n == 0 { 508 ctr.ps = nil 509 } 510 511 if len(ap.WinSpecList[idx].Expr.(*plan.Expr_W).W.OrderBy) > 0 { 512 ctr.os = partition.Partition(ctr.sels, ds, ps, ovec) 513 } else { 514 ctr.os = nil 515 } 516 517 if err := bat.Shuffle(ctr.sels, proc.Mp()); err != nil { 518 panic(err) 519 } 520 521 // shuffle agg vector 522 for k := idx; k < len(ctr.aggVecs); k++ { 523 if len(ctr.aggVecs[k].Vec) > 0 && !ctr.aggVecs[k].Executor[0].IsColumnExpr() { 524 if err := ctr.aggVecs[k].Vec[0].Shuffle(ctr.sels, proc.Mp()); err != nil { 525 panic(err) 526 } 527 } 528 } 529 530 t := len(ctr.orderVecs) - 1 531 if len(ctr.orderVecs[t].Vec) > 0 && !ctr.orderVecs[t].Executor[0].IsColumnExpr() { 532 if err := ctr.orderVecs[t].Vec[0].Shuffle(ctr.sels, proc.Mp()); err != nil { 533 panic(err) 534 } 535 } 536 537 ctr.ps = nil 538 539 return false, nil 540 } 541 542 func searchLeft(start, end, rowIdx int, vec *vector.Vector, expr *plan.Expr, plus bool) (int, error) { 543 if vec.GetNulls().Contains(uint64(rowIdx)) { 544 return rowIdx, nil 545 } 546 var left int 547 switch vec.GetType().Oid { 548 case types.T_bit: 549 col := vector.MustFixedCol[uint64](vec) 550 if expr == nil { 551 left = genericSearchLeft(start, end-1, col, col[rowIdx], genericEqual[uint64], genericGreater[uint64]) 552 } else { 553 c := expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_U64Val).U64Val 554 if plus { 555 left = genericSearchLeft(start, end-1, col, col[rowIdx]+c, genericEqual[uint64], genericGreater[uint64]) 556 } else { 557 if col[rowIdx] <= c { 558 return start, nil 559 } 560 left = genericSearchLeft(start, end-1, col, col[rowIdx]-c, genericEqual[uint64], genericGreater[uint64]) 561 } 562 } 563 case types.T_int8: 564 col := vector.MustFixedCol[int8](vec) 565 if expr == nil { 566 left = genericSearchLeft(start, end-1, col, col[rowIdx], genericEqual[int8], genericGreater[int8]) 567 } else { 568 c := int8(expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I8Val).I8Val) 569 if plus { 570 left = genericSearchLeft(start, end-1, col, col[rowIdx]+c, genericEqual[int8], genericGreater[int8]) 571 } else { 572 left = genericSearchLeft(start, end-1, col, col[rowIdx]-c, genericEqual[int8], genericGreater[int8]) 573 } 574 } 575 case types.T_int16: 576 col := vector.MustFixedCol[int16](vec) 577 if expr == nil { 578 left = genericSearchLeft(start, end-1, col, col[rowIdx], genericEqual[int16], genericGreater[int16]) 579 } else { 580 c := int16(expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I16Val).I16Val) 581 if plus { 582 left = genericSearchLeft(start, end-1, col, col[rowIdx]+c, genericEqual[int16], genericGreater[int16]) 583 } else { 584 left = genericSearchLeft(start, end-1, col, col[rowIdx]-c, genericEqual[int16], genericGreater[int16]) 585 } 586 } 587 case types.T_int32: 588 col := vector.MustFixedCol[int32](vec) 589 if expr == nil { 590 left = genericSearchLeft(start, end-1, col, col[rowIdx], genericEqual[int32], genericGreater[int32]) 591 } else { 592 c := expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I32Val).I32Val 593 if plus { 594 left = genericSearchLeft(start, end-1, col, col[rowIdx]+c, genericEqual[int32], genericGreater[int32]) 595 } else { 596 left = genericSearchLeft(start, end-1, col, col[rowIdx]-c, genericEqual[int32], genericGreater[int32]) 597 } 598 } 599 case types.T_int64: 600 col := vector.MustFixedCol[int64](vec) 601 if expr == nil { 602 left = genericSearchLeft(start, end-1, col, col[rowIdx], genericEqual[int64], genericGreater[int64]) 603 } else { 604 c := expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I64Val).I64Val 605 if plus { 606 left = genericSearchLeft(start, end-1, col, col[rowIdx]+c, genericEqual[int64], genericGreater[int64]) 607 } else { 608 left = genericSearchLeft(start, end-1, col, col[rowIdx]-c, genericEqual[int64], genericGreater[int64]) 609 } 610 } 611 case types.T_uint8: 612 col := vector.MustFixedCol[uint8](vec) 613 if expr == nil { 614 left = genericSearchLeft(start, end-1, col, col[rowIdx], genericEqual[uint8], genericGreater[uint8]) 615 } else { 616 c := uint8(expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_U8Val).U8Val) 617 if plus { 618 left = genericSearchLeft(start, end-1, col, col[rowIdx]+c, genericEqual[uint8], genericGreater[uint8]) 619 } else { 620 if col[rowIdx] <= c { 621 return start, nil 622 } 623 left = genericSearchLeft(start, end-1, col, col[rowIdx]-c, genericEqual[uint8], genericGreater[uint8]) 624 } 625 } 626 case types.T_uint16: 627 col := vector.MustFixedCol[uint16](vec) 628 if expr == nil { 629 left = genericSearchLeft(start, end-1, col, col[rowIdx], genericEqual[uint16], genericGreater[uint16]) 630 } else { 631 c := uint16(expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_U16Val).U16Val) 632 if plus { 633 left = genericSearchLeft(start, end-1, col, col[rowIdx]+c, genericEqual[uint16], genericGreater[uint16]) 634 } else { 635 if col[rowIdx] <= c { 636 return start, nil 637 } 638 left = genericSearchLeft(start, end-1, col, col[rowIdx]-c, genericEqual[uint16], genericGreater[uint16]) 639 } 640 } 641 case types.T_uint32: 642 col := vector.MustFixedCol[uint32](vec) 643 if expr == nil { 644 left = genericSearchLeft(start, end-1, col, col[rowIdx], genericEqual[uint32], genericGreater[uint32]) 645 } else { 646 c := expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_U32Val).U32Val 647 if plus { 648 left = genericSearchLeft(start, end-1, col, col[rowIdx]+c, genericEqual[uint32], genericGreater[uint32]) 649 } else { 650 if col[rowIdx] <= c { 651 return start, nil 652 } 653 left = genericSearchLeft(start, end-1, col, col[rowIdx]-c, genericEqual[uint32], genericGreater[uint32]) 654 } 655 } 656 case types.T_uint64: 657 col := vector.MustFixedCol[uint64](vec) 658 if expr == nil { 659 left = genericSearchLeft(start, end-1, col, col[rowIdx], genericEqual[uint64], genericGreater[uint64]) 660 } else { 661 c := expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_U64Val).U64Val 662 if plus { 663 left = genericSearchLeft(start, end-1, col, col[rowIdx]+c, genericEqual[uint64], genericGreater[uint64]) 664 } else { 665 if col[rowIdx] <= c { 666 return start, nil 667 } 668 left = genericSearchLeft(start, end-1, col, col[rowIdx]-c, genericEqual[uint64], genericGreater[uint64]) 669 } 670 } 671 case types.T_float32: 672 col := vector.MustFixedCol[float32](vec) 673 if expr == nil { 674 left = genericSearchLeft(start, end-1, col, col[rowIdx], genericEqual[float32], genericGreater[float32]) 675 } else { 676 c := expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_Fval).Fval 677 if plus { 678 left = genericSearchLeft(start, end-1, col, col[rowIdx]+c, genericEqual[float32], genericGreater[float32]) 679 } else { 680 left = genericSearchLeft(start, end-1, col, col[rowIdx]-c, genericEqual[float32], genericGreater[float32]) 681 } 682 } 683 case types.T_float64: 684 col := vector.MustFixedCol[float64](vec) 685 if expr == nil { 686 left = genericSearchLeft(start, end-1, col, col[rowIdx], genericEqual[float64], genericGreater[float64]) 687 } else { 688 c := expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_Dval).Dval 689 if plus { 690 left = genericSearchLeft(start, end-1, col, col[rowIdx]+c, genericEqual[float64], genericGreater[float64]) 691 } else { 692 left = genericSearchLeft(start, end-1, col, col[rowIdx]-c, genericEqual[float64], genericGreater[float64]) 693 } 694 } 695 case types.T_decimal64: 696 col := vector.MustFixedCol[types.Decimal64](vec) 697 if expr == nil { 698 left = genericSearchLeft(start, end-1, col, col[rowIdx], decimal64Equal, decimal64Greater) 699 } else { 700 c := expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_Decimal64Val).Decimal64Val.A 701 if plus { 702 fol, err := col[rowIdx].Add64(types.Decimal64(c)) 703 if err != nil { 704 return left, err 705 } 706 left = genericSearchLeft(start, end-1, col, fol, decimal64Equal, decimal64Greater) 707 } else { 708 fol, err := col[rowIdx].Sub64(types.Decimal64(c)) 709 if err != nil { 710 return left, err 711 } 712 left = genericSearchLeft(start, end-1, col, fol, decimal64Equal, decimal64Greater) 713 } 714 } 715 case types.T_decimal128: 716 col := vector.MustFixedCol[types.Decimal128](vec) 717 if expr == nil { 718 left = genericSearchLeft(start, end-1, col, col[rowIdx], decimal128Equal, decimal128Greater) 719 } else { 720 c := expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_Decimal128Val).Decimal128Val 721 if plus { 722 fol, err := col[rowIdx].Add128(types.Decimal128{B0_63: uint64(c.A), B64_127: uint64(c.B)}) 723 if err != nil { 724 return left, err 725 } 726 left = genericSearchLeft(start, end-1, col, fol, decimal128Equal, decimal128Greater) 727 } else { 728 fol, err := col[rowIdx].Sub128(types.Decimal128{B0_63: uint64(c.A), B64_127: uint64(c.B)}) 729 if err != nil { 730 return left, err 731 } 732 left = genericSearchLeft(start, end-1, col, fol, decimal128Equal, decimal128Greater) 733 } 734 } 735 case types.T_date: 736 col := vector.MustFixedCol[types.Date](vec) 737 if expr == nil { 738 left = genericSearchLeft(start, end-1, col, col[rowIdx], genericEqual[types.Date], genericGreater[types.Date]) 739 } else { 740 diff := expr.Expr.(*plan.Expr_List).List.List[0].Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I64Val).I64Val 741 unit := expr.Expr.(*plan.Expr_List).List.List[1].Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I64Val).I64Val 742 if plus { 743 fol, err := doDateAdd(col[rowIdx], diff, unit) 744 if err != nil { 745 return left, err 746 } 747 left = genericSearchLeft(start, end-1, col, fol, genericEqual[types.Date], genericGreater[types.Date]) 748 } else { 749 fol, err := doDateSub(col[rowIdx], diff, unit) 750 if err != nil { 751 return left, err 752 } 753 left = genericSearchLeft(start, end-1, col, fol, genericEqual[types.Date], genericGreater[types.Date]) 754 } 755 } 756 case types.T_datetime: 757 col := vector.MustFixedCol[types.Datetime](vec) 758 if expr == nil { 759 left = genericSearchLeft(start, end-1, col, col[rowIdx], genericEqual[types.Datetime], genericGreater[types.Datetime]) 760 } else { 761 diff := expr.Expr.(*plan.Expr_List).List.List[0].Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I64Val).I64Val 762 unit := expr.Expr.(*plan.Expr_List).List.List[1].Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I64Val).I64Val 763 if plus { 764 fol, err := doDatetimeAdd(col[rowIdx], diff, unit) 765 if err != nil { 766 return left, err 767 } 768 left = genericSearchLeft(start, end-1, col, fol, genericEqual[types.Datetime], genericGreater[types.Datetime]) 769 } else { 770 fol, err := doDatetimeSub(col[rowIdx], diff, unit) 771 if err != nil { 772 return left, err 773 } 774 left = genericSearchLeft(start, end-1, col, fol, genericEqual[types.Datetime], genericGreater[types.Datetime]) 775 } 776 } 777 case types.T_time: 778 col := vector.MustFixedCol[types.Time](vec) 779 if expr == nil { 780 left = genericSearchLeft(start, end-1, col, col[rowIdx], genericEqual[types.Time], genericGreater[types.Time]) 781 } else { 782 diff := expr.Expr.(*plan.Expr_List).List.List[0].Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I64Val).I64Val 783 unit := expr.Expr.(*plan.Expr_List).List.List[1].Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I64Val).I64Val 784 if plus { 785 fol, err := doTimeAdd(col[rowIdx], diff, unit) 786 if err != nil { 787 return left, err 788 } 789 left = genericSearchLeft(start, end-1, col, fol, genericEqual[types.Time], genericGreater[types.Time]) 790 } else { 791 fol, err := doTimeSub(col[rowIdx], diff, unit) 792 if err != nil { 793 return left, err 794 } 795 left = genericSearchLeft(start, end-1, col, fol, genericEqual[types.Time], genericGreater[types.Time]) 796 } 797 } 798 case types.T_timestamp: 799 col := vector.MustFixedCol[types.Timestamp](vec) 800 if expr == nil { 801 left = genericSearchLeft(start, end-1, col, col[rowIdx], genericEqual[types.Timestamp], genericGreater[types.Timestamp]) 802 } else { 803 diff := expr.Expr.(*plan.Expr_List).List.List[0].Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I64Val).I64Val 804 unit := expr.Expr.(*plan.Expr_List).List.List[1].Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I64Val).I64Val 805 if plus { 806 fol, err := doTimestampAdd(time.Local, col[rowIdx], diff, unit) 807 if err != nil { 808 return left, err 809 } 810 left = genericSearchLeft(start, end-1, col, fol, genericEqual[types.Timestamp], genericGreater[types.Timestamp]) 811 } else { 812 fol, err := doTimestampSub(time.Local, col[rowIdx], diff, unit) 813 if err != nil { 814 return left, err 815 } 816 left = genericSearchLeft(start, end-1, col, fol, genericEqual[types.Timestamp], genericGreater[types.Timestamp]) 817 } 818 } 819 } 820 return left, nil 821 } 822 823 func doDateSub(start types.Date, diff int64, unit int64) (types.Date, error) { 824 err := types.JudgeIntervalNumOverflow(diff, types.IntervalType(unit)) 825 if err != nil { 826 return 0, err 827 } 828 dt, success := start.ToDatetime().AddInterval(-diff, types.IntervalType(unit), types.DateType) 829 if success { 830 return dt.ToDate(), nil 831 } else { 832 return 0, moerr.NewOutOfRangeNoCtx("date", "") 833 } 834 } 835 836 func doTimeSub(start types.Time, diff int64, unit int64) (types.Time, error) { 837 err := types.JudgeIntervalNumOverflow(diff, types.IntervalType(unit)) 838 if err != nil { 839 return 0, err 840 } 841 t, success := start.AddInterval(-diff, types.IntervalType(unit)) 842 if success { 843 return t, nil 844 } else { 845 return 0, moerr.NewOutOfRangeNoCtx("time", "") 846 } 847 } 848 849 func doDatetimeSub(start types.Datetime, diff int64, unit int64) (types.Datetime, error) { 850 err := types.JudgeIntervalNumOverflow(diff, types.IntervalType(unit)) 851 if err != nil { 852 return 0, err 853 } 854 dt, success := start.AddInterval(-diff, types.IntervalType(unit), types.DateTimeType) 855 if success { 856 return dt, nil 857 } else { 858 return 0, moerr.NewOutOfRangeNoCtx("datetime", "") 859 } 860 } 861 862 func doTimestampSub(loc *time.Location, start types.Timestamp, diff int64, unit int64) (types.Timestamp, error) { 863 err := types.JudgeIntervalNumOverflow(diff, types.IntervalType(unit)) 864 if err != nil { 865 return 0, err 866 } 867 dt, success := start.ToDatetime(loc).AddInterval(-diff, types.IntervalType(unit), types.DateTimeType) 868 if success { 869 return dt.ToTimestamp(loc), nil 870 } else { 871 return 0, moerr.NewOutOfRangeNoCtx("timestamp", "") 872 } 873 } 874 875 func searchRight(start, end, rowIdx int, vec *vector.Vector, expr *plan.Expr, sub bool) (int, error) { 876 if vec.GetNulls().Contains(uint64(rowIdx)) { 877 return rowIdx + 1, nil 878 } 879 var right int 880 switch vec.GetType().Oid { 881 case types.T_bit: 882 col := vector.MustFixedCol[uint64](vec) 883 if expr == nil { 884 right = genericSearchEqualRight(rowIdx, end-1, col, col[rowIdx], genericEqual[uint64]) 885 } else { 886 c := expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_U64Val).U64Val 887 if sub { 888 right = genericSearchRight(start, end-1, col, col[rowIdx]-c, genericEqual[uint64], genericGreater[uint64]) 889 } else { 890 if col[rowIdx] <= c { 891 return start, nil 892 } 893 right = genericSearchRight(start, end-1, col, col[rowIdx]+c, genericEqual[uint64], genericGreater[uint64]) 894 } 895 } 896 case types.T_int8: 897 col := vector.MustFixedCol[int8](vec) 898 if expr == nil { 899 right = genericSearchEqualRight(rowIdx, end-1, col, col[rowIdx], genericEqual[int8]) 900 } else { 901 c := int8(expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I8Val).I8Val) 902 if sub { 903 right = genericSearchRight(start, end-1, col, col[rowIdx]-c, genericEqual[int8], genericGreater[int8]) 904 } else { 905 right = genericSearchRight(start, end-1, col, col[rowIdx]+c, genericEqual[int8], genericGreater[int8]) 906 } 907 } 908 case types.T_int16: 909 col := vector.MustFixedCol[int16](vec) 910 if expr == nil { 911 right = genericSearchEqualRight(rowIdx, end-1, col, col[rowIdx], genericEqual[int16]) 912 } else { 913 c := int16(expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I16Val).I16Val) 914 if sub { 915 right = genericSearchRight(start, end-1, col, col[rowIdx]-c, genericEqual[int16], genericGreater[int16]) 916 } else { 917 right = genericSearchRight(start, end-1, col, col[rowIdx]+c, genericEqual[int16], genericGreater[int16]) 918 } 919 } 920 case types.T_int32: 921 col := vector.MustFixedCol[int32](vec) 922 if expr == nil { 923 right = genericSearchEqualRight(rowIdx, end-1, col, col[rowIdx], genericEqual[int32]) 924 } else { 925 c := expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I32Val).I32Val 926 if sub { 927 right = genericSearchRight(start, end-1, col, col[rowIdx]-c, genericEqual[int32], genericGreater[int32]) 928 } else { 929 right = genericSearchRight(start, end-1, col, col[rowIdx]+c, genericEqual[int32], genericGreater[int32]) 930 } 931 } 932 case types.T_int64: 933 col := vector.MustFixedCol[int64](vec) 934 if expr == nil { 935 right = genericSearchEqualRight(rowIdx, end-1, col, col[rowIdx], genericEqual[int64]) 936 } else { 937 c := expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I64Val).I64Val 938 if sub { 939 right = genericSearchRight(start, end-1, col, col[rowIdx]-c, genericEqual[int64], genericGreater[int64]) 940 } else { 941 right = genericSearchRight(start, end-1, col, col[rowIdx]+c, genericEqual[int64], genericGreater[int64]) 942 } 943 } 944 case types.T_uint8: 945 col := vector.MustFixedCol[uint8](vec) 946 if expr == nil { 947 right = genericSearchEqualRight(rowIdx, end-1, col, col[rowIdx], genericEqual[uint8]) 948 } else { 949 c := uint8(expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_U8Val).U8Val) 950 if sub { 951 if col[rowIdx] <= c { 952 return start, nil 953 } 954 right = genericSearchRight(start, end-1, col, col[rowIdx]-c, genericEqual[uint8], genericGreater[uint8]) 955 } else { 956 right = genericSearchRight(start, end-1, col, col[rowIdx]+c, genericEqual[uint8], genericGreater[uint8]) 957 } 958 } 959 case types.T_uint16: 960 col := vector.MustFixedCol[uint16](vec) 961 if expr == nil { 962 right = genericSearchEqualRight(rowIdx, end-1, col, col[rowIdx], genericEqual[uint16]) 963 } else { 964 c := uint16(expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_U16Val).U16Val) 965 if sub { 966 if col[rowIdx] <= c { 967 return start, nil 968 } 969 right = genericSearchRight(start, end-1, col, col[rowIdx]-c, genericEqual[uint16], genericGreater[uint16]) 970 } else { 971 right = genericSearchRight(start, end-1, col, col[rowIdx]+c, genericEqual[uint16], genericGreater[uint16]) 972 } 973 } 974 case types.T_uint32: 975 col := vector.MustFixedCol[uint32](vec) 976 if expr == nil { 977 right = genericSearchEqualRight(rowIdx, end-1, col, col[rowIdx], genericEqual[uint32]) 978 } else { 979 c := expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_U32Val).U32Val 980 if sub { 981 if col[rowIdx] <= c { 982 return start, nil 983 } 984 right = genericSearchRight(start, end-1, col, col[rowIdx]-c, genericEqual[uint32], genericGreater[uint32]) 985 } else { 986 right = genericSearchRight(start, end-1, col, col[rowIdx]+c, genericEqual[uint32], genericGreater[uint32]) 987 } 988 } 989 case types.T_uint64: 990 col := vector.MustFixedCol[uint64](vec) 991 if expr == nil { 992 right = genericSearchEqualRight(rowIdx, end-1, col, col[rowIdx], genericEqual[uint64]) 993 } else { 994 c := expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_U64Val).U64Val 995 if sub { 996 right = genericSearchRight(start, end-1, col, col[rowIdx]-c, genericEqual[uint64], genericGreater[uint64]) 997 } else { 998 if col[rowIdx] <= c { 999 return start, nil 1000 } 1001 right = genericSearchRight(start, end-1, col, col[rowIdx]+c, genericEqual[uint64], genericGreater[uint64]) 1002 } 1003 } 1004 case types.T_float32: 1005 col := vector.MustFixedCol[float32](vec) 1006 if expr == nil { 1007 right = genericSearchEqualRight(rowIdx, end-1, col, col[rowIdx], genericEqual[float32]) 1008 } else { 1009 c := expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_Fval).Fval 1010 if sub { 1011 right = genericSearchRight(start, end-1, col, col[rowIdx]-c, genericEqual[float32], genericGreater[float32]) 1012 } else { 1013 right = genericSearchRight(start, end-1, col, col[rowIdx]+c, genericEqual[float32], genericGreater[float32]) 1014 } 1015 } 1016 case types.T_float64: 1017 col := vector.MustFixedCol[float64](vec) 1018 if expr == nil { 1019 right = genericSearchEqualRight(rowIdx, end-1, col, col[rowIdx], genericEqual[float64]) 1020 } else { 1021 c := expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_Dval).Dval 1022 if sub { 1023 right = genericSearchRight(start, end-1, col, col[rowIdx]-c, genericEqual[float64], genericGreater[float64]) 1024 } else { 1025 right = genericSearchRight(start, end-1, col, col[rowIdx]+c, genericEqual[float64], genericGreater[float64]) 1026 } 1027 } 1028 case types.T_decimal64: 1029 col := vector.MustFixedCol[types.Decimal64](vec) 1030 if expr == nil { 1031 right = genericSearchEqualRight(rowIdx, end-1, col, col[rowIdx], decimal64Equal) 1032 } else { 1033 c := expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_Decimal64Val).Decimal64Val.A 1034 if sub { 1035 fol, err := col[rowIdx].Sub64(types.Decimal64(c)) 1036 if err != nil { 1037 return right, err 1038 } 1039 right = genericSearchRight(start, end-1, col, fol, decimal64Equal, decimal64Greater) 1040 } else { 1041 fol, err := col[rowIdx].Add64(types.Decimal64(c)) 1042 if err != nil { 1043 return right, err 1044 } 1045 right = genericSearchRight(start, end-1, col, fol, decimal64Equal, decimal64Greater) 1046 } 1047 } 1048 case types.T_decimal128: 1049 col := vector.MustFixedCol[types.Decimal128](vec) 1050 if expr == nil { 1051 right = genericSearchEqualRight(rowIdx, end-1, col, col[rowIdx], decimal128Equal) 1052 } else { 1053 c := expr.Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_Decimal128Val).Decimal128Val 1054 if sub { 1055 fol, err := col[rowIdx].Sub128(types.Decimal128{B0_63: uint64(c.A), B64_127: uint64(c.B)}) 1056 if err != nil { 1057 return right, err 1058 } 1059 right = genericSearchRight(start, end-1, col, fol, decimal128Equal, decimal128Greater) 1060 } else { 1061 fol, err := col[rowIdx].Add128(types.Decimal128{B0_63: uint64(c.A), B64_127: uint64(c.B)}) 1062 if err != nil { 1063 return right, err 1064 } 1065 right = genericSearchRight(start, end-1, col, fol, decimal128Equal, decimal128Greater) 1066 } 1067 } 1068 case types.T_date: 1069 col := vector.MustFixedCol[types.Date](vec) 1070 if expr == nil { 1071 right = genericSearchEqualRight(rowIdx, end-1, col, col[rowIdx], genericEqual[types.Date]) 1072 } else { 1073 diff := expr.Expr.(*plan.Expr_List).List.List[0].Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I64Val).I64Val 1074 unit := expr.Expr.(*plan.Expr_List).List.List[1].Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I64Val).I64Val 1075 if sub { 1076 fol, err := doDateSub(col[rowIdx], diff, unit) 1077 if err != nil { 1078 return right, err 1079 } 1080 right = genericSearchRight(start, end-1, col, fol, genericEqual[types.Date], genericGreater[types.Date]) 1081 } else { 1082 fol, err := doDateAdd(col[rowIdx], diff, unit) 1083 if err != nil { 1084 return right, err 1085 } 1086 right = genericSearchRight(start, end-1, col, fol, genericEqual[types.Date], genericGreater[types.Date]) 1087 } 1088 } 1089 case types.T_datetime: 1090 col := vector.MustFixedCol[types.Datetime](vec) 1091 i := start 1092 for ; i < end; i++ { 1093 if !vec.GetNulls().Contains(uint64(i)) { 1094 break 1095 } 1096 } 1097 for j := start; j < i; j++ { 1098 col[j] = col[i] 1099 } 1100 if expr == nil { 1101 right = genericSearchEqualRight(rowIdx, end-1, col, col[rowIdx], genericEqual[types.Datetime]) 1102 } else { 1103 diff := expr.Expr.(*plan.Expr_List).List.List[0].Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I64Val).I64Val 1104 unit := expr.Expr.(*plan.Expr_List).List.List[1].Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I64Val).I64Val 1105 if sub { 1106 fol, err := doDatetimeSub(col[rowIdx], diff, unit) 1107 if err != nil { 1108 return right, err 1109 } 1110 right = genericSearchRight(start, end-1, col, fol, genericEqual[types.Datetime], genericGreater[types.Datetime]) 1111 } else { 1112 fol, err := doDatetimeAdd(col[rowIdx], diff, unit) 1113 if err != nil { 1114 return right, err 1115 } 1116 right = genericSearchRight(start, end-1, col, fol, genericEqual[types.Datetime], genericGreater[types.Datetime]) 1117 } 1118 } 1119 case types.T_time: 1120 col := vector.MustFixedCol[types.Time](vec) 1121 if expr == nil { 1122 right = genericSearchEqualRight(rowIdx, end-1, col, col[rowIdx], genericEqual[types.Time]) 1123 } else { 1124 diff := expr.Expr.(*plan.Expr_List).List.List[0].Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I64Val).I64Val 1125 unit := expr.Expr.(*plan.Expr_List).List.List[1].Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I64Val).I64Val 1126 if sub { 1127 fol, err := doTimeSub(col[rowIdx], diff, unit) 1128 if err != nil { 1129 return right, err 1130 } 1131 right = genericSearchRight(start, end-1, col, fol, genericEqual[types.Time], genericGreater[types.Time]) 1132 } else { 1133 fol, err := doTimeAdd(col[rowIdx], diff, unit) 1134 if err != nil { 1135 return right, err 1136 } 1137 right = genericSearchRight(start, end-1, col, fol, genericEqual[types.Time], genericGreater[types.Time]) 1138 } 1139 } 1140 case types.T_timestamp: 1141 col := vector.MustFixedCol[types.Timestamp](vec) 1142 if expr == nil { 1143 right = genericSearchEqualRight(rowIdx, end-1, col, col[rowIdx], genericEqual[types.Timestamp]) 1144 } else { 1145 diff := expr.Expr.(*plan.Expr_List).List.List[0].Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I64Val).I64Val 1146 unit := expr.Expr.(*plan.Expr_List).List.List[1].Expr.(*plan.Expr_Lit).Lit.Value.(*plan.Literal_I64Val).I64Val 1147 if sub { 1148 fol, err := doTimestampSub(time.Local, col[rowIdx], diff, unit) 1149 if err != nil { 1150 return right, err 1151 } 1152 right = genericSearchRight(start, end-1, col, fol, genericEqual[types.Timestamp], genericGreater[types.Timestamp]) 1153 } else { 1154 fol, err := doTimestampAdd(time.Local, col[rowIdx], diff, unit) 1155 if err != nil { 1156 return right, err 1157 } 1158 right = genericSearchRight(start, end-1, col, fol, genericEqual[types.Timestamp], genericGreater[types.Timestamp]) 1159 } 1160 } 1161 } 1162 return right + 1, nil 1163 } 1164 1165 func doDateAdd(start types.Date, diff int64, unit int64) (types.Date, error) { 1166 err := types.JudgeIntervalNumOverflow(diff, types.IntervalType(unit)) 1167 if err != nil { 1168 return 0, err 1169 } 1170 dt, success := start.ToDatetime().AddInterval(diff, types.IntervalType(unit), types.DateType) 1171 if success { 1172 return dt.ToDate(), nil 1173 } else { 1174 return 0, moerr.NewOutOfRangeNoCtx("date", "") 1175 } 1176 } 1177 1178 func doTimeAdd(start types.Time, diff int64, unit int64) (types.Time, error) { 1179 err := types.JudgeIntervalNumOverflow(diff, types.IntervalType(unit)) 1180 if err != nil { 1181 return 0, err 1182 } 1183 t, success := start.AddInterval(diff, types.IntervalType(unit)) 1184 if success { 1185 return t, nil 1186 } else { 1187 return 0, moerr.NewOutOfRangeNoCtx("time", "") 1188 } 1189 } 1190 1191 func doDatetimeAdd(start types.Datetime, diff int64, unit int64) (types.Datetime, error) { 1192 err := types.JudgeIntervalNumOverflow(diff, types.IntervalType(unit)) 1193 if err != nil { 1194 return 0, err 1195 } 1196 dt, success := start.AddInterval(diff, types.IntervalType(unit), types.DateTimeType) 1197 if success { 1198 return dt, nil 1199 } else { 1200 return 0, moerr.NewOutOfRangeNoCtx("datetime", "") 1201 } 1202 } 1203 1204 func doTimestampAdd(loc *time.Location, start types.Timestamp, diff int64, unit int64) (types.Timestamp, error) { 1205 err := types.JudgeIntervalNumOverflow(diff, types.IntervalType(unit)) 1206 if err != nil { 1207 return 0, err 1208 } 1209 dt, success := start.ToDatetime(loc).AddInterval(diff, types.IntervalType(unit), types.DateTimeType) 1210 if success { 1211 return dt.ToTimestamp(loc), nil 1212 } else { 1213 return 0, moerr.NewOutOfRangeNoCtx("timestamp", "") 1214 } 1215 } 1216 1217 func genericSearchLeft[T any](low, high int, nums []T, target T, equal func(a, b T) bool, greater func(a, b T) bool) int { 1218 for low <= high { 1219 mid := low + (high-low)/2 1220 if equal(nums[mid], target) { 1221 high = mid - 1 1222 } else if greater(nums[mid], target) { 1223 high = mid - 1 1224 } else { 1225 low = mid + 1 1226 } 1227 } 1228 return low 1229 } 1230 1231 func genericSearchRight[T any](low, high int, nums []T, target T, equal func(a, b T) bool, greater func(a, b T) bool) int { 1232 for low <= high { 1233 mid := low + (high-low)/2 1234 if equal(nums[mid], target) { 1235 low = mid + 1 1236 } else if greater(nums[mid], target) { 1237 high = mid - 1 1238 } else { 1239 low = mid + 1 1240 } 1241 } 1242 return high 1243 } 1244 1245 func genericSearchEqualRight[T any](low, high int, nums []T, target T, equal func(a, b T) bool) int { 1246 i := low + 1 1247 for ; i <= high; i++ { 1248 if !equal(nums[i], target) { 1249 break 1250 } 1251 } 1252 return i - 1 1253 } 1254 1255 func genericEqual[T types.OrderedT](a, b T) bool { 1256 return a == b 1257 } 1258 1259 func genericGreater[T types.OrderedT](a, b T) bool { 1260 return a > b 1261 } 1262 1263 func decimal64Equal(a, b types.Decimal64) bool { 1264 return a.Compare(b) == 0 1265 } 1266 1267 func decimal64Greater(a, b types.Decimal64) bool { 1268 return a.Compare(b) == 1 1269 } 1270 1271 func decimal128Equal(a, b types.Decimal128) bool { 1272 return a.Compare(b) == 0 1273 } 1274 1275 func decimal128Greater(a, b types.Decimal128) bool { 1276 return a.Compare(b) == 1 1277 }