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

     1  // Copyright 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 expression
    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  // CollatedExpression represents an expression (returning a string or byte slice) that carries a collation (which
    25  // implicitly also carries a character set). This does not handle any encoding or decoding of the character set, as this
    26  // is strictly for collations.
    27  type CollatedExpression struct {
    28  	expr      sql.Expression
    29  	collation sql.CollationID
    30  }
    31  
    32  var _ sql.Expression = (*CollatedExpression)(nil)
    33  var _ sql.CollationCoercible = (*CollatedExpression)(nil)
    34  var _ sql.DebugStringer = (*CollatedExpression)(nil)
    35  
    36  // NewCollatedExpression creates a new CollatedExpression expression. If the given expression is already a
    37  // CollatedExpression, then the previous collation is overriden with the given one.
    38  func NewCollatedExpression(expr sql.Expression, collation sql.CollationID) *CollatedExpression {
    39  	if collatedExpr, ok := expr.(*CollatedExpression); ok {
    40  		return &CollatedExpression{
    41  			expr:      collatedExpr.expr,
    42  			collation: collation,
    43  		}
    44  	}
    45  	return &CollatedExpression{
    46  		expr:      expr,
    47  		collation: collation,
    48  	}
    49  }
    50  
    51  // Resolved implements the sql.Expression interface.
    52  func (ce *CollatedExpression) Resolved() bool {
    53  	return ce.expr.Resolved()
    54  }
    55  
    56  // IsNullable implements the sql.Expression interface.
    57  func (ce *CollatedExpression) IsNullable() bool {
    58  	return ce.expr.IsNullable()
    59  }
    60  
    61  // Type implements the sql.Expression interface.
    62  func (ce *CollatedExpression) Type() sql.Type {
    63  	typ := ce.expr.Type()
    64  	if collatedType, ok := typ.(sql.TypeWithCollation); ok {
    65  		newType, err := collatedType.WithNewCollation(ce.collation)
    66  		if err == nil {
    67  			return newType
    68  		}
    69  	}
    70  	// If this isn't a collated type then this should fail, as we can't apply a collation to an expression that does not
    71  	// have a charset. We also can't check in the constructor, as expressions such as unresolved columns will not have
    72  	// the correct type until after analysis. Therefore, we'll have to check (and potentially fail) in the Eval function.
    73  	return typ
    74  }
    75  
    76  // CollationCoercibility implements the interface sql.CollationCoercible.
    77  func (ce *CollatedExpression) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    78  	return ce.collation, 0
    79  }
    80  
    81  // Eval implements the sql.Expression interface.
    82  func (ce *CollatedExpression) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
    83  	typ := ce.expr.Type()
    84  	if !types.IsText(typ) {
    85  		return nil, sql.ErrCollatedExprWrongType.New()
    86  	}
    87  	if ce.collation.CharacterSet() != typ.(sql.TypeWithCollation).Collation().CharacterSet() {
    88  		return nil, sql.ErrCollationInvalidForCharSet.New(
    89  			ce.collation.Name(), typ.(sql.TypeWithCollation).Collation().CharacterSet().Name())
    90  	}
    91  	return ce.expr.Eval(ctx, row)
    92  }
    93  
    94  func (ce *CollatedExpression) String() string {
    95  	return fmt.Sprintf("%s COLLATE %s", ce.expr.String(), ce.collation.String())
    96  }
    97  
    98  // DebugString implements the sql.DebugStringer interface.
    99  func (ce *CollatedExpression) DebugString() string {
   100  	var innerDebugStr string
   101  	if debugExpr, ok := ce.expr.(sql.DebugStringer); ok {
   102  		innerDebugStr = debugExpr.DebugString()
   103  	} else {
   104  		innerDebugStr = ce.expr.String()
   105  	}
   106  	return fmt.Sprintf("%s COLLATE %s", innerDebugStr, ce.collation.String())
   107  }
   108  
   109  // WithChildren implements the sql.Expression interface.
   110  func (ce *CollatedExpression) WithChildren(children ...sql.Expression) (sql.Expression, error) {
   111  	if len(children) != 1 {
   112  		return nil, sql.ErrInvalidChildrenNumber.New(ce, len(children), 1)
   113  	}
   114  	return &CollatedExpression{
   115  		expr:      children[0],
   116  		collation: ce.collation,
   117  	}, nil
   118  }
   119  
   120  // Children implements the sql.Expression interface.
   121  func (ce *CollatedExpression) Children() []sql.Expression {
   122  	return []sql.Expression{ce.expr}
   123  }
   124  
   125  // Child returns the inner expression.
   126  func (ce *CollatedExpression) Child() sql.Expression {
   127  	return ce.expr
   128  }