Skip to content

Commit e690cb0

Browse files
l46kokcopybara-github
authored andcommitted
Plan Comprehensions
PiperOrigin-RevId: 845533891
1 parent 506c2b6 commit e690cb0

22 files changed

+1226
-97
lines changed

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/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: 2 additions & 53 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 {
2623
Object resolve(GlobalResolver ctx);
2724

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

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,46 @@
1515
package dev.cel.runtime.planner;
1616

1717
import com.google.common.collect.ImmutableList;
18+
import com.google.common.collect.ImmutableSet;
1819
import com.google.errorprone.annotations.Immutable;
1920
import dev.cel.common.CelContainer;
2021
import dev.cel.common.types.CelTypeProvider;
21-
import dev.cel.runtime.planner.Attribute.MaybeAttribute;
22-
import dev.cel.runtime.planner.Attribute.NamespacedAttribute;
22+
import dev.cel.common.values.CelValueConverter;
2323

2424
@Immutable
2525
final class AttributeFactory {
2626

27-
private final CelContainer unusedContainer;
27+
private final CelContainer container;
2828
private final CelTypeProvider typeProvider;
29+
private final CelValueConverter celValueConverter;
2930

3031
NamespacedAttribute newAbsoluteAttribute(String... names) {
31-
return new NamespacedAttribute(typeProvider, ImmutableList.copyOf(names));
32+
return new NamespacedAttribute(typeProvider, celValueConverter, ImmutableSet.copyOf(names));
3233
}
3334

34-
MaybeAttribute newMaybeAttribute(String... names) {
35-
// TODO: Resolve container names
35+
RelativeAttribute newRelativeAttribute(PlannedInterpretable operand) {
36+
return new RelativeAttribute(operand, celValueConverter);
37+
}
38+
39+
MaybeAttribute newMaybeAttribute(String name) {
3640
return new MaybeAttribute(
37-
ImmutableList.of(new NamespacedAttribute(typeProvider, ImmutableList.copyOf(names))));
41+
this,
42+
ImmutableList.of(
43+
new NamespacedAttribute(
44+
typeProvider, celValueConverter, container.resolveCandidateNames(name))));
3845
}
3946

4047
static AttributeFactory newAttributeFactory(
41-
CelContainer celContainer, CelTypeProvider typeProvider) {
42-
return new AttributeFactory(celContainer, typeProvider);
48+
CelContainer celContainer,
49+
CelTypeProvider typeProvider,
50+
CelValueConverter celValueConverter) {
51+
return new AttributeFactory(celContainer, typeProvider, celValueConverter);
4352
}
4453

45-
private AttributeFactory(CelContainer container, CelTypeProvider typeProvider) {
46-
this.unusedContainer = container;
54+
private AttributeFactory(
55+
CelContainer container, CelTypeProvider typeProvider, CelValueConverter celValueConverter) {
56+
this.container = container;
4757
this.typeProvider = typeProvider;
58+
this.celValueConverter = celValueConverter;
4859
}
4960
}

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

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ java_library(
2222
":eval_create_list",
2323
":eval_create_map",
2424
":eval_create_struct",
25+
":eval_fold",
2526
":eval_or",
27+
":eval_test_only",
2628
":eval_unary",
2729
":eval_var_args_call",
2830
":eval_zero_arity",
31+
":interpretable_attribute",
2932
":planned_interpretable",
3033
":planned_program",
34+
":qualifier",
35+
":string_qualifier",
3136
"//:auto_value",
3237
"//common:cel_ast",
3338
"//common:container",
@@ -36,11 +41,11 @@ java_library(
3641
"//common/ast",
3742
"//common/types",
3843
"//common/types:type_providers",
44+
"//common/values",
3945
"//common/values:cel_value_provider",
4046
"//runtime:dispatcher",
4147
"//runtime:evaluation_exception",
4248
"//runtime:evaluation_exception_builder",
43-
"//runtime:interpretable",
4449
"//runtime:program",
4550
"//runtime:resolved_overload",
4651
"@maven//:com_google_code_findbugs_annotations",
@@ -84,28 +89,77 @@ java_library(
8489
],
8590
)
8691

92+
java_library(
93+
name = "interpretable_attribute",
94+
srcs = ["InterpretableAttribute.java"],
95+
deps = [
96+
":planned_interpretable",
97+
":qualifier",
98+
"@maven//:com_google_errorprone_error_prone_annotations",
99+
],
100+
)
101+
87102
java_library(
88103
name = "attribute",
89104
srcs = [
90105
"Attribute.java",
91106
"AttributeFactory.java",
107+
"MaybeAttribute.java",
108+
"MissingAttribute.java",
109+
"NamespacedAttribute.java",
110+
"RelativeAttribute.java",
92111
],
93112
deps = [
113+
":eval_helpers",
114+
":planned_interpretable",
115+
":qualifier",
94116
"//common:container",
117+
"//common/exceptions:attribute_not_found",
95118
"//common/types",
96119
"//common/types:type_providers",
120+
"//common/values",
121+
"//common/values:cel_value",
97122
"//runtime:interpretable",
98123
"@maven//:com_google_errorprone_error_prone_annotations",
99124
"@maven//:com_google_guava_guava",
100125
],
101126
)
102127

128+
java_library(
129+
name = "qualifier",
130+
srcs = ["Qualifier.java"],
131+
deps = [
132+
"@maven//:com_google_errorprone_error_prone_annotations",
133+
],
134+
)
135+
136+
java_library(
137+
name = "presence_test_qualifier",
138+
srcs = ["PresenceTestQualifier.java"],
139+
deps = [
140+
":attribute",
141+
":qualifier",
142+
"//common/values",
143+
],
144+
)
145+
146+
java_library(
147+
name = "string_qualifier",
148+
srcs = ["StringQualifier.java"],
149+
deps = [
150+
":qualifier",
151+
"//common/exceptions:attribute_not_found",
152+
"//common/values",
153+
],
154+
)
155+
103156
java_library(
104157
name = "eval_attribute",
105158
srcs = ["EvalAttribute.java"],
106159
deps = [
107160
":attribute",
108-
":planned_interpretable",
161+
":interpretable_attribute",
162+
":qualifier",
109163
"//runtime:evaluation_listener",
110164
"//runtime:function_resolver",
111165
"//runtime:interpretable",
@@ -114,6 +168,21 @@ java_library(
114168
],
115169
)
116170

171+
java_library(
172+
name = "eval_test_only",
173+
srcs = ["EvalTestOnly.java"],
174+
deps = [
175+
":interpretable_attribute",
176+
":presence_test_qualifier",
177+
":qualifier",
178+
"//runtime:evaluation_exception",
179+
"//runtime:evaluation_listener",
180+
"//runtime:function_resolver",
181+
"//runtime:interpretable",
182+
"@maven//:com_google_errorprone_error_prone_annotations",
183+
],
184+
)
185+
117186
java_library(
118187
name = "eval_zero_arity",
119188
srcs = ["EvalZeroArity.java"],
@@ -241,6 +310,22 @@ java_library(
241310
],
242311
)
243312

313+
java_library(
314+
name = "eval_fold",
315+
srcs = ["EvalFold.java"],
316+
deps = [
317+
":planned_interpretable",
318+
"//runtime:concatenated_list_view",
319+
"//runtime:evaluation_exception",
320+
"//runtime:evaluation_listener",
321+
"//runtime:function_resolver",
322+
"//runtime:interpretable",
323+
"@maven//:com_google_errorprone_error_prone_annotations",
324+
"@maven//:com_google_guava_guava",
325+
"@maven//:org_jspecify_jspecify",
326+
],
327+
)
328+
244329
java_library(
245330
name = "eval_helpers",
246331
srcs = ["EvalHelpers.java"],

0 commit comments

Comments
 (0)