github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/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 expression
    16  
    17  import (
    18  	"github.com/dolthub/go-mysql-server/sql"
    19  	"github.com/dolthub/go-mysql-server/sql/types"
    20  )
    21  
    22  // IsNull is an expression that checks if an expression is null.
    23  type IsNull struct {
    24  	UnaryExpression
    25  }
    26  
    27  var _ sql.Expression = (*IsNull)(nil)
    28  var _ sql.CollationCoercible = (*IsNull)(nil)
    29  
    30  // NewIsNull creates a new IsNull expression.
    31  func NewIsNull(child sql.Expression) *IsNull {
    32  	return &IsNull{UnaryExpression{child}}
    33  }
    34  
    35  // Type implements the Expression interface.
    36  func (e *IsNull) Type() sql.Type {
    37  	return types.Boolean
    38  }
    39  
    40  // CollationCoercibility implements the interface sql.CollationCoercible.
    41  func (*IsNull) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    42  	return sql.Collation_binary, 5
    43  }
    44  
    45  // IsNullable implements the Expression interface.
    46  func (e *IsNull) IsNullable() bool {
    47  	return false
    48  }
    49  
    50  // Eval implements the Expression interface.
    51  func (e *IsNull) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
    52  	v, err := e.Child.Eval(ctx, row)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	return v == nil, nil
    58  }
    59  
    60  func (e IsNull) String() string {
    61  	return e.Child.String() + " IS NULL"
    62  }
    63  
    64  func (e IsNull) DebugString() string {
    65  	return sql.DebugString(e.Child) + " IS NULL"
    66  }
    67  
    68  // WithChildren implements the Expression interface.
    69  func (e *IsNull) WithChildren(children ...sql.Expression) (sql.Expression, error) {
    70  	if len(children) != 1 {
    71  		return nil, sql.ErrInvalidChildrenNumber.New(e, len(children), 1)
    72  	}
    73  	return NewIsNull(children[0]), nil
    74  }