github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/isnull.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  // IsNull is a function that returns whether a value is null or not.
    26  type IsNull struct {
    27  	expression.UnaryExpression
    28  }
    29  
    30  var _ sql.FunctionExpression = (*IsNull)(nil)
    31  var _ sql.CollationCoercible = (*IsNull)(nil)
    32  
    33  // NewIsNull creates a new IsNull expression.
    34  func NewIsNull(e sql.Expression) sql.Expression {
    35  	return &IsNull{expression.UnaryExpression{Child: e}}
    36  }
    37  
    38  // FunctionName implements sql.FunctionExpression
    39  func (ib *IsNull) FunctionName() string {
    40  	return "isnull"
    41  }
    42  
    43  // Description implements sql.FunctionExpression
    44  func (ib *IsNull) Description() string {
    45  	return "returns whether a expr is null or not."
    46  }
    47  
    48  // Eval implements the Expression interface.
    49  func (ib *IsNull) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
    50  	v, err := ib.Child.Eval(ctx, row)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	if v != nil {
    56  		return false, nil
    57  	}
    58  
    59  	return true, nil
    60  }
    61  
    62  func (ib *IsNull) String() string {
    63  	return fmt.Sprintf("isnull(%s)", ib.Child)
    64  }
    65  
    66  // WithChildren implements the Expression interface.
    67  func (ib *IsNull) WithChildren(children ...sql.Expression) (sql.Expression, error) {
    68  	if len(children) != 1 {
    69  		return nil, sql.ErrInvalidChildrenNumber.New(ib, len(children), 1)
    70  	}
    71  	return NewIsNull(children[0]), nil
    72  }
    73  
    74  // Type implements the Expression interface.
    75  func (ib *IsNull) Type() sql.Type {
    76  	return types.Boolean
    77  }
    78  
    79  // CollationCoercibility implements the interface sql.CollationCoercible.
    80  func (*IsNull) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    81  	return sql.Collation_binary, 5
    82  }