2018-08-15 06:30:19 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2018 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package s3select
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2018-11-14 18:55:10 -05:00
|
|
|
"github.com/tidwall/gjson"
|
2018-08-15 06:30:19 -04:00
|
|
|
"github.com/xwb1989/sqlparser"
|
2018-11-14 18:55:10 -05:00
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/s3select/format"
|
2018-08-15 06:30:19 -04:00
|
|
|
)
|
|
|
|
|
2018-11-14 18:55:10 -05:00
|
|
|
// stringOps is a function which handles the case in a clause
|
|
|
|
// if there is a need to perform a string function
|
|
|
|
func stringOps(myFunc *sqlparser.FuncExpr, record []byte, myReturnVal string) string {
|
2018-08-15 06:30:19 -04:00
|
|
|
var value string
|
|
|
|
funcName := myFunc.Name.CompliantName()
|
|
|
|
switch tempArg := myFunc.Exprs[0].(type) {
|
|
|
|
case *sqlparser.AliasedExpr:
|
|
|
|
switch col := tempArg.Expr.(type) {
|
|
|
|
case *sqlparser.FuncExpr:
|
|
|
|
// myReturnVal is actually the tail recursive value being used in the eval func.
|
2018-11-14 18:55:10 -05:00
|
|
|
return applyStrFunc(gjson.Parse(myReturnVal), funcName)
|
2018-08-15 06:30:19 -04:00
|
|
|
case *sqlparser.ColName:
|
2018-11-14 18:55:10 -05:00
|
|
|
value = applyStrFunc(gjson.GetBytes(record, col.Name.CompliantName()), funcName)
|
2018-08-15 06:30:19 -04:00
|
|
|
case *sqlparser.SQLVal:
|
2018-11-14 18:55:10 -05:00
|
|
|
value = applyStrFunc(gjson.ParseBytes(col.Val), funcName)
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
// coalOps is a function which decomposes a COALESCE func expr into its struct.
|
2018-11-14 18:55:10 -05:00
|
|
|
func coalOps(myFunc *sqlparser.FuncExpr, record []byte, myReturnVal string) string {
|
2018-08-15 06:30:19 -04:00
|
|
|
myArgs := make([]string, len(myFunc.Exprs))
|
|
|
|
|
2018-11-14 18:55:10 -05:00
|
|
|
for i, expr := range myFunc.Exprs {
|
|
|
|
switch tempArg := expr.(type) {
|
2018-08-15 06:30:19 -04:00
|
|
|
case *sqlparser.AliasedExpr:
|
|
|
|
switch col := tempArg.Expr.(type) {
|
|
|
|
case *sqlparser.FuncExpr:
|
|
|
|
// myReturnVal is actually the tail recursive value being used in the eval func.
|
|
|
|
return myReturnVal
|
|
|
|
case *sqlparser.ColName:
|
2018-11-14 18:55:10 -05:00
|
|
|
myArgs[i] = gjson.GetBytes(record, col.Name.CompliantName()).String()
|
2018-08-15 06:30:19 -04:00
|
|
|
case *sqlparser.SQLVal:
|
|
|
|
myArgs[i] = string(col.Val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return processCoalNoIndex(myArgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// nullOps is a function which decomposes a NullIf func expr into its struct.
|
2018-11-14 18:55:10 -05:00
|
|
|
func nullOps(myFunc *sqlparser.FuncExpr, record []byte, myReturnVal string) string {
|
2018-08-15 06:30:19 -04:00
|
|
|
myArgs := make([]string, 2)
|
|
|
|
|
2018-11-14 18:55:10 -05:00
|
|
|
for i, expr := range myFunc.Exprs {
|
|
|
|
switch tempArg := expr.(type) {
|
2018-08-15 06:30:19 -04:00
|
|
|
case *sqlparser.AliasedExpr:
|
|
|
|
switch col := tempArg.Expr.(type) {
|
|
|
|
case *sqlparser.FuncExpr:
|
|
|
|
return myReturnVal
|
|
|
|
case *sqlparser.ColName:
|
2018-11-14 18:55:10 -05:00
|
|
|
myArgs[i] = gjson.GetBytes(record, col.Name.CompliantName()).String()
|
2018-08-15 06:30:19 -04:00
|
|
|
case *sqlparser.SQLVal:
|
|
|
|
myArgs[i] = string(col.Val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
if myArgs[0] == myArgs[1] {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return myArgs[0]
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
|
2018-11-14 18:55:10 -05:00
|
|
|
// isValidString is a function that ensures the
|
|
|
|
// current index is one with a StrFunc
|
2018-08-15 06:30:19 -04:00
|
|
|
func isValidFunc(myList []int, index int) bool {
|
|
|
|
if myList == nil {
|
|
|
|
return false
|
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
for _, i := range myList {
|
|
|
|
if i == index {
|
2018-08-15 06:30:19 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// processCoalNoIndex is a function which evaluates a given COALESCE clause.
|
|
|
|
func processCoalNoIndex(coalStore []string) string {
|
2018-11-14 18:55:10 -05:00
|
|
|
for _, coal := range coalStore {
|
|
|
|
if coal != "null" && coal != "missing" && coal != "" {
|
|
|
|
return coal
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "null"
|
|
|
|
}
|
|
|
|
|
|
|
|
// evaluateFuncExpr is a function that allows for tail recursive evaluation of
|
2018-10-22 15:12:22 -04:00
|
|
|
// nested function expressions
|
2018-11-14 18:55:10 -05:00
|
|
|
func evaluateFuncExpr(myVal *sqlparser.FuncExpr, myReturnVal string, record []byte) string {
|
2018-08-15 06:30:19 -04:00
|
|
|
if myVal == nil {
|
|
|
|
return myReturnVal
|
|
|
|
}
|
|
|
|
// retrieve all the relevant arguments of the function
|
|
|
|
var mySubFunc []*sqlparser.FuncExpr
|
|
|
|
mySubFunc = make([]*sqlparser.FuncExpr, len(myVal.Exprs))
|
2018-11-14 18:55:10 -05:00
|
|
|
for i, expr := range myVal.Exprs {
|
|
|
|
switch col := expr.(type) {
|
2018-08-15 06:30:19 -04:00
|
|
|
case *sqlparser.AliasedExpr:
|
|
|
|
switch temp := col.Expr.(type) {
|
|
|
|
case *sqlparser.FuncExpr:
|
|
|
|
mySubFunc[i] = temp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Need to do tree recursion so as to explore all possible directions of the
|
|
|
|
// nested function recursion
|
|
|
|
for i := 0; i < len(mySubFunc); i++ {
|
|
|
|
if supportedString(myVal.Name.CompliantName()) {
|
|
|
|
if mySubFunc != nil {
|
2018-11-14 18:55:10 -05:00
|
|
|
return stringOps(myVal, record, evaluateFuncExpr(mySubFunc[i], myReturnVal, record))
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
return stringOps(myVal, record, myReturnVal)
|
2018-08-15 06:30:19 -04:00
|
|
|
} else if strings.ToUpper(myVal.Name.CompliantName()) == "NULLIF" {
|
|
|
|
if mySubFunc != nil {
|
2018-11-14 18:55:10 -05:00
|
|
|
return nullOps(myVal, record, evaluateFuncExpr(mySubFunc[i], myReturnVal, record))
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
return nullOps(myVal, record, myReturnVal)
|
2018-08-15 06:30:19 -04:00
|
|
|
} else if strings.ToUpper(myVal.Name.CompliantName()) == "COALESCE" {
|
|
|
|
if mySubFunc != nil {
|
2018-11-14 18:55:10 -05:00
|
|
|
return coalOps(myVal, record, evaluateFuncExpr(mySubFunc[i], myReturnVal, record))
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
return coalOps(myVal, record, myReturnVal)
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// evaluateFuncErr is a function that flags errors in nested functions.
|
2018-10-22 15:12:22 -04:00
|
|
|
func evaluateFuncErr(myVal *sqlparser.FuncExpr, reader format.Select) error {
|
2018-08-15 06:30:19 -04:00
|
|
|
if myVal == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !supportedFunc(myVal.Name.CompliantName()) {
|
|
|
|
return ErrUnsupportedSQLOperation
|
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
for _, expr := range myVal.Exprs {
|
|
|
|
switch tempArg := expr.(type) {
|
2018-08-15 06:30:19 -04:00
|
|
|
case *sqlparser.StarExpr:
|
|
|
|
return ErrParseUnsupportedCallWithStar
|
|
|
|
case *sqlparser.AliasedExpr:
|
|
|
|
switch col := tempArg.Expr.(type) {
|
|
|
|
case *sqlparser.FuncExpr:
|
2018-10-22 15:12:22 -04:00
|
|
|
if err := evaluateFuncErr(col, reader); err != nil {
|
2018-08-15 06:30:19 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case *sqlparser.ColName:
|
2018-10-22 15:12:22 -04:00
|
|
|
if err := reader.ColNameErrs([]string{col.Name.CompliantName()}); err != nil {
|
2018-08-15 06:30:19 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-22 15:12:22 -04:00
|
|
|
// evaluateIsExpr is a function for evaluating expressions of the form "column is ...."
|
2018-11-14 18:55:10 -05:00
|
|
|
func evaluateIsExpr(myFunc *sqlparser.IsExpr, row []byte, alias string) (bool, error) {
|
|
|
|
getMyVal := func() (myVal string) {
|
|
|
|
switch myIs := myFunc.Expr.(type) {
|
|
|
|
// case for literal val
|
|
|
|
case *sqlparser.SQLVal:
|
|
|
|
myVal = string(myIs.Val)
|
|
|
|
// case for nested func val
|
|
|
|
case *sqlparser.FuncExpr:
|
|
|
|
myVal = evaluateFuncExpr(myIs, "", row)
|
|
|
|
// case for col val
|
|
|
|
case *sqlparser.ColName:
|
|
|
|
myVal = gjson.GetBytes(row, myIs.Name.CompliantName()).String()
|
|
|
|
}
|
|
|
|
return myVal
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
|
|
|
|
operator := strings.ToLower(myFunc.Operator)
|
|
|
|
switch operator {
|
|
|
|
case "is null":
|
|
|
|
return getMyVal() == "", nil
|
|
|
|
case "is not null":
|
|
|
|
return getMyVal() != "", nil
|
|
|
|
default:
|
|
|
|
return false, ErrUnsupportedSQLOperation
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// supportedString is a function that checks whether the function is a supported
|
|
|
|
// string one
|
|
|
|
func supportedString(strFunc string) bool {
|
2018-10-22 15:12:22 -04:00
|
|
|
return format.StringInSlice(strings.ToUpper(strFunc), []string{"TRIM", "SUBSTRING", "CHAR_LENGTH", "CHARACTER_LENGTH", "LOWER", "UPPER"})
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// supportedFunc is a function that checks whether the function is a supported
|
|
|
|
// S3 one.
|
|
|
|
func supportedFunc(strFunc string) bool {
|
2018-10-22 15:12:22 -04:00
|
|
|
return format.StringInSlice(strings.ToUpper(strFunc), []string{"TRIM", "SUBSTRING", "CHAR_LENGTH", "CHARACTER_LENGTH", "LOWER", "UPPER", "COALESCE", "NULLIF"})
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|