github.com/dolthub/go-mysql-server@v0.18.0/sql/plan/existssubquery.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 plan
    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  // ExistsSubquery is an expression that checks that a subquery returns a non-empty result set. It's in the plan package,
    25  // instead of the expression package, because Subquery is itself in the plan package (because it functions more like a
    26  // plan node than an expression in its evaluation).
    27  type ExistsSubquery struct {
    28  	Query *Subquery
    29  }
    30  
    31  var _ sql.Expression = (*ExistsSubquery)(nil)
    32  var _ sql.CollationCoercible = (*ExistsSubquery)(nil)
    33  
    34  // NewExistsSubquery created an ExistsSubquery expression.
    35  func NewExistsSubquery(sq *Subquery) *ExistsSubquery {
    36  	return &ExistsSubquery{sq}
    37  }
    38  
    39  // Eval implements the Expression interface.
    40  func (e *ExistsSubquery) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
    41  	hasResultRow, err := e.Query.HasResultRow(ctx, row)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	return hasResultRow, nil
    47  }
    48  
    49  // WithChildren implements the Expression interface.
    50  func (e *ExistsSubquery) WithChildren(children ...sql.Expression) (sql.Expression, error) {
    51  	if len(children) != 1 {
    52  		return nil, sql.ErrInvalidChildrenNumber.New(e, len(children), 1)
    53  	}
    54  
    55  	sq, ok := children[0].(*Subquery)
    56  	if !ok {
    57  		return nil, fmt.Errorf("expected subquery expression, found: %T", children[0])
    58  	}
    59  	ret := *e
    60  	ret.Query = sq
    61  	return &ret, nil
    62  }
    63  
    64  // Resolved implements the Expression interface.
    65  func (e *ExistsSubquery) Resolved() bool {
    66  	return e.Query.Resolved()
    67  }
    68  
    69  // IsNullable implements the Expression interface.
    70  func (e *ExistsSubquery) IsNullable() bool {
    71  	return false
    72  }
    73  
    74  // Children implements the Expression interface.
    75  func (e *ExistsSubquery) Children() []sql.Expression {
    76  	return []sql.Expression{e.Query}
    77  }
    78  
    79  // String implements the Expression interface.
    80  func (e *ExistsSubquery) String() string {
    81  	return fmt.Sprintf("EXISTS %s", e.Query)
    82  }
    83  
    84  // DebugString implements the Expression interface.
    85  func (e *ExistsSubquery) DebugString() string {
    86  	return fmt.Sprintf("EXISTS %s", sql.DebugString(e.Query))
    87  }
    88  
    89  // Type implements the Expression interface.
    90  func (e *ExistsSubquery) Type() sql.Type {
    91  	return types.Boolean
    92  }
    93  
    94  // CollationCoercibility implements the interface sql.CollationCoercible.
    95  func (*ExistsSubquery) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    96  	return sql.Collation_binary, 5
    97  }