From 99b21b8365dba608742a759972369ab421b18d68 Mon Sep 17 00:00:00 2001 From: TristonianJones Date: Thu, 6 Aug 2026 18:47:06 -0700 Subject: [PATCH 1/2] Fold list concat expressions together --- cel/folding.go | 27 ++++++++++++++++++++++++++- cel/folding_test.go | 14 +++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/cel/folding.go b/cel/folding.go index 5525f080..e3459034 100644 --- a/cel/folding.go +++ b/cel/folding.go @@ -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 @@ -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) + } + + combinedList := ctx.NewList(elems, optIndices) + ctx.UpdateExpr(expr, combinedList) + return true + } } return false } @@ -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) { diff --git a/cel/folding_test.go b/cel/folding_test.go index 56dd789d..06684fce 100644 --- a/cel/folding_test.go +++ b/cel/folding_test.go @@ -45,6 +45,18 @@ 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: `[x, 1] + [2, y]`, + folded: `[x, 1, 2, y]`, + }, + { + expr: `[1] + [x] + [2]`, + folded: `[1, x, 2]`, + }, { expr: `6 in [1, 1 + 2, 1 + (2 + 3)]`, folded: `true`, @@ -516,7 +528,7 @@ func TestConstantFoldingOptimizer(t *testing.T) { }, { expr: `[1] + [x]`, - folded: `[1] + [x]`, + folded: `[1, x]`, }, { From fe8eedb01f81f15dbee991d38a309828f5fbe7df Mon Sep 17 00:00:00 2001 From: TristonianJones Date: Fri, 7 Aug 2026 09:38:13 -0700 Subject: [PATCH 2/2] Test cases for optional tracking --- cel/folding_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/cel/folding_test.go b/cel/folding_test.go index 06684fce..8a292cb7 100644 --- a/cel/folding_test.go +++ b/cel/folding_test.go @@ -49,14 +49,34 @@ func TestConstantFoldingOptimizer(t *testing.T) { 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`,