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 (
|
|
|
|
"math"
|
2018-10-22 15:12:22 -04:00
|
|
|
"sort"
|
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"
|
2018-11-14 18:55:10 -05:00
|
|
|
"github.com/tidwall/gjson"
|
2018-08-15 06:30:19 -04:00
|
|
|
"github.com/xwb1989/sqlparser"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SelectFuncs contains the relevant values from the parser for S3 Select
|
|
|
|
// Functions
|
|
|
|
type SelectFuncs struct {
|
|
|
|
funcExpr []*sqlparser.FuncExpr
|
|
|
|
index []int
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunSqlParser allows us to easily bundle all the functions from above and run
|
|
|
|
// them in the appropriate order.
|
2018-11-14 18:55:10 -05:00
|
|
|
func runSelectParser(f format.Select, rowCh chan Row) {
|
|
|
|
reqCols, alias, limit, wc, aggFunctionNames, fns, err := ParseSelect(f)
|
|
|
|
if err != nil {
|
|
|
|
rowCh <- Row{
|
|
|
|
err: err,
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
processSelectReq(reqCols, alias, wc, limit, aggFunctionNames, rowCh, fns, f)
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseSelect parses the SELECT expression, and effectively tokenizes it into
|
|
|
|
// its separate parts. It returns the requested column names,alias,limit of
|
|
|
|
// records, and the where clause.
|
2018-11-14 18:55:10 -05:00
|
|
|
func ParseSelect(f format.Select) ([]string, string, int64, sqlparser.Expr, []string, SelectFuncs, error) {
|
2018-10-30 23:18:01 -04:00
|
|
|
var sFuncs = SelectFuncs{}
|
2018-11-14 18:55:10 -05:00
|
|
|
var whereClause sqlparser.Expr
|
2018-10-30 23:18:01 -04:00
|
|
|
var alias string
|
|
|
|
var limit int64
|
2018-10-22 15:12:22 -04:00
|
|
|
|
2018-10-30 23:18:01 -04:00
|
|
|
stmt, err := sqlparser.Parse(f.Expression())
|
2018-11-14 18:55:10 -05:00
|
|
|
// TODO: Maybe can parse their errors a bit to return some more of the s3 errors
|
2018-08-15 06:30:19 -04:00
|
|
|
if err != nil {
|
2018-10-30 23:18:01 -04:00
|
|
|
return nil, "", 0, nil, nil, sFuncs, ErrLexerInvalidChar
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-10-22 15:12:22 -04:00
|
|
|
|
2018-08-15 06:30:19 -04:00
|
|
|
switch stmt := stmt.(type) {
|
|
|
|
case *sqlparser.Select:
|
|
|
|
// evaluates the where clause
|
2018-11-14 18:55:10 -05:00
|
|
|
fnNames := make([]string, len(stmt.SelectExprs))
|
2018-08-15 06:30:19 -04:00
|
|
|
columnNames := make([]string, len(stmt.SelectExprs))
|
|
|
|
|
|
|
|
if stmt.Where != nil {
|
2018-11-14 18:55:10 -05:00
|
|
|
whereClause = stmt.Where.Expr
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
for i, sexpr := range stmt.SelectExprs {
|
|
|
|
switch expr := sexpr.(type) {
|
|
|
|
case *sqlparser.StarExpr:
|
|
|
|
columnNames[0] = "*"
|
|
|
|
case *sqlparser.AliasedExpr:
|
|
|
|
switch smallerexpr := expr.Expr.(type) {
|
|
|
|
case *sqlparser.FuncExpr:
|
|
|
|
if smallerexpr.IsAggregate() {
|
|
|
|
fnNames[i] = smallerexpr.Name.CompliantName()
|
|
|
|
// Will return function name
|
|
|
|
// Case to deal with if we have functions and not an asterix
|
|
|
|
switch tempagg := smallerexpr.Exprs[0].(type) {
|
|
|
|
case *sqlparser.StarExpr:
|
|
|
|
columnNames[0] = "*"
|
|
|
|
if smallerexpr.Name.CompliantName() != "count" {
|
|
|
|
return nil, "", 0, nil, nil, sFuncs, ErrParseUnsupportedCallWithStar
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
case *sqlparser.AliasedExpr:
|
|
|
|
switch col := tempagg.Expr.(type) {
|
|
|
|
case *sqlparser.BinaryExpr:
|
|
|
|
return nil, "", 0, nil, nil, sFuncs, ErrParseNonUnaryAgregateFunctionCall
|
|
|
|
case *sqlparser.ColName:
|
|
|
|
columnNames[i] = col.Name.CompliantName()
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
// Case to deal with if COALESCE was used..
|
|
|
|
} else if supportedFunc(smallerexpr.Name.CompliantName()) {
|
|
|
|
if sFuncs.funcExpr == nil {
|
|
|
|
sFuncs.funcExpr = make([]*sqlparser.FuncExpr, len(stmt.SelectExprs))
|
|
|
|
sFuncs.index = make([]int, len(stmt.SelectExprs))
|
|
|
|
}
|
|
|
|
sFuncs.funcExpr[i] = smallerexpr
|
|
|
|
sFuncs.index[i] = i
|
|
|
|
} else {
|
|
|
|
return nil, "", 0, nil, nil, sFuncs, ErrUnsupportedSQLOperation
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
case *sqlparser.ColName:
|
|
|
|
columnNames[i] = smallerexpr.Name.CompliantName()
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This code retrieves the alias and makes sure it is set to the correct
|
|
|
|
// value, if not it sets it to the tablename
|
2018-11-14 18:55:10 -05:00
|
|
|
for _, fexpr := range stmt.From {
|
|
|
|
switch smallerexpr := fexpr.(type) {
|
|
|
|
case *sqlparser.JoinTableExpr:
|
|
|
|
return nil, "", 0, nil, nil, sFuncs, ErrParseMalformedJoin
|
|
|
|
case *sqlparser.AliasedTableExpr:
|
|
|
|
alias = smallerexpr.As.CompliantName()
|
|
|
|
if alias == "" {
|
|
|
|
alias = sqlparser.GetTableName(smallerexpr.Expr).CompliantName()
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if stmt.Limit != nil {
|
|
|
|
switch expr := stmt.Limit.Rowcount.(type) {
|
|
|
|
case *sqlparser.SQLVal:
|
|
|
|
// The Value of how many rows we're going to limit by
|
2018-08-22 06:46:04 -04:00
|
|
|
parsedLimit, _ := strconv.Atoi(string(expr.Val[:]))
|
|
|
|
limit = int64(parsedLimit)
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if stmt.GroupBy != nil {
|
2018-10-30 23:18:01 -04:00
|
|
|
return nil, "", 0, nil, nil, sFuncs, ErrParseUnsupportedLiteralsGroupBy
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
if stmt.OrderBy != nil {
|
2018-10-30 23:18:01 -04:00
|
|
|
return nil, "", 0, nil, nil, sFuncs, ErrParseUnsupportedToken
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-10-30 23:18:01 -04:00
|
|
|
if err := parseErrs(columnNames, whereClause, alias, sFuncs, f); err != nil {
|
|
|
|
return nil, "", 0, nil, nil, sFuncs, err
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
return columnNames, alias, limit, whereClause, fnNames, sFuncs, nil
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-10-30 23:18:01 -04:00
|
|
|
return nil, "", 0, nil, nil, sFuncs, nil
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
|
2018-11-14 18:55:10 -05:00
|
|
|
type columnKv struct {
|
|
|
|
Key string
|
|
|
|
Value int
|
|
|
|
}
|
|
|
|
|
|
|
|
func columnsIndex(reqColNames []string, f format.Select) ([]columnKv, error) {
|
|
|
|
var (
|
|
|
|
columnsKv []columnKv
|
|
|
|
columnsMap = make(map[string]int)
|
|
|
|
columns = f.Header()
|
|
|
|
)
|
|
|
|
if f.HasHeader() {
|
|
|
|
err := checkForDuplicates(columns, columnsMap)
|
|
|
|
if format.IsInt(reqColNames[0]) {
|
|
|
|
err = ErrMissingHeaders
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for k, v := range columnsMap {
|
|
|
|
columnsKv = append(columnsKv, columnKv{
|
|
|
|
Key: k,
|
|
|
|
Value: v,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := range columns {
|
|
|
|
columnsKv = append(columnsKv, columnKv{
|
|
|
|
Key: "_" + strconv.Itoa(i),
|
|
|
|
Value: i,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Slice(columnsKv, func(i, j int) bool {
|
|
|
|
return columnsKv[i].Value < columnsKv[j].Value
|
|
|
|
})
|
|
|
|
return columnsKv, nil
|
|
|
|
}
|
|
|
|
|
2018-08-15 06:30:19 -04:00
|
|
|
// This is the main function, It goes row by row and for records which validate
|
|
|
|
// the where clause it currently prints the appropriate row given the requested
|
|
|
|
// columns.
|
2018-11-14 18:55:10 -05:00
|
|
|
func processSelectReq(reqColNames []string, alias string, wc sqlparser.Expr, lrecords int64, fnNames []string, rowCh chan Row, fn SelectFuncs, f format.Select) {
|
2018-08-15 06:30:19 -04:00
|
|
|
counter := -1
|
|
|
|
filtrCount := 0
|
|
|
|
functionFlag := false
|
2018-11-14 18:55:10 -05:00
|
|
|
|
|
|
|
// Values used to store our aggregation values.
|
|
|
|
aggVals := make([]float64, len(reqColNames))
|
|
|
|
if lrecords == 0 {
|
|
|
|
lrecords = math.MaxInt64
|
|
|
|
}
|
|
|
|
|
2018-12-07 17:55:32 -05:00
|
|
|
var results []string
|
|
|
|
var columnsKv []columnKv
|
|
|
|
if f.Type() == format.CSV {
|
|
|
|
var err error
|
|
|
|
columnsKv, err = columnsIndex(reqColNames, f)
|
|
|
|
if err != nil {
|
|
|
|
rowCh <- Row{
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
return
|
2018-11-14 18:55:10 -05:00
|
|
|
}
|
2018-12-07 17:55:32 -05:00
|
|
|
results = make([]string, len(columnsKv))
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
|
2018-08-15 06:30:19 -04:00
|
|
|
for {
|
2018-10-22 15:12:22 -04:00
|
|
|
record, err := f.Read()
|
|
|
|
if err != nil {
|
2018-11-14 18:55:10 -05:00
|
|
|
rowCh <- Row{
|
2018-10-22 15:12:22 -04:00
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2018-08-15 06:30:19 -04:00
|
|
|
if record == nil {
|
|
|
|
if functionFlag {
|
2018-11-14 18:55:10 -05:00
|
|
|
rowCh <- Row{
|
|
|
|
record: aggFuncToStr(aggVals, f) + "\n",
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
close(rowCh)
|
2018-08-15 06:30:19 -04:00
|
|
|
return
|
|
|
|
}
|
2018-10-22 15:12:22 -04:00
|
|
|
|
2018-12-07 17:55:32 -05:00
|
|
|
// For JSON multi-line input type columns needs
|
|
|
|
// to be handled for each record.
|
|
|
|
if f.Type() == format.JSON {
|
|
|
|
columnsKv, err = columnsIndex(reqColNames, f)
|
|
|
|
if err != nil {
|
|
|
|
rowCh <- Row{
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
results = make([]string, len(columnsKv))
|
|
|
|
}
|
|
|
|
|
2018-11-14 18:55:10 -05:00
|
|
|
f.UpdateBytesProcessed(int64(len(record)))
|
2018-10-22 15:12:22 -04:00
|
|
|
|
2018-11-14 18:55:10 -05:00
|
|
|
// Return in case the number of record reaches the LIMIT
|
|
|
|
// defined in select query
|
|
|
|
if int64(filtrCount) == lrecords {
|
|
|
|
close(rowCh)
|
2018-08-15 06:30:19 -04:00
|
|
|
return
|
|
|
|
}
|
2018-10-22 15:12:22 -04:00
|
|
|
|
2018-11-14 18:55:10 -05:00
|
|
|
// The call to the where function clause, ensures that
|
|
|
|
// the rows we print match our where clause.
|
|
|
|
condition, err := matchesMyWhereClause(record, alias, wc)
|
|
|
|
if err != nil {
|
|
|
|
rowCh <- Row{
|
|
|
|
err: err,
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
|
2018-08-15 06:30:19 -04:00
|
|
|
if condition {
|
|
|
|
// if its an asterix we just print everything in the row
|
2018-11-14 18:55:10 -05:00
|
|
|
if reqColNames[0] == "*" && fnNames[0] == "" {
|
2018-12-07 17:55:32 -05:00
|
|
|
switch f.OutputType() {
|
2018-10-22 15:12:22 -04:00
|
|
|
case format.CSV:
|
2018-11-14 18:55:10 -05:00
|
|
|
for i, kv := range columnsKv {
|
|
|
|
results[i] = gjson.GetBytes(record, kv.Key).String()
|
|
|
|
}
|
|
|
|
rowCh <- Row{
|
2018-12-07 17:55:32 -05:00
|
|
|
record: strings.Join(results, f.OutputFieldDelimiter()) + f.OutputRecordDelimiter(),
|
2018-10-22 15:12:22 -04:00
|
|
|
}
|
|
|
|
case format.JSON:
|
2018-11-14 18:55:10 -05:00
|
|
|
rowCh <- Row{
|
2018-12-07 17:55:32 -05:00
|
|
|
record: string(record) + f.OutputRecordDelimiter(),
|
2018-10-22 15:12:22 -04:00
|
|
|
}
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
} else if alias != "" {
|
|
|
|
// This is for dealing with the case of if we have to deal with a
|
|
|
|
// request for a column with an index e.g A_1.
|
2018-10-22 15:12:22 -04:00
|
|
|
if format.IsInt(reqColNames[0]) {
|
2018-08-15 06:30:19 -04:00
|
|
|
// This checks whether any aggregation function was called as now we
|
2018-10-22 15:12:22 -04:00
|
|
|
// no longer will go through printing each row, and only print at the end
|
2018-11-14 18:55:10 -05:00
|
|
|
if len(fnNames) > 0 && fnNames[0] != "" {
|
2018-08-15 06:30:19 -04:00
|
|
|
functionFlag = true
|
2018-11-14 18:55:10 -05:00
|
|
|
aggregationFns(counter, filtrCount, aggVals, reqColNames, fnNames, record)
|
2018-08-15 06:30:19 -04:00
|
|
|
} else {
|
|
|
|
// The code below finds the appropriate columns of the row given the
|
2018-11-14 18:55:10 -05:00
|
|
|
// indicies provided in the SQL request.
|
|
|
|
var rowStr string
|
|
|
|
rowStr, err = processColNameIndex(record, reqColNames, f)
|
|
|
|
if err != nil {
|
|
|
|
rowCh <- Row{
|
|
|
|
err: err,
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
rowCh <- Row{
|
|
|
|
record: rowStr + "\n",
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// This code does aggregation if we were provided column names in the
|
2018-11-14 18:55:10 -05:00
|
|
|
// form of actual names rather an indices.
|
|
|
|
if len(fnNames) > 0 && fnNames[0] != "" {
|
2018-08-15 06:30:19 -04:00
|
|
|
functionFlag = true
|
2018-11-14 18:55:10 -05:00
|
|
|
aggregationFns(counter, filtrCount, aggVals, reqColNames, fnNames, record)
|
2018-08-15 06:30:19 -04:00
|
|
|
} else {
|
|
|
|
// This code prints the appropriate part of the row given the filter
|
|
|
|
// and select request, if the select request was based on column
|
|
|
|
// names rather than indices.
|
2018-11-14 18:55:10 -05:00
|
|
|
var rowStr string
|
|
|
|
rowStr, err = processColNameLiteral(record, reqColNames, fn, f)
|
|
|
|
if err != nil {
|
|
|
|
rowCh <- Row{
|
|
|
|
err: err,
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
rowCh <- Row{
|
|
|
|
record: rowStr + "\n",
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
filtrCount++
|
|
|
|
}
|
|
|
|
counter++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// processColumnNames is a function which allows for cleaning of column names.
|
2018-10-22 15:12:22 -04:00
|
|
|
func processColumnNames(reqColNames []string, alias string, f format.Select) error {
|
|
|
|
switch f.Type() {
|
|
|
|
case format.CSV:
|
2018-11-14 18:55:10 -05:00
|
|
|
for i := range reqColNames {
|
2018-10-22 15:12:22 -04:00
|
|
|
// The code below basically cleans the column name of its alias and other
|
|
|
|
// syntax, so that we can extract its pure name.
|
|
|
|
reqColNames[i] = cleanCol(reqColNames[i], alias)
|
|
|
|
}
|
|
|
|
case format.JSON:
|
|
|
|
// JSON doesnt have columns so no cleaning required
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-10-22 15:12:22 -04:00
|
|
|
|
2018-08-15 06:30:19 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-14 18:55:10 -05:00
|
|
|
// processColNameIndex is the function which creates the row for an index based query.
|
|
|
|
func processColNameIndex(record []byte, reqColNames []string, f format.Select) (string, error) {
|
|
|
|
var row []string
|
|
|
|
for _, colName := range reqColNames {
|
2018-08-15 06:30:19 -04:00
|
|
|
// COALESCE AND NULLIF do not support index based access.
|
|
|
|
if reqColNames[0] == "0" {
|
2018-10-22 15:12:22 -04:00
|
|
|
return "", format.ErrInvalidColumnIndex
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
cindex, err := strconv.Atoi(colName)
|
2018-10-09 17:02:19 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", ErrMissingHeaders
|
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
if cindex > len(f.Header()) {
|
|
|
|
return "", format.ErrInvalidColumnIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subtract 1 because SELECT indexing is not 0 based, it
|
|
|
|
// starts at 1 generating the key like "_1".
|
|
|
|
row = append(row, gjson.GetBytes(record, string("_"+strconv.Itoa(cindex-1))).String())
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-10-22 15:12:22 -04:00
|
|
|
rowStr := strings.Join(row, f.OutputFieldDelimiter())
|
|
|
|
if len(rowStr) > MaxCharsPerRecord {
|
2018-08-15 06:30:19 -04:00
|
|
|
return "", ErrOverMaxRecordSize
|
|
|
|
}
|
2018-10-09 17:02:19 -04:00
|
|
|
return rowStr, nil
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
|
2018-11-14 18:55:10 -05:00
|
|
|
// processColNameLiteral is the function which creates the row for an name based query.
|
|
|
|
func processColNameLiteral(record []byte, reqColNames []string, fn SelectFuncs, f format.Select) (string, error) {
|
2018-10-09 17:02:19 -04:00
|
|
|
row := make([]string, len(reqColNames))
|
2018-11-14 18:55:10 -05:00
|
|
|
for i, colName := range reqColNames {
|
2018-08-15 06:30:19 -04:00
|
|
|
// this is the case to deal with COALESCE.
|
2018-11-14 18:55:10 -05:00
|
|
|
if colName == "" && isValidFunc(fn.index, i) {
|
|
|
|
row[i] = evaluateFuncExpr(fn.funcExpr[i], "", record)
|
2018-08-15 06:30:19 -04:00
|
|
|
continue
|
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
row[i] = gjson.GetBytes(record, colName).String()
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-10-22 15:12:22 -04:00
|
|
|
rowStr := strings.Join(row, f.OutputFieldDelimiter())
|
|
|
|
if len(rowStr) > MaxCharsPerRecord {
|
2018-08-15 06:30:19 -04:00
|
|
|
return "", ErrOverMaxRecordSize
|
|
|
|
}
|
2018-10-09 17:02:19 -04:00
|
|
|
return rowStr, nil
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
|
2018-11-14 18:55:10 -05:00
|
|
|
// aggregationFns is a function which performs the actual aggregation
|
2018-10-22 15:12:22 -04:00
|
|
|
// methods on the given row, it uses an array defined in the main parsing
|
|
|
|
// function to keep track of values.
|
2018-11-14 18:55:10 -05:00
|
|
|
func aggregationFns(counter int, filtrCount int, aggVals []float64, storeReqCols []string, storeFns []string, record []byte) error {
|
|
|
|
for i, storeFn := range storeFns {
|
|
|
|
switch storeFn {
|
|
|
|
case "":
|
|
|
|
continue
|
|
|
|
case "count":
|
|
|
|
aggVals[i]++
|
|
|
|
default:
|
|
|
|
// Column names are provided as an index it'll use
|
|
|
|
// this if statement instead.
|
2018-08-15 06:30:19 -04:00
|
|
|
var convAggFloat float64
|
2018-10-22 15:12:22 -04:00
|
|
|
if format.IsInt(storeReqCols[i]) {
|
2018-11-14 18:55:10 -05:00
|
|
|
index, _ := strconv.Atoi(storeReqCols[i])
|
|
|
|
convAggFloat = gjson.GetBytes(record, "_"+strconv.Itoa(index)).Float()
|
2018-08-15 06:30:19 -04:00
|
|
|
} else {
|
2018-11-14 18:55:10 -05:00
|
|
|
// Named columns rather than indices.
|
|
|
|
convAggFloat = gjson.GetBytes(record, storeReqCols[i]).Float()
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
switch storeFn {
|
|
|
|
case "min":
|
2018-08-15 06:30:19 -04:00
|
|
|
if counter == -1 {
|
2018-11-14 18:55:10 -05:00
|
|
|
aggVals[i] = math.MaxFloat64
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
if convAggFloat < aggVals[i] {
|
|
|
|
aggVals[i] = convAggFloat
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
case "max":
|
|
|
|
// Calculate the max.
|
2018-08-15 06:30:19 -04:00
|
|
|
if counter == -1 {
|
2018-11-14 18:55:10 -05:00
|
|
|
aggVals[i] = math.SmallestNonzeroFloat64
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
if convAggFloat > aggVals[i] {
|
|
|
|
aggVals[i] = convAggFloat
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
case "sum":
|
|
|
|
// Calculate the sum.
|
|
|
|
aggVals[i] += convAggFloat
|
|
|
|
case "avg":
|
|
|
|
// Calculating the average.
|
2018-08-15 06:30:19 -04:00
|
|
|
if filtrCount == 0 {
|
2018-11-14 18:55:10 -05:00
|
|
|
aggVals[i] = convAggFloat
|
2018-08-15 06:30:19 -04:00
|
|
|
} else {
|
2018-11-14 18:55:10 -05:00
|
|
|
aggVals[i] = (convAggFloat + (aggVals[i] * float64(filtrCount))) / float64((filtrCount + 1))
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
default:
|
2018-08-15 06:30:19 -04:00
|
|
|
return ErrParseNonUnaryAgregateFunctionCall
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|