github.com/matrixorigin/matrixone@v0.7.0/pkg/container/vector/functionTools.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 vector 16 17 import ( 18 "fmt" 19 "github.com/matrixorigin/matrixone/pkg/common/bitmap" 20 "github.com/matrixorigin/matrixone/pkg/common/mpool" 21 "github.com/matrixorigin/matrixone/pkg/container/types" 22 ) 23 24 // FunctionParameterWrapper is generated from a vector. 25 // It hides the relevant details of vector (like scalar and contain null or not.) 26 // and provides a series of methods to get values. 27 type FunctionParameterWrapper[T types.FixedSizeT] interface { 28 // GetType will return the type info of wrapped parameter. 29 GetType() types.Type 30 31 // GetSourceVector return the source vector. 32 GetSourceVector() *Vector 33 34 // GetValue return the Idx th value and if it's null or not. 35 // watch that, if str type, GetValue will return the []types.Varlena directly. 36 GetValue(idx uint64) (T, bool) 37 38 // GetStrValue return the Idx th string value and if it's null or not. 39 GetStrValue(idx uint64) ([]byte, bool) 40 41 // UnSafeGetAllValue return all the values. 42 // please use it carefully because we didn't check the null situation. 43 UnSafeGetAllValue() []T 44 } 45 46 var _ FunctionParameterWrapper[int64] = &FunctionParameterNormal[int64]{} 47 var _ FunctionParameterWrapper[int64] = &FunctionParameterWithoutNull[int64]{} 48 var _ FunctionParameterWrapper[int64] = &FunctionParameterScalar[int64]{} 49 var _ FunctionParameterWrapper[int64] = &FunctionParameterScalarNull[int64]{} 50 51 func GenerateFunctionFixedTypeParameter[T types.FixedSizeT](v *Vector) FunctionParameterWrapper[T] { 52 t := v.GetType() 53 if v.IsScalarNull() { 54 return &FunctionParameterScalarNull[T]{ 55 typ: t, 56 sourceVector: v, 57 } 58 } 59 cols := MustTCols[T](v) 60 if v.IsScalar() { 61 return &FunctionParameterScalar[T]{ 62 typ: t, 63 sourceVector: v, 64 scalarValue: cols[0], 65 } 66 } 67 if v.Nsp != nil && v.Nsp.Np != nil && v.Nsp.Np.Len() > 0 { 68 return &FunctionParameterNormal[T]{ 69 typ: t, 70 sourceVector: v, 71 values: cols, 72 nullMap: v.Nsp.Np, 73 } 74 } 75 return &FunctionParameterWithoutNull[T]{ 76 typ: t, 77 sourceVector: v, 78 values: cols, 79 } 80 } 81 82 func GenerateFunctionStrParameter(v *Vector) FunctionParameterWrapper[types.Varlena] { 83 t := v.GetType() 84 if v.IsScalarNull() { 85 return &FunctionParameterScalarNull[types.Varlena]{ 86 typ: t, 87 sourceVector: v, 88 } 89 } 90 cols := MustTCols[types.Varlena](v) 91 if v.IsScalar() { 92 return &FunctionParameterScalar[types.Varlena]{ 93 typ: t, 94 sourceVector: v, 95 scalarValue: cols[0], 96 scalarStr: cols[0].GetByteSlice(v.area), 97 } 98 } 99 if v.Nsp != nil && v.Nsp.Np != nil && v.Nsp.Np.Len() > 0 { 100 return &FunctionParameterNormal[types.Varlena]{ 101 typ: t, 102 sourceVector: v, 103 strValues: cols, 104 area: v.area, 105 nullMap: v.Nsp.Np, 106 } 107 } 108 return &FunctionParameterWithoutNull[types.Varlena]{ 109 typ: t, 110 sourceVector: v, 111 strValues: cols, 112 area: v.area, 113 } 114 } 115 116 // FunctionParameterNormal is a wrapper of normal vector which 117 // may contains null value. 118 type FunctionParameterNormal[T types.FixedSizeT] struct { 119 typ types.Type 120 sourceVector *Vector 121 values []T 122 strValues []types.Varlena 123 area []byte 124 nullMap *bitmap.Bitmap 125 } 126 127 func (p *FunctionParameterNormal[T]) GetType() types.Type { 128 return p.typ 129 } 130 131 func (p *FunctionParameterNormal[T]) GetSourceVector() *Vector { 132 return p.sourceVector 133 } 134 135 func (p *FunctionParameterNormal[T]) GetValue(idx uint64) (value T, isNull bool) { 136 if p.nullMap.Contains(idx) { 137 return value, true 138 } 139 return p.values[idx], false 140 } 141 142 func (p *FunctionParameterNormal[T]) GetStrValue(idx uint64) (value []byte, isNull bool) { 143 if p.nullMap.Contains(idx) { 144 return nil, true 145 } 146 return p.strValues[idx].GetByteSlice(p.area), false 147 } 148 149 func (p *FunctionParameterNormal[T]) UnSafeGetAllValue() []T { 150 return p.values 151 } 152 153 // FunctionParameterWithoutNull is a wrapper of normal vector but 154 // without null value. 155 type FunctionParameterWithoutNull[T types.FixedSizeT] struct { 156 typ types.Type 157 sourceVector *Vector 158 values []T 159 strValues []types.Varlena 160 area []byte 161 } 162 163 func (p *FunctionParameterWithoutNull[T]) GetType() types.Type { 164 return p.typ 165 } 166 167 func (p *FunctionParameterWithoutNull[T]) GetSourceVector() *Vector { 168 return p.sourceVector 169 } 170 171 func (p *FunctionParameterWithoutNull[T]) GetValue(idx uint64) (T, bool) { 172 return p.values[idx], false 173 } 174 175 func (p *FunctionParameterWithoutNull[T]) GetStrValue(idx uint64) ([]byte, bool) { 176 return p.strValues[idx].GetByteSlice(p.area), false 177 } 178 179 func (p *FunctionParameterWithoutNull[T]) UnSafeGetAllValue() []T { 180 return p.values 181 } 182 183 // FunctionParameterScalar is a wrapper of scalar vector. 184 type FunctionParameterScalar[T types.FixedSizeT] struct { 185 typ types.Type 186 sourceVector *Vector 187 scalarValue T 188 scalarStr []byte 189 } 190 191 func (p *FunctionParameterScalar[T]) GetType() types.Type { 192 return p.typ 193 } 194 195 func (p *FunctionParameterScalar[T]) GetSourceVector() *Vector { 196 return p.sourceVector 197 } 198 199 func (p *FunctionParameterScalar[T]) GetValue(_ uint64) (T, bool) { 200 return p.scalarValue, false 201 } 202 203 func (p *FunctionParameterScalar[T]) GetStrValue(_ uint64) ([]byte, bool) { 204 return p.scalarStr, false 205 } 206 207 func (p *FunctionParameterScalar[T]) UnSafeGetAllValue() []T { 208 return []T{p.scalarValue} 209 } 210 211 // FunctionParameterScalarNull is a wrapper of scalar null vector. 212 type FunctionParameterScalarNull[T types.FixedSizeT] struct { 213 typ types.Type 214 sourceVector *Vector 215 } 216 217 func (p *FunctionParameterScalarNull[T]) GetType() types.Type { 218 return p.typ 219 } 220 221 func (p *FunctionParameterScalarNull[T]) GetSourceVector() *Vector { 222 return p.sourceVector 223 } 224 225 func (p *FunctionParameterScalarNull[T]) GetValue(_ uint64) (value T, isNull bool) { 226 return value, true 227 } 228 229 func (p *FunctionParameterScalarNull[T]) GetStrValue(_ uint64) ([]byte, bool) { 230 return nil, true 231 } 232 233 func (p *FunctionParameterScalarNull[T]) UnSafeGetAllValue() []T { 234 return nil 235 } 236 237 type FunctionResultWrapper interface { 238 GetResultVector() *Vector 239 Free() 240 } 241 242 var _ FunctionResultWrapper = &FunctionResult[int64]{} 243 244 type FunctionResult[T types.FixedSizeT] struct { 245 vec *Vector 246 mp *mpool.MPool 247 } 248 249 func MustFunctionResult[T types.FixedSizeT](wrapper FunctionResultWrapper) *FunctionResult[T] { 250 if fr, ok := wrapper.(*FunctionResult[T]); ok { 251 return fr 252 } 253 panic("wrong type for FunctionResultWrapper") 254 } 255 256 func newResultFunc[T types.FixedSizeT](v *Vector, mp *mpool.MPool) *FunctionResult[T] { 257 return &FunctionResult[T]{ 258 vec: v, 259 mp: mp, 260 } 261 } 262 263 func (fr *FunctionResult[T]) Append(val T, isnull bool) error { 264 return fr.vec.Append(val, isnull, fr.mp) 265 } 266 267 func (fr *FunctionResult[T]) AppendStr(val []byte, isnull bool) error { 268 return fr.vec.Append(val, isnull, fr.mp) 269 } 270 271 func (fr *FunctionResult[T]) GetType() types.Type { 272 return fr.vec.Typ 273 } 274 275 func (fr *FunctionResult[T]) SetFromParameter(fp FunctionParameterWrapper[T]) { 276 // clean the old memory 277 if fr.vec != fp.GetSourceVector() { 278 fr.Free() 279 } 280 fr.vec = fp.GetSourceVector() 281 } 282 283 func (fr *FunctionResult[T]) GetResultVector() *Vector { 284 return fr.vec 285 } 286 287 func (fr *FunctionResult[T]) ConvertToParameter() FunctionParameterWrapper[T] { 288 return GenerateFunctionFixedTypeParameter[T](fr.vec) 289 } 290 291 func (fr *FunctionResult[T]) ConvertToStrParameter() FunctionParameterWrapper[types.Varlena] { 292 return GenerateFunctionStrParameter(fr.vec) 293 } 294 295 func (fr *FunctionResult[T]) Free() { 296 fr.vec.Free(fr.mp) 297 } 298 299 func NewFunctionResultWrapper(typ types.Type, mp *mpool.MPool, isConst bool, length int) FunctionResultWrapper { 300 var v *Vector 301 if isConst { 302 v = NewConst(typ, length) 303 } else { 304 v = New(typ) 305 } 306 307 switch typ.Oid { 308 case types.T_char, types.T_varchar, types.T_blob, types.T_text: 309 // IF STRING type. 310 return newResultFunc[types.Varlena](v, mp) 311 case types.T_json: 312 return newResultFunc[types.Varlena](v, mp) 313 } 314 315 // Pre allocate the memory 316 // XXX PreAllocType has BUG. It only shrinks the cols. 317 //v = PreAllocType(typ, 0, length, mp) 318 //SetLength(v, 0) 319 switch typ.Oid { 320 case types.T_bool: 321 return newResultFunc[bool](v, mp) 322 case types.T_int8: 323 return newResultFunc[int8](v, mp) 324 case types.T_int16: 325 return newResultFunc[int16](v, mp) 326 case types.T_int32: 327 return newResultFunc[int32](v, mp) 328 case types.T_int64: 329 return newResultFunc[int64](v, mp) 330 case types.T_uint8: 331 return newResultFunc[uint8](v, mp) 332 case types.T_uint16: 333 return newResultFunc[uint16](v, mp) 334 case types.T_uint32: 335 return newResultFunc[uint32](v, mp) 336 case types.T_uint64: 337 return newResultFunc[uint64](v, mp) 338 case types.T_float32: 339 return newResultFunc[float32](v, mp) 340 case types.T_float64: 341 return newResultFunc[float64](v, mp) 342 case types.T_date: 343 return newResultFunc[types.Date](v, mp) 344 case types.T_datetime: 345 return newResultFunc[types.Datetime](v, mp) 346 case types.T_time: 347 return newResultFunc[types.Time](v, mp) 348 case types.T_timestamp: 349 return newResultFunc[types.Timestamp](v, mp) 350 case types.T_decimal64: 351 return newResultFunc[types.Decimal64](v, mp) 352 case types.T_decimal128: 353 return newResultFunc[types.Decimal128](v, mp) 354 case types.T_TS: 355 return newResultFunc[types.TS](v, mp) 356 case types.T_Rowid: 357 return newResultFunc[types.Rowid](v, mp) 358 case types.T_uuid: 359 return newResultFunc[types.Uuid](v, mp) 360 } 361 panic(fmt.Sprintf("unexpected type %s for function result", typ)) 362 }