github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/nullif.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/expression" 22 "github.com/dolthub/go-mysql-server/sql/types" 23 ) 24 25 // NullIf function compares two expressions and returns NULL if they are equal. Otherwise, the first expression is returned. 26 type NullIf struct { 27 expression.BinaryExpressionStub 28 } 29 30 var _ sql.FunctionExpression = (*NullIf)(nil) 31 var _ sql.CollationCoercible = (*NullIf)(nil) 32 33 // NewNullIf returns a new NULLIF UDF 34 func NewNullIf(ex1, ex2 sql.Expression) sql.Expression { 35 return &NullIf{ 36 expression.BinaryExpressionStub{ 37 LeftChild: ex1, 38 RightChild: ex2, 39 }, 40 } 41 } 42 43 // FunctionName implements sql.FunctionExpression 44 func (f *NullIf) FunctionName() string { 45 return "nullif" 46 } 47 48 // Description implements sql.FunctionExpression 49 func (f *NullIf) Description() string { 50 return "returns NULL if expr1 = expr2 is true, otherwise returns expr1." 51 } 52 53 // Eval implements the Expression interface. 54 func (f *NullIf) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 55 if types.IsNull(f.LeftChild) && types.IsNull(f.RightChild) { 56 return nil, nil 57 } 58 59 val, err := expression.NewEquals(f.LeftChild, f.RightChild).Eval(ctx, row) 60 if err != nil { 61 return nil, err 62 } 63 if b, ok := val.(bool); ok && b { 64 return nil, nil 65 } 66 67 return f.LeftChild.Eval(ctx, row) 68 } 69 70 // Type implements the Expression interface. 71 func (f *NullIf) Type() sql.Type { 72 if types.IsNull(f.LeftChild) { 73 return types.Null 74 } 75 76 return f.LeftChild.Type() 77 } 78 79 // CollationCoercibility implements the interface sql.CollationCoercible. 80 func (f *NullIf) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { 81 if types.IsNull(f.LeftChild) { 82 return sql.Collation_binary, 6 83 } 84 return sql.GetCoercibility(ctx, f.LeftChild) 85 } 86 87 // IsNullable implements the Expression interface. 88 func (f *NullIf) IsNullable() bool { 89 return true 90 } 91 92 func (f *NullIf) String() string { 93 return fmt.Sprintf("%s(%s,%s)", f.FunctionName(), f.LeftChild, f.RightChild) 94 } 95 96 // WithChildren implements the Expression interface. 97 func (f *NullIf) WithChildren(children ...sql.Expression) (sql.Expression, error) { 98 if len(children) != 2 { 99 return nil, sql.ErrInvalidChildrenNumber.New(f, len(children), 2) 100 } 101 return NewNullIf(children[0], children[1]), nil 102 }