github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/interlock/aggfuncs/func_cume_dist.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package aggfuncs 15 16 import ( 17 "unsafe" 18 19 "github.com/whtcorpsinc/milevadb/stochastikctx" 20 "github.com/whtcorpsinc/milevadb/soliton/chunk" 21 ) 22 23 const ( 24 // DefPartialResult4CumeDistSize is the size of partialResult4CumeDist 25 DefPartialResult4CumeDistSize = int64(unsafe.Sizeof(partialResult4CumeDist{})) 26 ) 27 28 type cumeDist struct { 29 baseAggFunc 30 rowComparer 31 } 32 33 type partialResult4CumeDist struct { 34 curIdx int 35 lastRank int 36 rows []chunk.Event 37 } 38 39 func (r *cumeDist) AllocPartialResult() (pr PartialResult, memDelta int64) { 40 return PartialResult(&partialResult4CumeDist{}), DefPartialResult4CumeDistSize 41 } 42 43 func (r *cumeDist) ResetPartialResult(pr PartialResult) { 44 p := (*partialResult4CumeDist)(pr) 45 p.curIdx = 0 46 p.lastRank = 0 47 p.rows = p.rows[:0] 48 } 49 50 func (r *cumeDist) UFIDelatePartialResult(sctx stochastikctx.Context, rowsInGroup []chunk.Event, pr PartialResult) (memDelta int64, err error) { 51 p := (*partialResult4CumeDist)(pr) 52 p.rows = append(p.rows, rowsInGroup...) 53 memDelta += int64(len(rowsInGroup)) * DefEventSize 54 return memDelta, nil 55 } 56 57 func (r *cumeDist) AppendFinalResult2Chunk(sctx stochastikctx.Context, pr PartialResult, chk *chunk.Chunk) error { 58 p := (*partialResult4CumeDist)(pr) 59 numEvents := len(p.rows) 60 for p.lastRank < numEvents && r.compareEvents(p.rows[p.curIdx], p.rows[p.lastRank]) == 0 { 61 p.lastRank++ 62 } 63 p.curIdx++ 64 chk.AppendFloat64(r.ordinal, float64(p.lastRank)/float64(numEvents)) 65 return nil 66 }