Skip to content

Commit 18a12b6

Browse files
Stephen Robertscopybara-github
authored andcommitted
Support constant folding in chained SELECT expressions.
PiperOrigin-RevId: 843180459
1 parent 8b4c579 commit 18a12b6

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ private static boolean areChildrenArgConstant(CelNavigableMutableExpr expr) {
256256
if (expr.getKind().equals(Kind.CALL)
257257
|| expr.getKind().equals(Kind.LIST)
258258
|| expr.getKind().equals(Kind.MAP)
259+
|| expr.getKind().equals(Kind.SELECT)
259260
|| expr.getKind().equals(Kind.STRUCT)) {
260261
return expr.children().allMatch(ConstantFoldingOptimizer::areChildrenArgConstant);
261262
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ public class ConstantFoldingOptimizerTest {
170170
+ " optional.of(4), ?single_uint64: optional.ofNonZeroValue(x)}', expected:"
171171
+ " 'cel.expr.conformance.proto3.TestAllTypes{single_int64: 1, single_int32: 4,"
172172
+ " ?single_uint64: optional.ofNonZeroValue(x)}'}")
173+
@TestParameters(
174+
"{source: 'TestAllTypes{single_nested_message: TestAllTypes.NestedMessage{bb:"
175+
+ " 42}}.single_nested_message.bb', expected: '42'}")
176+
@TestParameters("{source: '{\"a\": 1}[\"a\"]', expected: '1'}")
177+
@TestParameters("{source: '{\"a\": {\"b\": 2}}[\"a\"][\"b\"]', expected: '2'}")
173178
@TestParameters("{source: '{\"hello\": \"world\"}.hello == x', expected: '\"world\" == x'}")
174179
@TestParameters("{source: '{\"hello\": \"world\"}[\"hello\"] == x', expected: '\"world\" == x'}")
175180
@TestParameters("{source: '{\"hello\": \"world\"}.?hello', expected: 'optional.of(\"world\")'}")

0 commit comments

Comments
 (0)