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 (
|
|
|
|
"fmt"
|
2018-10-04 20:33:53 -04:00
|
|
|
"math"
|
2018-08-15 06:30:19 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2018-10-22 15:12:22 -04:00
|
|
|
"github.com/minio/minio/pkg/s3select/format"
|
|
|
|
"github.com/tidwall/gjson"
|
2018-08-15 06:30:19 -04:00
|
|
|
"github.com/xwb1989/sqlparser"
|
|
|
|
)
|
|
|
|
|
2018-08-17 20:11:39 -04:00
|
|
|
// MaxExpressionLength - 256KiB
|
|
|
|
const MaxExpressionLength = 256 * 1024
|
|
|
|
|
2018-11-14 18:55:10 -05:00
|
|
|
// matchesMyWhereClause takes []byte, process the where clause and returns true if the row suffices
|
|
|
|
func matchesMyWhereClause(record []byte, alias string, whereClause sqlparser.Expr) (bool, error) {
|
2018-08-15 06:30:19 -04:00
|
|
|
var conversionColumn string
|
|
|
|
var operator string
|
2018-11-14 18:55:10 -05:00
|
|
|
var operand gjson.Result
|
2018-08-15 06:30:19 -04:00
|
|
|
if fmt.Sprintf("%v", whereClause) == "false" {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
switch expr := whereClause.(type) {
|
|
|
|
case *sqlparser.IsExpr:
|
2018-11-14 18:55:10 -05:00
|
|
|
return evaluateIsExpr(expr, record, alias)
|
2018-08-15 06:30:19 -04:00
|
|
|
case *sqlparser.RangeCond:
|
|
|
|
operator = expr.Operator
|
|
|
|
if operator != "between" && operator != "not between" {
|
|
|
|
return false, ErrUnsupportedSQLOperation
|
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
result, err := evaluateBetween(expr, alias, record)
|
2018-08-15 06:30:19 -04:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
if operator == "not between" {
|
|
|
|
return !result, nil
|
|
|
|
}
|
2018-10-22 15:12:22 -04:00
|
|
|
return result, nil
|
2018-08-15 06:30:19 -04:00
|
|
|
case *sqlparser.ComparisonExpr:
|
|
|
|
operator = expr.Operator
|
|
|
|
switch right := expr.Right.(type) {
|
|
|
|
case *sqlparser.FuncExpr:
|
2018-11-14 18:55:10 -05:00
|
|
|
operand = gjson.Parse(evaluateFuncExpr(right, "", record))
|
2018-08-15 06:30:19 -04:00
|
|
|
case *sqlparser.SQLVal:
|
2018-11-14 18:55:10 -05:00
|
|
|
operand = gjson.ParseBytes(right.Val)
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
var myVal string
|
|
|
|
switch left := expr.Left.(type) {
|
|
|
|
case *sqlparser.FuncExpr:
|
2018-11-14 18:55:10 -05:00
|
|
|
myVal = evaluateFuncExpr(left, "", record)
|
2018-08-15 06:30:19 -04:00
|
|
|
conversionColumn = ""
|
|
|
|
case *sqlparser.ColName:
|
2018-10-22 15:12:22 -04:00
|
|
|
conversionColumn = left.Name.CompliantName()
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
if myVal != "" {
|
2018-11-14 18:55:10 -05:00
|
|
|
return evaluateOperator(gjson.Parse(myVal), operator, operand)
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
return evaluateOperator(gjson.GetBytes(record, conversionColumn), operator, operand)
|
2018-08-15 06:30:19 -04:00
|
|
|
case *sqlparser.AndExpr:
|
|
|
|
var leftVal bool
|
|
|
|
var rightVal bool
|
|
|
|
switch left := expr.Left.(type) {
|
|
|
|
case *sqlparser.ComparisonExpr:
|
2018-10-22 15:12:22 -04:00
|
|
|
temp, err := matchesMyWhereClause(record, alias, left)
|
2018-08-15 06:30:19 -04:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
leftVal = temp
|
|
|
|
}
|
|
|
|
switch right := expr.Right.(type) {
|
|
|
|
case *sqlparser.ComparisonExpr:
|
2018-10-22 15:12:22 -04:00
|
|
|
temp, err := matchesMyWhereClause(record, alias, right)
|
2018-08-15 06:30:19 -04:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
rightVal = temp
|
|
|
|
}
|
|
|
|
return (rightVal && leftVal), nil
|
|
|
|
case *sqlparser.OrExpr:
|
|
|
|
var leftVal bool
|
|
|
|
var rightVal bool
|
|
|
|
switch left := expr.Left.(type) {
|
|
|
|
case *sqlparser.ComparisonExpr:
|
2018-10-22 15:12:22 -04:00
|
|
|
leftVal, _ = matchesMyWhereClause(record, alias, left)
|
2018-08-15 06:30:19 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
switch right := expr.Right.(type) {
|
|
|
|
case *sqlparser.ComparisonExpr:
|
2018-10-22 15:12:22 -04:00
|
|
|
rightVal, _ = matchesMyWhereClause(record, alias, right)
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
return (rightVal || leftVal), nil
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
2018-10-22 15:12:22 -04:00
|
|
|
|
2018-11-14 18:55:10 -05:00
|
|
|
func applyStrFunc(rawArg gjson.Result, funcName string) string {
|
2018-08-15 06:30:19 -04:00
|
|
|
switch strings.ToUpper(funcName) {
|
|
|
|
case "TRIM":
|
2018-11-14 18:55:10 -05:00
|
|
|
// parser has an issue which does not allow it to support
|
|
|
|
// Trim with other arguments
|
|
|
|
return strings.Trim(rawArg.String(), " ")
|
2018-08-15 06:30:19 -04:00
|
|
|
case "SUBSTRING":
|
2018-11-14 18:55:10 -05:00
|
|
|
// TODO: parser has an issue which does not support substring
|
|
|
|
return rawArg.String()
|
2018-08-15 06:30:19 -04:00
|
|
|
case "CHAR_LENGTH":
|
2018-11-14 18:55:10 -05:00
|
|
|
return strconv.Itoa(len(rawArg.String()))
|
2018-08-15 06:30:19 -04:00
|
|
|
case "CHARACTER_LENGTH":
|
2018-11-14 18:55:10 -05:00
|
|
|
return strconv.Itoa(len(rawArg.String()))
|
2018-08-15 06:30:19 -04:00
|
|
|
case "LOWER":
|
2018-11-14 18:55:10 -05:00
|
|
|
return strings.ToLower(rawArg.String())
|
2018-08-15 06:30:19 -04:00
|
|
|
case "UPPER":
|
2018-11-14 18:55:10 -05:00
|
|
|
return strings.ToUpper(rawArg.String())
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
return rawArg.String()
|
2018-08-15 06:30:19 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-22 15:12:22 -04:00
|
|
|
// evaluateBetween is a function which evaluates a Between Clause.
|
2018-11-14 18:55:10 -05:00
|
|
|
func evaluateBetween(betweenExpr *sqlparser.RangeCond, alias string, record []byte) (bool, error) {
|
|
|
|
var colToVal gjson.Result
|
|
|
|
var colFromVal gjson.Result
|
2018-10-22 15:12:22 -04:00
|
|
|
var conversionColumn string
|
|
|
|
var funcName string
|
|
|
|
switch colTo := betweenExpr.To.(type) {
|
|
|
|
case sqlparser.Expr:
|
|
|
|
switch colToMyVal := colTo.(type) {
|
|
|
|
case *sqlparser.FuncExpr:
|
2018-11-14 18:55:10 -05:00
|
|
|
colToVal = gjson.Parse(stringOps(colToMyVal, record, ""))
|
2018-10-22 15:12:22 -04:00
|
|
|
case *sqlparser.SQLVal:
|
2018-11-14 18:55:10 -05:00
|
|
|
colToVal = gjson.ParseBytes(colToMyVal.Val)
|
2018-10-22 15:12:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
switch colFrom := betweenExpr.From.(type) {
|
|
|
|
case sqlparser.Expr:
|
|
|
|
switch colFromMyVal := colFrom.(type) {
|
|
|
|
case *sqlparser.FuncExpr:
|
2018-11-14 18:55:10 -05:00
|
|
|
colFromVal = gjson.Parse(stringOps(colFromMyVal, record, ""))
|
2018-10-22 15:12:22 -04:00
|
|
|
case *sqlparser.SQLVal:
|
2018-11-14 18:55:10 -05:00
|
|
|
colFromVal = gjson.ParseBytes(colFromMyVal.Val)
|
2018-10-22 15:12:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var myFuncVal string
|
|
|
|
switch left := betweenExpr.Left.(type) {
|
|
|
|
case *sqlparser.FuncExpr:
|
|
|
|
myFuncVal = evaluateFuncExpr(left, "", record)
|
|
|
|
conversionColumn = ""
|
|
|
|
case *sqlparser.ColName:
|
|
|
|
conversionColumn = cleanCol(left.Name.CompliantName(), alias)
|
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
toGreater, err := evaluateOperator(colToVal, ">", colFromVal)
|
2018-10-22 15:12:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if toGreater {
|
|
|
|
return evalBetweenGreater(conversionColumn, record, funcName, colFromVal, colToVal, myFuncVal)
|
|
|
|
}
|
|
|
|
return evalBetweenLess(conversionColumn, record, funcName, colFromVal, colToVal, myFuncVal)
|
|
|
|
}
|
|
|
|
|
2018-11-14 18:55:10 -05:00
|
|
|
func evalBetween(conversionColumn string, record []byte, funcName string, colFromVal gjson.Result, colToVal gjson.Result, myColVal string, operator string) (bool, error) {
|
2018-10-22 15:12:22 -04:00
|
|
|
if format.IsInt(conversionColumn) {
|
2018-11-14 18:55:10 -05:00
|
|
|
myVal, err := evaluateOperator(gjson.GetBytes(record, "_"+conversionColumn), operator, colFromVal)
|
2018-10-22 15:12:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
var myOtherVal bool
|
2018-11-14 18:55:10 -05:00
|
|
|
myOtherVal, err = evaluateOperator(colToVal, operator, gjson.GetBytes(record, "_"+conversionColumn))
|
2018-10-22 15:12:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return (myVal && myOtherVal), nil
|
|
|
|
}
|
|
|
|
if myColVal != "" {
|
2018-11-14 18:55:10 -05:00
|
|
|
myVal, err := evaluateOperator(gjson.Parse(myColVal), operator, colFromVal)
|
2018-10-22 15:12:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
var myOtherVal bool
|
2018-11-14 18:55:10 -05:00
|
|
|
myOtherVal, err = evaluateOperator(colToVal, operator, gjson.Parse(myColVal))
|
2018-10-22 15:12:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return (myVal && myOtherVal), nil
|
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
myVal, err := evaluateOperator(gjson.GetBytes(record, conversionColumn), operator, colFromVal)
|
2018-10-22 15:12:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
var myOtherVal bool
|
2018-11-14 18:55:10 -05:00
|
|
|
myOtherVal, err = evaluateOperator(colToVal, operator, gjson.GetBytes(record, conversionColumn))
|
2018-10-22 15:12:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return (myVal && myOtherVal), nil
|
|
|
|
}
|
|
|
|
|
2018-11-14 18:55:10 -05:00
|
|
|
// evalBetweenGreater is a function which evaluates the between given that the
|
|
|
|
// TO is > than the FROM.
|
|
|
|
func evalBetweenGreater(conversionColumn string, record []byte, funcName string, colFromVal gjson.Result, colToVal gjson.Result, myColVal string) (bool, error) {
|
|
|
|
return evalBetween(conversionColumn, record, funcName, colFromVal, colToVal, myColVal, ">=")
|
|
|
|
}
|
|
|
|
|
2018-10-22 15:12:22 -04:00
|
|
|
// evalBetweenLess is a function which evaluates the between given that the
|
|
|
|
// FROM is > than the TO.
|
2018-11-14 18:55:10 -05:00
|
|
|
func evalBetweenLess(conversionColumn string, record []byte, funcName string, colFromVal gjson.Result, colToVal gjson.Result, myColVal string) (bool, error) {
|
|
|
|
return evalBetween(conversionColumn, record, funcName, colFromVal, colToVal, myColVal, "<=")
|
2018-10-22 15:12:22 -04:00
|
|
|
}
|
|
|
|
|
2018-08-15 06:30:19 -04:00
|
|
|
// This is a really important function it actually evaluates the boolean
|
|
|
|
// statement and therefore actually returns a bool, it functions as the lowest
|
|
|
|
// level of the state machine.
|
2018-11-14 18:55:10 -05:00
|
|
|
func evaluateOperator(myTblVal gjson.Result, operator string, operand gjson.Result) (bool, error) {
|
2018-08-15 06:30:19 -04:00
|
|
|
if err := checkValidOperator(operator); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
if !myTblVal.Exists() {
|
|
|
|
return false, nil
|
|
|
|
}
|
2018-08-15 06:30:19 -04:00
|
|
|
switch {
|
2018-11-14 18:55:10 -05:00
|
|
|
case operand.Type == gjson.String || operand.Type == gjson.Null:
|
|
|
|
return stringEval(myTblVal.String(), operator, operand.String())
|
|
|
|
case operand.Type == gjson.Number:
|
|
|
|
opInt := format.IsInt(operand.Raw)
|
|
|
|
tblValInt := format.IsInt(strings.Trim(myTblVal.Raw, "\""))
|
|
|
|
if opInt && tblValInt {
|
|
|
|
return intEval(int64(myTblVal.Float()), operator, operand.Int())
|
|
|
|
}
|
|
|
|
if !opInt && !tblValInt {
|
|
|
|
return floatEval(myTblVal.Float(), operator, operand.Float())
|
|
|
|
}
|
|
|
|
switch operator {
|
|
|
|
case "!=":
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
case myTblVal.Type != operand.Type:
|
2018-08-15 06:30:19 -04:00
|
|
|
return false, nil
|
2018-11-14 18:55:10 -05:00
|
|
|
default:
|
|
|
|
return false, ErrUnsupportedSyntax
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkValidOperator ensures that the current operator is supported
|
|
|
|
func checkValidOperator(operator string) error {
|
|
|
|
listOfOps := []string{">", "<", "=", "<=", ">=", "!=", "like"}
|
|
|
|
for i := range listOfOps {
|
|
|
|
if operator == listOfOps[i] {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ErrParseUnknownOperator
|
|
|
|
}
|
|
|
|
|
|
|
|
// stringEval is for evaluating the state of string comparison.
|
|
|
|
func stringEval(myRecordVal string, operator string, myOperand string) (bool, error) {
|
|
|
|
switch operator {
|
|
|
|
case ">":
|
|
|
|
return myRecordVal > myOperand, nil
|
|
|
|
case "<":
|
|
|
|
return myRecordVal < myOperand, nil
|
|
|
|
case "=":
|
|
|
|
return myRecordVal == myOperand, nil
|
|
|
|
case "<=":
|
|
|
|
return myRecordVal <= myOperand, nil
|
|
|
|
case ">=":
|
|
|
|
return myRecordVal >= myOperand, nil
|
|
|
|
case "!=":
|
|
|
|
return myRecordVal != myOperand, nil
|
|
|
|
case "like":
|
|
|
|
return likeConvert(myOperand, myRecordVal)
|
|
|
|
}
|
|
|
|
return false, ErrUnsupportedSyntax
|
|
|
|
}
|
|
|
|
|
|
|
|
// intEval is for evaluating integer comparisons.
|
|
|
|
func intEval(myRecordVal int64, operator string, myOperand int64) (bool, error) {
|
|
|
|
|
|
|
|
switch operator {
|
|
|
|
case ">":
|
|
|
|
return myRecordVal > myOperand, nil
|
|
|
|
case "<":
|
|
|
|
return myRecordVal < myOperand, nil
|
|
|
|
case "=":
|
|
|
|
return myRecordVal == myOperand, nil
|
|
|
|
case "<=":
|
|
|
|
return myRecordVal <= myOperand, nil
|
|
|
|
case ">=":
|
|
|
|
return myRecordVal >= myOperand, nil
|
|
|
|
case "!=":
|
|
|
|
return myRecordVal != myOperand, nil
|
|
|
|
}
|
|
|
|
return false, ErrUnsupportedSyntax
|
|
|
|
}
|
|
|
|
|
|
|
|
// floatEval is for evaluating the comparison of floats.
|
|
|
|
func floatEval(myRecordVal float64, operator string, myOperand float64) (bool, error) {
|
|
|
|
// Basically need some logic thats like, if the types dont match check for a cast
|
|
|
|
switch operator {
|
|
|
|
case ">":
|
|
|
|
return myRecordVal > myOperand, nil
|
|
|
|
case "<":
|
|
|
|
return myRecordVal < myOperand, nil
|
|
|
|
case "=":
|
|
|
|
return myRecordVal == myOperand, nil
|
|
|
|
case "<=":
|
|
|
|
return myRecordVal <= myOperand, nil
|
|
|
|
case ">=":
|
|
|
|
return myRecordVal >= myOperand, nil
|
|
|
|
case "!=":
|
|
|
|
return myRecordVal != myOperand, nil
|
|
|
|
}
|
|
|
|
return false, ErrUnsupportedSyntax
|
|
|
|
}
|
|
|
|
|
|
|
|
// prefixMatch allows for matching a prefix only like query e.g a%
|
|
|
|
func prefixMatch(pattern string, record string) bool {
|
|
|
|
for i := 0; i < len(pattern)-1; i++ {
|
|
|
|
if pattern[i] != record[i] && pattern[i] != byte('_') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// suffixMatch allows for matching a suffix only like query e.g %an
|
|
|
|
func suffixMatch(pattern string, record string) bool {
|
|
|
|
for i := len(pattern) - 1; i > 0; i-- {
|
|
|
|
if pattern[i] != record[len(record)-(len(pattern)-i)] && pattern[i] != byte('_') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function is for evaluating select statements which are case sensitive
|
|
|
|
func likeConvert(pattern string, record string) (bool, error) {
|
|
|
|
// If pattern is empty just return false
|
|
|
|
if pattern == "" || record == "" {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
// for suffix match queries e.g %a
|
|
|
|
if len(pattern) >= 2 && pattern[0] == byte('%') && strings.Count(pattern, "%") == 1 {
|
|
|
|
return suffixMatch(pattern, record), nil
|
|
|
|
}
|
|
|
|
// for prefix match queries e.g a%
|
|
|
|
if len(pattern) >= 2 && pattern[len(pattern)-1] == byte('%') && strings.Count(pattern, "%") == 1 {
|
|
|
|
return prefixMatch(pattern, record), nil
|
|
|
|
}
|
|
|
|
charCount := 0
|
|
|
|
currPos := 0
|
|
|
|
// Loop through the pattern so that a boolean can be returned
|
|
|
|
for i := 0; i < len(pattern); i++ {
|
|
|
|
if pattern[i] == byte('_') {
|
|
|
|
// if its an underscore it can be anything so shift current position for
|
|
|
|
// pattern and string
|
|
|
|
charCount++
|
|
|
|
// if there have been more characters in the pattern than record, clearly
|
|
|
|
// there should be a return
|
|
|
|
if i != len(pattern)-1 {
|
|
|
|
if pattern[i+1] != byte('%') && pattern[i+1] != byte('_') {
|
|
|
|
if currPos != len(record)-1 && pattern[i+1] != record[currPos+1] {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if charCount > len(record) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
// if the pattern has been fully evaluated, then just return.
|
|
|
|
if len(pattern) == i+1 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
currPos++
|
|
|
|
}
|
|
|
|
if pattern[i] == byte('%') || pattern[i] == byte('*') {
|
|
|
|
// if there is a wildcard then want to return true if its last and flag it.
|
|
|
|
if currPos == len(record) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if i+1 == len(pattern) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
charCount++
|
|
|
|
matched := false
|
|
|
|
// iterate through the pattern and check if there is a match for the
|
|
|
|
// character
|
|
|
|
for currPos < len(record) {
|
|
|
|
if record[currPos] == pattern[i] || pattern[i] == byte('_') {
|
|
|
|
matched = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
currPos++
|
|
|
|
}
|
|
|
|
currPos++
|
|
|
|
// if the character did not match then return should occur.
|
|
|
|
if !matched {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if charCount > len(record) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if currPos < len(record) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanCol cleans a column name from the parser so that the name is returned to
|
|
|
|
// original.
|
|
|
|
func cleanCol(myCol string, alias string) string {
|
|
|
|
if len(myCol) <= 0 {
|
|
|
|
return myCol
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(myCol, alias) && myCol[0] == '_' {
|
|
|
|
myCol = alias + myCol
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(myCol, ".") {
|
|
|
|
myCol = strings.Replace(myCol, alias+"._", "", len(myCol))
|
|
|
|
}
|
|
|
|
myCol = strings.Replace(myCol, alias+"_", "", len(myCol))
|
|
|
|
return myCol
|
|
|
|
}
|
|
|
|
|
|
|
|
// whereClauseNameErrs is a function which returns an error if there is a column
|
|
|
|
// in the where clause which does not exist.
|
2018-10-22 15:12:22 -04:00
|
|
|
func whereClauseNameErrs(whereClause interface{}, alias string, f format.Select) error {
|
2018-08-15 06:30:19 -04:00
|
|
|
var conversionColumn string
|
|
|
|
switch expr := whereClause.(type) {
|
|
|
|
// case for checking errors within a clause of the form "col_name is ..."
|
|
|
|
case *sqlparser.IsExpr:
|
|
|
|
switch myCol := expr.Expr.(type) {
|
|
|
|
case *sqlparser.FuncExpr:
|
2018-10-22 15:12:22 -04:00
|
|
|
if err := evaluateFuncErr(myCol, f); err != nil {
|
2018-08-15 06:30:19 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case *sqlparser.ColName:
|
|
|
|
conversionColumn = cleanCol(myCol.Name.CompliantName(), alias)
|
|
|
|
}
|
|
|
|
case *sqlparser.RangeCond:
|
|
|
|
switch left := expr.Left.(type) {
|
|
|
|
case *sqlparser.FuncExpr:
|
2018-10-22 15:12:22 -04:00
|
|
|
if err := evaluateFuncErr(left, f); err != nil {
|
2018-08-15 06:30:19 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case *sqlparser.ColName:
|
|
|
|
conversionColumn = cleanCol(left.Name.CompliantName(), alias)
|
|
|
|
}
|
|
|
|
case *sqlparser.ComparisonExpr:
|
|
|
|
switch left := expr.Left.(type) {
|
|
|
|
case *sqlparser.FuncExpr:
|
2018-10-22 15:12:22 -04:00
|
|
|
if err := evaluateFuncErr(left, f); err != nil {
|
2018-08-15 06:30:19 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case *sqlparser.ColName:
|
|
|
|
conversionColumn = cleanCol(left.Name.CompliantName(), alias)
|
|
|
|
}
|
|
|
|
case *sqlparser.AndExpr:
|
|
|
|
switch left := expr.Left.(type) {
|
|
|
|
case *sqlparser.ComparisonExpr:
|
2018-10-22 15:12:22 -04:00
|
|
|
return whereClauseNameErrs(left, alias, f)
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
switch right := expr.Right.(type) {
|
|
|
|
case *sqlparser.ComparisonExpr:
|
2018-10-22 15:12:22 -04:00
|
|
|
return whereClauseNameErrs(right, alias, f)
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
case *sqlparser.OrExpr:
|
|
|
|
switch left := expr.Left.(type) {
|
|
|
|
case *sqlparser.ComparisonExpr:
|
2018-10-22 15:12:22 -04:00
|
|
|
return whereClauseNameErrs(left, alias, f)
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
switch right := expr.Right.(type) {
|
|
|
|
case *sqlparser.ComparisonExpr:
|
2018-10-22 15:12:22 -04:00
|
|
|
return whereClauseNameErrs(right, alias, f)
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if conversionColumn != "" {
|
2018-10-22 15:12:22 -04:00
|
|
|
return f.ColNameErrs([]string{conversionColumn})
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// aggFuncToStr converts an array of floats into a properly formatted string.
|
2018-10-22 15:12:22 -04:00
|
|
|
func aggFuncToStr(aggVals []float64, f format.Select) string {
|
2018-10-09 17:02:19 -04:00
|
|
|
// Define a number formatting function
|
|
|
|
numToStr := func(f float64) string {
|
|
|
|
if f == math.Trunc(f) {
|
|
|
|
return strconv.FormatInt(int64(f), 10)
|
2018-10-04 20:33:53 -04:00
|
|
|
}
|
2018-10-09 17:02:19 -04:00
|
|
|
return strconv.FormatFloat(f, 'f', 6, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display all whole numbers in aggVals as integers
|
|
|
|
vals := make([]string, len(aggVals))
|
|
|
|
for i, v := range aggVals {
|
|
|
|
vals[i] = numToStr(v)
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-10-09 17:02:19 -04:00
|
|
|
|
|
|
|
// Intersperse field delimiter
|
2018-10-22 15:12:22 -04:00
|
|
|
return strings.Join(vals, f.OutputFieldDelimiter())
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkForDuplicates ensures we do not have an ambigious column name.
|
2018-11-14 18:55:10 -05:00
|
|
|
func checkForDuplicates(columns []string, columnsMap map[string]int) error {
|
|
|
|
for i, column := range columns {
|
|
|
|
columns[i] = strings.Replace(column, " ", "_", len(column))
|
2018-08-15 06:30:19 -04:00
|
|
|
if _, exist := columnsMap[columns[i]]; exist {
|
|
|
|
return ErrAmbiguousFieldName
|
|
|
|
}
|
|
|
|
columnsMap[columns[i]] = i
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseErrs is the function which handles all the errors that could occur
|
|
|
|
// through use of function arguments such as column names in NULLIF
|
2018-10-30 23:18:01 -04:00
|
|
|
func parseErrs(columnNames []string, whereClause interface{}, alias string, myFuncs SelectFuncs, f format.Select) error {
|
2018-08-15 06:30:19 -04:00
|
|
|
// Below code cleans up column names.
|
2018-10-22 15:12:22 -04:00
|
|
|
processColumnNames(columnNames, alias, f)
|
2018-08-15 06:30:19 -04:00
|
|
|
if columnNames[0] != "*" {
|
2018-10-22 15:12:22 -04:00
|
|
|
if err := f.ColNameErrs(columnNames); err != nil {
|
2018-08-15 06:30:19 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Below code ensures the whereClause has no errors.
|
|
|
|
if whereClause != nil {
|
|
|
|
tempClause := whereClause
|
2018-10-22 15:12:22 -04:00
|
|
|
if err := whereClauseNameErrs(tempClause, alias, f); err != nil {
|
2018-08-15 06:30:19 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := 0; i < len(myFuncs.funcExpr); i++ {
|
|
|
|
if myFuncs.funcExpr[i] == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-22 15:12:22 -04:00
|
|
|
if err := evaluateFuncErr(myFuncs.funcExpr[i], f); err != nil {
|
2018-08-15 06:30:19 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|