Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion cel/folding.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func evaluateExpr(ctx *OptimizerContext, a *ast.AST, navigableExpr ast.Navigable
}
prg, err := ctx.Program(subAST)
if err != nil {
return nil, err
return nil, errCannotFold
}
// Folding will not attempt to call async functions which are all marked as late-bound,
// but the presence of such functions requires the use of `ConcurrentEval` in order to
Expand Down Expand Up @@ -244,6 +244,26 @@ func maybePruneBranches(ctx *OptimizerContext, a *ast.AST, expr ast.NavigableExp
}
}
}
case operators.Add:
if len(args) == 2 && args[0].Kind() == ast.ListKind && args[1].Kind() == ast.ListKind {
leftList := args[0].AsList()
rightList := args[1].AsList()

elems := make([]ast.Expr, 0, leftList.Size()+rightList.Size())
elems = append(elems, leftList.Elements()...)
elems = append(elems, rightList.Elements()...)

optIndices := make([]int32, 0, len(leftList.OptionalIndices())+len(rightList.OptionalIndices()))
optIndices = append(optIndices, leftList.OptionalIndices()...)
offset := int32(leftList.Size())
for _, idx := range rightList.OptionalIndices() {
optIndices = append(optIndices, offset+idx)
}
Comment thread
TristonianJones marked this conversation as resolved.

combinedList := ctx.NewList(elems, optIndices)
ctx.UpdateExpr(expr, combinedList)
return true
}
}
return false
}
Expand Down Expand Up @@ -630,6 +650,11 @@ func constantCallMatcher(e ast.NavigableExpr) bool {
}
}
}
if fnName == operators.Add {
if len(children) == 2 && children[0].Kind() == ast.ListKind && children[1].Kind() == ast.ListKind {
return true
}
}
// convert all other calls with constant arguments
for _, child := range children {
if !constantMatcher(child) {
Expand Down
34 changes: 33 additions & 1 deletion cel/folding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,38 @@ func TestConstantFoldingOptimizer(t *testing.T) {
expr: `[1, 1 + 2, 1 + (2 + 3)]`,
folded: `[1, 3, 6]`,
},
{
expr: `[1, 2] + [3, 4]`,
folded: `[1, 2, 3, 4]`,
},
{
expr: `[1, ?optional.of(2)] + [3, 4]`,
folded: `[1, 2, 3, 4]`,
},
{
expr: `[1, ?optional.none()] + [2]`,
folded: `[1, 2]`,
},
{
expr: `[x, 1] + [2, y]`,
folded: `[x, 1, 2, y]`,
},
{
expr: `[x, ?optional.of(1)] + [?optional.of(2), y]`,
folded: `[x, 1, 2, y]`,
},
{
expr: `[1] + [x] + [2]`,
folded: `[1, x, 2]`,
},
{
expr: `[1] + [?x] + [2]`,
folded: `[1, ?x, 2]`,
},
{
expr: `[?x, 1] + [2, ?y]`,
folded: `[?x, 1, 2, ?y]`,
},
{
expr: `6 in [1, 1 + 2, 1 + (2 + 3)]`,
folded: `true`,
Expand Down Expand Up @@ -516,7 +548,7 @@ func TestConstantFoldingOptimizer(t *testing.T) {
},
{
expr: `[1] + [x]`,
folded: `[1] + [x]`,
folded: `[1, x]`,
},

{
Expand Down
Loading