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
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,16 @@
*
* <p>Dynamic or trailing indices and non-protobuf selects pass through untouched.
*
* <p><b>Optimizer Ordering:</b> Run {@code SelectOptimizer} last, after {@link
* ConstantFoldingOptimizer}, {@link InliningOptimizer}, and {@link SubexpressionOptimizer}, which
* operate on unoptimized {@code select} nodes.
*
* <p><b>Limitations:</b>
*
* <ul>
* <li><b>Protobuf Extensions:</b> Extension fields are not rename-resilient; runtime lookup
* resolves extensions by their fully qualified name rather than field number.
* resolves extensions by their fully qualified name rather than field number, and {@code
* CelLiteRuntime} does not support them.
* <li><b>Dynamic & Unpacked Payloads:</b> When traversing values typed as {@code dyn}, unpacked
* from {@code google.protobuf.Any}, or evaluated via classless wire payloads ({@code
* RawProtoMessageLiteValue}), no cross-check between the embedded field number and field name
Expand All @@ -150,6 +155,11 @@
* Any} field, whose packed type is only known at runtime. Fields selected past it ({@code
* msg.any_field.foo}) are resolved by name, so they are not rename-resilient and {@code
* CelLiteRuntime} needs the packed message's {@code CelLiteDescriptor}.
* <li><b>Message Creation:</b> Message literals ({@code Msg{field: value}}) are resolved by field
* name at runtime, so they are not rename-resilient. {@code CelLiteRuntime} only supports
* empty literals ({@code Msg{}}), and requires the message's {@code CelLiteDescriptor}.
* <li><b>JSON Field Names:</b> Selecting protobuf fields by {@code json_name} (e.g. {@code
* CelOptions.enableJsonFieldNames(true)}) is unsupported and throws during optimization.
* <li><b>Oneofs:</b> The lite runtime reads fields from the wire, so it reports every serialized
* member of a oneof as present. Protobuf serializes at most one, unless an older-schema
* binary merges or mutates the message, or the bytes are concatenated or altered by hand.
Expand Down Expand Up @@ -265,6 +275,10 @@ public OptimizationResult optimize(CelAbstractSyntaxTree ast, Cel cel) {
checkNotNull(ast);
checkNotNull(cel);
checkArgument(ast.isChecked(), "AST must be type-checked.");
if (ast.getSource().getExtensions().stream().anyMatch(e -> e.id().equals("json_name"))) {
throw new UnsupportedOperationException(
"Optimization of json_name field selection is unsupported.");
}

CelMutableAst astToModify = CelMutableAst.fromCelAst(ast);
CelNavigableMutableAst navAst = CelNavigableMutableAst.fromAst(astToModify);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,31 @@ public void optimize_groupField_throwsUnsupportedOperationException(
assertThat(e).hasMessageThat().contains("Optimization of Group fields is unsupported");
}

@Test
public void optimize_jsonFieldNames_throwsUnsupportedOperationException(
@TestParameter({
"msg.singleInt64",
"has(msg.singleInt64)",
"msg.?singleInt64",
"msg.singleNestedMessage.bb"
})
String expression)
throws Exception {
Cel jsonCel =
cel.toCelBuilder()
.setOptions(CEL_OPTIONS.toBuilder().enableJsonFieldNames(true).build())
.build();
CelAbstractSyntaxTree ast = jsonCel.compile(expression).getAst();
SelectOptimizer optimizer = SelectOptimizer.newInstance();

UnsupportedOperationException e =
assertThrows(UnsupportedOperationException.class, () -> optimizer.optimize(ast, jsonCel));

assertThat(e)
.hasMessageThat()
.isEqualTo("Optimization of json_name field selection is unsupported.");
}

@Test
public void newInstance_fileDescriptorsVarargs_defaultOptions_success() throws Exception {
FileDescriptor fd = PROTO2_TEST_ALL_TYPES_DESCRIPTOR.getFile();
Expand Down Expand Up @@ -2093,6 +2118,30 @@ public void optimize_afterConstantFolding_preservesMacroCalls() throws Exception
+ " [2, \"single_int64\", 3]], -64) + 2)");
}

@Test
public void optimize_afterSubexpressionOptimizer_rewritesBlockAndBody() throws Exception {
CelOptimizer optimizer =
CelOptimizerFactory.standardCelOptimizerBuilder(cel)
.addAstOptimizers(
SubexpressionOptimizer.getInstance(),
SelectOptimizer.newInstance(
PROTO2_TEST_ALL_TYPES_DESCRIPTOR.getFile(),
NestedTestAllTypes.getDescriptor().getFile()))
.build();
CelAbstractSyntaxTree ast =
cel.compile("nested_msg.child.payload.single_int32 + nested_msg.child.payload.single_int64")
.getAst();

CelAbstractSyntaxTree optimizedAst = optimizer.optimize(ast);

assertThat(CEL_UNPARSER.unparse(optimizedAst))
.isEqualTo(
"cel.@block([cel.@attribute(nested_msg, [[1, \"child\", 11], [2, \"payload\", 11]],"
+ " cel.expr.conformance.proto2.TestAllTypes{})],"
+ " cel.@attribute(@index0, [[1, \"single_int32\", 5]], -32)"
+ " + cel.@attribute(@index0, [[2, \"single_int64\", 3]], -64))");
}

private static CelOptimizer newSelectOptimizer(Cel cel) {
return CelOptimizerFactory.standardCelOptimizerBuilder(cel)
.addAstOptimizers(
Expand Down
Loading