Skip to content

Commit 293df8f

Browse files
stanleyycheungcopybara-github
authored andcommitted
Propagate accumulated unknowns in unary evaluation.
PiperOrigin-RevId: 955923584
1 parent 310d96f commit 293df8f

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ java_library(
446446
":planned_interpretable",
447447
"//common/ast",
448448
"//common/values",
449+
"//runtime:accumulated_unknowns",
449450
"//runtime:evaluation_exception",
450451
"//runtime:interpretable",
451452
"//runtime:resolved_overload",

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import dev.cel.common.ast.CelExpr;
2121
import dev.cel.common.values.CelValueConverter;
22+
import dev.cel.runtime.AccumulatedUnknowns;
2223
import dev.cel.runtime.CelEvaluationException;
2324
import dev.cel.runtime.CelResolvedOverload;
2425
import dev.cel.runtime.GlobalResolver;
@@ -36,6 +37,12 @@ Object evalInternal(GlobalResolver resolver, ExecutionFrame frame) throws CelEva
3637
resolvedOverload.isStrict()
3738
? evalStrictly(arg, resolver, frame)
3839
: evalNonstrictly(arg, resolver, frame);
40+
41+
AccumulatedUnknowns unknowns = AccumulatedUnknowns.maybeMerge(null, argVal);
42+
if (unknowns != null) {
43+
return unknowns;
44+
}
45+
3946
return EvalHelpers.dispatch(functionName, resolvedOverload, celValueConverter, argVal);
4047
}
4148

runtime/src/test/java/dev/cel/runtime/planner/ProgramPlannerTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,27 @@ public void plan_partialEval_withWildcardQualification() throws Exception {
977977
ImmutableSet.of(2L, 5L, 7L)));
978978
}
979979

980+
@Test
981+
public void plan_unaryFunction_withUnknownArg() throws Exception {
982+
CelCompiler compiler =
983+
CelCompilerFactory.standardCelCompilerBuilder()
984+
.addVar("unk", SimpleType.INT)
985+
.addFunctionDeclarations(
986+
newFunctionDeclaration(
987+
"neg", newGlobalOverload("neg_int", SimpleType.INT, SimpleType.INT)))
988+
.build();
989+
CelAbstractSyntaxTree ast = compile(compiler, "neg(unk)");
990+
991+
Program program = PLANNER.plan(ast);
992+
993+
CelUnknownSet result =
994+
(CelUnknownSet) program.eval(PartialVars.of(CelAttributePattern.create("unk")));
995+
996+
assertThat(result)
997+
.isEqualTo(
998+
CelUnknownSet.create(ImmutableSet.of(CelAttribute.create("unk")), ImmutableSet.of(2L)));
999+
}
1000+
9801001
@Test
9811002
public void localShadowIdentifier_inSelect() throws Exception {
9821003
CelCompiler celCompiler =

0 commit comments

Comments
 (0)