Skip to content

Commit 8072bab

Browse files
committed
Fix const fold
1 parent cf7db21 commit 8072bab

3 files changed

Lines changed: 28 additions & 3 deletions

File tree

optimizer/src/main/java/dev/cel/optimizer/optimizers/ConstantFoldingOptimizer.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private boolean canFold(CelNavigableMutableExpr navigableExpr) {
201201
CelNavigableMutableExpr operand = navigableExpr.children().collect(onlyElement());
202202
return areChildrenArgConstant(operand);
203203
case COMPREHENSION:
204-
return !isNestedComprehension(navigableExpr);
204+
return !isNestedComprehension(navigableExpr) && !containsUnfoldableFunctions(navigableExpr);
205205
default:
206206
return false;
207207
}
@@ -277,6 +277,16 @@ private static boolean isNestedComprehension(CelNavigableMutableExpr expr) {
277277
return false;
278278
}
279279

280+
private boolean containsUnfoldableFunctions(CelNavigableMutableExpr expr) {
281+
return expr.descendants()
282+
.filter(node -> node.expr().getKind().equals(Kind.CALL))
283+
.map(node -> node.expr().call())
284+
.anyMatch(
285+
call ->
286+
!foldableFunctions.contains(call.function())
287+
);
288+
}
289+
280290
private Optional<CelMutableAst> maybeFold(
281291
Cel cel, CelMutableAst mutableAst, CelNavigableMutableExpr node)
282292
throws CelOptimizationException {

optimizer/src/test/java/dev/cel/optimizer/optimizers/ConstantFoldingOptimizerTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
public class ConstantFoldingOptimizerTest {
4949
private static final CelOptions CEL_OPTIONS =
5050
CelOptions.current()
51+
.populateMacroCalls(true)
5152
.enableTimestampEpoch(true)
5253
.build();
5354
private static final Cel CEL =
@@ -56,12 +57,23 @@ public class ConstantFoldingOptimizerTest {
5657
.addVar("y", SimpleType.DYN)
5758
.addVar("list_var", ListType.create(SimpleType.STRING))
5859
.addVar("map_var", MapType.create(SimpleType.STRING, SimpleType.STRING))
60+
.setStandardMacros(CelStandardMacro.STANDARD_MACROS)
5961
.addFunctionDeclarations(
6062
CelFunctionDecl.newFunctionDeclaration(
6163
"get_true",
62-
CelOverloadDecl.newGlobalOverload("get_true_overload", SimpleType.BOOL)))
64+
CelOverloadDecl.newGlobalOverload("get_true_overload", SimpleType.BOOL)),
65+
CelFunctionDecl.newFunctionDeclaration(
66+
"get_list",
67+
CelOverloadDecl.newGlobalOverload(
68+
"get_list_overload",
69+
ListType.create(SimpleType.INT),
70+
ListType.create(SimpleType.INT)))
71+
)
6372
.addFunctionBindings(
64-
CelFunctionBinding.from("get_true_overload", ImmutableList.of(), unused -> true))
73+
CelFunctionBinding.from("get_true_overload", ImmutableList.of(), unused -> true),
74+
CelFunctionBinding.from(
75+
"get_list_overload", ImmutableList.class, arg -> arg)
76+
)
6577
.addMessageTypes(TestAllTypes.getDescriptor())
6678
.setContainer(CelContainer.ofName("cel.expr.conformance.proto3"))
6779
.setOptions(CEL_OPTIONS)
@@ -371,6 +383,8 @@ public void constantFold_macros_withoutMacroCallMetadata(String source) throws E
371383
@TestParameters("{source: 'x == 42'}")
372384
@TestParameters("{source: 'timestamp(100)'}")
373385
@TestParameters("{source: 'duration(\"1h\")'}")
386+
@TestParameters("{source: '[true].exists(x, x == get_true())'}")
387+
@TestParameters("{source: 'get_list([1, 2]).map(x, x * 2)'}")
374388
public void constantFold_noOp(String source) throws Exception {
375389
CelAbstractSyntaxTree ast = CEL.compile(source).getAst();
376390

tools/src/test/java/dev/cel/tools/ai/AgenticPolicyCompilerTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ private void runTests(Cel cel, CelAbstractSyntaxTree ast, PolicyTestSuite testSu
283283
? (List<AgentMessage>) inputMap.get("_test_history")
284284
: ImmutableList.of();
285285

286+
@SuppressWarnings("Immutable")
286287
CelLateFunctionBindings bindings = CelLateFunctionBindings.from(
287288
CelFunctionBinding.from(
288289
"agent_history",

0 commit comments

Comments
 (0)