github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/convertusing.go (about) 1 // Copyright 2023 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/encodings" 22 ) 23 24 // ConvertUsing represents a CONVERT(X USING T) operation that casts the expression X to the character set T. 25 type ConvertUsing struct { 26 UnaryExpression 27 TargetCharSet sql.CharacterSetID 28 } 29 30 var _ sql.Expression = (*ConvertUsing)(nil) 31 var _ sql.CollationCoercible = (*ConvertUsing)(nil) 32 33 func NewConvertUsing(expr sql.Expression, targetCharSet sql.CharacterSetID) *ConvertUsing { 34 return &ConvertUsing{ 35 UnaryExpression: UnaryExpression{Child: expr}, 36 TargetCharSet: targetCharSet, 37 } 38 } 39 40 // String implements the interface sql.Expression. 41 func (c *ConvertUsing) String() string { 42 return fmt.Sprintf("CONVERT(%s USING %s)", c.Child.String(), c.TargetCharSet.Name()) 43 } 44 45 // Type implements the interface sql.Expression. 46 func (c *ConvertUsing) Type() sql.Type { 47 typ := c.Child.Type() 48 if collatedType, ok := typ.(sql.TypeWithCollation); ok { 49 newTyp, _ := collatedType.WithNewCollation(c.TargetCharSet.DefaultCollation()) 50 return newTyp 51 } 52 return typ 53 } 54 55 // Eval implements the interface sql.Expression. 56 func (c *ConvertUsing) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 57 val, err := c.Child.Eval(ctx, row) 58 if err != nil { 59 return nil, err 60 } 61 if val == nil { 62 return nil, nil 63 } 64 65 var valBytes []byte 66 if v, ok := val.([]byte); ok { 67 valBytes = v 68 } else if v, ok := val.(string); ok { 69 valBytes = encodings.StringToBytes(v) 70 } 71 newString := c.TargetCharSet.Encoder().EncodeReplaceUnknown(valBytes) 72 return encodings.BytesToString(newString), nil 73 } 74 75 // WithChildren implements the interface sql.Expression. 76 func (c *ConvertUsing) WithChildren(children ...sql.Expression) (sql.Expression, error) { 77 if len(children) != 1 { 78 return nil, sql.ErrInvalidChildrenNumber.New(c, len(children), 1) 79 } 80 return NewConvertUsing(children[0], c.TargetCharSet), nil 81 } 82 83 // CollationCoercibility implements the interface sql.CollationCoercible. 84 func (c *ConvertUsing) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { 85 return c.TargetCharSet.DefaultCollation(), 2 86 }