mirror of
https://github.com/pikami/cosmium.git
synced 2026-05-15 17:47:10 +01:00
intermediary patch
This commit is contained in:
@@ -186,4 +186,55 @@ func Test_Parse(t *testing.T) {
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse SELECT with UDF function", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT t.name, udf.CalculateTax(t.income, t.category) FROM t`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
testutils.SelectItem_Path("t", "name"),
|
||||
{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallUDF,
|
||||
UdfName: "CalculateTax",
|
||||
Arguments: []interface{}{
|
||||
testutils.SelectItem_Path("t", "income"),
|
||||
testutils.SelectItem_Path("t", "category"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("t")},
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Should parse WHERE with UDF function", func(t *testing.T) {
|
||||
testQueryParse(
|
||||
t,
|
||||
`SELECT c.id FROM c WHERE udf.IsEligible(c.status) = true`,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
testutils.SelectItem_Path("c", "id"),
|
||||
},
|
||||
Table: parsers.Table{SelectItem: testutils.SelectItem_Path("c")},
|
||||
Filters: parsers.ComparisonExpression{
|
||||
Left: parsers.SelectItem{
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallUDF,
|
||||
UdfName: "IsEligible",
|
||||
Arguments: []interface{}{
|
||||
testutils.SelectItem_Path("c", "status"),
|
||||
},
|
||||
},
|
||||
},
|
||||
Operation: "=",
|
||||
Right: testutils.SelectItem_Constant_Bool(true),
|
||||
},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
+2002
-1819
File diff suppressed because it is too large
Load Diff
+24
-1
@@ -154,6 +154,14 @@ func createFunctionCall(functionType parsers.FunctionCallType, arguments []inter
|
||||
return parsers.FunctionCall{Type: functionType, Arguments: arguments}, nil
|
||||
}
|
||||
|
||||
func createUDFCall(functionName interface{}, arguments []interface{}) (parsers.FunctionCall, error) {
|
||||
return parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallUDF,
|
||||
UdfName: functionName.(string),
|
||||
Arguments: arguments,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func joinStrings(array []interface{}) string {
|
||||
var stringsArray []string
|
||||
for _, elem := range array {
|
||||
@@ -511,7 +519,8 @@ BooleanLiteral <- ("true"i / "false"i) {
|
||||
return parsers.Constant{Type: parsers.ConstantTypeBoolean, Value: boolValue}, nil
|
||||
}
|
||||
|
||||
FunctionCall <- StringFunctions
|
||||
FunctionCall <- UDFFunction
|
||||
/ StringFunctions
|
||||
/ TypeCheckingFunctions
|
||||
/ ArrayFunctions
|
||||
/ ConditionalFunctions
|
||||
@@ -519,6 +528,20 @@ FunctionCall <- StringFunctions
|
||||
/ AggregateFunctions
|
||||
/ MathFunctions
|
||||
|
||||
UDFFunction <- "udf"i ws "." ws functionName:Identifier ws "(" ws arguments:UDFArgumentList? ws ")" {
|
||||
if arguments == nil {
|
||||
return createUDFCall(functionName, []interface{}{})
|
||||
}
|
||||
return createUDFCall(functionName, arguments.([]interface{}))
|
||||
}
|
||||
|
||||
UDFArgumentList <- arg1:SelectItem others:(ws "," ws arg:SelectItem { return arg, nil })* {
|
||||
if others == nil {
|
||||
return []interface{}{arg1}, nil
|
||||
}
|
||||
return append([]interface{}{arg1}, others.([]interface{})...), nil
|
||||
}
|
||||
|
||||
StringFunctions <- StringEqualsExpression
|
||||
/ ToStringExpression
|
||||
/ ConcatExpression
|
||||
|
||||
Reference in New Issue
Block a user