github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/if.go (about) 1 // Copyright 2020-2021 Dolthub, Inc. 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 function 16 17 import ( 18 "fmt" 19 20 "github.com/dolthub/go-mysql-server/sql" 21 "github.com/dolthub/go-mysql-server/sql/types" 22 ) 23 24 // If function returns the second value if the first is true, the third value otherwise. 25 type If struct { 26 expr sql.Expression 27 ifTrue sql.Expression 28 ifFalse sql.Expression 29 } 30 31 var _ sql.FunctionExpression = (*If)(nil) 32 var _ sql.CollationCoercible = (*If)(nil) 33 34 // FunctionName implements sql.FunctionExpression 35 func (f *If) FunctionName() string { 36 return "if" 37 } 38 39 // Description implements sql.FunctionExpression 40 func (f *If) Description() string { 41 return "if expr evaluates to true, returns ifTrue. Otherwise returns ifFalse." 42 } 43 44 func (f *If) Resolved() bool { 45 return f.expr.Resolved() && f.ifTrue.Resolved() && f.ifFalse.Resolved() 46 } 47 48 func (f *If) Children() []sql.Expression { 49 return []sql.Expression{ 50 f.expr, f.ifTrue, f.ifFalse, 51 } 52 } 53 54 // NewIf returns a new IF UDF 55 func NewIf(expr, ifTrue, ifFalse sql.Expression) sql.Expression { 56 return &If{ 57 expr: expr, 58 ifTrue: ifTrue, 59 ifFalse: ifFalse, 60 } 61 } 62 63 // Eval implements the Expression interface. 64 func (f *If) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 65 e, err := f.expr.Eval(ctx, row) 66 if err != nil { 67 return nil, err 68 } 69 70 var asBool bool 71 if e == nil { 72 asBool = false 73 } else { 74 asBool, err = sql.ConvertToBool(ctx, e) 75 if err != nil { 76 return nil, err 77 } 78 } 79 80 if asBool { 81 return f.ifTrue.Eval(ctx, row) 82 } else { 83 return f.ifFalse.Eval(ctx, row) 84 } 85 } 86 87 // Type implements the Expression interface. 88 func (f *If) Type() sql.Type { 89 // if either type is string type, this should be a string type, regardless need to promote 90 typ1 := f.ifTrue.Type() 91 typ2 := f.ifFalse.Type() 92 if types.IsText(typ1) || types.IsText(typ2) { 93 return types.Text 94 } 95 96 if typ1 == types.Null { 97 return typ2.Promote() 98 } 99 return typ1.Promote() 100 } 101 102 // CollationCoercibility implements the interface sql.CollationCoercible. 103 func (f *If) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { 104 // We would need to evaluate the condition to return the correct result here, so we'll copy 105 // Type and just return the true result 106 return sql.GetCoercibility(ctx, f.ifTrue) 107 } 108 109 // IsNullable implements the Expression interface. 110 func (f *If) IsNullable() bool { 111 return f.ifTrue.IsNullable() 112 } 113 114 func (f *If) String() string { 115 return fmt.Sprintf("%s(%s,%s,%s)", f.FunctionName(), f.expr, f.ifTrue, f.ifFalse) 116 } 117 118 // WithChildren implements the Expression interface. 119 func (f *If) WithChildren(children ...sql.Expression) (sql.Expression, error) { 120 if len(children) != 3 { 121 return nil, sql.ErrInvalidChildrenNumber.New(f, len(children), 3) 122 } 123 return NewIf(children[0], children[1], children[2]), nil 124 }