diff --git a/common/src/main/java/dev/cel/common/values/SelectField.java b/common/src/main/java/dev/cel/common/values/SelectField.java index e94c2d817..37e708de1 100644 --- a/common/src/main/java/dev/cel/common/values/SelectField.java +++ b/common/src/main/java/dev/cel/common/values/SelectField.java @@ -39,7 +39,7 @@ public abstract class SelectField { private static final int MAX_FIELD_NUMBER = 536870911; /** CEL-specific type code used to encode CEL maps on the wire. */ - public static final int CEL_MAP_TYPE_CODE = -1; + public static final int CEL_MAP_TYPE_CODE = -10; /** Sentinel for a presence-test qualifier, whose 2-tuple carries no type code. */ static final int NO_TYPE_CODE = 0; diff --git a/common/src/test/java/dev/cel/common/values/SelectFieldTest.java b/common/src/test/java/dev/cel/common/values/SelectFieldTest.java index b725300cf..861baa3a7 100644 --- a/common/src/test/java/dev/cel/common/values/SelectFieldTest.java +++ b/common/src/test/java/dev/cel/common/values/SelectFieldTest.java @@ -227,7 +227,7 @@ public void mapEntrySpec_validKeyTypeCodes_success( @Test public void mapEntrySpec_invalidKeyTypeCode_throwsIllegalArgumentException( @TestParameter({ - "-1", // CEL_MAP_TYPE_CODE + "-10", // CEL_MAP_TYPE_CODE "0", // NO_TYPE_CODE "1", // DOUBLE "2", // FLOAT @@ -245,7 +245,7 @@ public void mapEntrySpec_invalidKeyTypeCode_throwsIllegalArgumentException( @Test public void mapEntrySpec_invalidValueTypeCode_throwsIllegalArgumentException( @TestParameter({ - "-1", // CEL_MAP_TYPE_CODE + "-10", // CEL_MAP_TYPE_CODE "0", // NO_TYPE_CODE "10", // GROUP "19" // OUT_OF_BOUNDS diff --git a/optimizer/src/main/java/dev/cel/optimizer/optimizers/SelectOptimizer.java b/optimizer/src/main/java/dev/cel/optimizer/optimizers/SelectOptimizer.java index 79dfb8fed..203ba2a26 100644 --- a/optimizer/src/main/java/dev/cel/optimizer/optimizers/SelectOptimizer.java +++ b/optimizer/src/main/java/dev/cel/optimizer/optimizers/SelectOptimizer.java @@ -97,7 +97,7 @@ *
The 3rd argument of {@code cel.@attribute} is a typed expression that binds the result type @@ -158,7 +158,7 @@ public final class SelectOptimizer implements CelAstOptimizer { *
Protobuf wire format encodes maps as repeated message entries ({@code MapEntry}). To avoid * wire-decoding ambiguities with singular submessages, maps use this dedicated type code. */ - private static final long CEL_MAP_TYPE_CODE = -1L; + private static final long CEL_MAP_TYPE_CODE = -10L; private static final TypeParamType TYPE_PARAM_T = TypeParamType.create("T"); diff --git a/optimizer/src/test/java/dev/cel/optimizer/optimizers/SelectOptimizerTest.java b/optimizer/src/test/java/dev/cel/optimizer/optimizers/SelectOptimizerTest.java index 10c2fad43..b84e7f6d6 100644 --- a/optimizer/src/test/java/dev/cel/optimizer/optimizers/SelectOptimizerTest.java +++ b/optimizer/src/test/java/dev/cel/optimizer/optimizers/SelectOptimizerTest.java @@ -325,49 +325,49 @@ private enum RewriteTestCase { // Map selects MAP_STRING_STRING( "msg.map_string_string", - "cel.@attribute(msg, [[61, \"map_string_string\", -1, [9, 9]]], {\"\": \"\"})"), + "cel.@attribute(msg, [[61, \"map_string_string\", -10, [9, 9]]], {\"\": \"\"})"), MAP_BOOL_STRING( "msg.map_bool_string", - "cel.@attribute(msg, [[64, \"map_bool_string\", -1, [8, 9]]], {false: \"\"})"), + "cel.@attribute(msg, [[64, \"map_bool_string\", -10, [8, 9]]], {false: \"\"})"), MAP_UINT32_DOUBLE( "msg.map_uint32_double", - "cel.@attribute(msg, [[204, \"map_uint32_double\", -1, [13, 1]]], {0u: 0.0})"), + "cel.@attribute(msg, [[204, \"map_uint32_double\", -10, [13, 1]]], {0u: 0.0})"), MAP_INT64_ENUM( "msg.map_int64_enum", - "cel.@attribute(msg, [[94, \"map_int64_enum\", -1, [3, 14]]], {0: 0})"), + "cel.@attribute(msg, [[94, \"map_int64_enum\", -10, [3, 14]]], {0: 0})"), MAP_STRING_DURATION( "msg.map_string_duration", - "cel.@attribute(msg, [[243, \"map_string_duration\", -1, [9, 11]]]," + "cel.@attribute(msg, [[243, \"map_string_duration\", -10, [9, 11]]]," + " {\"\": google.protobuf.Duration{}})"), MAP_FIELD_INDEXING( "msg.map_int64_message[1].bb", - "cel.@attribute(msg, [[95, \"map_int64_message\", -1, [3, 11]], 1, [1, \"bb\", 5]], 0)"), + "cel.@attribute(msg, [[95, \"map_int64_message\", -10, [3, 11]], 1, [1, \"bb\", 5]], 0)"), REPEATED_FIELD_INDEXING( "msg.repeated_nested_message[0].bb", "cel.@attribute(msg, [[51, \"repeated_nested_message\", 11], 0, [1, \"bb\", 5]], 0)"), MAP_AND_REPEATED_FIELD_INDEXING( "msg.map_int64_nested_type[1].child.payload.repeated_nested_message[0].bb", - "cel.@attribute(msg, [[62, \"map_int64_nested_type\", -1, [3, 11]], 1, " + "cel.@attribute(msg, [[62, \"map_int64_nested_type\", -10, [3, 11]], 1, " + "[1, \"child\", 11], [2, \"payload\", 11], " + "[51, \"repeated_nested_message\", 11], 0, [1, \"bb\", 5]], 0)"), MAP_FIELD_UINT_KEY_INDEXING( "msg.map_uint64_message[1u].bb", - "cel.@attribute(msg, [[217, \"map_uint64_message\", -1, [4, 11]], 1u, " + "cel.@attribute(msg, [[217, \"map_uint64_message\", -10, [4, 11]], 1u, " + "[1, \"bb\", 5]], 0)"), MAP_FIELD_BOOL_KEY_INDEXING( "msg.map_bool_message[true].bb", - "cel.@attribute(msg, [[73, \"map_bool_message\", -1, [8, 11]], true, " + "cel.@attribute(msg, [[73, \"map_bool_message\", -10, [8, 11]], true, " + "[1, \"bb\", 5]], 0)"), REPEATED_FIELD_HAS_THROUGH_INDEX( "has(msg.repeated_nested_message[0].bb)", "cel.@hasField(msg, [[51, \"repeated_nested_message\", 11], 0, [1, \"bb\"]])"), MAP_FIELD_HAS_THROUGH_INDEX( "has(msg.map_int64_nested_type[1].child.payload.single_int64)", - "cel.@hasField(msg, [[62, \"map_int64_nested_type\", -1, [3, 11]], 1, " + "cel.@hasField(msg, [[62, \"map_int64_nested_type\", -10, [3, 11]], 1, " + "[1, \"child\"], [2, \"payload\"], [2, \"single_int64\"]])"), HAS_THROUGH_TWO_INDICES( "has(msg.map_int64_nested_type[1].child.payload.repeated_nested_message[0].bb)", - "cel.@hasField(msg, [[62, \"map_int64_nested_type\", -1, [3, 11]], 1, " + "cel.@hasField(msg, [[62, \"map_int64_nested_type\", -10, [3, 11]], 1, " + "[1, \"child\", 11], [2, \"payload\", 11], " + "[51, \"repeated_nested_message\", 11], 0, [1, \"bb\"]])"), REPEATED_FIELD_DYNAMIC_INDEX_STOPS_AT_INDEX( @@ -404,18 +404,18 @@ private enum RewriteTestCase { PROTO_MAP_FIELD_SELECT_STOPS_AT_MAP_BOUNDARY( "msg.map_string_message.key.bb", "cel.@attribute(" - + "cel.@attribute(msg, [[227, \"map_string_message\", -1, [9, 11]]]," + + "cel.@attribute(msg, [[227, \"map_string_message\", -10, [9, 11]]]," + " {\"\": cel.expr.conformance.proto3.TestAllTypes.NestedMessage{}}).key, " + "[[1, \"bb\", 5]], 0)"), PROTO_MAP_FIELD_HAS_STOPS_AT_MAP_BOUNDARY( "has(msg.map_string_message.key.bb)", "cel.@hasField(" - + "cel.@attribute(msg, [[227, \"map_string_message\", -1, [9, 11]]]," + + "cel.@attribute(msg, [[227, \"map_string_message\", -10, [9, 11]]]," + " {\"\": cel.expr.conformance.proto3.TestAllTypes.NestedMessage{}}).key, " + "[[1, \"bb\"]])"), PROTO_MAP_FIELD_SELECT_STOPS_AT_ANY_BOUNDARY( "msg.map_string_any['k'].value", - "cel.@attribute(msg, [[311, \"map_string_any\", -1, [9, 11]]]," + "cel.@attribute(msg, [[311, \"map_string_any\", -10, [9, 11]]]," + " {\"\": google.protobuf.Any{}})[\"k\"].value"), ANY_FIELD( "msg.single_any", @@ -561,7 +561,7 @@ public void optimize_withFileDescriptorsIterable_success() throws Exception { + " cel.@attribute(msg, [[51, \"repeated_nested_message\", 11]]," + " [.cel.expr.conformance.proto3.TestAllTypes.NestedMessage{}]))'}", "{expression: '[[1]].map(cel, msg.map_int64_message)', expected: '[[1]].map(cel," - + " cel.@attribute(msg, [[95, \"map_int64_message\", -1, [3, 11]]]," + + " cel.@attribute(msg, [[95, \"map_int64_message\", -10, [3, 11]]]," + " {0: .cel.expr.conformance.proto3.TestAllTypes.NestedMessage{}}))'}" }) public void optimize_messageNameUnderShadowingComprehensionVar_rewrites( @@ -681,7 +681,7 @@ public void optimize_messageNameInBindInit_rewrites() throws Exception { + " cel.@attribute(msg, [[102, \"single_timestamp\", 11]]," + " .google.protobuf.Timestamp{}))'}", "{expression: '[1].map(duration, msg.map_string_duration)', expected: '[1].map(duration," - + " cel.@attribute(msg, [[243, \"map_string_duration\", -1, [9, 11]]]," + + " cel.@attribute(msg, [[243, \"map_string_duration\", -10, [9, 11]]]," + " {\"\": .google.protobuf.Duration{}}))'}" }) public void optimize_durationOrTimestampUnderShadowingComprehensionVar_rewrites( @@ -1724,7 +1724,7 @@ public void optimize_optionalMapKeySelection_rewritesMapField() throws Exception assertThat(CEL_UNPARSER.unparse(optimizedAst)) .isEqualTo( - "cel.@attribute(msg, [[61, \"map_string_string\", -1, [9, 9]]], {\"\": \"\"}).?key"); + "cel.@attribute(msg, [[61, \"map_string_string\", -10, [9, 9]]], {\"\": \"\"}).?key"); } @Test diff --git a/runtime/src/test/java/dev/cel/runtime/planner/OptimizedSelectPlannerTest.java b/runtime/src/test/java/dev/cel/runtime/planner/OptimizedSelectPlannerTest.java index 06e19d303..78a817da2 100644 --- a/runtime/src/test/java/dev/cel/runtime/planner/OptimizedSelectPlannerTest.java +++ b/runtime/src/test/java/dev/cel/runtime/planner/OptimizedSelectPlannerTest.java @@ -611,7 +611,7 @@ public void plan_invalidAst_mapHopWithMissingEntrySpec_throwsEvaluationException CelExpr.ofConstant(5L, CelConstant.ofValue(1L)), CelExpr.ofConstant( 6L, CelConstant.ofValue("map_string_string")), - CelExpr.ofConstant(7L, CelConstant.ofValue(-1L))), + CelExpr.ofConstant(7L, CelConstant.ofValue(-10L))), ImmutableList.of())), ImmutableList.of()), CelExpr.ofMap( @@ -648,7 +648,7 @@ public void plan_invalidAst_mapEntrySpecMalformed_throwsEvaluationException() { CelExpr.ofConstant(5L, CelConstant.ofValue(1L)), CelExpr.ofConstant( 6L, CelConstant.ofValue("map_string_string")), - CelExpr.ofConstant(7L, CelConstant.ofValue(-1L)), + CelExpr.ofConstant(7L, CelConstant.ofValue(-10L)), CelExpr.ofList( 8L, ImmutableList.of( @@ -715,7 +715,7 @@ private enum ScalarDummyMismatchTestCase { ImmutableList.of( CelExpr.ofConstant(5L, CelConstant.ofValue(1L)), CelExpr.ofConstant(6L, CelConstant.ofValue("map_string_string")), - CelExpr.ofConstant(7L, CelConstant.ofValue(-1L)), + CelExpr.ofConstant(7L, CelConstant.ofValue(-10L)), mapEntrySpec(9L, 9L)), CelExpr.ofMap( 20L, @@ -729,7 +729,7 @@ private enum ScalarDummyMismatchTestCase { ImmutableList.of( CelExpr.ofConstant(5L, CelConstant.ofValue(1L)), CelExpr.ofConstant(6L, CelConstant.ofValue("map_string_string")), - CelExpr.ofConstant(7L, CelConstant.ofValue(-1L)), + CelExpr.ofConstant(7L, CelConstant.ofValue(-10L)), mapEntrySpec(9L, 9L)), CelExpr.ofMap( 20L, @@ -800,7 +800,7 @@ public void plan_invalidAst_mapFieldWithEmptyMapDummy_throwsEvaluationException( CelExpr.ofConstant(5L, CelConstant.ofValue(1L)), CelExpr.ofConstant( 6L, CelConstant.ofValue("map_string_string")), - CelExpr.ofConstant(7L, CelConstant.ofValue(-1L)), + CelExpr.ofConstant(7L, CelConstant.ofValue(-10L)), CelExpr.ofList( 8L, ImmutableList.of( @@ -934,7 +934,7 @@ private static CelExpr mapHop( ImmutableList.of( CelExpr.ofConstant(0L, CelConstant.ofValue(fieldNumber)), CelExpr.ofConstant(0L, CelConstant.ofValue(fieldName)), - CelExpr.ofConstant(0L, CelConstant.ofValue(-1L)), + CelExpr.ofConstant(0L, CelConstant.ofValue(-10L)), mapEntrySpec(keyTypeCode, valueTypeCode)), ImmutableList.of()); }