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
40 changes: 40 additions & 0 deletions runtime/src/main/java/dev/cel/runtime/planner/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ java_library(
],
deps = [
":attribute",
":eval_and",
":eval_attribute",
":eval_const",
":eval_create_list",
":eval_create_map",
":eval_create_struct",
":eval_or",
":eval_unary",
":eval_var_args_call",
":eval_zero_arity",
":planned_program",
"//:auto_value",
"//common:cel_ast",
"//common:container",
"//common:operator",
"//common/annotations",
"//common/ast",
"//common/types",
Expand All @@ -48,6 +51,8 @@ java_library(
srcs = ["PlannedProgram.java"],
deps = [
"//:auto_value",
"//common:runtime_exception",
"//common/values",
"//runtime:activation",
"//runtime:evaluation_exception",
"//runtime:evaluation_exception_builder",
Expand Down Expand Up @@ -137,6 +142,32 @@ java_library(
],
)

java_library(
name = "eval_or",
srcs = ["EvalOr.java"],
deps = [
":eval_helpers",
"//common/values",
"//runtime:evaluation_listener",
"//runtime:function_resolver",
"//runtime:interpretable",
"@maven//:com_google_guava_guava",
],
)

java_library(
name = "eval_and",
srcs = ["EvalAnd.java"],
deps = [
":eval_helpers",
"//common/values",
"//runtime:evaluation_listener",
"//runtime:function_resolver",
"//runtime:interpretable",
"@maven//:com_google_guava_guava",
],
)

java_library(
name = "eval_create_struct",
srcs = ["EvalCreateStruct.java"],
Expand Down Expand Up @@ -178,3 +209,12 @@ java_library(
"@maven//:com_google_guava_guava",
],
)

java_library(
name = "eval_helpers",
srcs = ["EvalHelpers.java"],
deps = [
"//common/values",
"//runtime:interpretable",
],
)
86 changes: 86 additions & 0 deletions runtime/src/main/java/dev/cel/runtime/planner/EvalAnd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dev.cel.runtime.planner;

import static dev.cel.runtime.planner.EvalHelpers.evalNonstrictly;

import com.google.common.base.Preconditions;
import dev.cel.common.values.ErrorValue;
import dev.cel.runtime.CelEvaluationListener;
import dev.cel.runtime.CelFunctionResolver;
import dev.cel.runtime.GlobalResolver;
import dev.cel.runtime.Interpretable;

final class EvalAnd implements Interpretable {

@SuppressWarnings("Immutable")
private final Interpretable[] args;

@Override
public Object eval(GlobalResolver resolver) {
ErrorValue errorValue = null;
for (Interpretable arg : args) {
Object argVal = evalNonstrictly(arg, resolver);
if (argVal instanceof Boolean) {
// Short-circuit on false
if (!((boolean) argVal)) {
return false;
}
} else if (argVal instanceof ErrorValue) {
errorValue = (ErrorValue) argVal;
} else {
// TODO: Handle unknowns
throw new IllegalArgumentException(
String.format("Expected boolean value, found: %s", argVal));
}
}

if (errorValue != null) {
return errorValue;
}

return true;
}

@Override
public Object eval(GlobalResolver resolver, CelEvaluationListener listener) {
// TODO: Implement support
throw new UnsupportedOperationException("Not yet supported");
}

@Override
public Object eval(GlobalResolver resolver, CelFunctionResolver lateBoundFunctionResolver) {
// TODO: Implement support
throw new UnsupportedOperationException("Not yet supported");
}

@Override
public Object eval(
GlobalResolver resolver,
CelFunctionResolver lateBoundFunctionResolver,
CelEvaluationListener listener) {
// TODO: Implement support
throw new UnsupportedOperationException("Not yet supported");
}

static EvalAnd create(Interpretable[] args) {
return new EvalAnd(args);
}

private EvalAnd(Interpretable[] args) {
Preconditions.checkArgument(args.length == 2);
this.args = args;
}
}
32 changes: 32 additions & 0 deletions runtime/src/main/java/dev/cel/runtime/planner/EvalHelpers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dev.cel.runtime.planner;

import dev.cel.common.values.ErrorValue;
import dev.cel.runtime.GlobalResolver;
import dev.cel.runtime.Interpretable;

final class EvalHelpers {

static Object evalNonstrictly(Interpretable interpretable, GlobalResolver resolver) {
try {
return interpretable.eval(resolver);
} catch (Exception e) {
return ErrorValue.create(e);
}
}

private EvalHelpers() {}
}
86 changes: 86 additions & 0 deletions runtime/src/main/java/dev/cel/runtime/planner/EvalOr.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dev.cel.runtime.planner;

import static dev.cel.runtime.planner.EvalHelpers.evalNonstrictly;

import com.google.common.base.Preconditions;
import dev.cel.common.values.ErrorValue;
import dev.cel.runtime.CelEvaluationListener;
import dev.cel.runtime.CelFunctionResolver;
import dev.cel.runtime.GlobalResolver;
import dev.cel.runtime.Interpretable;

final class EvalOr implements Interpretable {

@SuppressWarnings("Immutable")
private final Interpretable[] args;

@Override
public Object eval(GlobalResolver resolver) {
ErrorValue errorValue = null;
for (Interpretable arg : args) {
Object argVal = evalNonstrictly(arg, resolver);
if (argVal instanceof Boolean) {
// Short-circuit on true
if (((boolean) argVal)) {
return true;
}
} else if (argVal instanceof ErrorValue) {
errorValue = (ErrorValue) argVal;
} else {
// TODO: Handle unknowns
throw new IllegalArgumentException(
String.format("Expected boolean value, found: %s", argVal));
}
}

if (errorValue != null) {
return errorValue;
}

return false;
}

@Override
public Object eval(GlobalResolver resolver, CelEvaluationListener listener) {
// TODO: Implement support
throw new UnsupportedOperationException("Not yet supported");
}

@Override
public Object eval(GlobalResolver resolver, CelFunctionResolver lateBoundFunctionResolver) {
// TODO: Implement support
throw new UnsupportedOperationException("Not yet supported");
}

@Override
public Object eval(
GlobalResolver resolver,
CelFunctionResolver lateBoundFunctionResolver,
CelEvaluationListener listener) {
// TODO: Implement support
throw new UnsupportedOperationException("Not yet supported");
}

static EvalOr create(Interpretable[] args) {
return new EvalOr(args);
}

private EvalOr(Interpretable[] args) {
Preconditions.checkArgument(args.length == 2);
this.args = args;
}
}
20 changes: 18 additions & 2 deletions runtime/src/main/java/dev/cel/runtime/planner/PlannedProgram.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import com.google.auto.value.AutoValue;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.CelRuntimeException;
import dev.cel.common.values.ErrorValue;
import dev.cel.runtime.Activation;
import dev.cel.runtime.CelEvaluationException;
import dev.cel.runtime.CelEvaluationExceptionBuilder;
Expand Down Expand Up @@ -49,14 +51,28 @@ public Object eval(Map<String, ?> mapValue, CelFunctionResolver lateBoundFunctio
private Object evalOrThrow(Interpretable interpretable, GlobalResolver resolver)
throws CelEvaluationException {
try {
return interpretable.eval(resolver);
Object evalResult = interpretable.eval(resolver);
if (evalResult instanceof ErrorValue) {
ErrorValue errorValue = (ErrorValue) evalResult;
throw newCelEvaluationException(errorValue.value());
}

return evalResult;
} catch (RuntimeException e) {
throw newCelEvaluationException(e);
}
}

private static CelEvaluationException newCelEvaluationException(Exception e) {
return CelEvaluationExceptionBuilder.newBuilder(e.getMessage()).setCause(e).build();
CelEvaluationExceptionBuilder builder;
if (e instanceof CelRuntimeException) {
// Preserve detailed error, including error codes if one exists.
builder = CelEvaluationExceptionBuilder.newBuilder((CelRuntimeException) e);
} else {
builder = CelEvaluationExceptionBuilder.newBuilder(e.getMessage()).setCause(e);
}

return builder.build();
}

static Program create(Interpretable interpretable) {
Expand Down
12 changes: 12 additions & 0 deletions runtime/src/main/java/dev/cel/runtime/planner/ProgramPlanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import javax.annotation.concurrent.ThreadSafe;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.CelContainer;
import dev.cel.common.Operator;
import dev.cel.common.annotations.Internal;
import dev.cel.common.ast.CelConstant;
import dev.cel.common.ast.CelExpr;
Expand Down Expand Up @@ -169,6 +170,17 @@ private Interpretable planCall(CelExpr expr, PlannerContext ctx) {

// TODO: Handle all specialized calls (logical operators, conditionals, equals etc)
String functionName = resolvedFunction.functionName();
Operator operator = Operator.findReverse(functionName).orElse(null);
if (operator != null) {
switch (operator) {
case LOGICAL_OR:
return EvalOr.create(evaluatedArgs);
case LOGICAL_AND:
return EvalAnd.create(evaluatedArgs);
default:
// fall-through
}
}

CelResolvedOverload resolvedOverload = null;
if (resolvedFunction.overloadId().isPresent()) {
Expand Down
1 change: 1 addition & 0 deletions runtime/src/test/java/dev/cel/runtime/planner/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ java_library(
"//common:cel_descriptor_util",
"//common:cel_source",
"//common:compiler_common",
"//common:error_codes",
"//common:operator",
"//common:options",
"//common/ast",
Expand Down
Loading
Loading