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

     1  // Copyright 2024 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  	"strings"
    20  
    21  	"github.com/dolthub/vitess/go/sqltypes"
    22  
    23  	"github.com/dolthub/go-mysql-server/sql"
    24  	"github.com/dolthub/go-mysql-server/sql/types"
    25  )
    26  
    27  // Char implements the sql function "char" which returns the character for each integer passed
    28  type Char struct {
    29  	args      []sql.Expression
    30  	Collation sql.CollationID
    31  }
    32  
    33  var _ sql.FunctionExpression = (*Char)(nil)
    34  var _ sql.CollationCoercible = (*Char)(nil)
    35  
    36  func NewChar(args ...sql.Expression) (sql.Expression, error) {
    37  	return &Char{args: args}, nil
    38  }
    39  
    40  // FunctionName implements sql.FunctionExpression
    41  func (c *Char) FunctionName() string {
    42  	return "char"
    43  }
    44  
    45  // Resolved implements sql.FunctionExpression
    46  func (c *Char) Resolved() bool {
    47  	for _, arg := range c.args {
    48  		if !arg.Resolved() {
    49  			return false
    50  		}
    51  	}
    52  	return true
    53  }
    54  
    55  // String implements sql.Expression
    56  func (c *Char) String() string {
    57  	args := make([]string, len(c.args))
    58  	for i, arg := range c.args {
    59  		args[i] = arg.String()
    60  	}
    61  	str := strings.Join(args, ", ")
    62  	return fmt.Sprintf("%s(%s)", c.FunctionName(), str)
    63  }
    64  
    65  // Type implements sql.Expression
    66  func (c *Char) Type() sql.Type {
    67  	if c.Collation == sql.Collation_binary || c.Collation == sql.Collation_Unspecified {
    68  		return types.MustCreateString(sqltypes.VarBinary, int64(len(c.args)*4), sql.Collation_binary)
    69  	}
    70  	return types.MustCreateString(sqltypes.VarChar, int64(len(c.args)*16), c.Collation)
    71  }
    72  
    73  // IsNullable implements sql.Expression
    74  func (c *Char) IsNullable() bool {
    75  	return true
    76  }
    77  
    78  // Description implements sql.FunctionExpression
    79  func (c *Char) Description() string {
    80  	return "interprets each argument N as an integer and returns a string consisting of the characters given by the code values of those integers."
    81  }
    82  
    83  // CollationCoercibility implements the interface sql.CollationCoercible.
    84  func (c *Char) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    85  	return sql.Collation_binary, 5
    86  }
    87  
    88  // char converts num into a byte array
    89  // This function is essentially converting the number to base 256
    90  func char(num uint32) []byte {
    91  	if num == 0 {
    92  		return []byte{}
    93  	}
    94  	return append(char(num>>8), byte(num&255))
    95  }
    96  
    97  // Eval implements the sql.Expression interface
    98  func (c *Char) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
    99  	res := []byte{}
   100  	for _, arg := range c.args {
   101  		if arg == nil {
   102  			continue
   103  		}
   104  
   105  		val, err := arg.Eval(ctx, row)
   106  		if err != nil {
   107  			return nil, err
   108  		}
   109  
   110  		if val == nil {
   111  			continue
   112  		}
   113  
   114  		v, _, err := types.Uint32.Convert(val)
   115  		if err != nil {
   116  			ctx.Warn(1292, "Truncated incorrect INTEGER value: '%v'", val)
   117  			res = append(res, 0)
   118  			continue
   119  		}
   120  
   121  		res = append(res, char(v.(uint32))...)
   122  	}
   123  
   124  	result, _, err := c.Type().Convert(res)
   125  	if err != nil {
   126  		return nil, err
   127  	}
   128  
   129  	return result, nil
   130  }
   131  
   132  // Children implements sql.Expression
   133  func (c *Char) Children() []sql.Expression {
   134  	return c.args
   135  }
   136  
   137  // WithChildren implements the sql.Expression interface
   138  func (c *Char) WithChildren(children ...sql.Expression) (sql.Expression, error) {
   139  	return NewChar(children...)
   140  }