@@ -167,6 +167,10 @@ private boolean canFold(CelNavigableMutableExpr navigableExpr) {
167167 && cond .constant ().getKind ().equals (CelConstant .Kind .BOOLEAN_VALUE );
168168 }
169169
170+ if (functionName .equals (Operator .EQUALS .getFunction ())) {
171+ return mutableCall .args ().stream ().anyMatch (x -> isExprConstantOfKind (x , CelConstant .Kind .BOOLEAN_VALUE ));
172+ }
173+
170174 if (functionName .equals (Operator .IN .getFunction ())) {
171175 return canFoldInOperator (navigableExpr );
172176 }
@@ -393,6 +397,27 @@ private Optional<CelMutableAst> maybePruneBranches(
393397 }
394398 }
395399 }
400+ } else if (function .equals (Operator .EQUALS .getFunction ())) {
401+ CelMutableExpr lhs = call .args ().get (0 );
402+ CelMutableExpr rhs = call .args ().get (1 );
403+ boolean lhsIsBoolean = isExprConstantOfKind (lhs , CelConstant .Kind .BOOLEAN_VALUE );
404+ boolean rhsIsBoolean = isExprConstantOfKind (rhs , CelConstant .Kind .BOOLEAN_VALUE );
405+ Optional <CelMutableExpr > branchToRetain = Optional .empty ();
406+
407+ if (lhsIsBoolean && rhsIsBoolean ) {
408+ boolean result = lhs .constant ().booleanValue () == rhs .constant ().booleanValue ();
409+ branchToRetain = Optional .of (CelMutableExpr .ofConstant (CelConstant .ofValue (result )));
410+ } else if (lhsIsBoolean ) {
411+ branchToRetain = Optional .of (lhs .constant ().booleanValue () ? rhs : lhs );
412+ } else if (rhsIsBoolean ) {
413+ branchToRetain = Optional .of (rhs .constant ().booleanValue () ? lhs : rhs );
414+ }
415+
416+ return branchToRetain .map (node ->
417+ astMutator .replaceSubtree (
418+ mutableAst ,
419+ node ,
420+ expr .id ()));
396421 }
397422
398423 return Optional .empty ();
@@ -663,6 +688,10 @@ public static Builder newBuilder() {
663688 ConstantFoldingOptions () {}
664689 }
665690
691+ private static boolean isExprConstantOfKind (CelMutableExpr expr , CelConstant .Kind constantKind ) {
692+ return expr .getKind ().equals (Kind .CONSTANT ) && expr .constant ().getKind ().equals (constantKind );
693+ }
694+
666695 private ConstantFoldingOptimizer (ConstantFoldingOptions constantFoldingOptions ) {
667696 this .constantFoldingOptions = constantFoldingOptions ;
668697 this .astMutator = AstMutator .newInstance (constantFoldingOptions .maxIterationLimit ());
0 commit comments