2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-01-28 20:59:48 -05:00
|
|
|
|
|
|
|
package sql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-02-04 23:54:45 -05:00
|
|
|
"time"
|
2019-01-28 20:59:48 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// FuncName - SQL function name.
|
|
|
|
type FuncName string
|
|
|
|
|
|
|
|
// SQL Function name constants
|
|
|
|
const (
|
|
|
|
// Conditionals
|
|
|
|
sqlFnCoalesce FuncName = "COALESCE"
|
|
|
|
sqlFnNullIf FuncName = "NULLIF"
|
|
|
|
|
|
|
|
// Conversion
|
|
|
|
sqlFnCast FuncName = "CAST"
|
|
|
|
|
|
|
|
// Date and time
|
|
|
|
sqlFnDateAdd FuncName = "DATE_ADD"
|
|
|
|
sqlFnDateDiff FuncName = "DATE_DIFF"
|
|
|
|
sqlFnExtract FuncName = "EXTRACT"
|
|
|
|
sqlFnToString FuncName = "TO_STRING"
|
|
|
|
sqlFnToTimestamp FuncName = "TO_TIMESTAMP"
|
|
|
|
sqlFnUTCNow FuncName = "UTCNOW"
|
|
|
|
|
|
|
|
// String
|
|
|
|
sqlFnCharLength FuncName = "CHAR_LENGTH"
|
|
|
|
sqlFnCharacterLength FuncName = "CHARACTER_LENGTH"
|
|
|
|
sqlFnLower FuncName = "LOWER"
|
|
|
|
sqlFnSubstring FuncName = "SUBSTRING"
|
|
|
|
sqlFnTrim FuncName = "TRIM"
|
|
|
|
sqlFnUpper FuncName = "UPPER"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errUnimplementedCast = errors.New("This cast not yet implemented")
|
|
|
|
errNonStringTrimArg = errors.New("TRIM() received a non-string argument")
|
2019-02-04 23:54:45 -05:00
|
|
|
errNonTimestampArg = errors.New("Expected a timestamp argument")
|
2019-01-28 20:59:48 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func (e *FuncExpr) getFunctionName() FuncName {
|
|
|
|
switch {
|
|
|
|
case e.SFunc != nil:
|
|
|
|
return FuncName(strings.ToUpper(e.SFunc.FunctionName))
|
|
|
|
case e.Count != nil:
|
|
|
|
return FuncName(aggFnCount)
|
|
|
|
case e.Cast != nil:
|
|
|
|
return sqlFnCast
|
|
|
|
case e.Substring != nil:
|
|
|
|
return sqlFnSubstring
|
|
|
|
case e.Extract != nil:
|
|
|
|
return sqlFnExtract
|
|
|
|
case e.Trim != nil:
|
|
|
|
return sqlFnTrim
|
2019-02-04 23:54:45 -05:00
|
|
|
case e.DateAdd != nil:
|
|
|
|
return sqlFnDateAdd
|
|
|
|
case e.DateDiff != nil:
|
|
|
|
return sqlFnDateDiff
|
2019-01-28 20:59:48 -05:00
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// evalSQLFnNode assumes that the FuncExpr is not an aggregation
|
|
|
|
// function.
|
2021-04-06 11:49:04 -04:00
|
|
|
func (e *FuncExpr) evalSQLFnNode(r Record, tableAlias string) (res *Value, err error) {
|
2019-01-28 20:59:48 -05:00
|
|
|
// Handle functions that have phrase arguments
|
|
|
|
switch e.getFunctionName() {
|
|
|
|
case sqlFnCast:
|
|
|
|
expr := e.Cast.Expr
|
2021-04-06 11:49:04 -04:00
|
|
|
res, err = expr.castTo(r, strings.ToUpper(e.Cast.CastType), tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
return
|
|
|
|
|
|
|
|
case sqlFnSubstring:
|
2021-04-06 11:49:04 -04:00
|
|
|
return handleSQLSubstring(r, e.Substring, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
|
|
|
|
case sqlFnExtract:
|
2021-04-06 11:49:04 -04:00
|
|
|
return handleSQLExtract(r, e.Extract, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
|
|
|
|
case sqlFnTrim:
|
2021-04-06 11:49:04 -04:00
|
|
|
return handleSQLTrim(r, e.Trim, tableAlias)
|
2019-02-04 23:54:45 -05:00
|
|
|
|
|
|
|
case sqlFnDateAdd:
|
2021-04-06 11:49:04 -04:00
|
|
|
return handleDateAdd(r, e.DateAdd, tableAlias)
|
2019-02-04 23:54:45 -05:00
|
|
|
|
|
|
|
case sqlFnDateDiff:
|
2021-04-06 11:49:04 -04:00
|
|
|
return handleDateDiff(r, e.DateDiff, tableAlias)
|
2019-02-04 23:54:45 -05:00
|
|
|
|
2019-01-28 20:59:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// For all simple argument functions, we evaluate the arguments here
|
|
|
|
argVals := make([]*Value, len(e.SFunc.ArgsList))
|
|
|
|
for i, arg := range e.SFunc.ArgsList {
|
2021-04-06 11:49:04 -04:00
|
|
|
argVals[i], err = arg.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch e.getFunctionName() {
|
|
|
|
case sqlFnCoalesce:
|
2019-02-04 23:54:45 -05:00
|
|
|
return coalesce(argVals)
|
2019-01-28 20:59:48 -05:00
|
|
|
|
|
|
|
case sqlFnNullIf:
|
2019-02-04 23:54:45 -05:00
|
|
|
return nullif(argVals[0], argVals[1])
|
2019-01-28 20:59:48 -05:00
|
|
|
|
|
|
|
case sqlFnCharLength, sqlFnCharacterLength:
|
2019-02-04 23:54:45 -05:00
|
|
|
return charlen(argVals[0])
|
2019-01-28 20:59:48 -05:00
|
|
|
|
|
|
|
case sqlFnLower:
|
2019-02-04 23:54:45 -05:00
|
|
|
return lowerCase(argVals[0])
|
2019-01-28 20:59:48 -05:00
|
|
|
|
|
|
|
case sqlFnUpper:
|
2019-02-04 23:54:45 -05:00
|
|
|
return upperCase(argVals[0])
|
|
|
|
|
|
|
|
case sqlFnUTCNow:
|
|
|
|
return handleUTCNow()
|
2019-01-28 20:59:48 -05:00
|
|
|
|
2019-02-04 23:54:45 -05:00
|
|
|
case sqlFnToString, sqlFnToTimestamp:
|
2019-01-28 20:59:48 -05:00
|
|
|
// TODO: implement
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
default:
|
2019-02-04 23:54:45 -05:00
|
|
|
return nil, errNotImplemented
|
2019-01-28 20:59:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 23:54:45 -05:00
|
|
|
func coalesce(args []*Value) (res *Value, err error) {
|
2019-01-28 20:59:48 -05:00
|
|
|
for _, arg := range args {
|
|
|
|
if arg.IsNull() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return arg, nil
|
|
|
|
}
|
|
|
|
return FromNull(), nil
|
|
|
|
}
|
|
|
|
|
2019-02-04 23:54:45 -05:00
|
|
|
func nullif(v1, v2 *Value) (res *Value, err error) {
|
2019-01-28 20:59:48 -05:00
|
|
|
// Handle Null cases
|
|
|
|
if v1.IsNull() || v2.IsNull() {
|
|
|
|
return v1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = inferTypesForCmp(v1, v2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
atleastOneNumeric := v1.isNumeric() || v2.isNumeric()
|
|
|
|
bothNumeric := v1.isNumeric() && v2.isNumeric()
|
|
|
|
if atleastOneNumeric || !bothNumeric {
|
|
|
|
return v1, nil
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:27 -04:00
|
|
|
if v1.SameTypeAs(*v2) {
|
2019-01-28 20:59:48 -05:00
|
|
|
return v1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cmpResult, cmpErr := v1.compareOp(opEq, v2)
|
|
|
|
if cmpErr != nil {
|
|
|
|
return nil, cmpErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmpResult {
|
|
|
|
return FromNull(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return v1, nil
|
|
|
|
}
|
|
|
|
|
2019-02-04 23:54:45 -05:00
|
|
|
func charlen(v *Value) (*Value, error) {
|
2019-01-28 20:59:48 -05:00
|
|
|
inferTypeAsString(v)
|
|
|
|
s, ok := v.ToString()
|
|
|
|
if !ok {
|
|
|
|
err := fmt.Errorf("%s/%s expects a string argument", sqlFnCharLength, sqlFnCharacterLength)
|
|
|
|
return nil, errIncorrectSQLFunctionArgumentType(err)
|
|
|
|
}
|
2019-07-05 12:43:10 -04:00
|
|
|
return FromInt(int64(len([]rune(s)))), nil
|
2019-01-28 20:59:48 -05:00
|
|
|
}
|
|
|
|
|
2019-02-04 23:54:45 -05:00
|
|
|
func lowerCase(v *Value) (*Value, error) {
|
2019-01-28 20:59:48 -05:00
|
|
|
inferTypeAsString(v)
|
|
|
|
s, ok := v.ToString()
|
|
|
|
if !ok {
|
|
|
|
err := fmt.Errorf("%s expects a string argument", sqlFnLower)
|
|
|
|
return nil, errIncorrectSQLFunctionArgumentType(err)
|
|
|
|
}
|
|
|
|
return FromString(strings.ToLower(s)), nil
|
|
|
|
}
|
|
|
|
|
2019-02-04 23:54:45 -05:00
|
|
|
func upperCase(v *Value) (*Value, error) {
|
2019-01-28 20:59:48 -05:00
|
|
|
inferTypeAsString(v)
|
|
|
|
s, ok := v.ToString()
|
|
|
|
if !ok {
|
|
|
|
err := fmt.Errorf("%s expects a string argument", sqlFnUpper)
|
|
|
|
return nil, errIncorrectSQLFunctionArgumentType(err)
|
|
|
|
}
|
|
|
|
return FromString(strings.ToUpper(s)), nil
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
func handleDateAdd(r Record, d *DateAddFunc, tableAlias string) (*Value, error) {
|
|
|
|
q, err := d.Quantity.evalNode(r, tableAlias)
|
2019-02-04 23:54:45 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
inferTypeForArithOp(q)
|
|
|
|
qty, ok := q.ToFloat()
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("QUANTITY must be a numeric argument to %s()", sqlFnDateAdd)
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
ts, err := d.Timestamp.evalNode(r, tableAlias)
|
2019-02-04 23:54:45 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-13 07:59:36 -05:00
|
|
|
if err = inferTypeAsTimestamp(ts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-04 23:54:45 -05:00
|
|
|
t, ok := ts.ToTimestamp()
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("%s() expects a timestamp argument", sqlFnDateAdd)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dateAdd(strings.ToUpper(d.DatePart), qty, t)
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
func handleDateDiff(r Record, d *DateDiffFunc, tableAlias string) (*Value, error) {
|
|
|
|
tval1, err := d.Timestamp1.evalNode(r, tableAlias)
|
2019-02-04 23:54:45 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-13 07:59:36 -05:00
|
|
|
if err = inferTypeAsTimestamp(tval1); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-04 23:54:45 -05:00
|
|
|
ts1, ok := tval1.ToTimestamp()
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("%s() expects two timestamp arguments", sqlFnDateDiff)
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
tval2, err := d.Timestamp2.evalNode(r, tableAlias)
|
2019-02-04 23:54:45 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-13 07:59:36 -05:00
|
|
|
if err = inferTypeAsTimestamp(tval2); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-04 23:54:45 -05:00
|
|
|
ts2, ok := tval2.ToTimestamp()
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("%s() expects two timestamp arguments", sqlFnDateDiff)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dateDiff(strings.ToUpper(d.DatePart), ts1, ts2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleUTCNow() (*Value, error) {
|
|
|
|
return FromTimestamp(time.Now().UTC()), nil
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
func handleSQLSubstring(r Record, e *SubstringFunc, tableAlias string) (val *Value, err error) {
|
2019-01-28 20:59:48 -05:00
|
|
|
// Both forms `SUBSTRING('abc' FROM 2 FOR 1)` and
|
|
|
|
// SUBSTRING('abc', 2, 1) are supported.
|
|
|
|
|
|
|
|
// Evaluate the string argument
|
2021-04-06 11:49:04 -04:00
|
|
|
v1, err := e.Expr.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
inferTypeAsString(v1)
|
|
|
|
s, ok := v1.ToString()
|
|
|
|
if !ok {
|
|
|
|
err := fmt.Errorf("Incorrect argument type passed to %s", sqlFnSubstring)
|
|
|
|
return nil, errIncorrectSQLFunctionArgumentType(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assemble other arguments
|
|
|
|
arg2, arg3 := e.From, e.For
|
|
|
|
// Check if the second form of substring is being used
|
|
|
|
if e.From == nil {
|
|
|
|
arg2, arg3 = e.Arg2, e.Arg3
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate the FROM argument
|
2021-04-06 11:49:04 -04:00
|
|
|
v2, err := arg2.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
inferTypeForArithOp(v2)
|
|
|
|
startIdx, ok := v2.ToInt()
|
|
|
|
if !ok {
|
|
|
|
err := fmt.Errorf("Incorrect type for start index argument in %s", sqlFnSubstring)
|
|
|
|
return nil, errIncorrectSQLFunctionArgumentType(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
length := -1
|
|
|
|
// Evaluate the optional FOR argument
|
|
|
|
if arg3 != nil {
|
2021-04-06 11:49:04 -04:00
|
|
|
v3, err := arg3.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
inferTypeForArithOp(v3)
|
|
|
|
lenInt, ok := v3.ToInt()
|
|
|
|
if !ok {
|
|
|
|
err := fmt.Errorf("Incorrect type for length argument in %s", sqlFnSubstring)
|
|
|
|
return nil, errIncorrectSQLFunctionArgumentType(err)
|
|
|
|
}
|
|
|
|
length = int(lenInt)
|
|
|
|
if length < 0 {
|
|
|
|
err := fmt.Errorf("Negative length argument in %s", sqlFnSubstring)
|
|
|
|
return nil, errIncorrectSQLFunctionArgumentType(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := evalSQLSubstring(s, int(startIdx), length)
|
|
|
|
return FromString(res), err
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
func handleSQLTrim(r Record, e *TrimFunc, tableAlias string) (res *Value, err error) {
|
2019-06-24 19:59:33 -04:00
|
|
|
chars := ""
|
|
|
|
ok := false
|
|
|
|
if e.TrimChars != nil {
|
2021-04-06 11:49:04 -04:00
|
|
|
charsV, cerr := e.TrimChars.evalNode(r, tableAlias)
|
2019-06-24 19:59:33 -04:00
|
|
|
if cerr != nil {
|
|
|
|
return nil, cerr
|
|
|
|
}
|
|
|
|
inferTypeAsString(charsV)
|
|
|
|
chars, ok = charsV.ToString()
|
|
|
|
if !ok {
|
|
|
|
return nil, errNonStringTrimArg
|
|
|
|
}
|
2019-01-28 20:59:48 -05:00
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
fromV, ferr := e.TrimFrom.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
if ferr != nil {
|
|
|
|
return nil, ferr
|
|
|
|
}
|
2019-06-24 19:59:33 -04:00
|
|
|
inferTypeAsString(fromV)
|
2019-01-28 20:59:48 -05:00
|
|
|
from, ok := fromV.ToString()
|
|
|
|
if !ok {
|
|
|
|
return nil, errNonStringTrimArg
|
|
|
|
}
|
|
|
|
|
|
|
|
result, terr := evalSQLTrim(e.TrimWhere, chars, from)
|
|
|
|
if terr != nil {
|
|
|
|
return nil, terr
|
|
|
|
}
|
|
|
|
return FromString(result), nil
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
func handleSQLExtract(r Record, e *ExtractFunc, tableAlias string) (res *Value, err error) {
|
|
|
|
timeVal, verr := e.From.evalNode(r, tableAlias)
|
2019-02-04 23:54:45 -05:00
|
|
|
if verr != nil {
|
|
|
|
return nil, verr
|
|
|
|
}
|
|
|
|
|
2019-02-13 07:59:36 -05:00
|
|
|
if err = inferTypeAsTimestamp(timeVal); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-04 23:54:45 -05:00
|
|
|
|
|
|
|
t, ok := timeVal.ToTimestamp()
|
|
|
|
if !ok {
|
|
|
|
return nil, errNonTimestampArg
|
|
|
|
}
|
|
|
|
|
|
|
|
return extract(strings.ToUpper(e.Timeword), t)
|
|
|
|
}
|
|
|
|
|
2019-01-28 20:59:48 -05:00
|
|
|
func errUnsupportedCast(fromType, toType string) error {
|
|
|
|
return fmt.Errorf("Cannot cast from %v to %v", fromType, toType)
|
|
|
|
}
|
|
|
|
|
|
|
|
func errCastFailure(msg string) error {
|
|
|
|
return fmt.Errorf("Error casting: %s", msg)
|
|
|
|
}
|
|
|
|
|
2019-02-04 23:54:45 -05:00
|
|
|
// Allowed cast types
|
|
|
|
const (
|
|
|
|
castBool = "BOOL"
|
|
|
|
castInt = "INT"
|
|
|
|
castInteger = "INTEGER"
|
|
|
|
castString = "STRING"
|
|
|
|
castFloat = "FLOAT"
|
|
|
|
castDecimal = "DECIMAL"
|
|
|
|
castNumeric = "NUMERIC"
|
|
|
|
castTimestamp = "TIMESTAMP"
|
|
|
|
)
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
func (e *Expression) castTo(r Record, castType string, tableAlias string) (res *Value, err error) {
|
|
|
|
v, err := e.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch castType {
|
|
|
|
case castInt, castInteger:
|
|
|
|
i, err := intCast(v)
|
|
|
|
return FromInt(i), err
|
|
|
|
|
|
|
|
case castFloat:
|
|
|
|
f, err := floatCast(v)
|
|
|
|
return FromFloat(f), err
|
|
|
|
|
|
|
|
case castString:
|
|
|
|
s, err := stringCast(v)
|
|
|
|
return FromString(s), err
|
|
|
|
|
2019-02-04 23:54:45 -05:00
|
|
|
case castTimestamp:
|
|
|
|
t, err := timestampCast(v)
|
|
|
|
return FromTimestamp(t), err
|
|
|
|
|
|
|
|
case castBool:
|
|
|
|
b, err := boolCast(v)
|
|
|
|
return FromBool(b), err
|
|
|
|
|
|
|
|
case castDecimal, castNumeric:
|
2019-01-28 20:59:48 -05:00
|
|
|
fallthrough
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errUnimplementedCast
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func intCast(v *Value) (int64, error) {
|
|
|
|
// This conversion truncates floating point numbers to
|
|
|
|
// integer.
|
|
|
|
strToInt := func(s string) (int64, bool) {
|
|
|
|
i, errI := strconv.ParseInt(s, 10, 64)
|
|
|
|
if errI == nil {
|
|
|
|
return i, true
|
|
|
|
}
|
|
|
|
f, errF := strconv.ParseFloat(s, 64)
|
|
|
|
if errF == nil {
|
|
|
|
return int64(f), true
|
|
|
|
}
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:27 -04:00
|
|
|
switch x := v.value.(type) {
|
|
|
|
case float64:
|
2019-01-28 20:59:48 -05:00
|
|
|
// Truncate fractional part
|
2019-09-16 20:26:27 -04:00
|
|
|
return int64(x), nil
|
|
|
|
case int64:
|
|
|
|
return x, nil
|
|
|
|
case string:
|
2019-01-28 20:59:48 -05:00
|
|
|
// Parse as number, truncate floating point if
|
|
|
|
// needed.
|
2020-07-21 15:57:09 -04:00
|
|
|
// String might contain trimming spaces, which
|
|
|
|
// needs to be trimmed.
|
|
|
|
res, ok := strToInt(strings.TrimSpace(x))
|
2019-01-28 20:59:48 -05:00
|
|
|
if !ok {
|
|
|
|
return 0, errCastFailure("could not parse as int")
|
|
|
|
}
|
|
|
|
return res, nil
|
2019-09-16 20:26:27 -04:00
|
|
|
case []byte:
|
2019-01-28 20:59:48 -05:00
|
|
|
// Parse as number, truncate floating point if
|
|
|
|
// needed.
|
2020-07-21 15:57:09 -04:00
|
|
|
// String might contain trimming spaces, which
|
|
|
|
// needs to be trimmed.
|
|
|
|
res, ok := strToInt(strings.TrimSpace(string(x)))
|
2019-01-28 20:59:48 -05:00
|
|
|
if !ok {
|
|
|
|
return 0, errCastFailure("could not parse as int")
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0, errUnsupportedCast(v.GetTypeString(), castInt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func floatCast(v *Value) (float64, error) {
|
2019-09-16 20:26:27 -04:00
|
|
|
switch x := v.value.(type) {
|
|
|
|
case float64:
|
|
|
|
return x, nil
|
2021-03-19 12:14:38 -04:00
|
|
|
case int64:
|
2019-09-16 20:26:27 -04:00
|
|
|
return float64(x), nil
|
|
|
|
case string:
|
2020-07-21 15:57:09 -04:00
|
|
|
f, err := strconv.ParseFloat(strings.TrimSpace(x), 64)
|
2019-01-28 20:59:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return 0, errCastFailure("could not parse as float")
|
|
|
|
}
|
|
|
|
return f, nil
|
2019-09-16 20:26:27 -04:00
|
|
|
case []byte:
|
2020-07-21 15:57:09 -04:00
|
|
|
f, err := strconv.ParseFloat(strings.TrimSpace(string(x)), 64)
|
2019-01-28 20:59:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return 0, errCastFailure("could not parse as float")
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
default:
|
|
|
|
return 0, errUnsupportedCast(v.GetTypeString(), castFloat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringCast(v *Value) (string, error) {
|
2019-09-16 20:26:27 -04:00
|
|
|
switch x := v.value.(type) {
|
|
|
|
case float64:
|
|
|
|
return fmt.Sprintf("%v", x), nil
|
|
|
|
case int64:
|
|
|
|
return fmt.Sprintf("%v", x), nil
|
|
|
|
case string:
|
|
|
|
return x, nil
|
|
|
|
case []byte:
|
|
|
|
return string(x), nil
|
|
|
|
case bool:
|
|
|
|
return fmt.Sprintf("%v", x), nil
|
|
|
|
case nil:
|
2019-01-28 20:59:48 -05:00
|
|
|
// FIXME: verify this case is correct
|
2019-09-16 20:26:27 -04:00
|
|
|
return "NULL", nil
|
2019-01-28 20:59:48 -05:00
|
|
|
}
|
|
|
|
// This does not happen
|
2019-09-16 20:26:27 -04:00
|
|
|
return "", errCastFailure(fmt.Sprintf("cannot cast %v to string type", v.GetTypeString()))
|
2019-01-28 20:59:48 -05:00
|
|
|
}
|
2019-02-04 23:54:45 -05:00
|
|
|
|
|
|
|
func timestampCast(v *Value) (t time.Time, _ error) {
|
2019-09-16 20:26:27 -04:00
|
|
|
switch x := v.value.(type) {
|
|
|
|
case string:
|
|
|
|
return parseSQLTimestamp(x)
|
|
|
|
case []byte:
|
|
|
|
return parseSQLTimestamp(string(x))
|
|
|
|
case time.Time:
|
|
|
|
return x, nil
|
2019-02-04 23:54:45 -05:00
|
|
|
default:
|
|
|
|
return t, errCastFailure(fmt.Sprintf("cannot cast %v to Timestamp type", v.GetTypeString()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func boolCast(v *Value) (b bool, _ error) {
|
|
|
|
sToB := func(s string) (bool, error) {
|
2019-03-18 10:46:20 -04:00
|
|
|
switch s {
|
|
|
|
case "true":
|
2019-02-04 23:54:45 -05:00
|
|
|
return true, nil
|
2019-03-18 10:46:20 -04:00
|
|
|
case "false":
|
2019-02-04 23:54:45 -05:00
|
|
|
return false, nil
|
2019-03-18 10:46:20 -04:00
|
|
|
default:
|
2019-02-04 23:54:45 -05:00
|
|
|
return false, errCastFailure("cannot cast to Bool")
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 20:26:27 -04:00
|
|
|
switch x := v.value.(type) {
|
|
|
|
case bool:
|
|
|
|
return x, nil
|
|
|
|
case string:
|
|
|
|
return sToB(strings.ToLower(x))
|
|
|
|
case []byte:
|
|
|
|
return sToB(strings.ToLower(string(x)))
|
2019-02-04 23:54:45 -05:00
|
|
|
default:
|
|
|
|
return false, errCastFailure("cannot cast %v to Bool")
|
|
|
|
}
|
|
|
|
}
|