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 (
|
2019-03-09 11:13:37 -05:00
|
|
|
"encoding/json"
|
2019-01-28 20:59:48 -05:00
|
|
|
"errors"
|
2019-10-06 10:52:45 -04:00
|
|
|
"fmt"
|
2020-02-13 17:03:52 -05:00
|
|
|
"math"
|
2022-02-24 12:10:48 -05:00
|
|
|
"strings"
|
2019-03-09 11:13:37 -05:00
|
|
|
|
|
|
|
"github.com/bcicen/jstream"
|
2020-02-13 17:03:52 -05:00
|
|
|
"github.com/minio/simdjson-go"
|
2019-01-28 20:59:48 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errInvalidASTNode = errors.New("invalid AST Node")
|
|
|
|
errExpectedBool = errors.New("expected bool")
|
|
|
|
errLikeNonStrArg = errors.New("LIKE clause requires string arguments")
|
|
|
|
errLikeInvalidEscape = errors.New("LIKE clause has invalid ESCAPE character")
|
|
|
|
errNotImplemented = errors.New("not implemented")
|
|
|
|
)
|
|
|
|
|
|
|
|
// AST Node Evaluation functions
|
|
|
|
//
|
|
|
|
// During evaluation, the query is known to be valid, as analysis is
|
|
|
|
// complete. The only errors possible are due to value type
|
|
|
|
// mismatches, etc.
|
|
|
|
//
|
|
|
|
// If an aggregation node is present as a descendant (when
|
|
|
|
// e.prop.isAggregation is true), we call evalNode on all child nodes,
|
|
|
|
// check for errors, but do not perform any combining of the results
|
|
|
|
// of child nodes. The final result row is returned after all rows are
|
|
|
|
// processed, and the `getAggregate` function is called.
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
func (e *AliasedExpression) evalNode(r Record, tableAlias string) (*Value, error) {
|
|
|
|
return e.Expression.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
func (e *Expression) evalNode(r Record, tableAlias string) (*Value, error) {
|
2019-01-28 20:59:48 -05:00
|
|
|
if len(e.And) == 1 {
|
|
|
|
// In this case, result is not required to be boolean
|
|
|
|
// type.
|
2021-04-06 11:49:04 -04:00
|
|
|
return e.And[0].evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compute OR of conditions
|
|
|
|
result := false
|
|
|
|
for _, ex := range e.And {
|
2021-04-06 11:49:04 -04:00
|
|
|
res, err := ex.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b, ok := res.ToBool()
|
|
|
|
if !ok {
|
|
|
|
return nil, errExpectedBool
|
|
|
|
}
|
2021-12-15 19:47:21 -05:00
|
|
|
if b {
|
|
|
|
return FromBool(true), nil
|
|
|
|
}
|
2019-01-28 20:59:48 -05:00
|
|
|
result = result || b
|
|
|
|
}
|
|
|
|
return FromBool(result), nil
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
func (e *AndCondition) evalNode(r Record, tableAlias string) (*Value, error) {
|
2019-01-28 20:59:48 -05:00
|
|
|
if len(e.Condition) == 1 {
|
|
|
|
// In this case, result does not have to be boolean
|
2021-04-06 11:49:04 -04:00
|
|
|
return e.Condition[0].evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compute AND of conditions
|
|
|
|
result := true
|
|
|
|
for _, ex := range e.Condition {
|
2021-04-06 11:49:04 -04:00
|
|
|
res, err := ex.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b, ok := res.ToBool()
|
|
|
|
if !ok {
|
|
|
|
return nil, errExpectedBool
|
|
|
|
}
|
2021-12-15 19:47:21 -05:00
|
|
|
if !b {
|
|
|
|
return FromBool(false), nil
|
|
|
|
}
|
2019-01-28 20:59:48 -05:00
|
|
|
result = result && b
|
|
|
|
}
|
|
|
|
return FromBool(result), nil
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
func (e *Condition) evalNode(r Record, tableAlias string) (*Value, error) {
|
2019-01-28 20:59:48 -05:00
|
|
|
if e.Operand != nil {
|
|
|
|
// In this case, result does not have to be boolean
|
2021-04-06 11:49:04 -04:00
|
|
|
return e.Operand.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compute NOT of condition
|
2021-04-06 11:49:04 -04:00
|
|
|
res, err := e.Not.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b, ok := res.ToBool()
|
|
|
|
if !ok {
|
|
|
|
return nil, errExpectedBool
|
|
|
|
}
|
|
|
|
return FromBool(!b), nil
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
func (e *ConditionOperand) evalNode(r Record, tableAlias string) (*Value, error) {
|
|
|
|
opVal, opErr := e.Operand.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
if opErr != nil || e.ConditionRHS == nil {
|
|
|
|
return opVal, opErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to evaluate the ConditionRHS
|
|
|
|
switch {
|
|
|
|
case e.ConditionRHS.Compare != nil:
|
2021-04-06 11:49:04 -04:00
|
|
|
cmpRight, cmpRErr := e.ConditionRHS.Compare.Operand.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
if cmpRErr != nil {
|
|
|
|
return nil, cmpRErr
|
|
|
|
}
|
|
|
|
|
2022-02-24 12:10:48 -05:00
|
|
|
b, err := opVal.compareOp(strings.ToUpper(e.ConditionRHS.Compare.Operator), cmpRight)
|
2019-01-28 20:59:48 -05:00
|
|
|
return FromBool(b), err
|
|
|
|
|
|
|
|
case e.ConditionRHS.Between != nil:
|
2021-04-06 11:49:04 -04:00
|
|
|
return e.ConditionRHS.Between.evalBetweenNode(r, opVal, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
|
|
|
|
case e.ConditionRHS.Like != nil:
|
2021-04-06 11:49:04 -04:00
|
|
|
return e.ConditionRHS.Like.evalLikeNode(r, opVal, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
|
|
|
|
case e.ConditionRHS.In != nil:
|
2021-04-06 11:49:04 -04:00
|
|
|
return e.ConditionRHS.In.evalInNode(r, opVal, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errInvalidASTNode
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
func (e *Between) evalBetweenNode(r Record, arg *Value, tableAlias string) (*Value, error) {
|
|
|
|
stVal, stErr := e.Start.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
if stErr != nil {
|
|
|
|
return nil, stErr
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
endVal, endErr := e.End.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
if endErr != nil {
|
|
|
|
return nil, endErr
|
|
|
|
}
|
|
|
|
|
|
|
|
part1, err1 := stVal.compareOp(opLte, arg)
|
|
|
|
if err1 != nil {
|
|
|
|
return nil, err1
|
|
|
|
}
|
|
|
|
|
|
|
|
part2, err2 := arg.compareOp(opLte, endVal)
|
|
|
|
if err2 != nil {
|
|
|
|
return nil, err2
|
|
|
|
}
|
|
|
|
|
|
|
|
result := part1 && part2
|
|
|
|
if e.Not {
|
|
|
|
result = !result
|
|
|
|
}
|
|
|
|
|
|
|
|
return FromBool(result), nil
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
func (e *Like) evalLikeNode(r Record, arg *Value, tableAlias string) (*Value, error) {
|
2019-01-28 20:59:48 -05:00
|
|
|
inferTypeAsString(arg)
|
|
|
|
|
|
|
|
s, ok := arg.ToString()
|
|
|
|
if !ok {
|
|
|
|
err := errLikeNonStrArg
|
|
|
|
return nil, errLikeInvalidInputs(err)
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
pattern, err1 := e.Pattern.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
if err1 != nil {
|
|
|
|
return nil, err1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Infer pattern as string (in case it is untyped)
|
|
|
|
inferTypeAsString(pattern)
|
|
|
|
|
|
|
|
patternStr, ok := pattern.ToString()
|
|
|
|
if !ok {
|
|
|
|
err := errLikeNonStrArg
|
|
|
|
return nil, errLikeInvalidInputs(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
escape := runeZero
|
|
|
|
if e.EscapeChar != nil {
|
2021-04-06 11:49:04 -04:00
|
|
|
escapeVal, err2 := e.EscapeChar.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
if err2 != nil {
|
|
|
|
return nil, err2
|
|
|
|
}
|
|
|
|
|
|
|
|
inferTypeAsString(escapeVal)
|
|
|
|
|
|
|
|
escapeStr, ok := escapeVal.ToString()
|
|
|
|
if !ok {
|
|
|
|
err := errLikeNonStrArg
|
|
|
|
return nil, errLikeInvalidInputs(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len([]rune(escapeStr)) > 1 {
|
|
|
|
err := errLikeInvalidEscape
|
|
|
|
return nil, errLikeInvalidInputs(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
matchResult, err := evalSQLLike(s, patternStr, escape)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Not {
|
|
|
|
matchResult = !matchResult
|
|
|
|
}
|
|
|
|
|
|
|
|
return FromBool(matchResult), nil
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
func (e *ListExpr) evalNode(r Record, tableAlias string) (*Value, error) {
|
2019-10-06 10:52:45 -04:00
|
|
|
res := make([]Value, len(e.Elements))
|
|
|
|
if len(e.Elements) == 1 {
|
|
|
|
// If length 1, treat as single value.
|
2021-04-06 11:49:04 -04:00
|
|
|
return e.Elements[0].evalNode(r, tableAlias)
|
2019-10-06 10:52:45 -04:00
|
|
|
}
|
|
|
|
for i, elt := range e.Elements {
|
2021-04-06 11:49:04 -04:00
|
|
|
v, err := elt.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-06 10:52:45 -04:00
|
|
|
res[i] = *v
|
|
|
|
}
|
|
|
|
return FromArray(res), nil
|
|
|
|
}
|
2019-01-28 20:59:48 -05:00
|
|
|
|
2020-08-24 15:11:20 -04:00
|
|
|
const floatCmpTolerance = 0.000001
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
func (e *In) evalInNode(r Record, lhs *Value, tableAlias string) (*Value, error) {
|
2019-10-06 10:52:45 -04:00
|
|
|
// Compare two values in terms of in-ness.
|
|
|
|
var cmp func(a, b Value) bool
|
|
|
|
cmp = func(a, b Value) bool {
|
2020-04-24 16:02:59 -04:00
|
|
|
// Convert if needed.
|
|
|
|
inferTypesForCmp(&a, &b)
|
|
|
|
|
2019-10-06 10:52:45 -04:00
|
|
|
if a.Equals(b) {
|
|
|
|
return true
|
|
|
|
}
|
2019-01-28 20:59:48 -05:00
|
|
|
|
2019-10-06 10:52:45 -04:00
|
|
|
// If elements, compare each.
|
|
|
|
aA, aOK := a.ToArray()
|
|
|
|
bA, bOK := b.ToArray()
|
|
|
|
if aOK && bOK {
|
|
|
|
if len(aA) != len(bA) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := range aA {
|
|
|
|
if !cmp(aA[i], bA[i]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2019-01-28 20:59:48 -05:00
|
|
|
}
|
2019-10-06 10:52:45 -04:00
|
|
|
// Try as numbers
|
|
|
|
aF, aOK := a.ToFloat()
|
|
|
|
bF, bOK := b.ToFloat()
|
|
|
|
|
2020-08-24 15:11:20 -04:00
|
|
|
diff := math.Abs(aF - bF)
|
|
|
|
return aOK && bOK && diff < floatCmpTolerance
|
2019-10-06 10:52:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var rhs Value
|
2023-12-23 02:19:11 -05:00
|
|
|
var err error
|
|
|
|
var eltVal *Value
|
|
|
|
switch {
|
|
|
|
case e.JPathExpr != nil:
|
|
|
|
eltVal, err = e.JPathExpr.evalNode(r, tableAlias)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case e.ListExpr != nil:
|
|
|
|
eltVal, err = e.ListExpr.evalNode(r, tableAlias)
|
2019-10-06 10:52:45 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-01-28 20:59:48 -05:00
|
|
|
}
|
2023-12-23 02:19:11 -05:00
|
|
|
default:
|
|
|
|
return nil, errInvalidASTNode
|
2019-01-28 20:59:48 -05:00
|
|
|
}
|
2023-12-23 02:19:11 -05:00
|
|
|
rhs = *eltVal
|
2019-10-06 10:52:45 -04:00
|
|
|
|
|
|
|
// If RHS is array compare each element.
|
|
|
|
if arr, ok := rhs.ToArray(); ok {
|
|
|
|
for _, element := range arr {
|
|
|
|
// If we have an array we are on the wrong level.
|
|
|
|
if cmp(element, *lhs) {
|
|
|
|
return FromBool(true), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FromBool(false), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return FromBool(cmp(rhs, *lhs)), nil
|
2019-01-28 20:59:48 -05:00
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
func (e *Operand) evalNode(r Record, tableAlias string) (*Value, error) {
|
|
|
|
lval, lerr := e.Left.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
if lerr != nil || len(e.Right) == 0 {
|
|
|
|
return lval, lerr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process remaining child nodes - result must be
|
|
|
|
// numeric. This AST node is for terms separated by + or -
|
|
|
|
// symbols.
|
|
|
|
for _, rightTerm := range e.Right {
|
|
|
|
op := rightTerm.Op
|
2021-04-06 11:49:04 -04:00
|
|
|
rval, rerr := rightTerm.Right.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
if rerr != nil {
|
|
|
|
return nil, rerr
|
|
|
|
}
|
|
|
|
err := lval.arithOp(op, rval)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lval, nil
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
func (e *MultOp) evalNode(r Record, tableAlias string) (*Value, error) {
|
|
|
|
lval, lerr := e.Left.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
if lerr != nil || len(e.Right) == 0 {
|
|
|
|
return lval, lerr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process other child nodes - result must be numeric. This
|
|
|
|
// AST node is for terms separated by *, / or % symbols.
|
|
|
|
for _, rightTerm := range e.Right {
|
|
|
|
op := rightTerm.Op
|
2021-04-06 11:49:04 -04:00
|
|
|
rval, rerr := rightTerm.Right.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
if rerr != nil {
|
|
|
|
return nil, rerr
|
|
|
|
}
|
|
|
|
|
|
|
|
err := lval.arithOp(op, rval)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lval, nil
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
func (e *UnaryTerm) evalNode(r Record, tableAlias string) (*Value, error) {
|
2019-01-28 20:59:48 -05:00
|
|
|
if e.Negated == nil {
|
2021-04-06 11:49:04 -04:00
|
|
|
return e.Primary.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
v, err := e.Negated.Term.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
inferTypeForArithOp(v)
|
2019-02-04 23:54:45 -05:00
|
|
|
v.negate()
|
|
|
|
if v.isNumeric() {
|
|
|
|
return v, nil
|
2019-01-28 20:59:48 -05:00
|
|
|
}
|
|
|
|
return nil, errArithMismatchedTypes
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
func (e *JSONPath) evalNode(r Record, tableAlias string) (*Value, error) {
|
|
|
|
alias := tableAlias
|
|
|
|
if tableAlias == "" {
|
|
|
|
alias = baseTableName
|
2019-01-28 20:59:48 -05:00
|
|
|
}
|
2021-04-06 11:49:04 -04:00
|
|
|
pathExpr := e.StripTableAlias(alias)
|
2020-02-13 17:03:52 -05:00
|
|
|
_, rawVal := r.Raw()
|
|
|
|
switch rowVal := rawVal.(type) {
|
|
|
|
case jstream.KVS, simdjson.Object:
|
2019-03-09 11:13:37 -05:00
|
|
|
if len(pathExpr) == 0 {
|
|
|
|
pathExpr = []*JSONPathElement{{Key: &ObjectKey{ID: e.BaseKey}}}
|
|
|
|
}
|
|
|
|
|
2019-10-06 10:52:45 -04:00
|
|
|
result, _, err := jsonpathEval(pathExpr, rowVal)
|
2019-03-09 11:13:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-06 10:52:45 -04:00
|
|
|
return jsonToValue(result)
|
|
|
|
default:
|
2021-04-06 11:49:04 -04:00
|
|
|
if pathExpr[len(pathExpr)-1].Key == nil {
|
|
|
|
return nil, errInvalidKeypath
|
|
|
|
}
|
|
|
|
return r.Get(pathExpr[len(pathExpr)-1].Key.keyString())
|
2019-10-06 10:52:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// jsonToValue will convert the json value to an internal value.
|
|
|
|
func jsonToValue(result interface{}) (*Value, error) {
|
|
|
|
switch rval := result.(type) {
|
|
|
|
case string:
|
|
|
|
return FromString(rval), nil
|
|
|
|
case float64:
|
|
|
|
return FromFloat(rval), nil
|
|
|
|
case int64:
|
|
|
|
return FromInt(rval), nil
|
2020-02-13 17:03:52 -05:00
|
|
|
case uint64:
|
|
|
|
if rval <= math.MaxInt64 {
|
|
|
|
return FromInt(int64(rval)), nil
|
|
|
|
}
|
|
|
|
return FromFloat(float64(rval)), nil
|
2019-10-06 10:52:45 -04:00
|
|
|
case bool:
|
|
|
|
return FromBool(rval), nil
|
|
|
|
case jstream.KVS:
|
|
|
|
bs, err := json.Marshal(result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return FromBytes(bs), nil
|
|
|
|
case []interface{}:
|
|
|
|
dst := make([]Value, len(rval))
|
|
|
|
for i := range rval {
|
|
|
|
v, err := jsonToValue(rval[i])
|
2019-03-09 11:13:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-06 10:52:45 -04:00
|
|
|
dst[i] = *v
|
2019-03-09 11:13:37 -05:00
|
|
|
}
|
2019-10-06 10:52:45 -04:00
|
|
|
return FromArray(dst), nil
|
2020-02-13 17:03:52 -05:00
|
|
|
case simdjson.Object:
|
|
|
|
o := rval
|
|
|
|
elems, err := o.Parse(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
bs, err := elems.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return FromBytes(bs), nil
|
2019-10-06 10:52:45 -04:00
|
|
|
case []Value:
|
|
|
|
return FromArray(rval), nil
|
|
|
|
case nil:
|
|
|
|
return FromNull(), nil
|
2022-02-25 15:31:19 -05:00
|
|
|
case Missing:
|
|
|
|
return FromMissing(), nil
|
2019-03-09 11:13:37 -05:00
|
|
|
}
|
2019-10-06 10:52:45 -04:00
|
|
|
return nil, fmt.Errorf("Unhandled value type: %T", result)
|
2019-01-28 20:59:48 -05:00
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
func (e *PrimaryTerm) evalNode(r Record, tableAlias string) (res *Value, err error) {
|
2019-01-28 20:59:48 -05:00
|
|
|
switch {
|
|
|
|
case e.Value != nil:
|
|
|
|
return e.Value.evalNode(r)
|
|
|
|
case e.JPathExpr != nil:
|
2021-04-06 11:49:04 -04:00
|
|
|
return e.JPathExpr.evalNode(r, tableAlias)
|
2019-10-06 10:52:45 -04:00
|
|
|
case e.ListExpr != nil:
|
2021-04-06 11:49:04 -04:00
|
|
|
return e.ListExpr.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
case e.SubExpression != nil:
|
2021-04-06 11:49:04 -04:00
|
|
|
return e.SubExpression.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
case e.FuncCall != nil:
|
2021-04-06 11:49:04 -04:00
|
|
|
return e.FuncCall.evalNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
}
|
|
|
|
return nil, errInvalidASTNode
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:49:04 -04:00
|
|
|
func (e *FuncExpr) evalNode(r Record, tableAlias string) (res *Value, err error) {
|
2019-01-28 20:59:48 -05:00
|
|
|
switch e.getFunctionName() {
|
|
|
|
case aggFnCount, aggFnAvg, aggFnMax, aggFnMin, aggFnSum:
|
|
|
|
return e.getAggregate()
|
|
|
|
default:
|
2021-04-06 11:49:04 -04:00
|
|
|
return e.evalSQLFnNode(r, tableAlias)
|
2019-01-28 20:59:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// evalNode on a literal value is independent of the node being an
|
|
|
|
// aggregation or a row function - it always returns a value.
|
|
|
|
func (e *LitValue) evalNode(_ Record) (res *Value, err error) {
|
|
|
|
switch {
|
2021-03-19 12:14:38 -04:00
|
|
|
case e.Int != nil:
|
|
|
|
if *e.Int < math.MaxInt64 && *e.Int > math.MinInt64 {
|
|
|
|
return FromInt(int64(*e.Int)), nil
|
|
|
|
}
|
|
|
|
return FromFloat(*e.Int), nil
|
|
|
|
case e.Float != nil:
|
|
|
|
return FromFloat(*e.Float), nil
|
2019-01-28 20:59:48 -05:00
|
|
|
case e.String != nil:
|
|
|
|
return FromString(string(*e.String)), nil
|
|
|
|
case e.Boolean != nil:
|
|
|
|
return FromBool(bool(*e.Boolean)), nil
|
2022-02-25 15:31:19 -05:00
|
|
|
case e.Missing:
|
|
|
|
return FromMissing(), nil
|
2019-01-28 20:59:48 -05:00
|
|
|
}
|
|
|
|
return FromNull(), nil
|
|
|
|
}
|