github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/operator/if.go (about) 1 // Copyright 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 operator 16 17 import ( 18 "github.com/matrixorigin/matrixone/pkg/container/types" 19 "github.com/matrixorigin/matrixone/pkg/container/vector" 20 "github.com/matrixorigin/matrixone/pkg/vm/process" 21 "golang.org/x/exp/constraints" 22 ) 23 24 // If operator supported format like that 25 // 26 // If(<boolean operator>, <value operator>, <value operator>) 27 var ( 28 IfBool = func(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 29 return ifGeneral[bool](vs, proc, types.Type{Oid: types.T_bool}) 30 } 31 32 IfUint8 = func(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 33 return ifGeneral[uint8](vs, proc, types.Type{Oid: types.T_uint8}) 34 } 35 36 IfUint16 = func(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 37 return ifGeneral[uint16](vs, proc, types.Type{Oid: types.T_uint16}) 38 } 39 40 IfUint32 = func(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 41 return ifGeneral[uint32](vs, proc, types.Type{Oid: types.T_uint32}) 42 } 43 44 IfUint64 = func(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 45 return ifGeneral[uint64](vs, proc, types.Type{Oid: types.T_uint64}) 46 } 47 48 IfInt8 = func(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 49 return ifGeneral[int8](vs, proc, types.Type{Oid: types.T_int8}) 50 } 51 52 IfInt16 = func(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 53 return ifGeneral[int16](vs, proc, types.Type{Oid: types.T_int16}) 54 } 55 56 IfInt32 = func(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 57 return ifGeneral[int32](vs, proc, types.Type{Oid: types.T_int32}) 58 } 59 60 IfInt64 = func(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 61 return ifGeneral[int64](vs, proc, types.Type{Oid: types.T_int64}) 62 } 63 64 IfFloat32 = func(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 65 return ifGeneral[float32](vs, proc, types.Type{Oid: types.T_float32}) 66 } 67 68 IfFloat64 = func(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 69 return ifGeneral[float64](vs, proc, types.Type{Oid: types.T_float64}) 70 } 71 72 IfDecimal64 = func(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 73 return ifGeneral[types.Decimal64](vs, proc, types.Type{Oid: types.T_decimal64}) 74 } 75 76 IfDecimal128 = func(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 77 return ifGeneral[types.Decimal128](vs, proc, types.Type{Oid: types.T_decimal128}) 78 } 79 80 IfDate = func(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 81 return ifGeneral[types.Date](vs, proc, types.Type{Oid: types.T_date}) 82 } 83 84 IfTime = func(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 85 return ifGeneral[types.Time](vs, proc, types.Type{Oid: types.T_time}) 86 } 87 88 IfDateTime = func(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 89 return ifGeneral[types.Datetime](vs, proc, types.Type{Oid: types.T_datetime}) 90 } 91 92 IfVarchar = func(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 93 return ifForString(vs, proc, types.Type{Oid: types.T_varchar, Width: types.MaxVarcharLen}) 94 } 95 96 IfChar = func(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 97 return ifForString(vs, proc, types.Type{Oid: types.T_char}) 98 } 99 100 IfTimestamp = func(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 101 return ifGeneral[types.Timestamp](vs, proc, types.Type{Oid: types.T_timestamp}) 102 } 103 104 IfBlob = func(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 105 return ifForString(vs, proc, types.Type{Oid: types.T_blob}) 106 } 107 108 IfText = func(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 109 return ifForString(vs, proc, types.Type{Oid: types.T_text}) 110 } 111 112 IfJson = func(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 113 return ifForString(vs, proc, types.Type{Oid: types.T_json}) 114 } 115 116 IfUuid = func(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 117 return ifForString(vs, proc, types.Type{Oid: types.T_uuid}) 118 } 119 ) 120 121 func IfTypeCheckFn(inputTypes []types.T, _ []types.T, ret types.T) bool { 122 if len(inputTypes) == 3 && inputTypes[0] == types.T_bool { 123 if inputTypes[1] != ret && inputTypes[1] != types.T_any { 124 return false 125 } 126 if inputTypes[2] != ret && inputTypes[2] != types.T_any { 127 return false 128 } 129 return true 130 } 131 return false 132 } 133 134 type IfRet interface { 135 constraints.Integer | constraints.Float | bool | types.Date | types.Datetime | 136 types.Decimal64 | types.Decimal128 | types.Timestamp 137 } 138 139 func ifGeneral[T IfRet](vs []*vector.Vector, proc *process.Process, ret types.Type) (*vector.Vector, error) { 140 return cwGeneral[T](vs, proc, ret) 141 } 142 143 func ifForString(vs []*vector.Vector, proc *process.Process, typ types.Type) (*vector.Vector, error) { 144 return cwString(vs, proc, typ) 145 }