Skip to content

Commit f2a69c4

Browse files
l46kokcopybara-github
authored andcommitted
Add iteration limit control to planned comprehensions
PiperOrigin-RevId: 845589739
1 parent 506c2b6 commit f2a69c4

42 files changed

Lines changed: 1436 additions & 424 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

common/exceptions/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,9 @@ java_library(
4040
# used_by_android
4141
exports = ["//common/src/main/java/dev/cel/common/exceptions:invalid_argument"],
4242
)
43+
44+
java_library(
45+
name = "iteration_budget_exceeded",
46+
# used_by_android
47+
exports = ["//common/src/main/java/dev/cel/common/exceptions:iteration_budget_exceeded"],
48+
)

common/src/main/java/dev/cel/common/exceptions/BUILD.bazel

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,16 @@ java_library(
8585
"//common/annotations",
8686
],
8787
)
88+
89+
java_library(
90+
name = "iteration_budget_exceeded",
91+
srcs = ["CelIterationLimitExceededException.java"],
92+
# used_by_android
93+
tags = [
94+
],
95+
deps = [
96+
"//common:error_codes",
97+
"//common:runtime_exception",
98+
"//common/annotations",
99+
],
100+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package dev.cel.common.exceptions;
16+
17+
import dev.cel.common.CelErrorCode;
18+
import dev.cel.common.CelRuntimeException;
19+
import dev.cel.common.annotations.Internal;
20+
import java.util.Locale;
21+
22+
/** Indicates that the iteration budget for a comprehension has been exceeded. */
23+
@Internal
24+
public final class CelIterationLimitExceededException extends CelRuntimeException {
25+
26+
public CelIterationLimitExceededException(int budget) {
27+
super(
28+
String.format(Locale.US, "Iteration budget exceeded: %d", budget),
29+
CelErrorCode.ITERATION_BUDGET_EXCEEDED);
30+
}
31+
}

common/src/main/java/dev/cel/common/values/CelValueConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
@SuppressWarnings("unchecked") // Unchecked cast of generics due to type-erasure (ex: MapValue).
3434
@Internal
3535
@Immutable
36-
abstract class CelValueConverter {
36+
public abstract class CelValueConverter {
3737

3838
/** Adapts a {@link CelValue} to a plain old Java Object. */
3939
public Object unwrap(CelValue celValue) {

runtime/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,11 @@ java_library(
255255
visibility = ["//:internal"],
256256
exports = ["//runtime/src/main/java/dev/cel/runtime:metadata"],
257257
)
258+
259+
java_library(
260+
name = "concatenated_list_view",
261+
visibility = ["//:internal"],
262+
exports = [
263+
"//runtime/src/main/java/dev/cel/runtime:concatenated_list_view",
264+
],
265+
)

runtime/src/main/java/dev/cel/runtime/AccumulatedUnknowns.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License aj
5+
// You may obtain a copy of the License at
66
//
77
// https://www.apache.org/licenses/LICENSE-2.0
88
//

runtime/src/main/java/dev/cel/runtime/BUILD.bazel

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,9 @@ java_library(
11421142
name = "concatenated_list_view",
11431143
srcs = ["ConcatenatedListView.java"],
11441144
# used_by_android
1145-
visibility = ["//visibility:private"],
1145+
tags = [
1146+
],
1147+
deps = ["//common/annotations"],
11461148
)
11471149

11481150
java_library(

runtime/src/main/java/dev/cel/runtime/CelEvaluationExceptionBuilder.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,16 @@ public static CelEvaluationExceptionBuilder newBuilder(String message, Object...
8383
*/
8484
@Internal
8585
public static CelEvaluationExceptionBuilder newBuilder(CelRuntimeException celRuntimeException) {
86+
// TODO: Temporary until migration is complete.
8687
Throwable cause = celRuntimeException.getCause();
87-
return new CelEvaluationExceptionBuilder(cause.getMessage())
88-
.setCause(cause)
89-
.setErrorCode(celRuntimeException.getErrorCode());
88+
String message =
89+
cause == null
90+
? celRuntimeException.getMessage()
91+
: celRuntimeException.getCause().getMessage();
92+
93+
return new CelEvaluationExceptionBuilder(message)
94+
.setErrorCode(celRuntimeException.getErrorCode())
95+
.setCause(cause);
9096
}
9197

9298
private CelEvaluationExceptionBuilder(String message) {

runtime/src/main/java/dev/cel/runtime/ConcatenatedListView.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License aj
5+
// You may obtain a copy of the License at
66
//
77
// https://www.apache.org/licenses/LICENSE-2.0
88
//
@@ -14,6 +14,7 @@
1414

1515
package dev.cel.runtime;
1616

17+
import dev.cel.common.annotations.Internal;
1718
import java.util.AbstractList;
1819
import java.util.ArrayList;
1920
import java.util.Collection;
@@ -27,16 +28,20 @@
2728
* comprehensions that dispatch `add_list` to concat N lists together).
2829
*
2930
* <p>This does not support any of the standard list operations from {@link java.util.List}.
31+
*
32+
33+
* <p>CEL Library Internals. Do Not Use.
3034
*/
31-
final class ConcatenatedListView<E> extends AbstractList<E> {
35+
@Internal
36+
public final class ConcatenatedListView<E> extends AbstractList<E> {
3237
private final List<List<? extends E>> sourceLists;
3338
private int totalSize = 0;
3439

3540
ConcatenatedListView() {
3641
this.sourceLists = new ArrayList<>();
3742
}
3843

39-
ConcatenatedListView(Collection<? extends E> collection) {
44+
public ConcatenatedListView(Collection<? extends E> collection) {
4045
this();
4146
addAll(collection);
4247
}

runtime/src/main/java/dev/cel/runtime/planner/Attribute.java

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -14,64 +14,13 @@
1414

1515
package dev.cel.runtime.planner;
1616

17-
18-
import com.google.common.collect.ImmutableList;
1917
import com.google.errorprone.annotations.Immutable;
20-
import dev.cel.common.types.CelTypeProvider;
21-
import dev.cel.common.types.TypeType;
2218
import dev.cel.runtime.GlobalResolver;
2319

20+
/** Represents a resolvable symbol or path (such as a variable or a field selection). */
2421
@Immutable
2522
interface Attribute {
26-
Object resolve(GlobalResolver ctx);
27-
28-
final class MaybeAttribute implements Attribute {
29-
private final ImmutableList<Attribute> attributes;
30-
31-
@Override
32-
public Object resolve(GlobalResolver ctx) {
33-
for (Attribute attr : attributes) {
34-
Object value = attr.resolve(ctx);
35-
if (value != null) {
36-
return value;
37-
}
38-
}
39-
40-
// TODO: Handle unknowns
41-
throw new UnsupportedOperationException("Unknown attributes is not supported yet");
42-
}
43-
44-
MaybeAttribute(ImmutableList<Attribute> attributes) {
45-
this.attributes = attributes;
46-
}
47-
}
48-
49-
final class NamespacedAttribute implements Attribute {
50-
private final ImmutableList<String> namespacedNames;
51-
private final CelTypeProvider typeProvider;
52-
53-
@Override
54-
public Object resolve(GlobalResolver ctx) {
55-
for (String name : namespacedNames) {
56-
Object value = ctx.resolve(name);
57-
if (value != null) {
58-
// TODO: apply qualifiers
59-
return value;
60-
}
61-
62-
TypeType type = typeProvider.findType(name).map(TypeType::create).orElse(null);
63-
if (type != null) {
64-
return type;
65-
}
66-
}
67-
68-
// TODO: Handle unknowns
69-
throw new UnsupportedOperationException("Unknown attributes is not supported yet");
70-
}
23+
Object resolve(GlobalResolver ctx, ExecutionFrame frame);
7124

72-
NamespacedAttribute(CelTypeProvider typeProvider, ImmutableList<String> namespacedNames) {
73-
this.typeProvider = typeProvider;
74-
this.namespacedNames = namespacedNames;
75-
}
76-
}
25+
Attribute addQualifier(Qualifier qualifier);
7726
}

0 commit comments

Comments
 (0)