github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/aggexec/window.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 aggexec 16 17 import ( 18 "github.com/matrixorigin/matrixone/pkg/common/moerr" 19 "github.com/matrixorigin/matrixone/pkg/common/mpool" 20 "github.com/matrixorigin/matrixone/pkg/container/types" 21 "github.com/matrixorigin/matrixone/pkg/container/vector" 22 ) 23 24 func SingleWindowReturnType(_ []types.Type) types.Type { 25 return types.T_int64.ToType() 26 } 27 28 // special structure for a single column window function. 29 type singleWindowExec struct { 30 singleAggInfo 31 ret aggFuncResult[int64] 32 33 groups [][]int64 34 } 35 36 func makeRankDenseRankRowNumber(mg AggMemoryManager, info singleAggInfo) AggFuncExec { 37 return &singleWindowExec{ 38 singleAggInfo: info, 39 ret: initFixedAggFuncResult[int64](mg, info.retType, info.emptyNull), 40 } 41 } 42 43 func (exec *singleWindowExec) GroupGrow(more int) error { 44 exec.groups = append(exec.groups, make([][]int64, more)...) 45 return exec.ret.grows(more) 46 } 47 48 func (exec *singleWindowExec) PreAllocateGroups(more int) error { 49 return exec.ret.preAllocate(more) 50 } 51 52 func (exec *singleWindowExec) Fill(groupIndex int, row int, vectors []*vector.Vector) error { 53 value := vector.MustFixedCol[int64](vectors[0])[row] 54 exec.groups[groupIndex] = append(exec.groups[groupIndex], value) 55 return nil 56 } 57 58 func (exec *singleWindowExec) marshal() ([]byte, error) { 59 d := exec.singleAggInfo.getEncoded() 60 r, err := exec.ret.marshal() 61 if err != nil { 62 return nil, err 63 } 64 65 encoded := &EncodedAgg{ 66 ExecType: EncodedAggExecType_single_window, 67 Info: d, 68 Result: r, 69 Groups: nil, 70 } 71 if len(exec.groups) > 0 { 72 encoded.Groups = make([][]byte, len(exec.groups)) 73 for i := range encoded.Groups { 74 encoded.Groups[i] = types.EncodeSlice[int64](exec.groups[i]) 75 } 76 } 77 return encoded.Marshal() 78 } 79 80 func (exec *singleWindowExec) unmarshal(mp *mpool.MPool, result []byte, groups [][]byte) error { 81 if len(exec.groups) > 0 { 82 exec.groups = make([][]int64, len(groups)) 83 for i := range exec.groups { 84 if len(groups[i]) > 0 { 85 exec.groups[i] = types.DecodeSlice[int64](groups[i]) 86 } 87 } 88 } 89 return exec.ret.unmarshal(result) 90 } 91 92 func (exec *singleWindowExec) BulkFill(groupIndex int, vectors []*vector.Vector) error { 93 panic("implement me") 94 } 95 96 func (exec *singleWindowExec) BatchFill(offset int, groups []uint64, vectors []*vector.Vector) error { 97 panic("implement me") 98 } 99 100 func (exec *singleWindowExec) Merge(next AggFuncExec, groupIdx1, groupIdx2 int) error { 101 other := next.(*singleWindowExec) 102 exec.groups[groupIdx1] = append(exec.groups[groupIdx1], other.groups[groupIdx2]...) 103 return nil 104 } 105 106 func (exec *singleWindowExec) BatchMerge(next AggFuncExec, offset int, groups []uint64) error { 107 other := next.(*singleWindowExec) 108 for i := range groups { 109 if groups[i] != GroupNotMatched { 110 groupIdx1 := int(groups[i] - 1) 111 groupIdx2 := i + offset 112 113 exec.groups[groupIdx1] = append(exec.groups[groupIdx1], other.groups[groupIdx2]...) 114 } 115 } 116 return nil 117 } 118 119 func (exec *singleWindowExec) SetExtraInformation(partialResult any, groupIndex int) error { 120 panic("window function do not support the extra information") 121 } 122 123 func (exec *singleWindowExec) Flush() (*vector.Vector, error) { 124 switch exec.singleAggInfo.aggID { 125 case winIdOfRank: 126 return exec.flushRank() 127 case winIdOfDenseRank: 128 return exec.flushDenseRank() 129 case winIdOfRowNumber: 130 return exec.flushRowNumber() 131 } 132 return nil, moerr.NewInternalErrorNoCtx("invalid window function") 133 } 134 135 func (exec *singleWindowExec) Free() { 136 exec.ret.free() 137 } 138 139 func (exec *singleWindowExec) flushRank() (*vector.Vector, error) { 140 values := exec.ret.values 141 142 idx := 0 143 for _, group := range exec.groups { 144 if len(group) == 0 { 145 continue 146 } 147 148 sn := int64(1) 149 for i := 1; i < len(group); i++ { 150 m := int(group[i] - group[i-1]) 151 152 for k := idx + m; idx < k; idx++ { 153 values[idx] = sn 154 } 155 sn += int64(m) 156 } 157 } 158 return exec.ret.flush(), nil 159 } 160 161 func (exec *singleWindowExec) flushDenseRank() (*vector.Vector, error) { 162 values := exec.ret.values 163 164 idx := 0 165 for _, group := range exec.groups { 166 if len(group) == 0 { 167 continue 168 } 169 170 sn := int64(1) 171 for i := 1; i < len(group); i++ { 172 m := int(group[i] - group[i-1]) 173 174 for k := idx + m; idx < k; idx++ { 175 values[idx] = sn 176 } 177 sn++ 178 } 179 } 180 return exec.ret.flush(), nil 181 } 182 183 func (exec *singleWindowExec) flushRowNumber() (*vector.Vector, error) { 184 values := exec.ret.values 185 186 idx := 0 187 for _, group := range exec.groups { 188 if len(group) == 0 { 189 continue 190 } 191 192 n := group[len(group)-1] - group[0] 193 for j := int64(1); j <= n; j++ { 194 values[idx] = j 195 idx++ 196 } 197 } 198 return exec.ret.flush(), nil 199 }