github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/strcmp.go (about)

     1  // Copyright 2020-2022 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  // StrCmp compares two strings
    26  type StrCmp struct {
    27  	expression.BinaryExpressionStub
    28  }
    29  
    30  var _ sql.FunctionExpression = (*StrCmp)(nil)
    31  var _ sql.CollationCoercible = (*StrCmp)(nil)
    32  
    33  // NewStrCmp creates a new NewStrCmp UDF.
    34  func NewStrCmp(e1, e2 sql.Expression) sql.Expression {
    35  	return &StrCmp{
    36  		expression.BinaryExpressionStub{
    37  			LeftChild:  e1,
    38  			RightChild: e2,
    39  		},
    40  	}
    41  }
    42  
    43  // FunctionName implements sql.FunctionExpression
    44  func (s *StrCmp) FunctionName() string {
    45  	return "strcmp"
    46  }
    47  
    48  // Description implements sql.FunctionExpression
    49  func (s *StrCmp) Description() string {
    50  	return "compares two strings"
    51  }
    52  
    53  // Type implements the Expression interface.
    54  func (s *StrCmp) Type() sql.Type {
    55  	return types.Int8
    56  }
    57  
    58  // CollationCoercibility implements the interface sql.CollationCoercible.
    59  func (s *StrCmp) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    60  	leftCollation, leftCoercibility := sql.GetCoercibility(ctx, s.LeftChild)
    61  	rightCollation, rightCoercibility := sql.GetCoercibility(ctx, s.RightChild)
    62  	return sql.ResolveCoercibility(leftCollation, leftCoercibility, rightCollation, rightCoercibility)
    63  }
    64  
    65  func (s *StrCmp) String() string {
    66  	return fmt.Sprintf("%s(%s,%s)", s.FunctionName(), s.LeftChild, s.RightChild)
    67  }
    68  
    69  // WithChildren implements the Expression interface.
    70  func (s *StrCmp) WithChildren(children ...sql.Expression) (sql.Expression, error) {
    71  	if len(children) != 2 {
    72  		return nil, sql.ErrInvalidChildrenNumber.New(s, len(children), 2)
    73  	}
    74  	return NewStrCmp(children[0], children[1]), nil
    75  }
    76  
    77  func (s *StrCmp) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
    78  	if s.LeftChild == nil || s.RightChild == nil {
    79  		return nil, nil
    80  	}
    81  
    82  	expr1, err := s.LeftChild.Eval(ctx, row)
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  	if expr1 == nil {
    87  		return nil, nil
    88  	}
    89  
    90  	expr2, err := s.RightChild.Eval(ctx, row)
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  	if expr2 == nil {
    95  		return nil, nil
    96  	}
    97  
    98  	collationPreference, _ := s.CollationCoercibility(ctx)
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  
   103  	strType := types.CreateLongText(collationPreference)
   104  	return strType.Compare(expr1, expr2)
   105  }