mirror of
https://github.com/pikami/cosmium.git
synced 2025-12-19 17:00:37 +00:00
Implement AVG, COUNT, MAX, MIN, SUM functions
This commit is contained in:
130
parsers/nosql/aggregate_functions_test.go
Normal file
130
parsers/nosql/aggregate_functions_test.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package nosql_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func Test_Parse_AggregateFunctions(t *testing.T) {
|
||||
|
||||
t.Run("Should parse function AVG()", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT AVG(c.a1) FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateAvg,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "a1"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function COUNT()", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT COUNT(c.a1) FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateCount,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "a1"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function MAX()", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT MAX(c.a1) FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateMax,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "a1"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function MIN()", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT MIN(c.a1) FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateMin,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "a1"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse function SUM()", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT SUM(c.a1) FROM c`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallAggregateSum,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "a1"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -327,6 +327,7 @@ FunctionCall <- StringFunctions
|
||||
/ TypeCheckingFunctions
|
||||
/ ArrayFunctions
|
||||
/ InFunction
|
||||
/ AggregateFunctions
|
||||
|
||||
StringFunctions <- StringEqualsExpression
|
||||
/ ToStringExpression
|
||||
@@ -356,6 +357,12 @@ TypeCheckingFunctions <- IsDefined
|
||||
/ IsPrimitive
|
||||
/ IsString
|
||||
|
||||
AggregateFunctions <- AvgAggregateExpression
|
||||
/ CountAggregateExpression
|
||||
/ MaxAggregateExpression
|
||||
/ MinAggregateExpression
|
||||
/ SumAggregateExpression
|
||||
|
||||
ArrayFunctions <- ArrayConcatExpression
|
||||
/ ArrayLengthExpression
|
||||
/ ArraySliceExpression
|
||||
@@ -509,6 +516,26 @@ InFunction <- ex1:SelectProperty ws "IN"i ws "(" ws ex2:SelectItem others:(ws ",
|
||||
return createFunctionCall(parsers.FunctionCallIn, append([]interface{}{ex1, ex2}, others.([]interface{})...))
|
||||
}
|
||||
|
||||
AvgAggregateExpression <- "AVG"i "(" ws ex:SelectItem ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallAggregateAvg, []interface{}{ex})
|
||||
}
|
||||
|
||||
CountAggregateExpression <- "COUNT"i "(" ws ex:SelectItem ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallAggregateCount, []interface{}{ex})
|
||||
}
|
||||
|
||||
MaxAggregateExpression <- "MAX"i "(" ws ex:SelectItem ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallAggregateMax, []interface{}{ex})
|
||||
}
|
||||
|
||||
MinAggregateExpression <- "MIN"i "(" ws ex:SelectItem ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallAggregateMin, []interface{}{ex})
|
||||
}
|
||||
|
||||
SumAggregateExpression <- "SUM"i "(" ws ex:SelectItem ws ")" {
|
||||
return createFunctionCall(parsers.FunctionCallAggregateSum, []interface{}{ex})
|
||||
}
|
||||
|
||||
Integer <- [0-9]+ {
|
||||
return strconv.Atoi(string(c.text))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user