Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchmarks for CelValue #191

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions bundle/src/main/java/dev/cel/bundle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ java_library(
"//common/internal:file_descriptor_converter",
"//common/types:cel_types",
"//common/types:type_providers",
"//common/values:cel_value_provider",
"//compiler",
"//compiler:compiler_builder",
"//parser",
Expand Down
12 changes: 12 additions & 0 deletions bundle/src/main/java/dev/cel/bundle/CelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import dev.cel.common.CelVarDecl;
import dev.cel.common.types.CelType;
import dev.cel.common.types.CelTypeProvider;
import dev.cel.common.values.CelValueProvider;
import dev.cel.compiler.CelCompilerLibrary;
import dev.cel.parser.CelMacro;
import dev.cel.parser.CelStandardMacro;
Expand Down Expand Up @@ -185,6 +186,17 @@ public interface CelBuilder {
@CanIgnoreReturnValue
CelBuilder setTypeFactory(Function<String, Message.Builder> typeFactory);

/**
* Sets the {@code celValueProvider} for resolving values during evaluation. The provided value
* provider will be used first before falling back to the built-in {@link
* dev.cel.common.values.ProtoMessageValueProvider} for resolving protobuf messages.
*
* <p>Note that {@link CelOptions#enableCelValue()} must be enabled or this method will be a
* no-op.
*/
@CanIgnoreReturnValue
CelBuilder setValueProvider(CelValueProvider celValueProvider);

/**
* Set the {@code typeProvider} for use with type-checking expressions.
*
Expand Down
7 changes: 7 additions & 0 deletions bundle/src/main/java/dev/cel/bundle/CelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import dev.cel.common.types.CelType;
import dev.cel.common.types.CelTypeProvider;
import dev.cel.common.types.CelTypes;
import dev.cel.common.values.CelValueProvider;
import dev.cel.compiler.CelCompiler;
import dev.cel.compiler.CelCompilerBuilder;
import dev.cel.compiler.CelCompilerImpl;
Expand Down Expand Up @@ -254,6 +255,12 @@ public Builder setTypeFactory(Function<String, Message.Builder> typeFactory) {
return this;
}

@Override
public Builder setValueProvider(CelValueProvider celValueProvider) {
runtimeBuilder.setValueProvider(celValueProvider);
return this;
}

@Override
@Deprecated
public Builder setTypeProvider(TypeProvider typeProvider) {
Expand Down
38 changes: 38 additions & 0 deletions bundle/src/test/java/dev/cel/bundle/CelImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,24 @@ public void program_withVars() throws Exception {
assertThat(program.eval(ImmutableMap.of("variable", "hello"))).isEqualTo(true);
}

@Test
public void program_withCelValue() throws Exception {
Cel cel =
standardCelBuilderWithMacros()
.setOptions(CelOptions.current().enableCelValue(true).build())
.addDeclarations(
Decl.newBuilder()
.setName("variable")
.setIdent(IdentDecl.newBuilder().setType(CelTypes.STRING))
.build())
.setResultType(SimpleType.BOOL)
.build();

CelRuntime.Program program = cel.createProgram(cel.compile("variable == 'hello'").getAst());

assertThat(program.eval(ImmutableMap.of("variable", "hello"))).isEqualTo(true);
}

@Test
public void program_withProtoVars() throws Exception {
Cel cel =
Expand Down Expand Up @@ -1285,6 +1303,26 @@ public void programAdvanceEvaluation_nestedSelect() throws Exception {
.isEqualTo(CelUnknownSet.create(CelAttribute.fromQualifiedIdentifier("com.google.a")));
}

@Test
public void programAdvanceEvaluation_nestedSelect_withCelValue() throws Exception {
Cel cel =
standardCelBuilderWithMacros()
.setOptions(
CelOptions.current().enableUnknownTracking(true).enableCelValue(true).build())
.addVar("com", MapType.create(SimpleType.STRING, SimpleType.DYN))
.addFunctionBindings()
.setResultType(SimpleType.BOOL)
.build();
CelRuntime.Program program = cel.createProgram(cel.compile("com.google.a || false").getAst());

assertThat(
program.advanceEvaluation(
UnknownContext.create(
fromMap(ImmutableMap.of()),
ImmutableList.of(CelAttributePattern.fromQualifiedIdentifier("com.google.a")))))
.isEqualTo(CelUnknownSet.create(CelAttribute.fromQualifiedIdentifier("com.google.a")));
}

@Test
public void programAdvanceEvaluation_argumentMergeErrorPriority() throws Exception {
Cel cel =
Expand Down
12 changes: 12 additions & 0 deletions common/src/main/java/dev/cel/common/CelOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public abstract class CelOptions {

public abstract boolean enableUnknownTracking();

public abstract boolean enableCelValue();

public abstract int comprehensionMaxIterations();

public abstract Builder toBuilder();
Expand Down Expand Up @@ -176,6 +178,7 @@ public static Builder newBuilder() {
.errorOnDuplicateMapKeys(false)
.resolveTypeDependencies(true)
.enableUnknownTracking(false)
.enableCelValue(false)
.comprehensionMaxIterations(-1);
}

Expand Down Expand Up @@ -428,6 +431,15 @@ public abstract static class Builder {
*/
public abstract Builder enableUnknownTracking(boolean value);

/**
* Enables the usage of {@code CelValue} for the runtime. It is a native value representation of
* CEL that wraps Java native objects, and comes with extended capabilities, such as allowing
* value constructs not understood by CEL (ex: POJOs).
*
* <p>Warning: This option is experimental.
*/
public abstract Builder enableCelValue(boolean value);

/**
* Limit the total number of iterations permitted within comprehension loops.
*
Expand Down
12 changes: 10 additions & 2 deletions common/src/main/java/dev/cel/common/types/TypeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
@Immutable
public abstract class TypeType extends CelType {

static final TypeType TYPE = create(SimpleType.DYN);

@Override
public CelKind kind() {
return CelKind.TYPE;
Expand All @@ -38,6 +36,16 @@ public String name() {
return "type";
}

/** Retrieves the underlying type name of the type-kind held. */
public String containingTypeName() {
CelType containingType = type();
if (containingType.kind() == CelKind.DYN) {
return "type";
}

return containingType.name();
}

@Override
public abstract ImmutableList<CelType> parameters();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public interface CelValueProvider {
final class CombinedCelValueProvider implements CelValueProvider {
private final ImmutableList<CelValueProvider> celValueProviders;

CombinedCelValueProvider(CelValueProvider first, CelValueProvider second) {
public CombinedCelValueProvider(CelValueProvider first, CelValueProvider second) {
Preconditions.checkNotNull(first);
Preconditions.checkNotNull(second);
celValueProviders = ImmutableList.of(first, second);
Expand Down
1 change: 1 addition & 0 deletions common/src/test/java/dev/cel/common/values/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ java_library(
srcs = glob(["*.java"]),
deps = [
"//:java_truth",
"//bundle:cel",
"//common",
"//common:options",
"//common:runtime_exception",
Expand Down
Loading