github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/multi/math_multi.go (about) 1 // Copyright 2021 - 2022 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 multi 16 17 import ( 18 "fmt" 19 20 "github.com/matrixorigin/matrixone/pkg/common/moerr" 21 "github.com/matrixorigin/matrixone/pkg/container/nulls" 22 "github.com/matrixorigin/matrixone/pkg/container/types" 23 "github.com/matrixorigin/matrixone/pkg/container/vector" 24 "github.com/matrixorigin/matrixone/pkg/vm/process" 25 "golang.org/x/exp/constraints" 26 ) 27 28 type mathMultiT interface { 29 constraints.Integer | constraints.Float | types.Decimal64 | types.Decimal128 30 } 31 32 type mathMultiFun[T mathMultiT] func([]T, []T, int64) []T 33 34 func generalMathMulti[T mathMultiT](funName string, vecs []*vector.Vector, proc *process.Process, cb mathMultiFun[T]) (*vector.Vector, error) { 35 typ := vecs[0].Typ.Oid.ToType() 36 digits := int64(0) 37 if len(vecs) > 1 { 38 // if vecs[1].IsScalarNull() { 39 // return proc.AllocScalarNullVector(typ), nil 40 // } 41 if !vecs[1].IsScalar() || vecs[1].Typ.Oid != types.T_int64 { 42 return nil, moerr.NewInvalidArg(proc.Ctx, fmt.Sprintf("the second argument of the %s", funName), "not const") 43 } 44 digits = vecs[1].Col.([]int64)[0] 45 } 46 vs := vector.MustTCols[T](vecs[0]) 47 if vecs[0].IsScalarNull() { 48 return proc.AllocScalarNullVector(typ), nil 49 } 50 51 if vecs[0].IsScalar() { 52 rs := make([]T, 1) 53 ret_rs := cb(vs, rs, digits) 54 55 vec := vector.NewConstFixed(typ, 1, ret_rs[0], proc.Mp()) 56 nulls.Set(vec.Nsp, vecs[0].Nsp) 57 return vec, nil 58 } else { 59 rs := make([]T, len(vs)) 60 ret_rs := cb(vs, rs, digits) 61 62 vec := vector.NewWithFixed(typ, ret_rs, nulls.NewWithSize(len(ret_rs)), proc.Mp()) 63 nulls.Set(vec.Nsp, vecs[0].Nsp) 64 65 return vec, nil 66 } 67 }