github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/function/agg/bitmap.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 "github.com/RoaringBitmap/roaring" 19 "github.com/matrixorigin/matrixone/pkg/container/types" 20 "github.com/matrixorigin/matrixone/pkg/sql/colexec/aggexec" 21 ) 22 23 func RegisterBitmapConstruct1(id int64) { 24 aggexec.RegisterSingleAggFromFixedToVar( 25 aggexec.MakeSingleAgg2RegisteredInfo( 26 aggexec.MakeSingleColumnAggInformation(id, types.T_uint64.ToType(), BitmapConstructReturnType, false, true), 27 newAggBitmapConstruct, 28 InitAggBitmapConstruct, 29 FillAggBitmapConstruct, nil, FillsAggBitmapConstruct, 30 MergeAggBitmapConstruct, 31 FlushAggBitmapConstruct, 32 )) 33 } 34 35 func RegisterBitmapOr1(id int64) { 36 aggexec.RegisterSingleAggFromVarToVar( 37 aggexec.MakeSingleAgg4RegisteredInfo( 38 aggexec.MakeSingleColumnAggInformation(id, types.T_varbinary.ToType(), BitmapOrReturnType, false, true), 39 newAggBitmapOr, 40 InitAggBitmapOr, 41 FillAggBitmapOr, nil, FillsAggBitmapOr, 42 MergeAggBitmapOr, 43 FlushAggBitmapOr, 44 )) 45 } 46 47 var BitmapConstructSupportedTypes = []types.T{ 48 types.T_uint64, 49 } 50 51 func BitmapConstructReturnType(_ []types.Type) types.Type { 52 return types.T_varbinary.ToType() 53 } 54 55 type aggBitmapConstruct struct { 56 bmp *roaring.Bitmap 57 } 58 59 func newAggBitmapConstruct() aggexec.SingleAggFromFixedRetVar[uint64] { 60 return &aggBitmapConstruct{} 61 } 62 63 func (a *aggBitmapConstruct) Marshal() []byte { 64 b, _ := a.bmp.MarshalBinary() 65 return b 66 } 67 func (a *aggBitmapConstruct) Unmarshal(bs []byte) { 68 a.bmp = roaring.New() 69 _ = a.bmp.UnmarshalBinary(bs) 70 } 71 72 func InitAggBitmapConstruct( 73 exec aggexec.SingleAggFromFixedRetVar[uint64], set aggexec.AggBytesSetter, arg, ret types.Type) error { 74 a := exec.(*aggBitmapConstruct) 75 a.bmp = roaring.New() 76 return nil 77 } 78 func FillAggBitmapConstruct( 79 exec aggexec.SingleAggFromFixedRetVar[uint64], 80 value uint64, getter aggexec.AggBytesGetter, setter aggexec.AggBytesSetter) error { 81 a := exec.(*aggBitmapConstruct) 82 a.bmp.Add(uint32(value)) 83 return nil 84 } 85 func FillsAggBitmapConstruct( 86 exec aggexec.SingleAggFromFixedRetVar[uint64], 87 value uint64, isNull bool, count int, getter aggexec.AggBytesGetter, setter aggexec.AggBytesSetter) error { 88 if !isNull { 89 return FillAggBitmapConstruct(exec, value, getter, setter) 90 } 91 return nil 92 } 93 func MergeAggBitmapConstruct( 94 exec1, exec2 aggexec.SingleAggFromFixedRetVar[uint64], 95 getter1, getter2 aggexec.AggBytesGetter, setter aggexec.AggBytesSetter) error { 96 a1 := exec1.(*aggBitmapConstruct) 97 a2 := exec2.(*aggBitmapConstruct) 98 a1.bmp.Or(a2.bmp) 99 return nil 100 } 101 func FlushAggBitmapConstruct( 102 exec aggexec.SingleAggFromFixedRetVar[uint64], 103 getter aggexec.AggBytesGetter, setter aggexec.AggBytesSetter) error { 104 a := exec.(*aggBitmapConstruct) 105 res, err := a.bmp.MarshalBinary() 106 if err != nil { 107 return err 108 } 109 return setter(res) 110 } 111 112 var BitmapOrSupportedTypes = []types.T{ 113 types.T_varbinary, 114 } 115 116 var BitmapOrReturnType = BitmapConstructReturnType 117 118 type aggBitmapOr struct { 119 bmp *roaring.Bitmap 120 } 121 122 func newAggBitmapOr() aggexec.SingleAggFromVarRetVar { 123 return &aggBitmapOr{} 124 } 125 126 func (a *aggBitmapOr) Marshal() []byte { 127 b, _ := a.bmp.MarshalBinary() 128 return b 129 } 130 func (a *aggBitmapOr) Unmarshal(bs []byte) { 131 a.bmp = roaring.New() 132 _ = a.bmp.UnmarshalBinary(bs) 133 } 134 135 func InitAggBitmapOr( 136 exec aggexec.SingleAggFromVarRetVar, set aggexec.AggBytesSetter, arg, ret types.Type) error { 137 a := exec.(*aggBitmapOr) 138 a.bmp = roaring.New() 139 return nil 140 } 141 func FillAggBitmapOr( 142 exec aggexec.SingleAggFromVarRetVar, 143 value []byte, getter aggexec.AggBytesGetter, setter aggexec.AggBytesSetter) error { 144 bmp := roaring.New() 145 if err := bmp.UnmarshalBinary(value); err != nil { 146 return err 147 } 148 149 a := exec.(*aggBitmapOr) 150 a.bmp.Or(bmp) 151 return nil 152 } 153 func FillsAggBitmapOr( 154 exec aggexec.SingleAggFromVarRetVar, 155 value []byte, isNull bool, count int, getter aggexec.AggBytesGetter, setter aggexec.AggBytesSetter) error { 156 if !isNull { 157 return FillAggBitmapOr(exec, value, getter, setter) 158 } 159 return nil 160 } 161 func MergeAggBitmapOr( 162 exec1, exec2 aggexec.SingleAggFromVarRetVar, 163 getter1, getter2 aggexec.AggBytesGetter, setter aggexec.AggBytesSetter) error { 164 a1 := exec1.(*aggBitmapOr) 165 a2 := exec2.(*aggBitmapOr) 166 a1.bmp.Or(a2.bmp) 167 return nil 168 } 169 func FlushAggBitmapOr( 170 exec aggexec.SingleAggFromVarRetVar, 171 getter aggexec.AggBytesGetter, setter aggexec.AggBytesSetter) error { 172 a := exec.(*aggBitmapOr) 173 res, err := a.bmp.MarshalBinary() 174 if err != nil { 175 return err 176 } 177 return setter(res) 178 }