From daf65c600e583ed0529d543851c4b63aad754ee0 Mon Sep 17 00:00:00 2001 From: Robert Stupp Date: Fri, 16 Oct 2026 19:30:17 +0200 Subject: [PATCH 1/2] Add native scalar planning and optimizer integration Evaluate eligible checked scalar expressions without intermediate `Val` wrappers. Preserve established fallback for incompatible adapters, overloads, and decorators; compose native islands with built-in constant folding and retain differential, root-shape, policy, and public API coverage. `ScriptHost` now plans from the checked AST, enabling checked-expression optimizations. Constant-list membership preserves CEL numeric equality, including mixed numeric types and NaN behavior. Existing public optimizer helper methods remain available as forwarding methods, preserving source and binary compatibility. --- .../main/java/org/projectnessie/cel/CEL.java | 34 +- .../org/projectnessie/cel/EvalOption.java | 40 +- .../java/org/projectnessie/cel/Library.java | 9 +- .../org/projectnessie/cel/ProgramOption.java | 9 +- .../common/types/pb/DefaultTypeAdapter.java | 3 +- .../common/types/pb/ProtoTypeRegistry.java | 5 +- .../ref/StandardScalarFieldProvider.java | 29 + .../types/ref/StandardScalarTypeAdapter.java | 30 + .../cel/interpreter/BuiltInOptimizer.java | 155 +++ .../ConstantSetMembershipNodes.java | 43 + .../cel/interpreter/ExprInterpreter.java | 72 +- .../interpreter/InterpretableDecorator.java | 118 +- .../cel/interpreter/InterpretablePlanner.java | 24 +- .../cel/interpreter/Interpreter.java | 32 +- .../cel/interpreter/NativeAttributeNodes.java | 127 ++ .../cel/interpreter/NativeCapabilities.java | 40 + .../cel/interpreter/NativeIsland.java | 89 ++ .../interpreter/NativeOverloadDescriptor.java | 35 + .../cel/interpreter/NativeScalarNodes.java | 1178 ++++++++++++++++ .../cel/interpreter/NativeSupport.java | 96 ++ .../cel/interpreter/Planner.java | 1187 +++++++++++++---- .../cel/interpreter/PlanningPolicy.java | 43 + .../StandardNativeOverloadCatalog.java | 168 +++ .../StandardOverloadProvenance.java | 61 + .../cel/interpreter/ValueSignal.java | 34 + .../cel/interpreter/functions/Overload.java | 17 +- .../projectnessie/cel/NativePlanRootTest.java | 570 ++++++++ .../org/projectnessie/cel/NativePlanTest.java | 852 ++++++++++++ .../cel/interpreter/BuiltInOptimizerTest.java | 108 ++ .../ConstantSetMembershipSupportTest.java | 70 + .../cel/interpreter/PlanningPolicyTest.java | 131 ++ .../SetMembershipOptimizationTest.java | 29 +- .../StandardNativeOverloadCatalogTest.java | 166 +++ .../interpreter/functions/OverloadTest.java | 41 + .../InterpreterPublicApiCompileTest.java | 3 + .../cel/types/jackson/JacksonRegistry.java | 3 +- .../cel/types/jackson3/Jackson3Registry.java | 3 +- .../projectnessie/cel/tools/ScriptHost.java | 9 +- .../cel/tools/ScriptHostTest.java | 61 + 39 files changed, 5317 insertions(+), 407 deletions(-) create mode 100644 core/src/main/java/org/projectnessie/cel/common/types/ref/StandardScalarFieldProvider.java create mode 100644 core/src/main/java/org/projectnessie/cel/common/types/ref/StandardScalarTypeAdapter.java create mode 100644 core/src/main/java/org/projectnessie/cel/interpreter/BuiltInOptimizer.java create mode 100644 core/src/main/java/org/projectnessie/cel/interpreter/ConstantSetMembershipNodes.java create mode 100644 core/src/main/java/org/projectnessie/cel/interpreter/NativeAttributeNodes.java create mode 100644 core/src/main/java/org/projectnessie/cel/interpreter/NativeCapabilities.java create mode 100644 core/src/main/java/org/projectnessie/cel/interpreter/NativeIsland.java create mode 100644 core/src/main/java/org/projectnessie/cel/interpreter/NativeOverloadDescriptor.java create mode 100644 core/src/main/java/org/projectnessie/cel/interpreter/NativeScalarNodes.java create mode 100644 core/src/main/java/org/projectnessie/cel/interpreter/NativeSupport.java create mode 100644 core/src/main/java/org/projectnessie/cel/interpreter/PlanningPolicy.java create mode 100644 core/src/main/java/org/projectnessie/cel/interpreter/StandardNativeOverloadCatalog.java create mode 100644 core/src/main/java/org/projectnessie/cel/interpreter/StandardOverloadProvenance.java create mode 100644 core/src/main/java/org/projectnessie/cel/interpreter/ValueSignal.java create mode 100644 core/src/test/java/org/projectnessie/cel/NativePlanRootTest.java create mode 100644 core/src/test/java/org/projectnessie/cel/NativePlanTest.java create mode 100644 core/src/test/java/org/projectnessie/cel/interpreter/BuiltInOptimizerTest.java create mode 100644 core/src/test/java/org/projectnessie/cel/interpreter/ConstantSetMembershipSupportTest.java create mode 100644 core/src/test/java/org/projectnessie/cel/interpreter/PlanningPolicyTest.java create mode 100644 core/src/test/java/org/projectnessie/cel/interpreter/StandardNativeOverloadCatalogTest.java create mode 100644 core/src/test/java/org/projectnessie/cel/interpreter/functions/OverloadTest.java diff --git a/core/src/main/java/org/projectnessie/cel/CEL.java b/core/src/main/java/org/projectnessie/cel/CEL.java index 25d55a9cd..32936b5a5 100644 --- a/core/src/main/java/org/projectnessie/cel/CEL.java +++ b/core/src/main/java/org/projectnessie/cel/CEL.java @@ -38,6 +38,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Set; import org.projectnessie.cel.interpreter.Activation; import org.projectnessie.cel.interpreter.Activation.PartialActivation; import org.projectnessie.cel.interpreter.AttributePattern; @@ -50,10 +51,23 @@ public final class CEL { /** - * newProgram creates a program instance with an environment, an ast, and an optional list of - * ProgramOption values. + * Creates an executable program for an AST in an environment. * - *

If the program cannot be configured the prog will be nil, with a non-nil error response. + *

Options are applied in argument order. A checked AST provides type and reference metadata + * used during planning; an unchecked AST is accepted but may require more runtime dispatch. The + * resulting program can be reused for multiple evaluations. + * + *

Native evaluation, when permitted by the selected options, is planner-selected evaluation + * over supported Java-native representations. Unsupported shapes fall back to the established + * evaluator; this does not generate Java or native machine code. + * + * @param e environment supplying types and name resolution + * @param ast parsed or checked expression + * @param opts program configuration tokens, normally created by {@link ProgramOption} factories + * @return reusable executable program + * @throws NullPointerException if {@code e}, {@code ast}, or {@code opts} is {@code null}, or if + * an option is {@code null} or returns {@code null} + * @throws RuntimeException if program planning or option application fails */ public static Program newProgram(Env e, Ast ast, ProgramOption... opts) { // Build the dispatcher, interpreter, and default program value. @@ -86,7 +100,12 @@ public static Program newProgram(Env e, Ast ast, ProgramOption... opts) { Interpreter interp = newInterpreter( - disp, e.getContainer(), e.getTypeProvider(), e.getTypeAdapter(), p.attrFactory); + disp, + e.getContainer(), + e.getTypeProvider(), + e.getTypeAdapter(), + p.attrFactory, + nativePlanningPermitted(p.evalOpts, p.decorators)); p.interpreter = interp; // Translate the EvalOption flags into InterpretableDecorator instances. @@ -158,6 +177,13 @@ private static Program initInterpretable( return p; } + static boolean nativePlanningPermitted( + Set evalOptions, List decorators) { + return decorators.isEmpty() + && (evalOptions.isEmpty() + || evalOptions.size() == 1 && evalOptions.contains(EvalOption.OptOptimize)); + } + /** CheckedExprToAst converts a checked expression proto message to an Ast. */ public static Ast checkedExprToAst(CheckedExpr checkedExpr) { Map refMap = checkedExpr.getReferenceMapMap(); diff --git a/core/src/main/java/org/projectnessie/cel/EvalOption.java b/core/src/main/java/org/projectnessie/cel/EvalOption.java index 7b24ffb9c..28aa7264b 100644 --- a/core/src/main/java/org/projectnessie/cel/EvalOption.java +++ b/core/src/main/java/org/projectnessie/cel/EvalOption.java @@ -16,8 +16,12 @@ package org.projectnessie.cel; /** - * EvalOption indicates an evaluation option that may affect the evaluation behavior or information - * in the output result. + * Built-in evaluation modes selected through {@link ProgramOption#evalOptions(EvalOption...)}. + * + *

Options are additive. Every built-in option except {@link #OptOptimize} selects the + * established interpreter planning path; custom decorators do so as well. With no such option, the + * planner may select native evaluation for eligible checked expression shapes and falls back to the + * established evaluator elsewhere. */ public enum EvalOption { @@ -28,21 +32,35 @@ public enum EvalOption { OptExhaustiveEval(2 | OptTrackState.mask), /** - * OptOptimize precomputes functions and operators with constants as arguments at program creation - * time. This flag is useful when the expression will be evaluated repeatedly against a series of - * different inputs. + * Enables program-creation-time optimization of functions and operators whose arguments are + * constant. + * + *

This is useful for programs evaluated repeatedly. It does not guarantee that a particular + * expression is folded, and it is independent of whether eligible nodes use native evaluation. */ OptOptimize(4), /** - * OptPartialEval enables the evaluation of a partial state where the input data that may be known - * to be missing, either as top-level variables, or somewhere within a variable's object member - * graph. + * Enables partial evaluation for variables or qualified attributes whose values are marked + * unknown. + * + *

By itself this does not change a result. Supply a {@link + * org.projectnessie.cel.interpreter.Activation.PartialActivation}, for example from {@link + * CEL#partialVars(Object, org.projectnessie.cel.interpreter.AttributePattern...)}, to mark + * unknown input. Combine with {@link #OptTrackState} when creating a residual AST through {@link + * Env#residualAst(Ast, EvalDetails)}. Partial evaluation selects established interpreter + * planning. + */ + OptPartialEval(8), + + /** + * Forces checked expressions to use established interpreter planning. * - *

By itself, OptPartialEval does not change evaluation behavior unless the input to the - * Program Eval is an PartialVars. + *

Without this option, the planner may evaluate eligible expression nodes directly over + * supported Java-native representations and falls back when a shape is not eligible. Native + * evaluation does not mean generated Java, bytecode, JNI, or machine code. */ - OptPartialEval(8); + OptDisableNativeEval(16); private final int mask; diff --git a/core/src/main/java/org/projectnessie/cel/Library.java b/core/src/main/java/org/projectnessie/cel/Library.java index a3dedb5b2..45e0a5336 100644 --- a/core/src/main/java/org/projectnessie/cel/Library.java +++ b/core/src/main/java/org/projectnessie/cel/Library.java @@ -17,12 +17,12 @@ import static org.projectnessie.cel.EnvOption.declarations; import static org.projectnessie.cel.EnvOption.macros; -import static org.projectnessie.cel.ProgramOption.functions; import static org.projectnessie.cel.checker.Checker.StandardDeclarations; import static org.projectnessie.cel.interpreter.functions.Overload.standardOverloads; import static org.projectnessie.cel.parser.Macro.AllMacros; import java.util.List; +import org.projectnessie.cel.interpreter.functions.Overload; /** * Library provides a collection of EnvOption and ProgramOption values used to confiugre a CEL @@ -83,7 +83,12 @@ public List getCompileOptions() { /** ProgramOptions returns function implementations for the standard CEL functions. */ @Override public List getProgramOptions() { - return List.of(functions(standardOverloads())); + Overload[] overloads = standardOverloads(); + return List.of( + p -> { + p.dispatcher.add(overloads); + return p; + }); } } } diff --git a/core/src/main/java/org/projectnessie/cel/ProgramOption.java b/core/src/main/java/org/projectnessie/cel/ProgramOption.java index e15e431f6..411cd0ee1 100644 --- a/core/src/main/java/org/projectnessie/cel/ProgramOption.java +++ b/core/src/main/java/org/projectnessie/cel/ProgramOption.java @@ -26,9 +26,14 @@ public interface ProgramOption { Prog apply(Prog prog); /** - * CustomDecorator appends an InterpreterDecorator to the program. + * Appends a low-level interpreter-plan decorator. * - *

InterpretableDecorators can be used to inspect, alter, or replace the Program plan. + *

Decorators can inspect, alter, or replace the established interpreter plan. Custom + * decorators are applied in insertion order; built-in optimization and state decorators may have + * fixed later placement. Supplying a custom decorator disables native planning for the program. + * + * @param dec decorator to append + * @return program configuration token */ static ProgramOption customDecorator(InterpretableDecorator dec) { return p -> { diff --git a/core/src/main/java/org/projectnessie/cel/common/types/pb/DefaultTypeAdapter.java b/core/src/main/java/org/projectnessie/cel/common/types/pb/DefaultTypeAdapter.java index 44f44fb2d..209cec5af 100644 --- a/core/src/main/java/org/projectnessie/cel/common/types/pb/DefaultTypeAdapter.java +++ b/core/src/main/java/org/projectnessie/cel/common/types/pb/DefaultTypeAdapter.java @@ -27,13 +27,14 @@ import org.projectnessie.cel.common.ULong; import org.projectnessie.cel.common.types.Err; import org.projectnessie.cel.common.types.Types; +import org.projectnessie.cel.common.types.ref.StandardScalarTypeAdapter; import org.projectnessie.cel.common.types.ref.Type; import org.projectnessie.cel.common.types.ref.TypeAdapter; import org.projectnessie.cel.common.types.ref.TypeAdapterSupport; import org.projectnessie.cel.common.types.ref.Val; /** defaultTypeAdapter converts go native types to CEL values. */ -public final class DefaultTypeAdapter implements TypeAdapter { +public final class DefaultTypeAdapter implements StandardScalarTypeAdapter { /** DefaultTypeAdapter adapts canonical CEL types from their equivalent Go values. */ public static final DefaultTypeAdapter Instance = new DefaultTypeAdapter(Db.defaultDb); diff --git a/core/src/main/java/org/projectnessie/cel/common/types/pb/ProtoTypeRegistry.java b/core/src/main/java/org/projectnessie/cel/common/types/pb/ProtoTypeRegistry.java index a2324fd8c..a6512686a 100644 --- a/core/src/main/java/org/projectnessie/cel/common/types/pb/ProtoTypeRegistry.java +++ b/core/src/main/java/org/projectnessie/cel/common/types/pb/ProtoTypeRegistry.java @@ -98,12 +98,15 @@ import org.projectnessie.cel.common.types.ref.FieldGetter; import org.projectnessie.cel.common.types.ref.FieldTester; import org.projectnessie.cel.common.types.ref.FieldType; +import org.projectnessie.cel.common.types.ref.StandardScalarFieldProvider; +import org.projectnessie.cel.common.types.ref.StandardScalarTypeAdapter; import org.projectnessie.cel.common.types.ref.TypeAdapterSupport; import org.projectnessie.cel.common.types.ref.TypeRegistry; import org.projectnessie.cel.common.types.ref.Val; import org.projectnessie.cel.common.types.traits.Lister; -public final class ProtoTypeRegistry implements TypeRegistry { +public final class ProtoTypeRegistry + implements TypeRegistry, StandardScalarTypeAdapter, StandardScalarFieldProvider { private static final ProtoTypeRegistry DEFAULT_REGISTRY = newDefaultRegistry(); private final Map revTypeMap; diff --git a/core/src/main/java/org/projectnessie/cel/common/types/ref/StandardScalarFieldProvider.java b/core/src/main/java/org/projectnessie/cel/common/types/ref/StandardScalarFieldProvider.java new file mode 100644 index 000000000..b8d463419 --- /dev/null +++ b/core/src/main/java/org/projectnessie/cel/common/types/ref/StandardScalarFieldProvider.java @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2021 The Authors of CEL-Java + * + * 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 + * + * http://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 org.projectnessie.cel.common.types.ref; + +/** + * Type provider whose scalar field accessors return canonical Java scalar representations. + * + *

For fields described as CEL boolean, signed integer, double, string, or null, the associated + * {@link FieldType#getFrom} accessor returns the corresponding Java scalar, Java {@code null}, or + * an existing terminal {@link Val}. Access must not depend on mutable evaluation context. + * + *

This semantic marker permits evaluators to consume supported scalar fields before adapting a + * final result. Implementations should only declare it when those guarantees apply to every + * instance. + */ +public interface StandardScalarFieldProvider extends TypeProvider {} diff --git a/core/src/main/java/org/projectnessie/cel/common/types/ref/StandardScalarTypeAdapter.java b/core/src/main/java/org/projectnessie/cel/common/types/ref/StandardScalarTypeAdapter.java new file mode 100644 index 000000000..23801e0a7 --- /dev/null +++ b/core/src/main/java/org/projectnessie/cel/common/types/ref/StandardScalarTypeAdapter.java @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2021 The Authors of CEL-Java + * + * 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 + * + * http://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 org.projectnessie.cel.common.types.ref; + +/** + * Type adapter whose Java scalar conversions have the standard CEL semantics. + * + *

Implementations preserve existing {@link Val} values and convert Java {@code null}, boolean, + * signed integral, floating-point, and string values exactly as {@link TypeAdapterSupport}. The + * primitive overloads must agree with {@link #nativeToValue(Object)}, and these conversions must + * not depend on mutable evaluation context. + * + *

This semantic marker permits evaluators to carry the corresponding Java scalars internally + * before adapting the final result. Implementations should only declare it when those guarantees + * apply to every instance. + */ +public interface StandardScalarTypeAdapter extends TypeAdapter {} diff --git a/core/src/main/java/org/projectnessie/cel/interpreter/BuiltInOptimizer.java b/core/src/main/java/org/projectnessie/cel/interpreter/BuiltInOptimizer.java new file mode 100644 index 000000000..ef0caf0c9 --- /dev/null +++ b/core/src/main/java/org/projectnessie/cel/interpreter/BuiltInOptimizer.java @@ -0,0 +1,155 @@ +/* + * Copyright (C) 2026 The Authors of CEL-Java + * + * 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 + * + * http://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 org.projectnessie.cel.interpreter; + +import static org.projectnessie.cel.common.types.BoolT.True; +import static org.projectnessie.cel.common.types.Err.throwErrorAsIllegalStateException; +import static org.projectnessie.cel.common.types.IntT.IntZero; +import static org.projectnessie.cel.interpreter.Activation.emptyActivation; + +import java.util.HashSet; +import java.util.Set; +import org.projectnessie.cel.common.types.IteratorT; +import org.projectnessie.cel.common.types.Overloads; +import org.projectnessie.cel.common.types.Util; +import org.projectnessie.cel.common.types.ref.Type; +import org.projectnessie.cel.common.types.ref.Val; +import org.projectnessie.cel.common.types.traits.Lister; +import org.projectnessie.cel.interpreter.Interpretable.InterpretableCall; +import org.projectnessie.cel.interpreter.Interpretable.InterpretableConst; + +/** Built-in constant folding and set-membership optimizer. */ +final class BuiltInOptimizer implements InterpretableDecorator { + static final BuiltInOptimizer INSTANCE = new BuiltInOptimizer(); + + private BuiltInOptimizer() {} + + @Override + public Interpretable decorate(Interpretable interpretable) { + if (interpretable instanceof EvalList list) { + return maybeBuildListLiteral(interpretable, list); + } + if (interpretable instanceof EvalMap map) { + return maybeBuildMapLiteral(interpretable, map); + } + if (interpretable instanceof InterpretableCall call) { + if (call.overloadID().equals(Overloads.InList)) { + return maybeOptimizeSetMembership(interpretable, call); + } + if (Overloads.isTypeConversionFunction(call.function())) { + return maybeOptimizeConstUnary(interpretable, call); + } + } + return interpretable; + } + + static Interpretable maybeOptimizeConstUnary( + Interpretable interpretable, InterpretableCall call) { + EvalConst folded = foldConstantUnary(call); + return folded != null ? folded : interpretable; + } + + static Interpretable maybeBuildListLiteral(Interpretable interpretable, EvalList list) { + EvalConst folded = foldList(list); + return folded != null ? folded : interpretable; + } + + static Interpretable maybeBuildMapLiteral(Interpretable interpretable, EvalMap map) { + EvalConst folded = foldMap(map); + return folded != null ? folded : interpretable; + } + + static EvalConst foldConstantUnary(InterpretableCall call) { + Interpretable[] args = call.args(); + if (args.length != 1 || !(args[0] instanceof InterpretableConst)) { + return null; + } + Val val = call.eval(emptyActivation()); + throwErrorAsIllegalStateException(val); + return new EvalConst(call.id(), val); + } + + static EvalConst foldList(EvalList list) { + for (Interpretable elem : list.elems) { + if (!(elem instanceof InterpretableConst)) { + return null; + } + } + return new EvalConst(list.id(), list.eval(emptyActivation())); + } + + static EvalConst foldMap(EvalMap map) { + for (int index = 0; index < map.keys.length; index++) { + if (!(map.keys[index] instanceof InterpretableConst) + || !(map.vals[index] instanceof InterpretableConst)) { + return null; + } + } + return new EvalConst(map.id(), map.eval(emptyActivation())); + } + + /** Converts {@code in} over a homogeneous primitive constant list to constant-set membership. */ + static Interpretable maybeOptimizeSetMembership( + Interpretable interpretable, InterpretableCall inList) { + ConstantSet constantSet = constantSet(inList); + if (constantSet == null) { + return interpretable; + } + Interpretable[] args = inList.args(); + return new EvalSetMembership(inList, args[0], constantSet.typeName(), constantSet.values()); + } + + static ConstantSet constantSet(InterpretableCall inList) { + Interpretable[] args = inList.args(); + if (args.length != 2) { + return null; + } + return constantSet(args[1]); + } + + static ConstantSet constantSet(Interpretable rhs) { + if (!(rhs instanceof InterpretableConst listConstant)) { + return null; + } + // An InList call always has a Lister right operand. + Lister list = (Lister) listConstant.value(); + if (list.size() == IntZero) { + return new ConstantSet(null, Set.of()); + } + IteratorT iterator = list.iterator(); + Type type = null; + Set valueSet = new HashSet<>(); + while (iterator.hasNext() == True) { + Val element = iterator.next(); + if (!Util.isPrimitiveType(element)) { + return null; + } + if (type == null) { + type = element.type(); + } else if (!type.typeName().equals(element.type().typeName())) { + return null; + } + valueSet.add(element); + } + if (type == null) { + // A custom Lister can report a non-zero size while yielding no elements. + return null; + } + return new ConstantSet(type.typeName(), valueSet); + } + + record ConstantSet(String typeName, Set values) {} +} diff --git a/core/src/main/java/org/projectnessie/cel/interpreter/ConstantSetMembershipNodes.java b/core/src/main/java/org/projectnessie/cel/interpreter/ConstantSetMembershipNodes.java new file mode 100644 index 000000000..b448a7f2e --- /dev/null +++ b/core/src/main/java/org/projectnessie/cel/interpreter/ConstantSetMembershipNodes.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2026 The Authors of CEL-Java + * + * 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 + * + * http://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 org.projectnessie.cel.interpreter; + +import static org.projectnessie.cel.common.types.BoolT.False; +import static org.projectnessie.cel.common.types.BoolT.True; +import static org.projectnessie.cel.common.types.Err.noSuchOverload; +import static org.projectnessie.cel.common.types.Util.isUnknownOrError; + +import java.util.Set; +import org.projectnessie.cel.common.operators.Operator; +import org.projectnessie.cel.common.types.ref.Val; + +/** Shared post-evaluation semantics for optimized constant-set membership. */ +final class ConstantSetMembershipSupport { + private ConstantSetMembershipSupport() {} + + static Val evaluate(Val needle, String needleTypeName, Set values) { + if (isUnknownOrError(needle)) { + return needle; + } + if (values.isEmpty()) { + return False; + } + if (!needle.type().typeName().equals(needleTypeName)) { + return noSuchOverload(null, Operator.In.id, needle); + } + return values.contains(needle) ? True : False; + } +} diff --git a/core/src/main/java/org/projectnessie/cel/interpreter/ExprInterpreter.java b/core/src/main/java/org/projectnessie/cel/interpreter/ExprInterpreter.java index 30df41489..289a1711c 100644 --- a/core/src/main/java/org/projectnessie/cel/interpreter/ExprInterpreter.java +++ b/core/src/main/java/org/projectnessie/cel/interpreter/ExprInterpreter.java @@ -15,43 +15,44 @@ */ package org.projectnessie.cel.interpreter; -import static org.projectnessie.cel.interpreter.InterpretablePlanner.newPlanner; -import static org.projectnessie.cel.interpreter.InterpretablePlanner.newUncheckedPlanner; - import com.google.api.expr.v1alpha1.CheckedExpr; import com.google.api.expr.v1alpha1.Expr; import com.google.api.expr.v1alpha1.Reference; import com.google.api.expr.v1alpha1.Type; +import java.util.HashMap; import java.util.Map; import org.projectnessie.cel.common.containers.Container; import org.projectnessie.cel.common.types.ref.TypeAdapter; import org.projectnessie.cel.common.types.ref.TypeProvider; +/** Default {@link Interpreter} implementation. */ final class ExprInterpreter implements Interpreter { private final Dispatcher dispatcher; private final Container container; private final TypeProvider provider; private final TypeAdapter adapter; private final AttributeFactory attrFactory; + private final PlanningPolicy planningPolicy; ExprInterpreter( Dispatcher dispatcher, Container container, TypeProvider provider, TypeAdapter adapter, - AttributeFactory attrFactory) { + AttributeFactory attrFactory, + PlanningPolicy planningPolicy) { this.dispatcher = dispatcher; this.container = container; this.provider = provider; this.adapter = adapter; this.attrFactory = attrFactory; + this.planningPolicy = planningPolicy; } @Override public Interpretable newInterpretable(CheckedExpr checked, InterpretableDecorator... decorators) { - InterpretablePlanner p = - newPlanner(dispatcher, provider, adapter, attrFactory, container, checked, decorators); - return p.plan(checked.getExpr()); + return newInterpretable( + checked.getExpr(), checked.getReferenceMapMap(), checked.getTypeMapMap(), decorators); } @Override @@ -60,16 +61,59 @@ public Interpretable newInterpretable( Map refMap, Map typeMap, InterpretableDecorator... decorators) { - InterpretablePlanner p = - newPlanner( - dispatcher, provider, adapter, attrFactory, container, refMap, typeMap, decorators); - return p.plan(expr); + return checkedPlanner(refMap, typeMap, decorators).plan(expr); } @Override public Interpretable newUncheckedInterpretable(Expr expr, InterpretableDecorator... decorators) { - InterpretablePlanner p = - newUncheckedPlanner(dispatcher, provider, adapter, attrFactory, container, decorators); - return p.plan(expr); + return uncheckedPlanner(decorators).plan(expr); + } + + Planner checkedPlanner(CheckedExpr checked, InterpretableDecorator... decorators) { + return checkedPlanner(checked.getReferenceMapMap(), checked.getTypeMapMap(), decorators); + } + + Planner checkedPlanner( + Map refMap, Map typeMap, InterpretableDecorator... decorators) { + PlanningPolicy effectivePolicy = effectiveCheckedPolicy(decorators); + InterpretableDecorator[] effectiveDecorators = + effectivePolicy == PlanningPolicy.NATIVE_OPTIMIZED + ? new InterpretableDecorator[0] + : decorators; + return new Planner( + dispatcher, + provider, + adapter, + attrFactory, + container, + refMap, + typeMap, + effectivePolicy, + effectiveDecorators); + } + + Planner uncheckedPlanner(InterpretableDecorator... decorators) { + return new Planner( + dispatcher, + provider, + adapter, + attrFactory, + container, + new HashMap<>(), + new HashMap<>(), + PlanningPolicy.ESTABLISHED_ONLY, + decorators); + } + + private PlanningPolicy effectiveCheckedPolicy(InterpretableDecorator[] decorators) { + if (!planningPolicy.nativeSpecializationPermitted()) { + return PlanningPolicy.ESTABLISHED_ONLY; + } + if (decorators.length == 0) { + return planningPolicy; + } + return decorators.length == 1 && decorators[0] == BuiltInOptimizer.INSTANCE + ? PlanningPolicy.NATIVE_OPTIMIZED + : PlanningPolicy.ESTABLISHED_ONLY; } } diff --git a/core/src/main/java/org/projectnessie/cel/interpreter/InterpretableDecorator.java b/core/src/main/java/org/projectnessie/cel/interpreter/InterpretableDecorator.java index 34dd021dc..e9eae3c3d 100644 --- a/core/src/main/java/org/projectnessie/cel/interpreter/InterpretableDecorator.java +++ b/core/src/main/java/org/projectnessie/cel/interpreter/InterpretableDecorator.java @@ -15,22 +15,8 @@ */ package org.projectnessie.cel.interpreter; -import static org.projectnessie.cel.common.types.BoolT.True; -import static org.projectnessie.cel.common.types.Err.throwErrorAsIllegalStateException; -import static org.projectnessie.cel.common.types.IntT.IntZero; -import static org.projectnessie.cel.interpreter.Activation.emptyActivation; -import static org.projectnessie.cel.interpreter.Interpretable.newConstValue; - -import java.util.HashSet; -import java.util.Set; -import org.projectnessie.cel.common.types.IteratorT; -import org.projectnessie.cel.common.types.Overloads; -import org.projectnessie.cel.common.types.Util; -import org.projectnessie.cel.common.types.ref.Type; import org.projectnessie.cel.common.types.ref.Val; -import org.projectnessie.cel.common.types.traits.Lister; import org.projectnessie.cel.interpreter.Interpretable.InterpretableAttribute; -import org.projectnessie.cel.interpreter.Interpretable.InterpretableCall; import org.projectnessie.cel.interpreter.Interpretable.InterpretableConst; /** @@ -107,106 +93,18 @@ static InterpretableDecorator decDisableShortcircuits() { } /** - * decOptimize optimizes the program plan by looking for common evaluation patterns and - * conditionally precomputating the result. + * Returns the built-in plan optimizer. * *

- */ - static InterpretableDecorator decOptimize() { - return i -> { - if (i instanceof EvalList) { - return maybeBuildListLiteral(i, (EvalList) i); - } - if (i instanceof EvalMap) { - return maybeBuildMapLiteral(i, (EvalMap) i); - } - if (i instanceof InterpretableCall inst) { - if (inst.overloadID().equals(Overloads.InList)) { - return maybeOptimizeSetMembership(i, inst); - } - if (Overloads.isTypeConversionFunction(inst.function())) { - return maybeOptimizeConstUnary(i, inst); - } - } - return i; - }; - } - - static Interpretable maybeOptimizeConstUnary(Interpretable i, InterpretableCall call) { - Interpretable[] args = call.args(); - if (args.length != 1) { - return i; - } - if (!(args[0] instanceof InterpretableConst)) { - return i; - } - Val val = call.eval(emptyActivation()); - throwErrorAsIllegalStateException(val); - return newConstValue(call.id(), val); - } - - static Interpretable maybeBuildListLiteral(Interpretable i, EvalList l) { - for (Interpretable elem : l.elems) { - if (!(elem instanceof InterpretableConst)) { - return i; - } - } - return newConstValue(l.id(), l.eval(emptyActivation())); - } - - static Interpretable maybeBuildMapLiteral(Interpretable i, EvalMap mp) { - for (int idx = 0; idx < mp.keys.length; idx++) { - if (!(mp.keys[idx] instanceof InterpretableConst)) { - return i; - } - if (!(mp.vals[idx] instanceof InterpretableConst)) { - return i; - } - } - return newConstValue(mp.id(), mp.eval(emptyActivation())); - } - - /** - * maybeOptimizeSetMembership may convert an 'in' operation against a list to map key membership - * test if the following conditions are true: * - * + *

Unsupported or semantically unsafe shapes retain their existing nodes. The optimizer does + * not guarantee that a particular expression is folded or specialized. */ - static Interpretable maybeOptimizeSetMembership(Interpretable i, InterpretableCall inlist) { - Interpretable[] args = inlist.args(); - Interpretable lhs = args[0]; - Interpretable rhs = args[1]; - if (!(rhs instanceof InterpretableConst l)) { - return i; - } - // When the incoming binary call is flagged with as the InList overload, the value will - // always be convertible to a `traits.Lister` type. - Lister list = (Lister) l.value(); - if (list.size() == IntZero) { - return new EvalSetMembership(inlist, lhs, null, Set.of()); - } - IteratorT it = list.iterator(); - Type typ = null; - Set valueSet = new HashSet<>(); - while (it.hasNext() == True) { - Val elem = it.next(); - if (!Util.isPrimitiveType(elem)) { - // Note, non-primitive type are not yet supported. - return i; - } - if (typ == null) { - typ = elem.type(); - } else if (!typ.typeName().equals(elem.type().typeName())) { - return i; - } - valueSet.add(elem); - } - return new EvalSetMembership(inlist, lhs, typ.typeName(), valueSet); + static InterpretableDecorator decOptimize() { + return BuiltInOptimizer.INSTANCE; } } diff --git a/core/src/main/java/org/projectnessie/cel/interpreter/InterpretablePlanner.java b/core/src/main/java/org/projectnessie/cel/interpreter/InterpretablePlanner.java index a5ba3e3c4..3121bab12 100644 --- a/core/src/main/java/org/projectnessie/cel/interpreter/InterpretablePlanner.java +++ b/core/src/main/java/org/projectnessie/cel/interpreter/InterpretablePlanner.java @@ -52,6 +52,7 @@ static InterpretablePlanner newPlanner( cont, checked.getReferenceMapMap(), checked.getTypeMapMap(), + PlanningPolicy.ESTABLISHED_ONLY, decorators); } @@ -68,7 +69,16 @@ static InterpretablePlanner newPlanner( Map refMap, Map typeMap, InterpretableDecorator... decorators) { - return new Planner(disp, provider, adapter, attrFactory, cont, refMap, typeMap, decorators); + return new Planner( + disp, + provider, + adapter, + attrFactory, + cont, + refMap, + typeMap, + PlanningPolicy.ESTABLISHED_ONLY, + decorators); } /** @@ -84,8 +94,14 @@ static InterpretablePlanner newUncheckedPlanner( Container cont, InterpretableDecorator... decorators) { return new Planner( - disp, provider, adapter, attrFactory, cont, new HashMap<>(), new HashMap<>(), decorators); + disp, + provider, + adapter, + attrFactory, + cont, + new HashMap<>(), + new HashMap<>(), + PlanningPolicy.ESTABLISHED_ONLY, + decorators); } - - /** planner is an implementatio of the interpretablePlanner interface. */ } diff --git a/core/src/main/java/org/projectnessie/cel/interpreter/Interpreter.java b/core/src/main/java/org/projectnessie/cel/interpreter/Interpreter.java index 4f351db46..ecf2d7609 100644 --- a/core/src/main/java/org/projectnessie/cel/interpreter/Interpreter.java +++ b/core/src/main/java/org/projectnessie/cel/interpreter/Interpreter.java @@ -81,8 +81,10 @@ static InterpretableDecorator exhaustiveEval(EvalState state) { } /** - * Optimize will pre-compute operations such as list and map construction and optimize call - * arguments to set membership tests. The set of optimizations will increase over time. + * Returns the established-plan decorator for constant folding and constant-data optimization. + * + *

This decorator is independent of native planning and does not guarantee that a particular + * expression is rewritten. */ static InterpretableDecorator optimize() { return decOptimize(); @@ -98,7 +100,31 @@ static Interpreter newInterpreter( TypeProvider provider, TypeAdapter adapter, AttributeFactory attrFactory) { - return new ExprInterpreter(dispatcher, container, provider, adapter, attrFactory); + return new ExprInterpreter( + dispatcher, container, provider, adapter, attrFactory, PlanningPolicy.ESTABLISHED_ONLY); + } + + /** + * Builds an interpreter with planning-time permission to use native specializations for eligible + * checked expressions. + * + *

Native planning remains disabled for unchecked expressions and whenever decorators are + * supplied directly to {@link #newInterpretable}. + */ + static Interpreter newInterpreter( + Dispatcher dispatcher, + Container container, + TypeProvider provider, + TypeAdapter adapter, + AttributeFactory attrFactory, + boolean allowNativePlanning) { + return new ExprInterpreter( + dispatcher, + container, + provider, + adapter, + attrFactory, + PlanningPolicy.nativeSpecialization(allowNativePlanning)); } /** diff --git a/core/src/main/java/org/projectnessie/cel/interpreter/NativeAttributeNodes.java b/core/src/main/java/org/projectnessie/cel/interpreter/NativeAttributeNodes.java new file mode 100644 index 000000000..0ede4de63 --- /dev/null +++ b/core/src/main/java/org/projectnessie/cel/interpreter/NativeAttributeNodes.java @@ -0,0 +1,127 @@ +/* + * Copyright (C) 2026 The Authors of CEL-Java + * + * 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 + * + * http://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 org.projectnessie.cel.interpreter; + +import static org.projectnessie.cel.common.types.Err.newErr; +import static org.projectnessie.cel.interpreter.ValueSignal.signal; + +import org.projectnessie.cel.common.types.ref.TypeAdapter; +import org.projectnessie.cel.interpreter.AttributeFactory.Attribute; + +abstract class NativeScalarAttr extends EvalAttr { + private final long semanticId; + private final Attribute partialAttribute; + + NativeScalarAttr(long id, TypeAdapter adapter, Attribute attribute, Attribute partialAttribute) { + super(adapter, attribute); + this.semanticId = id; + this.partialAttribute = partialAttribute; + } + + @Override + public final long id() { + return semanticId; + } + + final Object resolveNative(Activation activation) { + try { + return resolve(activation); + } catch (ValueSignal valueSignal) { + throw valueSignal; + } catch (Exception e) { + throw signal(newErr(e, e.toString())); + } + } + + @Override + public final Object resolve(Activation activation) { + return usesPartialAttribute(activation) + ? partialAttribute.resolve(activation) + : attr.resolve(activation); + } + + final boolean usesPartialAttribute(Activation activation) { + return activation instanceof Activation.PartialActivation && partialAttribute != null; + } +} + +class NativeIntAttr extends NativeScalarAttr implements NativeIntCapability { + NativeIntAttr(long id, TypeAdapter adapter, Attribute attribute, Attribute partialAttribute) { + super(id, adapter, attribute, partialAttribute); + } + + @Override + public long evalInt(Activation activation) { + return NativeSupport.intValue(adapter, resolveNative(activation)); + } +} + +final class NativeUintAttr extends NativeScalarAttr implements NativeUintCapability { + NativeUintAttr(long id, TypeAdapter adapter, Attribute attribute, Attribute partialAttribute) { + super(id, adapter, attribute, partialAttribute); + } + + @Override + public long evalUint(Activation activation) { + return NativeSupport.uintValue(adapter, resolveNative(activation)); + } +} + +final class NativeBooleanAttr extends NativeScalarAttr implements NativeBooleanCapability { + NativeBooleanAttr(long id, TypeAdapter adapter, Attribute attribute, Attribute partialAttribute) { + super(id, adapter, attribute, partialAttribute); + } + + @Override + public boolean evalBoolean(Activation activation) { + return NativeSupport.booleanValue(adapter, resolveNative(activation)); + } +} + +final class NativeDoubleAttr extends NativeScalarAttr implements NativeDoubleCapability { + NativeDoubleAttr(long id, TypeAdapter adapter, Attribute attribute, Attribute partialAttribute) { + super(id, adapter, attribute, partialAttribute); + } + + @Override + public double evalDouble(Activation activation) { + return NativeSupport.doubleValue(adapter, resolveNative(activation)); + } +} + +final class NativeStringAttr extends NativeScalarAttr implements NativeStringCapability { + NativeStringAttr(long id, TypeAdapter adapter, Attribute attribute, Attribute partialAttribute) { + super(id, adapter, attribute, partialAttribute); + } + + @Override + public String evalString(Activation activation) { + return NativeSupport.stringValue(adapter, resolveNative(activation)); + } +} + +final class NativeNullAttr extends NativeScalarAttr implements NativeNullCapability { + NativeNullAttr(long id, TypeAdapter adapter, Attribute attribute, Attribute partialAttribute) { + super(id, adapter, attribute, partialAttribute); + } + + @Override + public void evalNull(Activation activation) { + NativeSupport.nullValue(adapter, resolveNative(activation)); + } +} + +/** Checked exact aggregate field whose getter result remains raw until its consumer chooses. */ diff --git a/core/src/main/java/org/projectnessie/cel/interpreter/NativeCapabilities.java b/core/src/main/java/org/projectnessie/cel/interpreter/NativeCapabilities.java new file mode 100644 index 000000000..e3979642c --- /dev/null +++ b/core/src/main/java/org/projectnessie/cel/interpreter/NativeCapabilities.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2026 The Authors of CEL-Java + * + * 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 + * + * http://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 org.projectnessie.cel.interpreter; + +interface NativeIntCapability extends Interpretable { + long evalInt(Activation activation); +} + +interface NativeUintCapability extends Interpretable { + long evalUint(Activation activation); +} + +interface NativeDoubleCapability extends Interpretable { + double evalDouble(Activation activation); +} + +interface NativeBooleanCapability extends Interpretable { + boolean evalBoolean(Activation activation); +} + +interface NativeStringCapability extends Interpretable { + String evalString(Activation activation); +} + +interface NativeNullCapability extends Interpretable { + void evalNull(Activation activation); +} diff --git a/core/src/main/java/org/projectnessie/cel/interpreter/NativeIsland.java b/core/src/main/java/org/projectnessie/cel/interpreter/NativeIsland.java new file mode 100644 index 000000000..32a542a17 --- /dev/null +++ b/core/src/main/java/org/projectnessie/cel/interpreter/NativeIsland.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2026 The Authors of CEL-Java + * + * 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 + * + * http://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 org.projectnessie.cel.interpreter; + +import static java.util.Objects.requireNonNull; +import static org.projectnessie.cel.common.types.UintT.uintOf; + +import org.projectnessie.cel.common.types.ref.TypeAdapter; +import org.projectnessie.cel.common.types.ref.Val; + +/** One-way adaptation boundary around a maximal typed evaluation island. */ +final class NativeIsland implements Interpretable, Coster { + private final Interpretable root; + private final TypeAdapter adapter; + + NativeIsland(Interpretable root, TypeAdapter adapter) { + if (!(root instanceof NativeIntCapability) + && !(root instanceof NativeUintCapability) + && !(root instanceof NativeDoubleCapability) + && !(root instanceof NativeBooleanCapability) + && !(root instanceof NativeStringCapability) + && !(root instanceof NativeNullCapability)) { + throw new IllegalArgumentException("native island root has no terminal scalar capability"); + } + this.root = requireNonNull(root, "root"); + this.adapter = requireNonNull(adapter, "adapter"); + } + + static boolean supports(Interpretable interpretable) { + return interpretable instanceof NativeIntCapability + || interpretable instanceof NativeUintCapability + || interpretable instanceof NativeDoubleCapability + || interpretable instanceof NativeBooleanCapability + || interpretable instanceof NativeStringCapability + || interpretable instanceof NativeNullCapability; + } + + Interpretable root() { + return root; + } + + @Override + public long id() { + return root.id(); + } + + @Override + public Val eval(Activation activation) { + try { + if (root instanceof NativeIntCapability intCapability) { + return adapter.nativeToValue(intCapability.evalInt(activation)); + } + if (root instanceof NativeUintCapability uintCapability) { + return uintOf(uintCapability.evalUint(activation)); + } + if (root instanceof NativeBooleanCapability booleanCapability) { + return adapter.nativeToValue(booleanCapability.evalBoolean(activation)); + } + if (root instanceof NativeDoubleCapability doubleCapability) { + return adapter.nativeToValue(doubleCapability.evalDouble(activation)); + } + if (root instanceof NativeStringCapability stringCapability) { + return adapter.nativeToValue(stringCapability.evalString(activation)); + } + ((NativeNullCapability) root).evalNull(activation); + return org.projectnessie.cel.common.types.NullT.NullValue; + } catch (ValueSignal signal) { + return signal.value; + } + } + + @Override + public Cost cost() { + return Cost.estimateCost(root); + } +} diff --git a/core/src/main/java/org/projectnessie/cel/interpreter/NativeOverloadDescriptor.java b/core/src/main/java/org/projectnessie/cel/interpreter/NativeOverloadDescriptor.java new file mode 100644 index 000000000..c98e7a50d --- /dev/null +++ b/core/src/main/java/org/projectnessie/cel/interpreter/NativeOverloadDescriptor.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2026 The Authors of CEL-Java + * + * 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 + * + * http://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 org.projectnessie.cel.interpreter; + +import static java.util.Objects.requireNonNull; + +/** + * Interpreter-owned metadata for one checked overload that may gain a typed implementation. + * + *

The exact resolved {@code Overload} identity remains the primary provenance key. Function and + * overload names are additional checked-expression guards, not substitutes for that identity. + */ +record NativeOverloadDescriptor(String function, String overloadId) { + NativeOverloadDescriptor { + requireNonNull(function, "function"); + requireNonNull(overloadId, "overloadId"); + } + + boolean matches(String checkedFunction, String checkedOverloadId) { + return function.equals(checkedFunction) && overloadId.equals(checkedOverloadId); + } +} diff --git a/core/src/main/java/org/projectnessie/cel/interpreter/NativeScalarNodes.java b/core/src/main/java/org/projectnessie/cel/interpreter/NativeScalarNodes.java new file mode 100644 index 000000000..598e30c67 --- /dev/null +++ b/core/src/main/java/org/projectnessie/cel/interpreter/NativeScalarNodes.java @@ -0,0 +1,1178 @@ +/* + * Copyright (C) 2026 The Authors of CEL-Java + * + * 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 + * + * http://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 org.projectnessie.cel.interpreter; + +import static java.util.Objects.requireNonNull; +import static org.projectnessie.cel.common.types.DoubleT.doubleOf; +import static org.projectnessie.cel.common.types.Err.divideByZero; +import static org.projectnessie.cel.common.types.Err.errIntOverflow; +import static org.projectnessie.cel.common.types.Err.isError; +import static org.projectnessie.cel.common.types.Err.maybeNoSuchOverloadErr; +import static org.projectnessie.cel.common.types.Err.modulusByZero; +import static org.projectnessie.cel.common.types.Err.noSuchOverload; +import static org.projectnessie.cel.common.types.IntT.intOf; +import static org.projectnessie.cel.common.types.Overflow.addInt64Checked; +import static org.projectnessie.cel.common.types.Overflow.divideInt64Checked; +import static org.projectnessie.cel.common.types.Overflow.moduloInt64Checked; +import static org.projectnessie.cel.common.types.Overflow.multiplyInt64Checked; +import static org.projectnessie.cel.common.types.Overflow.negateInt64Checked; +import static org.projectnessie.cel.common.types.Overflow.subtractInt64Checked; +import static org.projectnessie.cel.common.types.StringT.stringOf; +import static org.projectnessie.cel.common.types.Types.boolOf; +import static org.projectnessie.cel.common.types.UintT.uintOf; +import static org.projectnessie.cel.common.types.UnknownT.isUnknown; +import static org.projectnessie.cel.interpreter.ValueSignal.signal; + +import org.projectnessie.cel.common.operators.Operator; +import org.projectnessie.cel.common.types.BoolT; +import org.projectnessie.cel.common.types.DoubleT; +import org.projectnessie.cel.common.types.IntT; +import org.projectnessie.cel.common.types.NullT; +import org.projectnessie.cel.common.types.Overflow.OverflowException; +import org.projectnessie.cel.common.types.StringT; +import org.projectnessie.cel.common.types.UintT; +import org.projectnessie.cel.common.types.ref.TypeAdapter; +import org.projectnessie.cel.common.types.ref.Val; +import org.projectnessie.cel.interpreter.AttributeFactory.Attribute; +import org.projectnessie.cel.interpreter.functions.Overload; + +final class NativeIntConst extends EvalConst implements NativeIntCapability { + private final long value; + + NativeIntConst(long id, long value) { + super(id, intOf(value)); + this.value = value; + } + + NativeIntConst(long id, IntT value) { + super(id, value); + this.value = value.intValue(); + } + + @Override + public long evalInt(Activation activation) { + return value; + } +} + +final class NativeUintConst extends EvalConst implements NativeUintCapability { + private final long value; + + NativeUintConst(long id, long value) { + super(id, uintOf(value)); + this.value = value; + } + + NativeUintConst(long id, UintT value) { + super(id, value); + this.value = value.intValue(); + } + + @Override + public long evalUint(Activation activation) { + return value; + } +} + +final class NativeDoubleConst extends EvalConst implements NativeDoubleCapability { + private final double value; + + NativeDoubleConst(long id, double value) { + super(id, doubleOf(value)); + this.value = value; + } + + NativeDoubleConst(long id, DoubleT value) { + super(id, value); + this.value = value.doubleValue(); + } + + @Override + public double evalDouble(Activation activation) { + return value; + } +} + +final class NativeBooleanConst extends EvalConst implements NativeBooleanCapability { + private final boolean value; + + NativeBooleanConst(long id, boolean value) { + super(id, boolOf(value)); + this.value = value; + } + + NativeBooleanConst(long id, BoolT value) { + super(id, value); + this.value = value.booleanValue(); + } + + @Override + public boolean evalBoolean(Activation activation) { + return value; + } +} + +final class NativeStringConst extends EvalConst implements NativeStringCapability { + private final String value; + + NativeStringConst(long id, String value) { + super(id, stringOf(value)); + this.value = value; + } + + NativeStringConst(long id, StringT value) { + super(id, value); + this.value = requireNonNull((String) value.value()); + } + + @Override + public String evalString(Activation activation) { + return value; + } +} + +final class NativeNullConst extends EvalConst implements NativeNullCapability { + NativeNullConst(long id) { + super(id, NullT.NullValue); + } + + @Override + public void evalNull(Activation activation) {} +} + +final class NativeIntIdent extends EvalIdent implements NativeIntCapability { + NativeIntIdent(long id, String name, TypeAdapter adapter) { + super(id, name, adapter); + } + + @Override + public long evalInt(Activation activation) { + return NativeSupport.intValue(adapter, resolveRaw(activation)); + } +} + +final class NativeUintIdent extends EvalIdent implements NativeUintCapability { + NativeUintIdent(long id, String name, TypeAdapter adapter) { + super(id, name, adapter); + } + + @Override + public long evalUint(Activation activation) { + return NativeSupport.uintValue(adapter, resolveRaw(activation)); + } +} + +final class NativeBooleanIdent extends EvalIdent implements NativeBooleanCapability { + NativeBooleanIdent(long id, String name, TypeAdapter adapter) { + super(id, name, adapter); + } + + @Override + public boolean evalBoolean(Activation activation) { + return NativeSupport.booleanValue(adapter, resolveRaw(activation)); + } +} + +final class NativeDoubleIdent extends EvalIdent implements NativeDoubleCapability { + NativeDoubleIdent(long id, String name, TypeAdapter adapter) { + super(id, name, adapter); + } + + @Override + public double evalDouble(Activation activation) { + return NativeSupport.doubleValue(adapter, resolveRaw(activation)); + } +} + +final class NativeStringIdent extends EvalIdent implements NativeStringCapability { + NativeStringIdent(long id, String name, TypeAdapter adapter) { + super(id, name, adapter); + } + + @Override + public String evalString(Activation activation) { + return NativeSupport.stringValue(adapter, resolveRaw(activation)); + } +} + +final class NativeNullIdent extends EvalIdent implements NativeNullCapability { + NativeNullIdent(long id, String name, TypeAdapter adapter) { + super(id, name, adapter); + } + + @Override + public void evalNull(Activation activation) { + NativeSupport.nullValue(adapter, resolveRaw(activation)); + } +} + +class NativeIntBinary extends EvalBinary implements NativeIntCapability { + private final NativeArithmetic operation; + + NativeIntBinary( + long id, + String function, + String overload, + Interpretable left, + Interpretable right, + Overload implementation, + NativeArithmetic operation) { + super(id, function, overload, left, right, implementation.operandTrait, implementation.binary); + this.operation = operation; + } + + @SuppressWarnings({"DuplicatedCode", "ReassignedVariable", "SuspiciousNameCombination"}) + @Override + public final long evalInt(Activation activation) { + long leftValue = 0L; + long rightValue = 0L; + Val leftSlow = null; + Val rightSlow = null; + try { + leftValue = ((NativeIntCapability) lhs).evalInt(activation); + } catch (ValueSignal signal) { + leftSlow = signal.value; + } + try { + rightValue = ((NativeIntCapability) rhs).evalInt(activation); + } catch (ValueSignal signal) { + rightSlow = signal.value; + } + if (leftSlow != null || rightSlow != null) { + return NativeScalarContinuations.intResult( + evalPrepared( + leftSlow != null ? leftSlow : intOf(leftValue), + rightSlow != null ? rightSlow : intOf(rightValue))); + } + try { + return switch (operation) { + case ADD -> addInt64Checked(leftValue, rightValue); + case SUBTRACT -> subtractInt64Checked(leftValue, rightValue); + case MULTIPLY -> multiplyInt64Checked(leftValue, rightValue); + case DIVIDE -> { + if (rightValue == 0L) { + throw signal(divideByZero()); + } + yield divideInt64Checked(leftValue, rightValue); + } + case MODULO -> { + if (rightValue == 0L) { + throw signal(modulusByZero()); + } + yield moduloInt64Checked(leftValue, rightValue); + } + }; + } catch (OverflowException e) { + throw signal(errIntOverflow); + } + } +} + +final class NativeIntAdd extends NativeIntBinary { + NativeIntAdd( + long id, + String function, + String overload, + Interpretable left, + Interpretable right, + Overload implementation) { + super(id, function, overload, left, right, implementation, NativeArithmetic.ADD); + } +} + +final class NativeBooleanNot extends EvalUnary implements NativeBooleanCapability { + NativeBooleanNot( + long id, String function, String overload, Interpretable argument, Overload implementation) { + super(id, function, overload, argument, implementation.operandTrait, implementation.unary); + } + + @Override + public boolean evalBoolean(Activation activation) { + try { + return !((NativeBooleanCapability) arg).evalBoolean(activation); + } catch (ValueSignal signal) { + return NativeScalarContinuations.booleanResult(evalPrepared(signal.value)); + } + } +} + +final class NativeIntNegate extends EvalUnary implements NativeIntCapability { + NativeIntNegate( + long id, String function, String overload, Interpretable argument, Overload implementation) { + super(id, function, overload, argument, implementation.operandTrait, implementation.unary); + } + + @Override + public long evalInt(Activation activation) { + try { + return negateInt64Checked(((NativeIntCapability) arg).evalInt(activation)); + } catch (ValueSignal signal) { + return NativeScalarContinuations.intResult(evalPrepared(signal.value)); + } catch (OverflowException e) { + throw signal(errIntOverflow); + } + } +} + +final class NativeDoubleNegate extends EvalUnary implements NativeDoubleCapability { + NativeDoubleNegate( + long id, String function, String overload, Interpretable argument, Overload implementation) { + super(id, function, overload, argument, implementation.operandTrait, implementation.unary); + } + + @Override + public double evalDouble(Activation activation) { + try { + return -((NativeDoubleCapability) arg).evalDouble(activation); + } catch (ValueSignal signal) { + return NativeScalarContinuations.doubleResult(evalPrepared(signal.value)); + } + } +} + +final class NativeDoubleBinary extends EvalBinary implements NativeDoubleCapability { + private final NativeArithmetic operation; + + NativeDoubleBinary( + long id, + String function, + String overload, + Interpretable left, + Interpretable right, + Overload implementation, + NativeArithmetic operation) { + super(id, function, overload, left, right, implementation.operandTrait, implementation.binary); + this.operation = operation; + } + + @SuppressWarnings("DuplicatedCode") + @Override + public double evalDouble(Activation activation) { + double leftValue = 0.0d; + double rightValue = 0.0d; + Val leftSlow = null; + Val rightSlow = null; + try { + leftValue = ((NativeDoubleCapability) lhs).evalDouble(activation); + } catch (ValueSignal signal) { + leftSlow = signal.value; + } + try { + rightValue = ((NativeDoubleCapability) rhs).evalDouble(activation); + } catch (ValueSignal signal) { + rightSlow = signal.value; + } + if (leftSlow != null || rightSlow != null) { + return NativeScalarContinuations.doubleResult( + evalPrepared( + leftSlow != null ? leftSlow : doubleOf(leftValue), + rightSlow != null ? rightSlow : doubleOf(rightValue))); + } + return switch (operation) { + case ADD -> leftValue + rightValue; + case SUBTRACT -> leftValue - rightValue; + case MULTIPLY -> leftValue * rightValue; + case DIVIDE -> leftValue / rightValue; + case MODULO -> throw new IllegalStateException("double modulo is not supported"); + }; + } +} + +final class NativeStringConcat extends EvalBinary implements NativeStringCapability { + NativeStringConcat( + long id, + String function, + String overload, + Interpretable left, + Interpretable right, + Overload implementation) { + super(id, function, overload, left, right, implementation.operandTrait, implementation.binary); + } + + @SuppressWarnings("DuplicatedCode") + @Override + public String evalString(Activation activation) { + String leftValue = null; + String rightValue = null; + Val leftSlow = null; + Val rightSlow = null; + try { + leftValue = ((NativeStringCapability) lhs).evalString(activation); + } catch (ValueSignal signal) { + leftSlow = signal.value; + } + try { + rightValue = ((NativeStringCapability) rhs).evalString(activation); + } catch (ValueSignal signal) { + rightSlow = signal.value; + } + if (leftSlow != null || rightSlow != null) { + return NativeScalarContinuations.stringResult( + evalPrepared( + leftSlow != null ? leftSlow : stringOf(leftValue), + rightSlow != null ? rightSlow : stringOf(rightValue))); + } + return leftValue + rightValue; + } +} + +class NativeScalarEq extends EvalEq implements NativeBooleanCapability { + private final NativeScalarKind kind; + + NativeScalarEq(long id, Interpretable left, Interpretable right, NativeScalarKind kind) { + super(id, left, right); + this.kind = kind; + } + + @Override + public final boolean evalBoolean(Activation activation) { + return NativeScalarEquality.evaluate(kind, lhs, rhs, activation, this, null); + } +} + +final class NativeScalarNe extends EvalNe implements NativeBooleanCapability { + private final NativeScalarKind kind; + + NativeScalarNe(long id, Interpretable left, Interpretable right, NativeScalarKind kind) { + super(id, left, right); + this.kind = kind; + } + + @Override + public boolean evalBoolean(Activation activation) { + return NativeScalarEquality.evaluate(kind, lhs, rhs, activation, null, this); + } +} + +final class NativeBooleanEq extends NativeScalarEq { + NativeBooleanEq(long id, Interpretable left, Interpretable right) { + super(id, left, right, NativeScalarKind.BOOLEAN); + } +} + +final class NativeIntEq extends NativeScalarEq { + NativeIntEq(long id, Interpretable left, Interpretable right) { + super(id, left, right, NativeScalarKind.INT); + } +} + +final class NativeScalarEquality { + private NativeScalarEquality() {} + + static boolean evaluate( + NativeScalarKind kind, + Interpretable left, + Interpretable right, + Activation activation, + EvalEq equality, + EvalNe inequality) { + return switch (kind) { + case BOOLEAN -> equalBoolean(left, right, activation, equality, inequality); + case INT -> equalInt(left, right, activation, equality, inequality); + case UINT -> equalUint(left, right, activation, equality, inequality); + case DOUBLE -> equalDouble(left, right, activation, equality, inequality); + case STRING -> equalString(left, right, activation, equality, inequality); + case NULL -> equalNull(left, right, activation, equality, inequality); + }; + } + + @SuppressWarnings("DuplicatedCode") + private static boolean equalBoolean( + Interpretable left, + Interpretable right, + Activation activation, + EvalEq equality, + EvalNe inequality) { + boolean leftValue = false; + boolean rightValue = false; + Val leftSlow = null; + Val rightSlow = null; + try { + leftValue = ((NativeBooleanCapability) left).evalBoolean(activation); + } catch (ValueSignal valueSignal) { + leftSlow = valueSignal.value; + } + try { + rightValue = ((NativeBooleanCapability) right).evalBoolean(activation); + } catch (ValueSignal valueSignal) { + rightSlow = valueSignal.value; + } + if (leftSlow != null || rightSlow != null) { + return slow( + leftSlow != null ? leftSlow : boolOf(leftValue), + rightSlow != null ? rightSlow : boolOf(rightValue), + equality, + inequality); + } + return result(leftValue == rightValue, inequality); + } + + @SuppressWarnings("DuplicatedCode") + private static boolean equalInt( + Interpretable left, + Interpretable right, + Activation activation, + EvalEq equality, + EvalNe inequality) { + long leftValue = 0L; + long rightValue = 0L; + Val leftSlow = null; + Val rightSlow = null; + try { + leftValue = ((NativeIntCapability) left).evalInt(activation); + } catch (ValueSignal valueSignal) { + leftSlow = valueSignal.value; + } + try { + rightValue = ((NativeIntCapability) right).evalInt(activation); + } catch (ValueSignal valueSignal) { + rightSlow = valueSignal.value; + } + if (leftSlow != null || rightSlow != null) { + return slow( + leftSlow != null ? leftSlow : intOf(leftValue), + rightSlow != null ? rightSlow : intOf(rightValue), + equality, + inequality); + } + return result(leftValue == rightValue, inequality); + } + + @SuppressWarnings("DuplicatedCode") + private static boolean equalUint( + Interpretable left, + Interpretable right, + Activation activation, + EvalEq equality, + EvalNe inequality) { + long leftValue = 0L; + long rightValue = 0L; + Val leftSlow = null; + Val rightSlow = null; + try { + leftValue = ((NativeUintCapability) left).evalUint(activation); + } catch (ValueSignal valueSignal) { + leftSlow = valueSignal.value; + } + try { + rightValue = ((NativeUintCapability) right).evalUint(activation); + } catch (ValueSignal valueSignal) { + rightSlow = valueSignal.value; + } + if (leftSlow != null || rightSlow != null) { + return slow( + leftSlow != null ? leftSlow : uintOf(leftValue), + rightSlow != null ? rightSlow : uintOf(rightValue), + equality, + inequality); + } + return result(leftValue == rightValue, inequality); + } + + @SuppressWarnings("DuplicatedCode") + private static boolean equalDouble( + Interpretable left, + Interpretable right, + Activation activation, + EvalEq equality, + EvalNe inequality) { + double leftValue = 0.0d; + double rightValue = 0.0d; + Val leftSlow = null; + Val rightSlow = null; + try { + leftValue = ((NativeDoubleCapability) left).evalDouble(activation); + } catch (ValueSignal valueSignal) { + leftSlow = valueSignal.value; + } + try { + rightValue = ((NativeDoubleCapability) right).evalDouble(activation); + } catch (ValueSignal valueSignal) { + rightSlow = valueSignal.value; + } + if (leftSlow != null || rightSlow != null) { + return slow( + leftSlow != null ? leftSlow : doubleOf(leftValue), + rightSlow != null ? rightSlow : doubleOf(rightValue), + equality, + inequality); + } + return result(leftValue == rightValue, inequality); + } + + @SuppressWarnings("DuplicatedCode") + private static boolean equalString( + Interpretable left, + Interpretable right, + Activation activation, + EvalEq equality, + EvalNe inequality) { + String leftValue = null; + String rightValue = null; + Val leftSlow = null; + Val rightSlow = null; + try { + leftValue = ((NativeStringCapability) left).evalString(activation); + } catch (ValueSignal valueSignal) { + leftSlow = valueSignal.value; + } + try { + rightValue = ((NativeStringCapability) right).evalString(activation); + } catch (ValueSignal valueSignal) { + rightSlow = valueSignal.value; + } + if (leftSlow != null || rightSlow != null) { + return slow( + leftSlow != null ? leftSlow : stringOf(leftValue), + rightSlow != null ? rightSlow : stringOf(rightValue), + equality, + inequality); + } + String fastLeft = requireNonNull(leftValue, "native string capability returned null"); + String fastRight = requireNonNull(rightValue, "native string capability returned null"); + return result(fastLeft.equals(fastRight), inequality); + } + + private static boolean equalNull( + Interpretable left, + Interpretable right, + Activation activation, + EvalEq equality, + EvalNe inequality) { + Val leftSlow = null; + Val rightSlow = null; + try { + ((NativeNullCapability) left).evalNull(activation); + } catch (ValueSignal valueSignal) { + leftSlow = valueSignal.value; + } + try { + ((NativeNullCapability) right).evalNull(activation); + } catch (ValueSignal valueSignal) { + rightSlow = valueSignal.value; + } + if (leftSlow == null && rightSlow == null) { + return result(true, inequality); + } + return slow( + leftSlow != null ? leftSlow : NullT.NullValue, + rightSlow != null ? rightSlow : NullT.NullValue, + equality, + inequality); + } + + private static boolean slow(Val left, Val right, EvalEq equality, EvalNe inequality) { + return NativeScalarContinuations.booleanResult( + equality != null + ? equality.evalPrepared(left, right) + : inequality.evalPrepared(left, right)); + } + + private static boolean result(boolean equal, EvalNe inequality) { + return (inequality == null) == equal; + } +} + +final class NativeLogicalAnd extends EvalAnd implements NativeBooleanCapability { + NativeLogicalAnd(long id, Interpretable left, Interpretable right) { + super(id, left, right); + } + + @Override + public boolean evalBoolean(Activation activation) { + return NativeLogical.evaluate(lhs, rhs, activation, true); + } +} + +final class NativeLogicalOr extends EvalOr implements NativeBooleanCapability { + NativeLogicalOr(long id, Interpretable left, Interpretable right) { + super(id, left, right); + } + + @Override + public boolean evalBoolean(Activation activation) { + return NativeLogical.evaluate(lhs, rhs, activation, false); + } +} + +final class NativeLogical { + private NativeLogical() {} + + @SuppressWarnings("DuplicatedCode") + static boolean evaluate( + Interpretable left, Interpretable right, Activation activation, boolean and) { + boolean leftValue = false; + Val leftSlow = null; + try { + leftValue = ((NativeBooleanCapability) left).evalBoolean(activation); + } catch (ValueSignal valueSignal) { + leftSlow = valueSignal.value; + } + if (leftSlow == null && leftValue != and) { + return !and; + } + + boolean rightValue = false; + Val rightSlow = null; + try { + rightValue = ((NativeBooleanCapability) right).evalBoolean(activation); + } catch (ValueSignal valueSignal) { + rightSlow = valueSignal.value; + } + if (rightSlow == null && rightValue != and) { + return !and; + } + if (leftSlow == null && rightSlow == null) { + return and; + } + + Val leftResult = leftSlow != null ? leftSlow : boolOf(leftValue); + Val rightResult = rightSlow != null ? rightSlow : boolOf(rightValue); + if (isUnknown(leftResult)) { + throw signal(leftResult); + } + if (isUnknown(rightResult)) { + throw signal(rightResult); + } + if (isError(leftResult)) { + throw signal(leftResult); + } + throw signal( + noSuchOverload( + leftResult, and ? Operator.LogicalAnd.id : Operator.LogicalOr.id, rightResult)); + } +} + +abstract class NativeConditional extends EvalAttr { + final NativeBooleanCapability condition; + + NativeConditional(TypeAdapter adapter, Attribute attribute, NativeBooleanCapability condition) { + super(adapter, attribute); + this.condition = condition; + } + + final boolean selectTruthy(Activation activation) { + try { + return condition.evalBoolean(activation); + } catch (ValueSignal valueSignal) { + if (isUnknown(valueSignal.value)) { + throw valueSignal; + } + if (isError(valueSignal.value)) { + throw NativeSupport.propagatedError(valueSignal.value); + } + throw signal(maybeNoSuchOverloadErr(valueSignal.value)); + } + } + + final ValueSignal selectedBranch(ValueSignal valueSignal, Interpretable branch) { + // Attribute branches resolve directly; only relative attributes wrap returned errors. + if (branch instanceof InterpretableAttribute || !isError(valueSignal.value)) { + return valueSignal; + } + return NativeSupport.propagatedError(valueSignal.value); + } +} + +final class NativeBooleanConditional extends NativeConditional implements NativeBooleanCapability { + private final NativeBooleanCapability truthy; + private final NativeBooleanCapability falsy; + + NativeBooleanConditional( + TypeAdapter adapter, + Attribute attribute, + NativeBooleanCapability condition, + NativeBooleanCapability truthy, + NativeBooleanCapability falsy) { + super(adapter, attribute, condition); + this.truthy = truthy; + this.falsy = falsy; + } + + @Override + public boolean evalBoolean(Activation activation) { + boolean selectTruthy = selectTruthy(activation); + try { + return selectTruthy ? truthy.evalBoolean(activation) : falsy.evalBoolean(activation); + } catch (ValueSignal valueSignal) { + throw selectedBranch(valueSignal, selectTruthy ? truthy : falsy); + } + } +} + +final class NativeIntConditional extends NativeConditional implements NativeIntCapability { + private final NativeIntCapability truthy; + private final NativeIntCapability falsy; + + NativeIntConditional( + TypeAdapter adapter, + Attribute attribute, + NativeBooleanCapability condition, + NativeIntCapability truthy, + NativeIntCapability falsy) { + super(adapter, attribute, condition); + this.truthy = truthy; + this.falsy = falsy; + } + + @Override + public long evalInt(Activation activation) { + boolean selectTruthy = selectTruthy(activation); + try { + return selectTruthy ? truthy.evalInt(activation) : falsy.evalInt(activation); + } catch (ValueSignal valueSignal) { + throw selectedBranch(valueSignal, selectTruthy ? truthy : falsy); + } + } +} + +final class NativeUintConditional extends NativeConditional implements NativeUintCapability { + private final NativeUintCapability truthy; + private final NativeUintCapability falsy; + + NativeUintConditional( + TypeAdapter adapter, + Attribute attribute, + NativeBooleanCapability condition, + NativeUintCapability truthy, + NativeUintCapability falsy) { + super(adapter, attribute, condition); + this.truthy = truthy; + this.falsy = falsy; + } + + @Override + public long evalUint(Activation activation) { + boolean selectTruthy = selectTruthy(activation); + try { + return selectTruthy ? truthy.evalUint(activation) : falsy.evalUint(activation); + } catch (ValueSignal valueSignal) { + throw selectedBranch(valueSignal, selectTruthy ? truthy : falsy); + } + } +} + +final class NativeDoubleConditional extends NativeConditional implements NativeDoubleCapability { + private final NativeDoubleCapability truthy; + private final NativeDoubleCapability falsy; + + NativeDoubleConditional( + TypeAdapter adapter, + Attribute attribute, + NativeBooleanCapability condition, + NativeDoubleCapability truthy, + NativeDoubleCapability falsy) { + super(adapter, attribute, condition); + this.truthy = truthy; + this.falsy = falsy; + } + + @Override + public double evalDouble(Activation activation) { + boolean selectTruthy = selectTruthy(activation); + try { + return selectTruthy ? truthy.evalDouble(activation) : falsy.evalDouble(activation); + } catch (ValueSignal valueSignal) { + throw selectedBranch(valueSignal, selectTruthy ? truthy : falsy); + } + } +} + +final class NativeStringConditional extends NativeConditional implements NativeStringCapability { + private final NativeStringCapability truthy; + private final NativeStringCapability falsy; + + NativeStringConditional( + TypeAdapter adapter, + Attribute attribute, + NativeBooleanCapability condition, + NativeStringCapability truthy, + NativeStringCapability falsy) { + super(adapter, attribute, condition); + this.truthy = truthy; + this.falsy = falsy; + } + + @Override + public String evalString(Activation activation) { + boolean selectTruthy = selectTruthy(activation); + try { + return selectTruthy ? truthy.evalString(activation) : falsy.evalString(activation); + } catch (ValueSignal valueSignal) { + throw selectedBranch(valueSignal, selectTruthy ? truthy : falsy); + } + } +} + +final class NativeNullConditional extends NativeConditional implements NativeNullCapability { + private final NativeNullCapability truthy; + private final NativeNullCapability falsy; + + NativeNullConditional( + TypeAdapter adapter, + Attribute attribute, + NativeBooleanCapability condition, + NativeNullCapability truthy, + NativeNullCapability falsy) { + super(adapter, attribute, condition); + this.truthy = truthy; + this.falsy = falsy; + } + + @Override + public void evalNull(Activation activation) { + boolean selectTruthy = selectTruthy(activation); + try { + if (selectTruthy) { + truthy.evalNull(activation); + } else { + falsy.evalNull(activation); + } + } catch (ValueSignal valueSignal) { + throw selectedBranch(valueSignal, selectTruthy ? truthy : falsy); + } + } +} + +final class NativeScalarComparison extends EvalBinary implements NativeBooleanCapability { + private final NativeScalarKind kind; + private final NativeComparison comparison; + + NativeScalarComparison( + long id, + String function, + String overload, + Interpretable left, + Interpretable right, + Overload implementation, + NativeScalarKind kind, + NativeComparison comparison) { + super(id, function, overload, left, right, implementation.operandTrait, implementation.binary); + this.kind = kind; + this.comparison = comparison; + } + + @Override + public boolean evalBoolean(Activation activation) { + return switch (kind) { + case BOOLEAN -> compareBoolean(activation); + case INT -> compareInt(activation); + case UINT -> compareUint(activation); + case DOUBLE -> compareDouble(activation); + case STRING -> compareString(activation); + case NULL -> throw new IllegalStateException("null values are not ordered"); + }; + } + + @SuppressWarnings("DuplicatedCode") + private boolean compareBoolean(Activation activation) { + boolean leftValue = false; + boolean rightValue = false; + Val leftSlow = null; + Val rightSlow = null; + try { + leftValue = ((NativeBooleanCapability) lhs).evalBoolean(activation); + } catch (ValueSignal signal) { + leftSlow = signal.value; + } + try { + rightValue = ((NativeBooleanCapability) rhs).evalBoolean(activation); + } catch (ValueSignal signal) { + rightSlow = signal.value; + } + if (leftSlow != null || rightSlow != null) { + return slowComparison( + leftSlow != null ? leftSlow : boolOf(leftValue), + rightSlow != null ? rightSlow : boolOf(rightValue)); + } + return comparison.test(Boolean.compare(leftValue, rightValue)); + } + + @SuppressWarnings("DuplicatedCode") + private boolean compareInt(Activation activation) { + long leftValue = 0L; + long rightValue = 0L; + Val leftSlow = null; + Val rightSlow = null; + try { + leftValue = ((NativeIntCapability) lhs).evalInt(activation); + } catch (ValueSignal signal) { + leftSlow = signal.value; + } + try { + rightValue = ((NativeIntCapability) rhs).evalInt(activation); + } catch (ValueSignal signal) { + rightSlow = signal.value; + } + if (leftSlow != null || rightSlow != null) { + return slowComparison( + leftSlow != null ? leftSlow : intOf(leftValue), + rightSlow != null ? rightSlow : intOf(rightValue)); + } + return comparison.test(Long.compare(leftValue, rightValue)); + } + + @SuppressWarnings("DuplicatedCode") + private boolean compareUint(Activation activation) { + long leftValue = 0L; + long rightValue = 0L; + Val leftSlow = null; + Val rightSlow = null; + try { + leftValue = ((NativeUintCapability) lhs).evalUint(activation); + } catch (ValueSignal signal) { + leftSlow = signal.value; + } + try { + rightValue = ((NativeUintCapability) rhs).evalUint(activation); + } catch (ValueSignal signal) { + rightSlow = signal.value; + } + if (leftSlow != null || rightSlow != null) { + return slowComparison( + leftSlow != null ? leftSlow : uintOf(leftValue), + rightSlow != null ? rightSlow : uintOf(rightValue)); + } + return comparison.test(Long.compareUnsigned(leftValue, rightValue)); + } + + @SuppressWarnings("DuplicatedCode") + private boolean compareDouble(Activation activation) { + double leftValue = 0.0d; + double rightValue = 0.0d; + Val leftSlow = null; + Val rightSlow = null; + try { + leftValue = ((NativeDoubleCapability) lhs).evalDouble(activation); + } catch (ValueSignal signal) { + leftSlow = signal.value; + } + try { + rightValue = ((NativeDoubleCapability) rhs).evalDouble(activation); + } catch (ValueSignal signal) { + rightSlow = signal.value; + } + if (leftSlow != null || rightSlow != null) { + return slowComparison( + leftSlow != null ? leftSlow : doubleOf(leftValue), + rightSlow != null ? rightSlow : doubleOf(rightValue)); + } + int result = leftValue == rightValue ? 0 : Double.compare(leftValue, rightValue); + return comparison.test(result); + } + + @SuppressWarnings("DuplicatedCode") + private boolean compareString(Activation activation) { + String leftValue = null; + String rightValue = null; + Val leftSlow = null; + Val rightSlow = null; + try { + leftValue = ((NativeStringCapability) lhs).evalString(activation); + } catch (ValueSignal signal) { + leftSlow = signal.value; + } + try { + rightValue = ((NativeStringCapability) rhs).evalString(activation); + } catch (ValueSignal signal) { + rightSlow = signal.value; + } + if (leftSlow != null || rightSlow != null) { + return slowComparison( + leftSlow != null ? leftSlow : stringOf(leftValue), + rightSlow != null ? rightSlow : stringOf(rightValue)); + } + String fastLeft = requireNonNull(leftValue, "native string capability returned null"); + String fastRight = requireNonNull(rightValue, "native string capability returned null"); + return comparison.test(fastLeft.compareTo(fastRight)); + } + + private boolean slowComparison(Val leftValue, Val rightValue) { + return NativeScalarContinuations.booleanResult(evalPrepared(leftValue, rightValue)); + } +} + +enum NativeArithmetic { + ADD, + SUBTRACT, + MULTIPLY, + DIVIDE, + MODULO +} + +enum NativeComparison { + LESS, + LESS_EQUALS, + GREATER, + GREATER_EQUALS; + + boolean test(int result) { + return switch (this) { + case LESS -> result < 0; + case LESS_EQUALS -> result <= 0; + case GREATER -> result > 0; + case GREATER_EQUALS -> result >= 0; + }; + } +} + +enum NativeScalarKind { + BOOLEAN, + INT, + UINT, + DOUBLE, + STRING, + NULL +} + +final class NativeScalarContinuations { + private NativeScalarContinuations() {} + + static boolean booleanResult(Val result) { + if (result instanceof BoolT) { + return result.booleanValue(); + } + throw signal(result); + } + + static long intResult(Val result) { + if (result instanceof IntT) { + return result.intValue(); + } + throw signal(result); + } + + static long uintResult(Val result) { + if (result instanceof UintT) { + return result.intValue(); + } + throw signal(result); + } + + static double doubleResult(Val result) { + if (result instanceof DoubleT) { + return result.doubleValue(); + } + throw signal(result); + } + + static String stringResult(Val result) { + if (result instanceof StringT && result.value() != null) { + return (String) result.value(); + } + throw signal(result); + } +} diff --git a/core/src/main/java/org/projectnessie/cel/interpreter/NativeSupport.java b/core/src/main/java/org/projectnessie/cel/interpreter/NativeSupport.java new file mode 100644 index 000000000..dfd70188d --- /dev/null +++ b/core/src/main/java/org/projectnessie/cel/interpreter/NativeSupport.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2026 The Authors of CEL-Java + * + * 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 + * + * http://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 org.projectnessie.cel.interpreter; + +import static org.projectnessie.cel.common.types.Err.newErr; +import static org.projectnessie.cel.interpreter.ValueSignal.signal; + +import org.projectnessie.cel.common.ULong; +import org.projectnessie.cel.common.types.Err.ErrException; +import org.projectnessie.cel.common.types.NullT; +import org.projectnessie.cel.common.types.StringT; +import org.projectnessie.cel.common.types.ref.TypeAdapter; +import org.projectnessie.cel.common.types.ref.Val; + +final class NativeSupport { + private NativeSupport() {} + + static Val adapt(TypeAdapter adapter, Object value) { + return adapter.nativeToValue(value); + } + + static long intValue(TypeAdapter adapter, Object value) { + if (value instanceof Long longValue) { + return longValue; + } + Val val = adapt(adapter, value); + return NativeScalarContinuations.intResult(val); + } + + static long uintValue(TypeAdapter adapter, Object value) { + if (value instanceof ULong unsigned) { + return unsigned.longValue(); + } + Val val = adapt(adapter, value); + return NativeScalarContinuations.uintResult(val); + } + + static boolean booleanValue(TypeAdapter adapter, Object value) { + if (value instanceof Boolean booleanValue) { + return booleanValue; + } + Val val = adapt(adapter, value); + return NativeScalarContinuations.booleanResult(val); + } + + static double doubleValue(TypeAdapter adapter, Object value) { + if (value instanceof Double doubleValue) { + return doubleValue; + } + Val val = adapt(adapter, value); + return NativeScalarContinuations.doubleResult(val); + } + + static String stringValue(TypeAdapter adapter, Object value) { + if (value instanceof String stringValue) { + return stringValue; + } + Val val = adapt(adapter, value); + if (val instanceof StringT) { + String stringValue = (String) val.value(); + if (stringValue != null) { + return stringValue; + } + } + throw signal(val); + } + + static void nullValue(TypeAdapter adapter, Object value) { + if (value == null) { + return; + } + Val val = adapt(adapter, value); + if (val == NullT.NullValue) { + return; + } + throw signal(val); + } + + static ValueSignal propagatedError(Val value) { + ErrException error = new ErrException("message: %s", value); + return signal(newErr(error, error.toString())); + } +} diff --git a/core/src/main/java/org/projectnessie/cel/interpreter/Planner.java b/core/src/main/java/org/projectnessie/cel/interpreter/Planner.java index 4adadeead..9d245d360 100644 --- a/core/src/main/java/org/projectnessie/cel/interpreter/Planner.java +++ b/core/src/main/java/org/projectnessie/cel/interpreter/Planner.java @@ -23,6 +23,7 @@ import static org.projectnessie.cel.common.types.TimestampT.timestampOf; import static org.projectnessie.cel.common.types.Types.boolOf; import static org.projectnessie.cel.common.types.UintT.uintOf; +import static org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory; import static org.projectnessie.cel.interpreter.Interpretable.newConstValue; import com.google.api.expr.v1alpha1.Constant; @@ -36,13 +37,24 @@ import com.google.api.expr.v1alpha1.Expr.Select; import com.google.api.expr.v1alpha1.Reference; import com.google.api.expr.v1alpha1.Type; +import com.google.api.expr.v1alpha1.Type.PrimitiveType; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.projectnessie.cel.checker.Decls; import org.projectnessie.cel.common.containers.Container; import org.projectnessie.cel.common.operators.Operator; +import org.projectnessie.cel.common.types.BoolT; +import org.projectnessie.cel.common.types.DoubleT; +import org.projectnessie.cel.common.types.IntT; import org.projectnessie.cel.common.types.NullT; +import org.projectnessie.cel.common.types.Overloads; +import org.projectnessie.cel.common.types.StringT; +import org.projectnessie.cel.common.types.UintT; +import org.projectnessie.cel.common.types.pb.ProtoTypeRegistry; import org.projectnessie.cel.common.types.ref.FieldType; +import org.projectnessie.cel.common.types.ref.StandardScalarFieldProvider; +import org.projectnessie.cel.common.types.ref.StandardScalarTypeAdapter; import org.projectnessie.cel.common.types.ref.TypeAdapter; import org.projectnessie.cel.common.types.ref.TypeProvider; import org.projectnessie.cel.common.types.ref.Val; @@ -50,6 +62,7 @@ import org.projectnessie.cel.interpreter.AttributeFactory.Attribute; import org.projectnessie.cel.interpreter.AttributeFactory.Qualifier; import org.projectnessie.cel.interpreter.Interpretable.InterpretableAttribute; +import org.projectnessie.cel.interpreter.Interpretable.InterpretableCall; import org.projectnessie.cel.interpreter.Interpretable.InterpretableConst; import org.projectnessie.cel.interpreter.functions.BinaryOp; import org.projectnessie.cel.interpreter.functions.FunctionOp; @@ -59,6 +72,7 @@ import org.projectnessie.cel.interpreter.functions.TernaryOp; import org.projectnessie.cel.interpreter.functions.UnaryOp; +/** Plans an {@link Interpretable} evaluation tree from a proto expression. */ final class Planner implements InterpretablePlanner { private final Dispatcher disp; private final TypeProvider provider; @@ -68,7 +82,10 @@ final class Planner implements InterpretablePlanner { private final Map refMap; private final Map typeMap; private final Map fieldTypes = new HashMap<>(); + private final PlanningPolicy policy; private final InterpretableDecorator[] decorators; + private AttributeFactory partialAttrFactory; + private int planDepth; Planner( Dispatcher disp, @@ -78,6 +95,7 @@ final class Planner implements InterpretablePlanner { Container container, Map refMap, Map typeMap, + PlanningPolicy policy, InterpretableDecorator[] decorators) { this.disp = disp; this.provider = provider; @@ -86,9 +104,14 @@ final class Planner implements InterpretablePlanner { this.container = container; this.refMap = refMap; this.typeMap = typeMap; + this.policy = policy; this.decorators = decorators; } + PlanningPolicy policy() { + return policy; + } + /** * Plan implements the interpretablePlanner interface. This implementation of the Plan method also * applies decorators to each Interpretable generated as part of the overall plan. Decorators are @@ -98,18 +121,33 @@ final class Planner implements InterpretablePlanner { */ @Override public Interpretable plan(Expr expr) { - return switch (expr.getExprKindCase()) { - case CALL_EXPR -> decorate(planCall(expr)); - case IDENT_EXPR -> decorate(planIdent(expr)); - case SELECT_EXPR -> decorate(planSelect(expr)); - case LIST_EXPR -> decorate(planCreateList(expr)); - case STRUCT_EXPR -> decorate(planCreateStruct(expr)); - case COMPREHENSION_EXPR -> decorate(planComprehension(expr)); - case CONST_EXPR -> decorate(planConst(expr)); - default -> - throw new IllegalArgumentException( - String.format("unsupported expr of kind %s: '%s'", expr.getExprKindCase(), expr)); - }; + boolean root = planDepth == 0; + planDepth++; + try { + Interpretable result = + switch (expr.getExprKindCase()) { + case CALL_EXPR -> planCall(expr); + case IDENT_EXPR -> planIdent(expr); + case SELECT_EXPR -> planSelect(expr); + case LIST_EXPR -> planCreateList(expr); + case STRUCT_EXPR -> planCreateStruct(expr); + case COMPREHENSION_EXPR -> planComprehension(expr); + case CONST_EXPR -> planConst(expr); + default -> + throw new IllegalArgumentException( + String.format( + "unsupported expr of kind %s: '%s'", expr.getExprKindCase(), expr)); + }; + if (root + && policy.nativeSpecializationPermitted() + && NativeIsland.supports(result) + && !(policy.builtInOptimizationEnabled() && result instanceof InterpretableConst)) { + result = new NativeIsland(result, adapter); + } + return decorate(result); + } finally { + planDepth--; + } } /** @@ -161,6 +199,26 @@ Interpretable planCheckedIdent(long id, Reference identRef) { // Otherwise, evaluate the checked top-level variable directly for ordinary plans. Decorated // programs keep the attribute shape because custom decorators may inspect attributes. if (decorators.length == 0) { + if (nativeScalarPlanning()) { + if (hasPrimitiveType(id, PrimitiveType.INT64)) { + return new NativeIntIdent(id, identName, adapter); + } + if (hasPrimitiveType(id, PrimitiveType.UINT64)) { + return new NativeUintIdent(id, identName, adapter); + } + if (hasPrimitiveType(id, PrimitiveType.BOOL)) { + return new NativeBooleanIdent(id, identName, adapter); + } + if (hasPrimitiveType(id, PrimitiveType.DOUBLE)) { + return new NativeDoubleIdent(id, identName, adapter); + } + if (hasPrimitiveType(id, PrimitiveType.STRING)) { + return new NativeStringIdent(id, identName, adapter); + } + if (hasNullType(id)) { + return new NativeNullIdent(id, identName, adapter); + } + } return new EvalIdent(id, identName, adapter); } return new EvalAttr(adapter, attrFactory.absoluteAttribute(id, identName)); @@ -213,28 +271,51 @@ Interpretable planSelect(Expr expr) { // values. if (sel.getTestOnly()) { // Return the test only eval expression. - return new EvalTestOnly(expr.getId(), op, stringOf(sel.getField()), fieldType); + return new EvalTestOnly(expr.getId(), asEstablished(op), stringOf(sel.getField()), fieldType); } // Build a qualifier. Qualifier qual = - fieldType != null - ? new FieldQualifier(expr.getId(), sel.getField(), fieldType, adapter) - : attrFactory.newQualifier(opType, expr.getId(), sel.getField()); + fieldType == null + ? attrFactory.newQualifier(opType, expr.getId(), sel.getField()) + : new FieldQualifier(expr.getId(), sel.getField(), fieldType, adapter); if (qual == null) { return null; } // Lastly, create a field selection Interpretable. if (op instanceof InterpretableAttribute attr) { attr.addQualifier(qual); - return attr; + return specializeScalarSelector( + expr, opType, fieldType, attr, partialSelectAttribute(expr, sel, opType)); } - InterpretableAttribute relAttr = relativeAttr(op.id(), op); + InterpretableAttribute relAttr = relativeAttr(op.id(), asEstablished(op)); if (relAttr == null) { return null; } relAttr.addQualifier(qual); - return relAttr; + return specializeScalarSelector( + expr, opType, fieldType, relAttr, partialSelectAttribute(expr, sel, opType)); + } + + private Interpretable specializeScalarSelector( + Expr expr, + Type operandType, + FieldType fieldType, + InterpretableAttribute attribute, + Attribute partialAttribute) { + if (!nativeScalarPlanning() || operandType == null || partialAttribute == null) { + return attribute; + } + boolean exactMap = exactStringMapResult(expr, operandType); + boolean exactField = + fieldType != null + && provider instanceof StandardScalarFieldProvider + && !operandType.getMessageType().startsWith("google.protobuf.") + && (provider instanceof ProtoTypeRegistry + || hasPrimitiveType(expr.getId(), PrimitiveType.STRING)); + return exactMap || exactField + ? specializeScalarAttribute(expr, attribute, partialAttribute) + : attribute; } private Interpretable planSelectOperand(Expr operand) { @@ -299,44 +380,303 @@ Interpretable planCall(Expr expr) { args[i + offset] = arg; } + Interpretable foldedConversion = foldConstantConversion(expr, resolvedFunc, args); + if (foldedConversion != null) { + return foldedConversion; + } + // Generate specialized Interpretable operators by function name if possible. - if (resolvedFunc.fnName.equals(Operator.LogicalAnd.id)) return planCallLogicalAnd(expr, args); - if (resolvedFunc.fnName.equals(Operator.LogicalOr.id)) return planCallLogicalOr(expr, args); - if (resolvedFunc.fnName.equals(Operator.Conditional.id)) return planCallConditional(expr, args); - if (resolvedFunc.fnName.equals(Operator.Equals.id)) return planCallEqual(expr, args); - if (resolvedFunc.fnName.equals(Operator.NotEquals.id)) return planCallNotEqual(expr, args); - if (resolvedFunc.fnName.equals(Operator.Index.id)) return planCallIndex(expr, args); + if (resolvedFunc.fnName.equals(Operator.LogicalAnd.id)) + return planCallLogicalAnd(expr, resolvedFunc, args); + if (resolvedFunc.fnName.equals(Operator.LogicalOr.id)) + return planCallLogicalOr(expr, resolvedFunc, args); + if (resolvedFunc.fnName.equals(Operator.Conditional.id)) + return planCallConditional(expr, resolvedFunc, args); + if (resolvedFunc.fnName.equals(Operator.Equals.id)) + return planCallEqual(expr, resolvedFunc, args); + if (resolvedFunc.fnName.equals(Operator.NotEquals.id)) + return planCallNotEqual(expr, resolvedFunc, args); + if (resolvedFunc.fnName.equals(Operator.Index.id)) + return planCallIndex(expr, resolvedFunc, args); if ("optional_or".equals(resolvedFunc.overloadId) || (resolvedFunc.overloadId.isEmpty() && "or".equals(resolvedFunc.fnName) - && disp.findOverload("optional_or") != null)) + && disp.findOverload("optional_or") != null)) { + Interpretable[] established = asEstablished(args); return new EvalOptionalOr( - expr.getId(), resolvedFunc.fnName, resolvedFunc.overloadId, args[0], args[1], false); + expr.getId(), + resolvedFunc.fnName, + resolvedFunc.overloadId, + established[0], + established[1], + false); + } if ("optional_or_value".equals(resolvedFunc.overloadId) || (resolvedFunc.overloadId.isEmpty() && "orValue".equals(resolvedFunc.fnName) - && disp.findOverload("optional_or_value") != null)) + && disp.findOverload("optional_or_value") != null)) { + Interpretable[] established = asEstablished(args); return new EvalOptionalOr( - expr.getId(), resolvedFunc.fnName, resolvedFunc.overloadId, args[0], args[1], true); + expr.getId(), + resolvedFunc.fnName, + resolvedFunc.overloadId, + established[0], + established[1], + true); + } + if (resolvedFunc.fnName.equals(Operator.In.id)) { + Interpretable constantSetMembership = foldConstantSetMembership(expr, resolvedFunc, args); + if (constantSetMembership != null) { + return constantSetMembership; + } + } // Otherwise, generate Interpretable calls specialized by argument count. - // Try to find the specific function by overload id. - Overload fnDef = null; - if (resolvedFunc.overloadId != null && !resolvedFunc.overloadId.isEmpty()) { - fnDef = disp.findOverload(resolvedFunc.overloadId); - } - // If the overload id couldn't resolve the function, try the simple function name. - if (fnDef == null) { - fnDef = disp.findOverload(resolvedFunc.fnName); + Overload fnDef = resolvedFunc.implementation; + Interpretable nativeScalar = specializeStrictScalarCall(expr, resolvedFunc, args); + if (nativeScalar != null) { + return nativeScalar; } + Interpretable[] established = asEstablished(args); return switch (argCount) { case 0 -> planCallZero(expr, resolvedFunc.fnName, resolvedFunc.overloadId, fnDef); - case 1 -> planCallUnary(expr, resolvedFunc.fnName, resolvedFunc.overloadId, fnDef, args); - case 2 -> planCallBinary(expr, resolvedFunc.fnName, resolvedFunc.overloadId, fnDef, args); - case 3 -> planCallTernary(expr, resolvedFunc.fnName, resolvedFunc.overloadId, fnDef, args); - case 4 -> planCallQuaternary(expr, resolvedFunc.fnName, resolvedFunc.overloadId, fnDef, args); - case 5 -> planCallQuinary(expr, resolvedFunc.fnName, resolvedFunc.overloadId, fnDef, args); - default -> planCallVarArgs(expr, resolvedFunc.fnName, resolvedFunc.overloadId, fnDef, args); + case 1 -> + planCallUnary(expr, resolvedFunc.fnName, resolvedFunc.overloadId, fnDef, established); + case 2 -> + planCallBinary(expr, resolvedFunc.fnName, resolvedFunc.overloadId, fnDef, established); + case 3 -> + planCallTernary(expr, resolvedFunc.fnName, resolvedFunc.overloadId, fnDef, established); + case 4 -> + planCallQuaternary( + expr, resolvedFunc.fnName, resolvedFunc.overloadId, fnDef, established); + case 5 -> + planCallQuinary(expr, resolvedFunc.fnName, resolvedFunc.overloadId, fnDef, established); + default -> + planCallVarArgs(expr, resolvedFunc.fnName, resolvedFunc.overloadId, fnDef, established); + }; + } + + private Interpretable foldConstantConversion( + Expr expr, ResolvedFunction resolvedFunction, Interpretable[] arguments) { + if (!policy.builtInOptimizationEnabled() + || !Overloads.isTypeConversionFunction(resolvedFunction.fnName) + || arguments.length != 1 + || !(arguments[0] instanceof InterpretableConst)) { + return null; + } + + Interpretable call = + planCallUnary( + expr, + resolvedFunction.fnName, + resolvedFunction.overloadId, + resolvedFunction.implementation, + arguments); + EvalConst folded = BuiltInOptimizer.foldConstantUnary((InterpretableCall) call); + if (folded == null + || !nativeScalarPlanning() + || !StandardOverloadProvenance.isExactStandard(disp, refMap, expr)) { + return folded; + } + + Val value = folded.value(); + if (value == null) { + return folded; + } + NativeScalarKind kind = nativeKind(expr.getId()); + if (kind == NativeScalarKind.BOOLEAN && value.getClass() == BoolT.class) { + return new NativeBooleanConst(expr.getId(), (BoolT) value); + } + if (kind == NativeScalarKind.INT && value.getClass() == IntT.class) { + return new NativeIntConst(expr.getId(), (IntT) value); + } + if (kind == NativeScalarKind.UINT && value.getClass() == UintT.class) { + return new NativeUintConst(expr.getId(), (UintT) value); + } + if (kind == NativeScalarKind.DOUBLE && value.getClass() == DoubleT.class) { + return new NativeDoubleConst(expr.getId(), (DoubleT) value); + } + if (kind == NativeScalarKind.STRING + && value.getClass() == StringT.class + && value.value() instanceof String) { + return new NativeStringConst(expr.getId(), (StringT) value); + } + return folded; + } + + private Interpretable foldConstantSetMembership( + Expr expr, ResolvedFunction resolvedFunction, Interpretable[] arguments) { + Call call = expr.getCallExpr(); + if (!policy.builtInOptimizationEnabled() + || !resolvedFunction.overloadId.equals(Overloads.InList) + || call.hasTarget() + || call.getArgsCount() != 2 + || arguments.length != 2 + || !(arguments[1] instanceof InterpretableConst)) { + return null; + } + + BuiltInOptimizer.ConstantSet constantSet = BuiltInOptimizer.constantSet(arguments[1]); + if (constantSet == null) { + return null; + } + + Interpretable[] established = asEstablished(arguments.clone()); + Interpretable original = + planCallBinary( + expr, + resolvedFunction.fnName, + resolvedFunction.overloadId, + resolvedFunction.implementation, + established); + return new EvalSetMembership( + original, established[0], constantSet.typeName(), constantSet.values()); + } + + private Interpretable specializeStrictScalarCall( + Expr expr, ResolvedFunction resolved, Interpretable[] args) { + if (!nativeScalarPlanning() || resolved.nativeDescriptor() == null) { + return null; + } + String overload = resolved.overloadId; + Overload implementation = resolved.implementation; + if (args.length == 1) { + Interpretable argument = args[0]; + if (overload.equals(Overloads.LogicalNot) + && argument instanceof NativeBooleanCapability + && hasPrimitiveType(expr.getId(), PrimitiveType.BOOL)) { + return new NativeBooleanNot( + expr.getId(), resolved.fnName, overload, argument, implementation); + } + if (overload.equals(Overloads.NegateInt64) + && argument instanceof NativeIntCapability + && hasPrimitiveType(expr.getId(), PrimitiveType.INT64)) { + return new NativeIntNegate( + expr.getId(), resolved.fnName, overload, argument, implementation); + } + if (overload.equals(Overloads.NegateDouble) + && argument instanceof NativeDoubleCapability + && hasPrimitiveType(expr.getId(), PrimitiveType.DOUBLE)) { + return new NativeDoubleNegate( + expr.getId(), resolved.fnName, overload, argument, implementation); + } + return null; + } + if (args.length != 2) { + return null; + } + + NativeArithmetic arithmetic = arithmetic(overload); + if (arithmetic != null) { + if (args[0] instanceof NativeIntCapability + && args[1] instanceof NativeIntCapability + && hasPrimitiveType(expr.getId(), PrimitiveType.INT64)) { + if (arithmetic == NativeArithmetic.ADD) { + return new NativeIntAdd( + expr.getId(), resolved.fnName, overload, args[0], args[1], implementation); + } + return new NativeIntBinary( + expr.getId(), resolved.fnName, overload, args[0], args[1], implementation, arithmetic); + } + if (args[0] instanceof NativeDoubleCapability + && args[1] instanceof NativeDoubleCapability + && hasPrimitiveType(expr.getId(), PrimitiveType.DOUBLE)) { + return new NativeDoubleBinary( + expr.getId(), resolved.fnName, overload, args[0], args[1], implementation, arithmetic); + } + return null; + } + + if (overload.equals(Overloads.AddString) + && args[0] instanceof NativeStringCapability + && args[1] instanceof NativeStringCapability + && hasPrimitiveType(expr.getId(), PrimitiveType.STRING)) { + return new NativeStringConcat( + expr.getId(), resolved.fnName, overload, args[0], args[1], implementation); + } + + NativeComparison comparison = comparison(resolved.fnName); + NativeScalarKind kind = comparisonKind(overload); + if (comparison != null + && kind != null + && supportsComparison(kind, args[0]) + && supportsComparison(kind, args[1]) + && hasPrimitiveType(expr.getId(), PrimitiveType.BOOL)) { + return new NativeScalarComparison( + expr.getId(), + resolved.fnName, + overload, + args[0], + args[1], + implementation, + kind, + comparison); + } + return null; + } + + private static NativeArithmetic arithmetic(String overload) { + return switch (overload) { + case Overloads.AddInt64, Overloads.AddDouble -> NativeArithmetic.ADD; + case Overloads.SubtractInt64, Overloads.SubtractDouble -> NativeArithmetic.SUBTRACT; + case Overloads.MultiplyInt64, Overloads.MultiplyDouble -> NativeArithmetic.MULTIPLY; + case Overloads.DivideInt64, Overloads.DivideDouble -> NativeArithmetic.DIVIDE; + default -> overload.equals(Overloads.ModuloInt64) ? NativeArithmetic.MODULO : null; + }; + } + + private static NativeComparison comparison(String function) { + if (function.equals(Operator.Less.id)) { + return NativeComparison.LESS; + } + if (function.equals(Operator.LessEquals.id)) { + return NativeComparison.LESS_EQUALS; + } + if (function.equals(Operator.Greater.id)) { + return NativeComparison.GREATER; + } + return function.equals(Operator.GreaterEquals.id) ? NativeComparison.GREATER_EQUALS : null; + } + + private static NativeScalarKind comparisonKind(String overload) { + return switch (overload) { + case Overloads.LessBool, + Overloads.LessEqualsBool, + Overloads.GreaterBool, + Overloads.GreaterEqualsBool -> + NativeScalarKind.BOOLEAN; + case Overloads.LessInt64, + Overloads.LessEqualsInt64, + Overloads.GreaterInt64, + Overloads.GreaterEqualsInt64 -> + NativeScalarKind.INT; + case Overloads.LessUint64, + Overloads.LessEqualsUint64, + Overloads.GreaterUint64, + Overloads.GreaterEqualsUint64 -> + NativeScalarKind.UINT; + case Overloads.LessDouble, + Overloads.LessEqualsDouble, + Overloads.GreaterDouble, + Overloads.GreaterEqualsDouble -> + NativeScalarKind.DOUBLE; + case Overloads.LessString, + Overloads.LessEqualsString, + Overloads.GreaterString, + Overloads.GreaterEqualsString -> + NativeScalarKind.STRING; + default -> null; + }; + } + + private static boolean supportsComparison(NativeScalarKind kind, Interpretable argument) { + return switch (kind) { + case BOOLEAN -> argument instanceof NativeBooleanCapability; + case INT -> argument instanceof NativeIntCapability; + case UINT -> argument instanceof NativeUintCapability; + case DOUBLE -> argument instanceof NativeDoubleCapability; + case STRING -> argument instanceof NativeStringCapability; + case NULL -> false; }; } @@ -455,8 +795,8 @@ static Interpretable planCallVarArgs( if (impl == null) { return new EvalReceiverVarArgs(expr.getId(), function, overload, args); } - FunctionOp fn = null; - Trait trait = null; + FunctionOp fn; + Trait trait; if (impl.function == null) { throw new IllegalStateException(String.format("no such overload: %s(...)", function)); } @@ -466,67 +806,181 @@ static Interpretable planCallVarArgs( } /** planCallEqual generates an equals (==) Interpretable. */ - static Interpretable planCallEqual(Expr expr, Interpretable... args) { - return new EvalEq(expr.getId(), args[0], args[1]); + Interpretable planCallEqual(Expr expr, ResolvedFunction resolvedFunction, Interpretable... args) { + if (nativeScalarPlanning() + && resolvedFunction.overloadId.equals(Overloads.Equals) + && args.length == 2 + && hasPrimitiveType(expr.getId(), PrimitiveType.BOOL)) { + NativeScalarKind kind = equalityKind(args[0], args[1]); + if (kind != null) { + if (kind == NativeScalarKind.BOOLEAN) { + return new NativeBooleanEq(expr.getId(), args[0], args[1]); + } + if (kind == NativeScalarKind.INT) { + return new NativeIntEq(expr.getId(), args[0], args[1]); + } + return new NativeScalarEq(expr.getId(), args[0], args[1], kind); + } + } + Interpretable[] established = asEstablished(args); + return new EvalEq(expr.getId(), established[0], established[1]); } /** planCallNotEqual generates a not equals (!=) Interpretable. */ - static Interpretable planCallNotEqual(Expr expr, Interpretable... args) { - return new EvalNe(expr.getId(), args[0], args[1]); + Interpretable planCallNotEqual( + Expr expr, ResolvedFunction resolvedFunction, Interpretable... args) { + if (nativeScalarPlanning() + && resolvedFunction.overloadId.equals(Overloads.NotEquals) + && args.length == 2 + && hasPrimitiveType(expr.getId(), PrimitiveType.BOOL)) { + NativeScalarKind kind = equalityKind(args[0], args[1]); + if (kind != null) { + return new NativeScalarNe(expr.getId(), args[0], args[1], kind); + } + } + Interpretable[] established = asEstablished(args); + return new EvalNe(expr.getId(), established[0], established[1]); } /** planCallLogicalAnd generates a logical and (&&) Interpretable. */ - static Interpretable planCallLogicalAnd(Expr expr, Interpretable... args) { - return new EvalAnd(expr.getId(), args[0], args[1]); + Interpretable planCallLogicalAnd( + Expr expr, ResolvedFunction resolvedFunction, Interpretable... args) { + if (nativeScalarPlanning() + && resolvedFunction.overloadId.equals(Overloads.LogicalAnd) + && args.length == 2 + && args[0] instanceof NativeBooleanCapability + && args[1] instanceof NativeBooleanCapability + && hasPrimitiveType(expr.getId(), PrimitiveType.BOOL)) { + return new NativeLogicalAnd(expr.getId(), args[0], args[1]); + } + Interpretable[] established = asEstablished(args); + return new EvalAnd(expr.getId(), established[0], established[1]); } /** planCallLogicalOr generates a logical or (||) Interpretable. */ - static Interpretable planCallLogicalOr(Expr expr, Interpretable... args) { - return new EvalOr(expr.getId(), args[0], args[1]); + Interpretable planCallLogicalOr( + Expr expr, ResolvedFunction resolvedFunction, Interpretable... args) { + if (nativeScalarPlanning() + && resolvedFunction.overloadId.equals(Overloads.LogicalOr) + && args.length == 2 + && args[0] instanceof NativeBooleanCapability + && args[1] instanceof NativeBooleanCapability + && hasPrimitiveType(expr.getId(), PrimitiveType.BOOL)) { + return new NativeLogicalOr(expr.getId(), args[0], args[1]); + } + Interpretable[] established = asEstablished(args); + return new EvalOr(expr.getId(), established[0], established[1]); } /** planCallConditional generates a conditional / ternary (c ? t : f) Interpretable. */ - Interpretable planCallConditional(Expr expr, Interpretable... args) { + Interpretable planCallConditional( + Expr expr, ResolvedFunction resolvedFunction, Interpretable... args) { Interpretable cond = args[0]; - Interpretable t = args[1]; - Attribute tAttr; - if (t instanceof InterpretableAttribute truthyAttr) { - tAttr = truthyAttr.attr(); - } else { - tAttr = attrFactory.relativeAttribute(t.id(), t); + Interpretable f = args[2]; + + Interpretable establishedCond = asEstablished(cond); + Attribute tAttr = conditionalBranchAttribute(t); + Attribute fAttr = conditionalBranchAttribute(f); + + Attribute attribute = + attrFactory.conditionalAttribute(expr.getId(), establishedCond, tAttr, fAttr); + if (nativeScalarPlanning() + && resolvedFunction.overloadId.equals(Overloads.Conditional) + && cond instanceof NativeBooleanCapability condition) { + if (t instanceof NativeBooleanCapability truthy + && f instanceof NativeBooleanCapability falsy + && hasPrimitiveType(expr.getId(), PrimitiveType.BOOL)) { + return new NativeBooleanConditional(adapter, attribute, condition, truthy, falsy); + } + if (t instanceof NativeIntCapability truthy + && f instanceof NativeIntCapability falsy + && hasPrimitiveType(expr.getId(), PrimitiveType.INT64)) { + return new NativeIntConditional(adapter, attribute, condition, truthy, falsy); + } + if (t instanceof NativeUintCapability truthy + && f instanceof NativeUintCapability falsy + && hasPrimitiveType(expr.getId(), PrimitiveType.UINT64)) { + return new NativeUintConditional(adapter, attribute, condition, truthy, falsy); + } + if (t instanceof NativeDoubleCapability truthy + && f instanceof NativeDoubleCapability falsy + && hasPrimitiveType(expr.getId(), PrimitiveType.DOUBLE)) { + return new NativeDoubleConditional(adapter, attribute, condition, truthy, falsy); + } + if (t instanceof NativeStringCapability truthy + && f instanceof NativeStringCapability falsy + && hasPrimitiveType(expr.getId(), PrimitiveType.STRING)) { + return new NativeStringConditional(adapter, attribute, condition, truthy, falsy); + } + if (t instanceof NativeNullCapability truthy + && f instanceof NativeNullCapability falsy + && hasNullType(expr.getId())) { + return new NativeNullConditional(adapter, attribute, condition, truthy, falsy); + } } + return new EvalAttr(adapter, attribute); + } - Interpretable f = args[2]; - Attribute fAttr; - if (f instanceof InterpretableAttribute falsyAttr) { - fAttr = falsyAttr.attr(); - } else { - fAttr = attrFactory.relativeAttribute(f.id(), f); + private Attribute conditionalBranchAttribute(Interpretable branch) { + if (branch instanceof NativeScalarAttr scalarAttribute) { + // Preserve both direct attribute error propagation and partial qualifier matching. + return scalarAttribute; + } + if (branch instanceof InterpretableAttribute attribute) { + return attribute.attr(); } + return attrFactory.relativeAttribute(branch.id(), asEstablished(branch)); + } - return new EvalAttr( - adapter, attrFactory.conditionalAttribute(expr.getId(), cond, tAttr, fAttr)); + private static NativeScalarKind equalityKind(Interpretable left, Interpretable right) { + if (left instanceof NativeBooleanCapability && right instanceof NativeBooleanCapability) { + return NativeScalarKind.BOOLEAN; + } + if (left instanceof NativeIntCapability && right instanceof NativeIntCapability) { + return NativeScalarKind.INT; + } + if (left instanceof NativeUintCapability && right instanceof NativeUintCapability) { + return NativeScalarKind.UINT; + } + if (left instanceof NativeDoubleCapability && right instanceof NativeDoubleCapability) { + return NativeScalarKind.DOUBLE; + } + if (left instanceof NativeStringCapability && right instanceof NativeStringCapability) { + return NativeScalarKind.STRING; + } + return left instanceof NativeNullCapability && right instanceof NativeNullCapability + ? NativeScalarKind.NULL + : null; } /** * planCallIndex either extends an attribute with the argument to the index operation, or creates * a relative attribute based on the return of a function call or operation. */ - Interpretable planCallIndex(Expr expr, Interpretable... args) { - Interpretable op = args[0]; + Interpretable planCallIndex(Expr expr, ResolvedFunction resolvedFunction, Interpretable... args) { + Interpretable op = asEstablished(args[0]); Interpretable ind = args[1]; InterpretableAttribute opAttr = relativeAttr(op.id(), op); if (opAttr == null) { return null; } - Type opType = typeMap.get(expr.getCallExpr().getTarget().getId()); + Call call = expr.getCallExpr(); + Expr operandExpr = call.hasTarget() ? call.getTarget() : call.getArgs(0); + Type opType = typeMap.get(operandExpr.getId()); if (ind instanceof InterpretableConst indConst) { Qualifier qual = attrFactory.newQualifier(opType, expr.getId(), indConst.value()); if (qual == null) { return null; } opAttr.addQualifier(qual); + if (ind instanceof InterpretableConst indConst2 + && resolvedFunction.nativeDescriptor() != null + && resolvedFunction.overloadId.equals(Overloads.IndexMap) + && exactStringMapResult(expr, opType)) { + return specializeScalarAttribute( + expr, opAttr, partialIndexAttribute(expr, operandExpr, opType, indConst2.value())); + } return opAttr; } if (ind instanceof InterpretableAttribute indAttr) { @@ -545,6 +999,86 @@ Interpretable planCallIndex(Expr expr, Interpretable... args) { return opAttr; } + private Interpretable specializeScalarAttribute( + Expr expr, InterpretableAttribute attribute, Attribute partialAttribute) { + if (!nativeScalarPlanning() || partialAttribute == null) { + return attribute; + } + if (hasPrimitiveType(expr.getId(), PrimitiveType.BOOL)) { + return new NativeBooleanAttr(expr.getId(), adapter, attribute.attr(), partialAttribute); + } + if (hasPrimitiveType(expr.getId(), PrimitiveType.INT64)) { + return new NativeIntAttr(expr.getId(), adapter, attribute.attr(), partialAttribute); + } + if (hasPrimitiveType(expr.getId(), PrimitiveType.UINT64)) { + return new NativeUintAttr(expr.getId(), adapter, attribute.attr(), partialAttribute); + } + if (hasPrimitiveType(expr.getId(), PrimitiveType.DOUBLE)) { + return new NativeDoubleAttr(expr.getId(), adapter, attribute.attr(), partialAttribute); + } + if (hasPrimitiveType(expr.getId(), PrimitiveType.STRING)) { + return new NativeStringAttr(expr.getId(), adapter, attribute.attr(), partialAttribute); + } + return hasNullType(expr.getId()) + ? new NativeNullAttr(expr.getId(), adapter, attribute.attr(), partialAttribute) + : attribute; + } + + private Attribute partialSelectAttribute(Expr expr, Select select, Type operandType) { + return partialQualifiedAttribute( + expr.getId(), select.getOperand(), operandType, select.getField()); + } + + private Attribute partialIndexAttribute(Expr expr, Expr operand, Type operandType, Val index) { + return partialQualifiedAttribute(expr.getId(), operand, operandType, index); + } + + private Attribute partialQualifiedAttribute( + long id, Expr operand, Type operandType, Object qualifierValue) { + Reference reference = topLevelReference(operand); + if (reference == null) { + return null; + } + AttributeFactory partial = partialAttributeFactory(); + Attribute attribute = partial.absoluteAttribute(operand.getId(), reference.getName()); + Qualifier qualifier = partial.newQualifier(operandType, id, qualifierValue); + if (qualifier == null) { + return null; + } + attribute.addQualifier(qualifier); + return attribute; + } + + private Reference topLevelReference(Expr expression) { + if (expression.getExprKindCase() != Expr.ExprKindCase.IDENT_EXPR) { + return null; + } + Reference reference = refMap.get(expression.getId()); + if (reference == null || reference.getValue() != Reference.getDefaultInstance().getValue()) { + return null; + } + Type checkedType = typeMap.get(expression.getId()); + return checkedType != null && checkedType.getType() == Type.getDefaultInstance() + ? reference + : null; + } + + private AttributeFactory partialAttributeFactory() { + if (partialAttrFactory == null) { + partialAttrFactory = newPartialAttributeFactory(container, adapter, provider); + } + return partialAttrFactory; + } + + private boolean exactStringMapResult(Expr expr, Type operandType) { + Type resultType = typeMap.get(expr.getId()); + return resultType != null + && operandType != null + && operandType.getTypeKindCase() == Type.TypeKindCase.MAP_TYPE + && operandType.getMapType().getKeyType().getPrimitive() == PrimitiveType.STRING + && resultType.equals(operandType.getMapType().getValueType()); + } + /** planCreateList generates a list construction Interpretable. */ Interpretable planCreateList(Expr expr) { CreateList list = expr.getListExpr(); @@ -561,7 +1095,25 @@ Interpretable planCreateList(Expr expr) { } elems[i] = elemVal; } - return new EvalList(expr.getId(), elems, optionalIndices, adapter); + Interpretable folded = foldConstantList(expr, elems, optionalIndices); + if (folded != null) { + return folded; + } + return new EvalList(expr.getId(), asEstablished(elems), optionalIndices, adapter); + } + + private Interpretable foldConstantList( + Expr expr, Interpretable[] elements, boolean[] optionalIndices) { + if (!policy.builtInOptimizationEnabled()) { + return null; + } + EvalConst folded = + BuiltInOptimizer.foldList( + new EvalList(expr.getId(), elements.clone(), optionalIndices.clone(), adapter)); + if (folded == null) { + return null; + } + return folded; } /** planCreateStruct generates a map or object construction Interpretable. */ @@ -589,7 +1141,21 @@ Interpretable planCreateStruct(Expr expr) { } vals[i] = valVal; } - return new EvalMap(expr.getId(), keys, vals, optionalEntries, adapter); + if (policy.builtInOptimizationEnabled()) { + EvalConst folded = + BuiltInOptimizer.foldMap( + new EvalMap( + expr.getId(), keys.clone(), vals.clone(), optionalEntries.clone(), adapter)); + if (folded != null) { + return folded; + } + } + return new EvalMap( + expr.getId(), + asEstablished(keys.clone()), + asEstablished(vals.clone()), + optionalEntries, + adapter); } /** planCreateObj generates an object construction Interpretable. */ @@ -613,7 +1179,8 @@ Interpretable planCreateObj(Expr expr) { } vals[i] = val; } - return new EvalObj(expr.getId(), typeName, fields, vals, optionalEntries, provider); + return new EvalObj( + expr.getId(), typeName, fields, asEstablished(vals), optionalEntries, provider); } /** planComprehension generates an Interpretable fold operation. */ @@ -640,9 +1207,9 @@ Interpretable planComprehension(Expr expr) { expr.getId(), fold.getIterVar(), fold.getIterVar2(), - iterRange, - filter, - transform, + asEstablished(iterRange), + asEstablished(filter), + asEstablished(transform), adapter); } @@ -667,9 +1234,9 @@ Interpretable planComprehension(Expr expr) { expr.getId(), fold.getIterVar(), fold.getIterVar2(), - iterRange, - filter, - transform, + asEstablished(iterRange), + asEstablished(filter), + asEstablished(transform), adapter); } @@ -696,13 +1263,243 @@ Interpretable planComprehension(Expr expr) { return new EvalFold( expr.getId(), fold.getAccuVar(), - accu, + asEstablished(accu), fold.getIterVar(), fold.getIterVar2(), - iterRange, - cond, - step, - result); + asEstablished(iterRange), + asEstablished(cond), + asEstablished(step), + asEstablished(result)); + } + + /** planConst generates a constant valued Interpretable. */ + Interpretable planConst(Expr expr) { + Val val = constValue(expr.getConstExpr()); + if (val == null) { + return null; + } + if (nativeScalarPlanning()) { + if (hasPrimitiveType(expr.getId(), PrimitiveType.INT64)) { + return new NativeIntConst(expr.getId(), val.intValue()); + } + if (hasPrimitiveType(expr.getId(), PrimitiveType.UINT64)) { + return new NativeUintConst(expr.getId(), val.intValue()); + } + if (hasPrimitiveType(expr.getId(), PrimitiveType.BOOL)) { + return new NativeBooleanConst(expr.getId(), val.booleanValue()); + } + if (hasPrimitiveType(expr.getId(), PrimitiveType.DOUBLE)) { + return new NativeDoubleConst(expr.getId(), val.doubleValue()); + } + if (hasPrimitiveType(expr.getId(), PrimitiveType.STRING)) { + return new NativeStringConst(expr.getId(), (String) val.value()); + } + if (hasNullType(expr.getId())) { + return new NativeNullConst(expr.getId()); + } + } + return newConstValue(expr.getId(), val); + } + + /** constValue converts a proto Constant value to a ref.Val. */ + @SuppressWarnings("deprecation") + static Val constValue(Constant c) { + return switch (c.getConstantKindCase()) { + case BOOL_VALUE -> boolOf(c.getBoolValue()); + case BYTES_VALUE -> bytesOf(c.getBytesValue()); + case DOUBLE_VALUE -> doubleOf(c.getDoubleValue()); + case DURATION_VALUE -> durationOf(c.getDurationValue()); + case INT64_VALUE -> intOf(c.getInt64Value()); + case NULL_VALUE -> NullT.NullValue; + case STRING_VALUE -> stringOf(c.getStringValue()); + case TIMESTAMP_VALUE -> timestampOf(c.getTimestampValue()); + case UINT64_VALUE -> uintOf(c.getUint64Value()); + default -> + throw new IllegalArgumentException( + String.format( + "unknown constant type: '%s' of kind '%s'", c, c.getConstantKindCase())); + }; + } + + /** + * resolveTypeName takes a qualified string constructed at parse time, applies the proto namespace + * resolution rules to it in a scan over possible matching types in the TypeProvider. + */ + String resolveTypeName(String typeName) { + for (String qualifiedTypeName : container.resolveCandidateNames(typeName)) { + if (provider.findType(qualifiedTypeName) != null) { + return qualifiedTypeName; + } + } + return null; + } + + static class ResolvedFunction { + final Expr target; + final String fnName; + final String overloadId; + final Overload implementation; + + ResolvedFunction(Expr target, String fnName, String overloadId, Overload implementation) { + this.target = target; + this.fnName = fnName; + this.overloadId = overloadId; + this.implementation = implementation; + } + + NativeOverloadDescriptor nativeDescriptor() { + return null; + } + } + + static final class NativeResolvedFunction extends ResolvedFunction { + private final NativeOverloadDescriptor nativeDescriptor; + + NativeResolvedFunction( + Expr target, + String fnName, + String overloadId, + Overload implementation, + NativeOverloadDescriptor nativeDescriptor) { + super(target, fnName, overloadId, implementation); + this.nativeDescriptor = nativeDescriptor; + } + + @Override + NativeOverloadDescriptor nativeDescriptor() { + return nativeDescriptor; + } + } + + /** + * resolveFunction determines the call target, function name, and overload name from a given Expr + * value. + * + *

The resolveFunction resolves ambiguities where a function may either be a receiver-style + * invocation or a qualified global function name. + * + *

+ */ + private ResolvedFunction resolvedFunction(Expr target, String function, String overloadId) { + Overload implementation = null; + if (overloadId != null && !overloadId.isEmpty()) { + implementation = disp.findOverload(overloadId); + } + if (implementation == null) { + implementation = disp.findOverload(function); + } + NativeOverloadDescriptor nativeDescriptor = + policy.nativeSpecializationPermitted() + ? StandardNativeOverloadCatalog.find(implementation, function, overloadId) + : null; + return nativeDescriptor != null + ? new NativeResolvedFunction(target, function, overloadId, implementation, nativeDescriptor) + : new ResolvedFunction(target, function, overloadId, implementation); + } + + private Interpretable[] asEstablished(Interpretable[] arguments) { + if (!policy.nativeSpecializationPermitted()) { + return arguments; + } + for (int i = 0; i < arguments.length; i++) { + Interpretable argument = arguments[i]; + if (NativeIsland.supports(argument) + && !(policy.builtInOptimizationEnabled() && argument instanceof InterpretableConst)) { + arguments[i] = new NativeIsland(argument, adapter); + } + } + return arguments; + } + + private Interpretable asEstablished(Interpretable argument) { + if (policy.nativeSpecializationPermitted() + && NativeIsland.supports(argument) + && !(policy.builtInOptimizationEnabled() && argument instanceof InterpretableConst)) { + return new NativeIsland(argument, adapter); + } + return argument; + } + + private boolean nativeScalarPlanning() { + return policy.nativeSpecializationPermitted() && adapter instanceof StandardScalarTypeAdapter; + } + + private NativeScalarKind nativeKind(long id) { + return nativeKind(typeMap.get(id)); + } + + private static NativeScalarKind nativeKind(Type type) { + if (type == null) { + return null; + } + if (Decls.Null.equals(type)) { + return NativeScalarKind.NULL; + } + if (type.getTypeKindCase() != Type.TypeKindCase.PRIMITIVE) { + return null; + } + return switch (type.getPrimitive()) { + case BOOL -> NativeScalarKind.BOOLEAN; + case INT64 -> NativeScalarKind.INT; + case UINT64 -> NativeScalarKind.UINT; + case DOUBLE -> NativeScalarKind.DOUBLE; + case STRING -> NativeScalarKind.STRING; + default -> null; + }; + } + + private boolean hasPrimitiveType(long id, PrimitiveType primitiveType) { + Type type = typeMap.get(id); + return type != null + && type.getTypeKindCase() == Type.TypeKindCase.PRIMITIVE + && type.getPrimitive() == primitiveType; + } + + private boolean hasNullType(long id) { + return org.projectnessie.cel.checker.Decls.Null.equals(typeMap.get(id)); + } + + InterpretableAttribute relativeAttr(long id, Interpretable eval) { + InterpretableAttribute eAttr; + if (eval instanceof InterpretableAttribute) { + eAttr = (InterpretableAttribute) eval; + } else { + eAttr = new EvalAttr(adapter, attrFactory.relativeAttribute(id, eval)); + } + Interpretable decAttr = decorate(eAttr); + if (decAttr == null) { + return null; + } + if (!(decAttr instanceof InterpretableAttribute)) { + throw new IllegalStateException( + String.format( + "invalid attribute decoration: %s(%s)", decAttr, decAttr.getClass().getName())); + } + eAttr = (InterpretableAttribute) decAttr; + return eAttr; + } + + /** + * toQualifiedName converts an expression AST into a qualified name if possible, with a boolean + * 'found' value that indicates if the conversion is successful. + */ + String toQualifiedName(Expr operand) { + // If the checker identified the expression as an attribute by the type-checker, then it can't + // possibly be part of qualified name in a namespace. + if (refMap.containsKey(operand.getId())) { + return ""; + } + // Since functions cannot be both namespaced and receiver functions, if the operand is not an + // qualified variable name, return the (possibly) qualified name given the expressions. + return Container.toQualifiedName(operand); + } + + String stripLeadingDot(String name) { + return name.startsWith(".") ? name.substring(1) : name; } private static MacroMapFold macroMapFold(Comprehension fold) { @@ -811,62 +1608,6 @@ private static Expr mapEntryValue(String keyVar, Expr step) { return entry.getValue(); } - private static boolean isBoolConst(Expr expr, boolean value) { - return expr.getExprKindCase() == Expr.ExprKindCase.CONST_EXPR - && expr.getConstExpr().getConstantKindCase() == Constant.ConstantKindCase.BOOL_VALUE - && expr.getConstExpr().getBoolValue() == value; - } - - private static boolean referencesIdent(Expr expr, String name) { - switch (expr.getExprKindCase()) { - case IDENT_EXPR: - return expr.getIdentExpr().getName().equals(name); - case SELECT_EXPR: - return referencesIdent(expr.getSelectExpr().getOperand(), name); - case CALL_EXPR: - Call call = expr.getCallExpr(); - if (call.hasTarget() && referencesIdent(call.getTarget(), name)) { - return true; - } - for (Expr arg : call.getArgsList()) { - if (referencesIdent(arg, name)) { - return true; - } - } - return false; - case LIST_EXPR: - for (Expr elem : expr.getListExpr().getElementsList()) { - if (referencesIdent(elem, name)) { - return true; - } - } - return false; - case STRUCT_EXPR: - for (Entry entry : expr.getStructExpr().getEntriesList()) { - if (referencesIdent(entry.getValue(), name)) { - return true; - } - } - return false; - case COMPREHENSION_EXPR: - Comprehension comprehension = expr.getComprehensionExpr(); - return referencesIdent(comprehension.getIterRange(), name) - || referencesIdent(comprehension.getAccuInit(), name) - || (!comprehension.getIterVar().equals(name) - && !comprehension.getIterVar2().equals(name) - && !comprehension.getAccuVar().equals(name) - && referencesIdent(comprehension.getLoopCondition(), name)) - || (!comprehension.getIterVar().equals(name) - && !comprehension.getIterVar2().equals(name) - && !comprehension.getAccuVar().equals(name) - && referencesIdent(comprehension.getLoopStep(), name)) - || (!comprehension.getAccuVar().equals(name) - && referencesIdent(comprehension.getResult(), name)); - default: - return false; - } - } - private static final class MacroListFold { final Expr filter; final Expr transform; @@ -887,73 +1628,6 @@ private MacroMapFold(Expr filter, Expr transform) { } } - /** planConst generates a constant valued Interpretable. */ - static Interpretable planConst(Expr expr) { - Val val = constValue(expr.getConstExpr()); - if (val == null) { - return null; - } - return newConstValue(expr.getId(), val); - } - - /** constValue converts a proto Constant value to a ref.Val. */ - @SuppressWarnings("deprecation") - static Val constValue(Constant c) { - return switch (c.getConstantKindCase()) { - case BOOL_VALUE -> boolOf(c.getBoolValue()); - case BYTES_VALUE -> bytesOf(c.getBytesValue()); - case DOUBLE_VALUE -> doubleOf(c.getDoubleValue()); - case DURATION_VALUE -> durationOf(c.getDurationValue()); - case INT64_VALUE -> intOf(c.getInt64Value()); - case NULL_VALUE -> NullT.NullValue; - case STRING_VALUE -> stringOf(c.getStringValue()); - case TIMESTAMP_VALUE -> timestampOf(c.getTimestampValue()); - case UINT64_VALUE -> uintOf(c.getUint64Value()); - default -> - throw new IllegalArgumentException( - String.format( - "unknown constant type: '%s' of kind '%s'", c, c.getConstantKindCase())); - }; - } - - /** - * resolveTypeName takes a qualified string constructed at parse time, applies the proto namespace - * resolution rules to it in a scan over possible matching types in the TypeProvider. - */ - String resolveTypeName(String typeName) { - for (String qualifiedTypeName : container.resolveCandidateNames(typeName)) { - if (provider.findType(qualifiedTypeName) != null) { - return qualifiedTypeName; - } - } - return null; - } - - static final class ResolvedFunction { - final Expr target; - final String fnName; - final String overloadId; - - ResolvedFunction(Expr target, String fnName, String overloadId) { - this.target = target; - this.fnName = fnName; - this.overloadId = overloadId; - } - } - - /** - * resolveFunction determines the call target, function name, and overload name from a given Expr - * value. - * - *

The resolveFunction resolves ambiguities where a function may either be a receiver-style - * invocation or a qualified global function name. - * - *

- */ ResolvedFunction resolveFunction(Expr expr) { // Note: similar logic exists within the `checker/checker.go`. If making changes here // please consider the impact on checker.go and consolidate implementations or mirror code @@ -968,12 +1642,12 @@ ResolvedFunction resolveFunction(Expr expr) { Reference oRef = refMap.get(expr.getId()); if (oRef != null) { if (oRef.getOverloadIdCount() == 1) { - return new ResolvedFunction(target, fnName, oRef.getOverloadId(0)); + return resolvedFunction(target, fnName, oRef.getOverloadId(0)); } // Note, this namespaced function name will not appear as a fully qualified name in ASTs // built and stored before cel-go v0.5.0; however, this functionality did not work at all // before the v0.5.0 release. - return new ResolvedFunction(target, fnName, ""); + return resolvedFunction(target, fnName, ""); } // Parse-only expressions need to handle the same logic as is normally performed at check @@ -985,7 +1659,7 @@ ResolvedFunction resolveFunction(Expr expr) { // the interpreter dispatcher as it may have been omitted from the checker environment. for (String qualifiedName : container.resolveCandidateNames(fnName)) { if (disp.findOverload(qualifiedName) != null) { - return new ResolvedFunction(target, qualifiedName, ""); + return resolvedFunction(null, qualifiedName, ""); } } // It's possible that the overload was not found, but this situation is accounted for in @@ -993,7 +1667,7 @@ ResolvedFunction resolveFunction(Expr expr) { // namespaced identifiers must be stripped, as all declarations already use fully-qualified // names. This stripping behavior is handled automatically by the ResolveCandidateNames // call. - return new ResolvedFunction(target, stripLeadingDot(fnName), ""); + return resolvedFunction(null, stripLeadingDot(fnName), ""); } // Handle the situation where the function target actually indicates a qualified function @@ -1005,51 +1679,68 @@ ResolvedFunction resolveFunction(Expr expr) { if (disp.findOverload(qualifiedName) != null) { // Clear the target to ensure the proper arity is used for finding the // implementation. - return new ResolvedFunction(null, qualifiedName, ""); + return resolvedFunction(null, qualifiedName, ""); } } } // In the default case, the function is exactly as it was advertised: a receiver call on with // an expression-based target with the given simple function name. - return new ResolvedFunction(target, fnName, ""); + return resolvedFunction(target, fnName, ""); } - InterpretableAttribute relativeAttr(long id, Interpretable eval) { - InterpretableAttribute eAttr; - if (eval instanceof InterpretableAttribute) { - eAttr = (InterpretableAttribute) eval; - } else { - eAttr = new EvalAttr(adapter, attrFactory.relativeAttribute(id, eval)); - } - Interpretable decAttr = decorate(eAttr); - if (decAttr == null) { - return null; - } - if (!(decAttr instanceof InterpretableAttribute)) { - throw new IllegalStateException( - String.format( - "invalid attribute decoration: %s(%s)", decAttr, decAttr.getClass().getName())); - } - eAttr = (InterpretableAttribute) decAttr; - return eAttr; + private static boolean isBoolConst(Expr expr, boolean value) { + return expr.getExprKindCase() == Expr.ExprKindCase.CONST_EXPR + && expr.getConstExpr().getConstantKindCase() == Constant.ConstantKindCase.BOOL_VALUE + && expr.getConstExpr().getBoolValue() == value; } - /** - * toQualifiedName converts an expression AST into a qualified name if possible, with a boolean - * 'found' value that indicates if the conversion is successful. - */ - String toQualifiedName(Expr operand) { - // If the checker identified the expression as an attribute by the type-checker, then it can't - // possibly be part of qualified name in a namespace. - if (refMap.containsKey(operand.getId())) { - return ""; + private static boolean referencesIdent(Expr expr, String name) { + switch (expr.getExprKindCase()) { + case IDENT_EXPR: + return expr.getIdentExpr().getName().equals(name); + case SELECT_EXPR: + return referencesIdent(expr.getSelectExpr().getOperand(), name); + case CALL_EXPR: + Call call = expr.getCallExpr(); + if (call.hasTarget() && referencesIdent(call.getTarget(), name)) { + return true; + } + for (Expr arg : call.getArgsList()) { + if (referencesIdent(arg, name)) { + return true; + } + } + return false; + case LIST_EXPR: + for (Expr elem : expr.getListExpr().getElementsList()) { + if (referencesIdent(elem, name)) { + return true; + } + } + return false; + case STRUCT_EXPR: + for (Entry entry : expr.getStructExpr().getEntriesList()) { + if (referencesIdent(entry.getValue(), name)) { + return true; + } + } + return false; + case COMPREHENSION_EXPR: + Comprehension comprehension = expr.getComprehensionExpr(); + return referencesIdent(comprehension.getIterRange(), name) + || referencesIdent(comprehension.getAccuInit(), name) + || (!comprehension.getIterVar().equals(name) + && !comprehension.getIterVar2().equals(name) + && !comprehension.getAccuVar().equals(name) + && referencesIdent(comprehension.getLoopCondition(), name)) + || (!comprehension.getIterVar().equals(name) + && !comprehension.getIterVar2().equals(name) + && !comprehension.getAccuVar().equals(name) + && referencesIdent(comprehension.getLoopStep(), name)) + || (!comprehension.getAccuVar().equals(name) + && referencesIdent(comprehension.getResult(), name)); + default: + return false; } - // Since functions cannot be both namespaced and receiver functions, if the operand is not an - // qualified variable name, return the (possibly) qualified name given the expressions. - return Container.toQualifiedName(operand); - } - - String stripLeadingDot(String name) { - return name.startsWith(".") ? name.substring(1) : name; } } diff --git a/core/src/main/java/org/projectnessie/cel/interpreter/PlanningPolicy.java b/core/src/main/java/org/projectnessie/cel/interpreter/PlanningPolicy.java new file mode 100644 index 000000000..f5f22f0ae --- /dev/null +++ b/core/src/main/java/org/projectnessie/cel/interpreter/PlanningPolicy.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2026 The Authors of CEL-Java + * + * 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 + * + * http://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 org.projectnessie.cel.interpreter; + +/** Immutable planning-time permissions for native specialization and built-in optimization. */ +enum PlanningPolicy { + ESTABLISHED_ONLY(false, false), + NATIVE_SPECIALIZATION_PERMITTED(true, false), + NATIVE_OPTIMIZED(true, true); + + private final boolean nativeSpecializationPermitted; + private final boolean builtInOptimizationEnabled; + + PlanningPolicy(boolean nativeSpecializationPermitted, boolean builtInOptimizationEnabled) { + this.nativeSpecializationPermitted = nativeSpecializationPermitted; + this.builtInOptimizationEnabled = builtInOptimizationEnabled; + } + + static PlanningPolicy nativeSpecialization(boolean permitted) { + return permitted ? NATIVE_SPECIALIZATION_PERMITTED : ESTABLISHED_ONLY; + } + + boolean nativeSpecializationPermitted() { + return nativeSpecializationPermitted; + } + + boolean builtInOptimizationEnabled() { + return builtInOptimizationEnabled; + } +} diff --git a/core/src/main/java/org/projectnessie/cel/interpreter/StandardNativeOverloadCatalog.java b/core/src/main/java/org/projectnessie/cel/interpreter/StandardNativeOverloadCatalog.java new file mode 100644 index 000000000..91e91e162 --- /dev/null +++ b/core/src/main/java/org/projectnessie/cel/interpreter/StandardNativeOverloadCatalog.java @@ -0,0 +1,168 @@ +/* + * Copyright (C) 2026 The Authors of CEL-Java + * + * 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 + * + * http://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 org.projectnessie.cel.interpreter; + +import static org.projectnessie.cel.common.types.Overloads.AddDouble; +import static org.projectnessie.cel.common.types.Overloads.AddInt64; +import static org.projectnessie.cel.common.types.Overloads.AddList; +import static org.projectnessie.cel.common.types.Overloads.AddString; +import static org.projectnessie.cel.common.types.Overloads.DivideDouble; +import static org.projectnessie.cel.common.types.Overloads.DivideInt64; +import static org.projectnessie.cel.common.types.Overloads.GreaterBool; +import static org.projectnessie.cel.common.types.Overloads.GreaterDouble; +import static org.projectnessie.cel.common.types.Overloads.GreaterEqualsBool; +import static org.projectnessie.cel.common.types.Overloads.GreaterEqualsDouble; +import static org.projectnessie.cel.common.types.Overloads.GreaterEqualsInt64; +import static org.projectnessie.cel.common.types.Overloads.GreaterEqualsString; +import static org.projectnessie.cel.common.types.Overloads.GreaterInt64; +import static org.projectnessie.cel.common.types.Overloads.GreaterString; +import static org.projectnessie.cel.common.types.Overloads.InList; +import static org.projectnessie.cel.common.types.Overloads.InMap; +import static org.projectnessie.cel.common.types.Overloads.IndexList; +import static org.projectnessie.cel.common.types.Overloads.IndexMap; +import static org.projectnessie.cel.common.types.Overloads.LessBool; +import static org.projectnessie.cel.common.types.Overloads.LessDouble; +import static org.projectnessie.cel.common.types.Overloads.LessEqualsBool; +import static org.projectnessie.cel.common.types.Overloads.LessEqualsDouble; +import static org.projectnessie.cel.common.types.Overloads.LessEqualsInt64; +import static org.projectnessie.cel.common.types.Overloads.LessEqualsString; +import static org.projectnessie.cel.common.types.Overloads.LessInt64; +import static org.projectnessie.cel.common.types.Overloads.LessString; +import static org.projectnessie.cel.common.types.Overloads.LogicalNot; +import static org.projectnessie.cel.common.types.Overloads.ModuloInt64; +import static org.projectnessie.cel.common.types.Overloads.MultiplyDouble; +import static org.projectnessie.cel.common.types.Overloads.MultiplyInt64; +import static org.projectnessie.cel.common.types.Overloads.NegateDouble; +import static org.projectnessie.cel.common.types.Overloads.NegateInt64; +import static org.projectnessie.cel.common.types.Overloads.SizeList; +import static org.projectnessie.cel.common.types.Overloads.SizeListInst; +import static org.projectnessie.cel.common.types.Overloads.SizeMap; +import static org.projectnessie.cel.common.types.Overloads.SizeMapInst; +import static org.projectnessie.cel.common.types.Overloads.SubtractDouble; +import static org.projectnessie.cel.common.types.Overloads.SubtractInt64; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.IdentityHashMap; +import java.util.List; +import java.util.Map; +import org.projectnessie.cel.common.operators.Operator; +import org.projectnessie.cel.interpreter.functions.Overload; + +/** Exact standard-overload provenance used only while planning checked expressions. */ +final class StandardNativeOverloadCatalog { + private StandardNativeOverloadCatalog() {} + + static NativeOverloadDescriptor find( + Overload implementation, String function, String overloadId) { + if (implementation == null || function == null || overloadId == null) { + return null; + } + for (NativeOverloadDescriptor descriptor : descriptors(implementation)) { + if (descriptor.matches(function, overloadId)) { + return descriptor; + } + } + return null; + } + + static List descriptors(Overload implementation) { + List descriptors = Holder.BY_IMPLEMENTATION.get(implementation); + return descriptors != null ? descriptors : List.of(); + } + + private static final class Holder { + private static final Map> BY_IMPLEMENTATION = + createCatalog(); + } + + private static Map> createCatalog() { + Map implementations = new HashMap<>(); + for (Overload implementation : Overload.standardOverloads()) { + Overload previous = implementations.put(implementation.operator, implementation); + if (previous != null) { + throw new IllegalStateException( + "duplicate standard overload implementation: " + implementation.operator); + } + } + + IdentityHashMap> mutable = new IdentityHashMap<>(); + bind(mutable, implementations, Operator.LogicalNot.id, LogicalNot); + bind(mutable, implementations, Operator.Negate.id, NegateInt64, NegateDouble); + bind(mutable, implementations, Operator.Add.id, AddInt64, AddDouble, AddString, AddList); + bind(mutable, implementations, Operator.Subtract.id, SubtractInt64, SubtractDouble); + bind(mutable, implementations, Operator.Multiply.id, MultiplyInt64, MultiplyDouble); + bind(mutable, implementations, Operator.Divide.id, DivideInt64, DivideDouble); + bind(mutable, implementations, Operator.Modulo.id, ModuloInt64); + bind(mutable, implementations, Operator.Less.id, LessBool, LessInt64, LessDouble, LessString); + bind( + mutable, + implementations, + Operator.LessEquals.id, + LessEqualsBool, + LessEqualsInt64, + LessEqualsDouble, + LessEqualsString); + bind( + mutable, + implementations, + Operator.Greater.id, + GreaterBool, + GreaterInt64, + GreaterDouble, + GreaterString); + bind( + mutable, + implementations, + Operator.GreaterEquals.id, + GreaterEqualsBool, + GreaterEqualsInt64, + GreaterEqualsDouble, + GreaterEqualsString); + bind(mutable, implementations, Operator.Index.id, IndexList, IndexMap); + bind(mutable, implementations, Operator.In.id, InList, InMap); + bind( + mutable, + implementations, + org.projectnessie.cel.common.types.Overloads.Size, + SizeList, + SizeListInst, + SizeMap, + SizeMapInst); + + IdentityHashMap> immutable = + new IdentityHashMap<>(mutable.size()); + mutable.forEach( + (implementation, descriptors) -> immutable.put(implementation, List.copyOf(descriptors))); + return immutable; + } + + private static void bind( + IdentityHashMap> catalog, + Map implementations, + String function, + String... overloadIds) { + Overload implementation = implementations.get(function); + if (implementation == null) { + throw new IllegalStateException("missing standard overload implementation: " + function); + } + List descriptors = + catalog.computeIfAbsent(implementation, ignored -> new ArrayList<>()); + for (String overloadId : overloadIds) { + descriptors.add(new NativeOverloadDescriptor(function, overloadId)); + } + } +} diff --git a/core/src/main/java/org/projectnessie/cel/interpreter/StandardOverloadProvenance.java b/core/src/main/java/org/projectnessie/cel/interpreter/StandardOverloadProvenance.java new file mode 100644 index 000000000..08de95687 --- /dev/null +++ b/core/src/main/java/org/projectnessie/cel/interpreter/StandardOverloadProvenance.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2026 The Authors of CEL-Java + * + * 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 + * + * http://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 org.projectnessie.cel.interpreter; + +import com.google.api.expr.v1alpha1.Expr; +import com.google.api.expr.v1alpha1.Reference; +import java.util.Map; +import org.projectnessie.cel.common.operators.Operator; +import org.projectnessie.cel.common.types.Overloads; +import org.projectnessie.cel.interpreter.functions.Overload; + +/** Exact identity checks for checked calls resolved to the built-in overload objects. */ +final class StandardOverloadProvenance { + private StandardOverloadProvenance() {} + + static boolean isExactStandard( + Dispatcher dispatcher, Map references, Expr expression) { + if (expression.getExprKindCase() != Expr.ExprKindCase.CALL_EXPR) { + return false; + } + Reference reference = references.get(expression.getId()); + if (reference == null || reference.getOverloadIdCount() != 1) { + return false; + } + String function = expression.getCallExpr().getFunction(); + Overload implementation = dispatcher.findOverload(reference.getOverloadId(0)); + if (implementation == null) { + implementation = dispatcher.findOverload(function); + } + if (implementation == null) { + return isIntrinsicStandard(function, reference.getOverloadId(0)); + } + for (Overload standard : Overload.standardOverloads()) { + if (standard == implementation && standard.operator.equals(function)) { + return true; + } + } + return false; + } + + private static boolean isIntrinsicStandard(String function, String overload) { + return (function.equals(Operator.LogicalAnd.id) && overload.equals(Overloads.LogicalAnd)) + || (function.equals(Operator.LogicalOr.id) && overload.equals(Overloads.LogicalOr)) + || (function.equals(Operator.Conditional.id) && overload.equals(Overloads.Conditional)) + || (function.equals(Operator.Equals.id) && overload.equals(Overloads.Equals)) + || (function.equals(Operator.NotEquals.id) && overload.equals(Overloads.NotEquals)); + } +} diff --git a/core/src/main/java/org/projectnessie/cel/interpreter/ValueSignal.java b/core/src/main/java/org/projectnessie/cel/interpreter/ValueSignal.java new file mode 100644 index 000000000..28f8b4180 --- /dev/null +++ b/core/src/main/java/org/projectnessie/cel/interpreter/ValueSignal.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2026 The Authors of CEL-Java + * + * 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 + * + * http://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 org.projectnessie.cel.interpreter; + +import static java.util.Objects.requireNonNull; + +import org.projectnessie.cel.common.types.ref.Val; + +/** Stackless transport for an already produced CEL value inside a typed evaluation island. */ +final class ValueSignal extends RuntimeException { + final Val value; + + ValueSignal(Val value) { + super(null, null, false, false); + this.value = requireNonNull(value, "value"); + } + + static ValueSignal signal(Val value) { + return new ValueSignal(value); + } +} diff --git a/core/src/main/java/org/projectnessie/cel/interpreter/functions/Overload.java b/core/src/main/java/org/projectnessie/cel/interpreter/functions/Overload.java index 0f2e3326d..3f6825799 100644 --- a/core/src/main/java/org/projectnessie/cel/interpreter/functions/Overload.java +++ b/core/src/main/java/org/projectnessie/cel/interpreter/functions/Overload.java @@ -235,8 +235,23 @@ public String toString() { return sb.toString(); } - /** StandardOverloads returns the definitions of the built-in overloads. */ + /** + * Returns the built-in runtime overload definitions. + * + *

The returned array is a new shallow copy. The overload definitions and their operation + * objects are shared and immutable. + * + * @return a copy of the standard overload array + */ public static Overload[] standardOverloads() { + return StandardOverloadsHolder.OVERLOADS.clone(); + } + + private static final class StandardOverloadsHolder { + private static final Overload[] OVERLOADS = createStandardOverloads(); + } + + private static Overload[] createStandardOverloads() { return new Overload[] { // Logical not (!a) unary( diff --git a/core/src/test/java/org/projectnessie/cel/NativePlanRootTest.java b/core/src/test/java/org/projectnessie/cel/NativePlanRootTest.java new file mode 100644 index 000000000..7d9ef7089 --- /dev/null +++ b/core/src/test/java/org/projectnessie/cel/NativePlanRootTest.java @@ -0,0 +1,570 @@ +/* + * Copyright (C) 2026 The Authors of CEL-Java + * + * 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 + * + * http://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 org.projectnessie.cel; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.projectnessie.cel.Env.newEnv; +import static org.projectnessie.cel.EnvOption.customTypeAdapter; +import static org.projectnessie.cel.EnvOption.declarations; +import static org.projectnessie.cel.EvalOption.OptDisableNativeEval; +import static org.projectnessie.cel.EvalOption.OptExhaustiveEval; +import static org.projectnessie.cel.EvalOption.OptTrackState; +import static org.projectnessie.cel.ProgramOption.customDecorator; +import static org.projectnessie.cel.ProgramOption.evalOptions; +import static org.projectnessie.cel.ProgramOption.functions; +import static org.projectnessie.cel.checker.Decls.Int; +import static org.projectnessie.cel.checker.Decls.newFunction; +import static org.projectnessie.cel.checker.Decls.newOverload; +import static org.projectnessie.cel.checker.Decls.newVar; +import static org.projectnessie.cel.common.types.Err.newErr; +import static org.projectnessie.cel.common.types.IntT.intOf; +import static org.projectnessie.cel.common.types.StringT.stringOf; +import static org.projectnessie.cel.common.types.UnknownT.unknownOf; +import static org.projectnessie.cel.interpreter.Activation.newActivation; +import static org.projectnessie.cel.interpreter.Coster.Cost.estimateCost; + +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Stream; +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestFactory; +import org.projectnessie.cel.Env.AstIssuesTuple; +import org.projectnessie.cel.checker.Decls; +import org.projectnessie.cel.common.types.Err; +import org.projectnessie.cel.common.types.NullT; +import org.projectnessie.cel.common.types.pb.DefaultTypeAdapter; +import org.projectnessie.cel.common.types.ref.StandardScalarTypeAdapter; +import org.projectnessie.cel.common.types.ref.Val; +import org.projectnessie.cel.interpreter.Activation; +import org.projectnessie.cel.interpreter.ActivationFunction; +import org.projectnessie.cel.interpreter.Interpretable; +import org.projectnessie.cel.interpreter.Interpretable.InterpretableCall; +import org.projectnessie.cel.interpreter.functions.Overload; + +class NativePlanRootTest { + + @TestFactory + Stream scalarConstantAndIdentifierRootsBelongOnlyToTheIntegratedPlanner() { + Map nullInput = new HashMap<>(); + nullInput.put("n", null); + List cases = + List.of( + new RootCase("x", Map.of("x", 41L)), + new RootCase("s", Map.of("s", "value")), + new RootCase("d", Map.of("d", 1.5d)), + new RootCase("b", Map.of("b", true)), + new RootCase("n", nullInput), + new RootCase("41", Map.of()), + new RootCase("'value'", Map.of()), + new RootCase("1.5", Map.of()), + new RootCase("true", Map.of()), + new RootCase("null", Map.of())); + + return integratedRootTests(cases); + } + + @TestFactory + Stream strictScalarOperationRootsBelongOnlyToTheIntegratedPlanner() { + Map values = Map.of("x", 42L, "d", 42.5d, "s", "value", "b", false); + return integratedRootTests( + Stream.of( + "!b", + "-x", + "-d", + "x + 1", + "x - 1", + "x * 2", + "x / 2", + "x % 2", + "d + 1.0", + "d - 1.0", + "d * 2.0", + "d / 2.0", + "s + '-suffix'", + "b < true", + "b <= true", + "b > true", + "b >= true", + "x < 43", + "x <= 42", + "x > 41", + "x >= 42", + "d < 43.0", + "d <= 42.5", + "d > 41.0", + "d >= 42.5", + "s < 'z'", + "s <= 'value'", + "s > 'a'", + "s >= 'value'") + .map(expression -> new RootCase(expression, values)) + .toList()); + } + + @TestFactory + Stream scalarControlRootsBelongOnlyToTheIntegratedPlanner() { + Map values = new HashMap<>(); + values.put("x", 42L); + values.put("y", 41L); + values.put("d", 42.5d); + values.put("e", 42.5d); + values.put("s", "value"); + values.put("t", "other"); + values.put("b", true); + values.put("c", false); + values.put("n", null); + + return integratedRootTests( + Stream.of( + "b == c", + "b != c", + "x == y", + "x != y", + "d == e", + "d != e", + "s == t", + "s != t", + "n == null", + "n != null", + "b && c", + "b || c", + "b ? c : true", + "b ? x : y", + "b ? d : e", + "b ? s : t", + "b ? n : null") + .map(expression -> new RootCase(expression, values)) + .toList()); + } + + @TestFactory + Stream mapSelectorRootsBelongOnlyToTheIntegratedPlanner() { + Map nullValues = new HashMap<>(); + nullValues.put("answer", null); + List cases = + List.of( + new RootCase("bools.answer", Map.of("bools", Map.of("answer", true))), + new RootCase("bools['answer']", Map.of("bools", Map.of("answer", true))), + new RootCase("ints.answer", Map.of("ints", Map.of("answer", 41L))), + new RootCase("ints['answer']", Map.of("ints", Map.of("answer", 41L))), + new RootCase("doubles.answer", Map.of("doubles", Map.of("answer", -0.0d))), + new RootCase("doubles['answer']", Map.of("doubles", Map.of("answer", -0.0d))), + new RootCase("strings.answer", Map.of("strings", Map.of("answer", "value"))), + new RootCase("strings['answer']", Map.of("strings", Map.of("answer", "value"))), + new RootCase("nulls.answer", Map.of("nulls", nullValues)), + new RootCase("nulls['answer']", Map.of("nulls", nullValues))); + + return integratedRootTests(mapSelectorEnv(), cases); + } + + @TestFactory + Stream mapSelectorsPreserveEstablishedCompatibilityResults() { + Map nullValue = new HashMap<>(); + nullValue.put("answer", null); + Map nullSource = new HashMap<>(); + nullSource.put("ints", null); + List cases = + List.of( + new CompatibilityCase("native value", Map.of("ints", Map.of("answer", 41L))), + new CompatibilityCase("boxed value", Map.of("ints", Map.of("answer", 41))), + new CompatibilityCase("wrapped value", Map.of("ints", Map.of("answer", intOf(41L)))), + new CompatibilityCase( + "wrapped source", + Map.of("ints", DefaultTypeAdapter.Instance.nativeToValue(Map.of("answer", 41L)))), + new CompatibilityCase( + "carried error", Map.of("ints", Map.of("answer", newErr("carried")))), + new CompatibilityCase( + "carried unknown", Map.of("ints", Map.of("answer", unknownOf(51L)))), + new CompatibilityCase("wrong value", Map.of("ints", Map.of("answer", "wrong"))), + new CompatibilityCase("present null value", Map.of("ints", nullValue)), + new CompatibilityCase("absent key", Map.of("ints", Map.of("other", 41L))), + new CompatibilityCase("source error", Map.of("ints", newErr("source"))), + new CompatibilityCase("source unknown", Map.of("ints", unknownOf(52L))), + new CompatibilityCase("wrong source", Map.of("ints", "wrong source")), + new CompatibilityCase("present null source", nullSource), + new CompatibilityCase("absent source", Map.of())); + + return Stream.of("ints.answer", "ints['answer']") + .flatMap( + expression -> + cases.stream() + .map( + testCase -> + DynamicTest.dynamicTest( + expression + ": " + testCase.label(), + () -> { + Env env = mapSelectorEnv(); + Ast ast = compile(env, expression); + Prog integrated = (Prog) env.program(ast); + Prog established = + (Prog) env.program(ast, evalOptions(OptDisableNativeEval)); + + assertIntegratedRoot(integrated, established); + assertEquivalent( + integrated.eval(testCase.input()).getVal(), + established.eval(testCase.input()).getVal()); + }))); + } + + @TestFactory + Stream mapSelectorRootsResolveTheirSourceOnceWithoutRepeatedKeyLookup() { + return Stream.of("ints.answer", "ints['answer']") + .map( + expression -> + DynamicTest.dynamicTest( + expression, + () -> { + Env env = mapSelectorEnv(); + Ast ast = compile(env, expression); + Prog integrated = (Prog) env.program(ast); + CountingMap values = new CountingMap(); + values.put("answer", 41L); + AtomicInteger resolutions = new AtomicInteger(); + Activation activation = + newActivation( + (ActivationFunction) + name -> { + resolutions.incrementAndGet(); + return name.equals("ints") ? values : ActivationFunction.ABSENT; + }); + + assertThat(integrated.interpretable.getClass().getSimpleName()) + .isEqualTo("NativeIsland"); + assertThat(integrated.interpretable.eval(activation).intValue()) + .isEqualTo(41L); + assertThat(resolutions).hasValue(1); + assertThat(values.gets()).isEqualTo(expression.indexOf('[') < 0 ? 1 : 0); + })); + } + + @TestFactory + Stream nullMapSelectorsDistinguishPresentNullFromAnAbsentKey() { + return Stream.of("nulls.answer", "nulls['answer']") + .map( + expression -> + DynamicTest.dynamicTest( + expression, + () -> { + Env env = mapSelectorEnv(); + Ast ast = compile(env, expression); + Prog integrated = (Prog) env.program(ast); + Prog established = (Prog) env.program(ast, evalOptions(OptDisableNativeEval)); + Map presentNull = new HashMap<>(); + presentNull.put("answer", null); + + Val actualPresent = integrated.eval(Map.of("nulls", presentNull)).getVal(); + Val expectedPresent = established.eval(Map.of("nulls", presentNull)).getVal(); + Val actualAbsent = + integrated + .eval(Map.of("nulls", Map.of("other", "not selected"))) + .getVal(); + Val expectedAbsent = + established + .eval(Map.of("nulls", Map.of("other", "not selected"))) + .getVal(); + + assertIntegratedRoot(integrated, established); + assertEquivalent(actualPresent, expectedPresent); + assertThat(actualPresent).isSameAs(NullT.NullValue); + assertEquivalent(actualAbsent, expectedAbsent); + assertThat(actualAbsent).isInstanceOf(Err.class); + })); + } + + private static void assertIntegratedRoot(Prog integrated, Prog established) { + assertThat(integrated.interpretable.getClass().getSimpleName()).isEqualTo("NativeIsland"); + assertThat(established.interpretable.getClass().getSimpleName()).isNotEqualTo("NativeIsland"); + assertThat(containsNativeNode(established.interpretable)).isFalse(); + } + + private static void assertEquivalent(Val actual, Val expected) { + assertThat(actual.getClass()).isEqualTo(expected.getClass()); + assertThat(actual.type()).isSameAs(expected.type()); + assertThat(actual.toString()).isEqualTo(expected.toString()); + if (actual instanceof Err actualError && expected instanceof Err expectedError) { + assertThat(actualError.hasCause()).isEqualTo(expectedError.hasCause()); + if (actualError.hasCause()) { + assertThat(actualError.getCause().getClass()) + .isEqualTo(expectedError.getCause().getClass()); + assertThat(actualError.getCause().getMessage()) + .isEqualTo(expectedError.getCause().getMessage()); + } + return; + } + if (actual.value() instanceof Double actualDouble + && expected.value() instanceof Double expectedDouble) { + assertThat(Double.doubleToRawLongBits(actualDouble)) + .isEqualTo(Double.doubleToRawLongBits(expectedDouble)); + } else { + assertThat(actual.value()).isEqualTo(expected.value()); + } + } + + private static Stream integratedRootTests(List cases) { + return integratedRootTests(env(DefaultTypeAdapter.Instance), cases); + } + + private static Stream integratedRootTests(Env env, List cases) { + return cases.stream() + .map( + testCase -> + DynamicTest.dynamicTest( + testCase.expression(), + () -> { + Ast ast = compile(env, testCase.expression()); + Prog integrated = (Prog) env.program(ast); + Prog established = (Prog) env.program(ast, evalOptions(OptDisableNativeEval)); + + assertIntegratedRoot(integrated, established); + assertThat(integrated.interpretable.id()).isEqualTo(ast.getExpr().getId()); + assertThat(integrated.cost()).isEqualTo(established.cost()); + Val actual = integrated.eval(testCase.input()).getVal(); + Val expected = established.eval(testCase.input()).getVal(); + assertEquivalent(actual, expected); + })); + } + + @Test + void storesEitherEvaluatorAsOneInterpretableWithTheCheckedRootIdentityAndCost() { + Env env = env(DefaultTypeAdapter.Instance); + Ast ast = compile(env, "x + 1"); + Prog nativeProgram = (Prog) env.program(ast); + Prog currentProgram = (Prog) env.program(ast, evalOptions(OptDisableNativeEval)); + + assertThat(nativeProgram.interpretable.getClass().getSimpleName()).isEqualTo("NativeIsland"); + assertThat(currentProgram.interpretable.getClass().getSimpleName()) + .isNotEqualTo("NativeIsland"); + assertThat(nativeProgram.interpretable.id()).isEqualTo(ast.getExpr().getId()); + assertThat(currentProgram.interpretable.id()).isEqualTo(ast.getExpr().getId()); + assertThat(nativeProgram.cost()).isEqualTo(currentProgram.cost()); + } + + @Test + void adaptsANativeSuccessExactlyOnceAtTheExecutableRoot() { + CountingAdapter adapter = new CountingAdapter(); + Env env = env(adapter); + Prog program = (Prog) env.program(compile(env, "x + 1")); + Interpretable root = program.interpretable; + + adapter.reset(); + assertThat(root.eval(newActivation(Map.of("x", 41L))).intValue()).isEqualTo(42L); + assertThat(adapter.count()).isEqualTo(1); + + adapter.reset(); + assertThat(program.eval(Map.of("x", 41L)).getVal().intValue()).isEqualTo(42L); + assertThat(adapter.count()).isEqualTo(1); + } + + @Test + void returnsCarriedCompatibilityValuesWithoutRootReadaptation() { + CountingAdapter adapter = new CountingAdapter(); + Env env = env(adapter); + Prog intProgram = (Prog) env.program(compile(env, "x")); + Prog stringProgram = (Prog) env.program(compile(env, "s")); + Val error = newErr("carried error"); + Val unknown = unknownOf(42L); + Val nullString = stringOf(null); + + assertCarried(intProgram, adapter, "x", error); + assertCarried(intProgram, adapter, "x", unknown); + assertCarried(stringProgram, adapter, "s", nullString); + } + + @Test + void statefulProgramFactoriesAlwaysCreateAnExecutableRootForEvalAndCost() { + Env env = env(DefaultTypeAdapter.Instance); + Ast ast = compile(env, "x + 1"); + Program current = env.program(ast, evalOptions(OptDisableNativeEval)); + Program tracked = env.program(ast, evalOptions(OptTrackState)); + Program exhaustive = env.program(ast, evalOptions(OptExhaustiveEval)); + + assertThat(tracked.eval(Map.of("x", 41L)).getVal().intValue()).isEqualTo(42L); + assertThat(exhaustive.eval(Map.of("x", 41L)).getVal().intValue()).isEqualTo(42L); + assertThat(estimateCost(tracked)).isEqualTo(estimateCost(current)); + assertThat(estimateCost(exhaustive)).isEqualTo(estimateCost(current)); + } + + @Test + void onlyEmptyOrOptimizeOnlyEvalOptionSubsetsPermitNativePlanning() { + EvalOption[] values = EvalOption.values(); + assertThat(values).hasSize(5); + Env env = + newEnv( + declarations( + newVar("x", Int), + newFunction("opaque", newOverload("opaque_int", List.of(Int), Int)))); + Ast ast = compile(env, "opaque(x + 1)"); + Overload opaque = Overload.unary("opaque_int", value -> value); + + for (int mask = 0; mask < 1 << values.length; mask++) { + Set subset = EnumSet.noneOf(EvalOption.class); + for (int bit = 0; bit < values.length; bit++) { + if ((mask & (1 << bit)) != 0) { + subset.add(values[bit]); + } + } + + boolean nativePermitted = + subset.isEmpty() || subset.equals(EnumSet.of(EvalOption.OptOptimize)); + assertThat(CEL.nativePlanningPermitted(subset, List.of())) + .as(subset.toString()) + .isEqualTo(nativePermitted); + + Program program = + env.program(ast, functions(opaque), evalOptions(subset.toArray(EvalOption[]::new))); + assertThat(program.eval(Map.of("x", 41L)).getVal().intValue()) + .as(subset.toString()) + .isEqualTo(42L); + + if (program instanceof Prog prog) { + assertThat(containsNativeNode(prog.interpretable)) + .as(subset.toString()) + .isEqualTo(nativePermitted); + } + } + } + + @Test + void customDecoratorsDisableNativePlanningBeforeAnyNodeIsBuilt() { + Env env = env(DefaultTypeAdapter.Instance); + Ast ast = compile(env, "x + 1"); + List observed = new ArrayList<>(); + + Prog program = + (Prog) + env.program( + ast, + customDecorator( + node -> { + observed.add(node.getClass().getName()); + return node; + })); + + assertThat(CEL.nativePlanningPermitted(Set.of(), List.of(node -> node))).isFalse(); + assertThat(observed).noneMatch(name -> name.contains("Native")); + assertThat(containsNativeNode(program.interpretable)).isFalse(); + } + + private static boolean containsNativeNode(Interpretable node) { + if (node.getClass().getSimpleName().startsWith("Native")) { + return true; + } + if (node instanceof InterpretableCall call) { + for (Interpretable argument : call.args()) { + if (containsNativeNode(argument)) { + return true; + } + } + } + return false; + } + + private static void assertCarried( + Prog program, CountingAdapter adapter, String variable, Val carried) { + adapter.reset(); + assertThat(program.interpretable.eval(newActivation(Map.of(variable, carried)))) + .isSameAs(carried); + assertThat(adapter.count()).isEqualTo(1); + + adapter.reset(); + assertThat(program.eval(Map.of(variable, carried)).getVal()).isSameAs(carried); + assertThat(adapter.count()).isEqualTo(1); + } + + private static Env env(StandardScalarTypeAdapter adapter) { + return newEnv( + customTypeAdapter(adapter), + declarations( + Decls.newVar("x", Decls.Int), + Decls.newVar("y", Decls.Int), + Decls.newVar("s", Decls.String), + Decls.newVar("t", Decls.String), + Decls.newVar("d", Decls.Double), + Decls.newVar("e", Decls.Double), + Decls.newVar("b", Decls.Bool), + Decls.newVar("c", Decls.Bool), + Decls.newVar("n", Decls.Null))); + } + + private static Env mapSelectorEnv() { + return newEnv( + declarations( + Decls.newVar("bools", Decls.newMapType(Decls.String, Decls.Bool)), + Decls.newVar("ints", Decls.newMapType(Decls.String, Decls.Int)), + Decls.newVar("doubles", Decls.newMapType(Decls.String, Decls.Double)), + Decls.newVar("strings", Decls.newMapType(Decls.String, Decls.String)), + Decls.newVar("nulls", Decls.newMapType(Decls.String, Decls.Null)))); + } + + private static Env listEnv() { + return newEnv( + declarations( + Decls.newVar("flags", Decls.newListType(Decls.Bool)), + Decls.newVar("numbers", Decls.newListType(Decls.Int)), + Decls.newVar("doubles", Decls.newListType(Decls.Double)), + Decls.newVar("words", Decls.newListType(Decls.String)), + Decls.newVar("nulls", Decls.newListType(Decls.Null)), + Decls.newVar("wordTarget", Decls.String))); + } + + private static Ast compile(Env env, String expression) { + AstIssuesTuple result = env.compile(expression); + assertThat(result.hasIssues()).as(expression).isFalse(); + return result.getAst(); + } + + private static final class CountingAdapter implements StandardScalarTypeAdapter { + private final AtomicInteger conversions = new AtomicInteger(); + + @Override + public Val nativeToValue(Object value) { + conversions.incrementAndGet(); + return DefaultTypeAdapter.Instance.nativeToValue(value); + } + + void reset() { + conversions.set(0); + } + + int count() { + return conversions.get(); + } + } + + private static final class CountingMap extends HashMap { + private int gets; + + @Override + public Object get(Object key) { + gets++; + return super.get(key); + } + + int gets() { + return gets; + } + } + + private record RootCase(String expression, Map input) {} + + private record CompatibilityCase(String label, Map input) {} +} diff --git a/core/src/test/java/org/projectnessie/cel/NativePlanTest.java b/core/src/test/java/org/projectnessie/cel/NativePlanTest.java new file mode 100644 index 000000000..d9cc0f333 --- /dev/null +++ b/core/src/test/java/org/projectnessie/cel/NativePlanTest.java @@ -0,0 +1,852 @@ +/* + * Copyright (C) 2021 The Authors of CEL-Java + * + * 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 + * + * http://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 org.projectnessie.cel; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.junit.jupiter.api.DynamicTest.dynamicTest; +import static org.projectnessie.cel.CEL.attributePattern; +import static org.projectnessie.cel.CEL.partialVars; +import static org.projectnessie.cel.Env.newCustomEnv; +import static org.projectnessie.cel.Env.newEnv; +import static org.projectnessie.cel.EnvOption.customTypeAdapter; +import static org.projectnessie.cel.EnvOption.declarations; +import static org.projectnessie.cel.EvalOption.OptDisableNativeEval; +import static org.projectnessie.cel.EvalOption.OptOptimize; +import static org.projectnessie.cel.EvalOption.OptPartialEval; +import static org.projectnessie.cel.ProgramOption.customDecorator; +import static org.projectnessie.cel.ProgramOption.evalOptions; +import static org.projectnessie.cel.ProgramOption.functions; +import static org.projectnessie.cel.ProgramOption.globals; +import static org.projectnessie.cel.common.types.BoolT.False; +import static org.projectnessie.cel.common.types.BoolT.True; +import static org.projectnessie.cel.common.types.DoubleT.doubleOf; +import static org.projectnessie.cel.common.types.Err.newErr; +import static org.projectnessie.cel.common.types.IntT.intOf; +import static org.projectnessie.cel.common.types.StringT.stringOf; +import static org.projectnessie.cel.common.types.UnknownT.unknownOf; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Stream; +import org.jspecify.annotations.NonNull; +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestFactory; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; +import org.projectnessie.cel.Env.AstIssuesTuple; +import org.projectnessie.cel.checker.Decls; +import org.projectnessie.cel.common.operators.Operator; +import org.projectnessie.cel.common.types.Err; +import org.projectnessie.cel.common.types.IntT; +import org.projectnessie.cel.common.types.Overloads; +import org.projectnessie.cel.common.types.pb.DefaultTypeAdapter; +import org.projectnessie.cel.common.types.pb.ProtoTypeRegistry; +import org.projectnessie.cel.common.types.ref.StandardScalarFieldProvider; +import org.projectnessie.cel.common.types.ref.StandardScalarTypeAdapter; +import org.projectnessie.cel.common.types.ref.TypeAdapter; +import org.projectnessie.cel.common.types.ref.Val; +import org.projectnessie.cel.common.types.traits.Adder; +import org.projectnessie.cel.common.types.traits.Trait; +import org.projectnessie.cel.interpreter.ActivationFunction; +import org.projectnessie.cel.interpreter.Interpretable; +import org.projectnessie.cel.interpreter.functions.Overload; + +class NativePlanTest { + private final Env env = + newEnv( + declarations( + Decls.newVar("x", Decls.Int), + Decls.newVar("y", Decls.Int), + Decls.newVar("d", Decls.Double), + Decls.newVar("e", Decls.Double), + Decls.newVar("b", Decls.Bool), + Decls.newVar("c", Decls.Bool), + Decls.newVar("s", Decls.String), + Decls.newVar("n", Decls.Null), + Decls.newVar("attrs", Decls.newMapType(Decls.String, Decls.Int)), + Decls.newVar("labels", Decls.newMapType(Decls.String, Decls.String)), + Decls.newVar("numbers", Decls.newListType(Decls.Int)), + Decls.newVar("doubles", Decls.newListType(Decls.Double)), + Decls.newVar("words", Decls.newListType(Decls.String)), + Decls.newVar("flags", Decls.newListType(Decls.Bool)), + Decls.newVar("key", Decls.String), + Decls.newVar("target", Decls.Int), + Decls.newVar("wordTarget", Decls.String))); + + @Test + void builtInAdaptersDeclareStandardScalarSemantics() { + assertThat(DefaultTypeAdapter.Instance).isInstanceOf(StandardScalarTypeAdapter.class); + assertThat(ProtoTypeRegistry.newRegistry()).isInstanceOf(StandardScalarTypeAdapter.class); + assertThat(ProtoTypeRegistry.newRegistry()).isInstanceOf(StandardScalarFieldProvider.class); + } + + static Stream supportedScalarEvaluations() { + return Stream.of( + new Evaluation("x", Map.of("x", 41L)), + new Evaluation("x", Map.of("x", "not an int")), + new Evaluation("x + 1", Map.of("x", intOf(41L))), + new Evaluation("d + 1.0", Map.of("d", doubleOf(41.5d))), + new Evaluation("b && true", Map.of("b", True)), + new Evaluation("s + '!'", Map.of("s", stringOf("hello"))), + new Evaluation("s", Map.of("s", stringOf(null))), + new Evaluation("b ? s : 'unused'", Map.of("b", true, "s", stringOf(null))), + new Evaluation("s + ' suffix'", Map.of("s", stringOf(null))), + new Evaluation("'prefix ' + s", Map.of("s", stringOf(null))), + new Evaluation("x + 1", Map.of("x", 41L)), + new Evaluation("(x + 1) * (y - 2) / 3 % 5", Map.of("x", 8L, "y", 7L)), + new Evaluation("-(x + 1)", Map.of("x", 41L)), + new Evaluation("d / 3.0 + 0.5", Map.of("d", -0.0d)), + new Evaluation("s + ' world' + '!'", Map.of("s", "hello")), + new Evaluation("b && x < y", Map.of("b", false, "x", 1L, "y", 2L)), + new Evaluation("b || x >= y", Map.of("b", true, "x", 1L, "y", 2L)), + new Evaluation("!(x == y)", Map.of("x", 1L, "y", 2L)), + new Evaluation("b ? x + 1 : y + 2", Map.of("b", true, "x", 1L, "y", 9L)), + new Evaluation("d == -0.0", Map.of("d", 0.0d)), + new Evaluation("d != d", Map.of("d", Double.NaN)), + new Evaluation("b ? x / y : x + y", Map.of("b", true, "x", 1L, "y", 0L)), + new Evaluation("b ? x : y", Map.of("b", "not a bool", "x", 1L, "y", 2L)), + new Evaluation("x / y", Map.of("x", 1L, "y", 0L)), + new Evaluation("x + 1", Map.of("x", Long.MAX_VALUE)), + new Evaluation("x + 1", Map.of("x", "not an int")), + new Evaluation("x + 1", Map.of("x", IntT.IntType)), + new Evaluation("x", Map.of())); + } + + @ParameterizedTest(name = "{index}: {0}") + @MethodSource("supportedScalarEvaluations") + void evaluatesSupportedScalarTreesLikeTheExistingInterpreter(Evaluation evaluation) { + assertExpression(evaluation.expression(), evaluation.input()); + } + + @ParameterizedTest(name = "{index}: {0}") + @ValueSource(strings = {"s == 'value'", "s < 'value'"}) + void nullBackedStringOperationsUseTheCurrentCompatibilityPath(String expression) { + Programs programs = programs(expression); + Object input = Map.of("s", stringOf(null)); + + Throwable nativeFailure = catchThrowable(() -> programs.nativeProgram.eval(input)); + Throwable interpreterFailure = catchThrowable(() -> programs.interpreterProgram.eval(input)); + + assertThat(nativeFailure).isNotNull(); + assertThat(interpreterFailure).isNotNull(); + assertThat(nativeFailure.getClass()).isEqualTo(interpreterFailure.getClass()); + assertThat(nativeFailure.getMessage()).isEqualTo(interpreterFailure.getMessage()); + } + + @Test + void preservesNullAndAbsentAsDifferentActivationStates() { + Map presentNull = new java.util.HashMap<>(); + presentNull.put("n", null); + + Programs programs = programs("n == null"); + assertEquivalent( + programs.nativeProgram.eval(presentNull).getVal(), + programs.interpreterProgram.eval(presentNull).getVal()); + assertEquivalent( + programs.nativeProgram.eval(Map.of()).getVal(), + programs.interpreterProgram.eval(Map.of()).getVal()); + } + + static Stream checkedMapSelectorEvaluations() { + Val adaptedMap = DefaultTypeAdapter.Instance.nativeToValue(Map.of("answer", 50_021L)); + Map presentNull = new java.util.HashMap<>(); + presentNull.put("answer", null); + Map presentNullString = new java.util.HashMap<>(); + presentNullString.put("name", stringOf(null)); + return Stream.of( + new Evaluation("attrs.answer", Map.of("attrs", Map.of("answer", 50_021L))), + new Evaluation( + "attrs.answer == target", + Map.of("attrs", Map.of("answer", 50_021L), "target", 50_021L)), + new Evaluation("labels.name == 'cel'", Map.of("labels", Map.of("name", "cel"))), + new Evaluation("attrs.missing", Map.of("attrs", Map.of())), + new Evaluation("attrs.answer", Map.of("attrs", Map.of("answer", "wrong"))), + new Evaluation("attrs.answer", Map.of("attrs", newErr("map error"))), + new Evaluation("attrs.answer", Map.of("attrs", unknownOf(87L))), + new Evaluation("attrs.answer", Map.of("attrs", "wrong container")), + new Evaluation("attrs.answer", Map.of()), + new Evaluation("attrs.answer == 50021", Map.of("attrs", adaptedMap)), + new Evaluation("attrs.answer", Map.of("attrs", presentNull)), + new Evaluation("labels.name", Map.of("labels", presentNullString))); + } + + @ParameterizedTest(name = "{index}: {0}") + @MethodSource("checkedMapSelectorEvaluations") + void evaluatesCheckedMapSelectorsWithoutTerminalValueAdaptation(Evaluation evaluation) { + assertExpression(evaluation.expression(), evaluation.input()); + } + + @Test + void mapSelectorsPreservePartialQualifierMatchingAndResolutionCounts() { + Ast ast = compile("attrs.answer == target"); + Prog nativeProgram = (Prog) env.program(ast); + Prog interpreterProgram = + (Prog) env.program(ast, evalOptions(OptPartialEval, OptDisableNativeEval)); + Object input = + partialVars( + Map.of("attrs", Map.of("answer", 50_021L), "target", 50_021L), + attributePattern("attrs").qualString("answer")); + + assertIntegratedPlan(nativeProgram, "attrs.answer == target"); + assertEquivalent(nativeProgram.eval(input).getVal(), interpreterProgram.eval(input).getVal()); + } + + @Test + void dynamicMapKeysRemainOnTheCurrentEvaluator() { + Ast ast = compile("attrs[key]"); + Prog program = (Prog) env.program(ast); + + assertCurrentPlan(program); + assertThat(program.eval(Map.of("attrs", Map.of("answer", 50_021L), "key", "answer")).getVal()) + .isEqualTo(intOf(50_021L)); + } + + @TestFactory + Stream preservesTerminalOrderingAndShortCircuitSuppression() { + Val error = newErr("terminal error"); + Val unknown = unknownOf(123L); + + return Stream.of( + dynamicTest( + "false right operand suppresses left error for logical and", + () -> assertExpression("b && c", activation(Map.of("b", error, "c", false)))), + dynamicTest( + "false right operand suppresses left unknown for logical and", + () -> assertExpression("b && c", activation(Map.of("b", unknown, "c", false)))), + dynamicTest( + "true right operand suppresses left error for logical or", + () -> assertExpression("b || c", activation(Map.of("b", error, "c", true)))), + dynamicTest( + "true right operand suppresses left unknown for logical or", + () -> assertExpression("b || c", activation(Map.of("b", unknown, "c", true)))), + dynamicTest( + "left error precedes right unknown for equality", + () -> assertExpression("b == c", activation(Map.of("b", error, "c", unknown)))), + dynamicTest( + "left unknown precedes right error for equality", + () -> assertExpression("b == c", activation(Map.of("b", unknown, "c", error)))), + dynamicTest( + "conditional propagates an error condition", + () -> assertExpression("b ? x : y", activation(Map.of("b", error, "x", 1L, "y", 2L)))), + dynamicTest( + "conditional propagates an unknown condition", + () -> + assertExpression("b ? x : y", activation(Map.of("b", unknown, "x", 1L, "y", 2L))))); + } + + @Test + void shortCircuitAndConditionalReachOnlyRequiredIdentifiers() { + assertResolutionCounts("b && c", Map.of("b", false, "c", true), Map.of("b", 1, "c", 0)); + assertResolutionCounts("b || c", Map.of("b", true, "c", false), Map.of("b", 1, "c", 0)); + assertResolutionCounts( + "b ? x : y", Map.of("b", true, "x", 1L, "y", 2L), Map.of("b", 1, "x", 1, "y", 0)); + assertResolutionCounts( + "b ? x : y", Map.of("b", false, "x", 1L, "y", 2L), Map.of("b", 1, "x", 0, "y", 1)); + } + + static Stream numericBoundaryEvaluations() { + return Stream.of( + new Evaluation("x + y", Map.of("x", Long.MAX_VALUE, "y", 1L)), + new Evaluation("x - y", Map.of("x", Long.MIN_VALUE, "y", 1L)), + new Evaluation("x * y", Map.of("x", Long.MAX_VALUE, "y", 2L)), + new Evaluation("-x", Map.of("x", Long.MIN_VALUE)), + new Evaluation("x / y", Map.of("x", Long.MIN_VALUE, "y", -1L)), + new Evaluation("x % y", Map.of("x", 10L, "y", 0L)), + new Evaluation("d + 1.0", Map.of("d", Double.POSITIVE_INFINITY)), + new Evaluation("d - 1.0", Map.of("d", Double.NEGATIVE_INFINITY)), + new Evaluation("d < 0.0", Map.of("d", Double.NaN)), + new Evaluation("d >= -0.0", Map.of("d", 0.0d)), + new Evaluation("d == d", Map.of("d", Double.NaN))); + } + + @ParameterizedTest(name = "{index}: {0}") + @MethodSource("numericBoundaryEvaluations") + void coversIntegerBoundariesAndDoubleSpecialValues(Evaluation evaluation) { + assertExpression(evaluation.expression(), evaluation.input()); + } + + @ParameterizedTest + @ValueSource(strings = {"attrs['answer']", "attrs['answer'] + 1"}) + void constantMapIndexPreservesCustomAdapterSemantics(String expression) { + TypeAdapter customAdapter = + value -> + value instanceof Long number + ? intOf(number + 100L) + : DefaultTypeAdapter.Instance.nativeToValue(value); + Env adapterEnv = + newEnv( + customTypeAdapter(customAdapter), + declarations(Decls.newVar("attrs", Decls.newMapType(Decls.String, Decls.Int)))); + Ast ast = compile(adapterEnv, expression); + Object input = Map.of("attrs", Map.of("answer", 1L)); + for (ProgramOption[] options : new ProgramOption[][] {{}, {evalOptions(OptOptimize)}}) { + Prog program = (Prog) adapterEnv.program(ast, options); + assertCurrentPlan(program); + Val expected = + adapterEnv.program(ast, evalOptions(OptDisableNativeEval)).eval(input).getVal(); + assertEquivalent(program.eval(input).getVal(), expected); + } + } + + @ParameterizedTest + @ValueSource( + strings = { + "b ? attrs.answer : 0", "b ? 0 : attrs.answer", + "b ? attrs.answer : numbers[0]", "b ? numbers[0] : attrs.answer", + "b ? (c ? attrs.answer : 0) : 0", "b ? attrs['answer'] : 0" + }) + void conditionalAttributeBranchesPreserveErrors(String expression) { + for (boolean condition : new boolean[] {true, false}) { + for (Map attrs : + List.>of(Map.of("answer", newErr("branch error")), Map.of())) { + Object input = Map.of("b", condition, "c", true, "attrs", attrs, "numbers", List.of(0L)); + Ast ast = compile(expression); + Val expected = env.program(ast, evalOptions(OptDisableNativeEval)).eval(input).getVal(); + assertEquivalent(env.program(ast).eval(input).getVal(), expected); + assertEquivalent(env.program(ast, evalOptions(OptOptimize)).eval(input).getVal(), expected); + } + } + } + + @ParameterizedTest + @ValueSource(strings = {"b ? attrs.answer : 0", "b ? attrs.answer : numbers[0]"}) + void conditionalAttributeBranchesPreservePartialQualifierMatching(String expression) { + Ast ast = compile(expression); + Object input = + partialVars( + Map.of("b", true, "attrs", Map.of("answer", 1L), "numbers", List.of(0L)), + attributePattern("attrs").qualString("answer")); + Val expected = + env.program(ast, evalOptions(OptPartialEval, OptDisableNativeEval)).eval(input).getVal(); + assertEquivalent(env.program(ast).eval(input).getVal(), expected); + } + + @Test + void customAdapterAndUntrustedStandardLookingOverloadFallBack() { + TypeAdapter customAdapter = DefaultTypeAdapter.Instance::nativeToValue; + Env adapterEnv = + newEnv(customTypeAdapter(customAdapter), declarations(Decls.newVar("x", Decls.Int))); + Prog adapterProgram = (Prog) adapterEnv.program(compile(adapterEnv, "x + 1")); + assertCurrentPlan(adapterProgram); + + Library.StdLibrary standard = new Library.StdLibrary(); + List compileOptions = new ArrayList<>(standard.getCompileOptions()); + compileOptions.add( + declarations( + Decls.newVar("x", Decls.Int), + Decls.newVar("wordTarget", Decls.String), + Decls.newVar("words", Decls.newListType(Decls.String)), + Decls.newVar("target", Decls.Int), + Decls.newVar("numbers", Decls.newListType(Decls.Int)))); + Env untrustedEnv = newCustomEnv(compileOptions.toArray(EnvOption[]::new)); + Overload add = + Overload.binary(Operator.Add, Trait.AdderType, (left, right) -> ((Adder) left).add(right)); + Prog untrustedProgram = + (Prog) untrustedEnv.program(compile(untrustedEnv, "x + 1"), functions(add)); + assertCurrentPlan(untrustedProgram); + assertThat(untrustedProgram.eval(Map.of("x", 41L)).getVal().intValue()).isEqualTo(42L); + + Ast mappedExistsAst = + compile( + untrustedEnv, "numbers.map(value, value + target).exists(mapped, mapped == target)"); + Prog untrustedMappedExists = (Prog) untrustedEnv.program(mappedExistsAst, functions(add)); + assertCurrentPlan(untrustedMappedExists); + + Ast mappedAllAst = + compile(untrustedEnv, "numbers.map(value, value + target).all(mapped, mapped != target)"); + Prog untrustedMappedAll = (Prog) untrustedEnv.program(mappedAllAst, functions(add)); + assertCurrentPlan(untrustedMappedAll); + + Ast mappedExistsOneAst = + compile( + untrustedEnv, + "numbers.map(value, value + target).exists_one(mapped, mapped == target)"); + Prog untrustedMappedExistsOne = (Prog) untrustedEnv.program(mappedExistsOneAst, functions(add)); + assertCurrentPlan(untrustedMappedExistsOne); + + Overload membership = Overload.binary(Overloads.InList, (left, right) -> False); + Ast membershipAst = compile(untrustedEnv, "wordTarget in words"); + Prog untrustedMembership = (Prog) untrustedEnv.program(membershipAst, functions(membership)); + assertCurrentPlan(untrustedMembership); + assertThat( + untrustedMembership + .eval(Map.of("wordTarget", "cel", "words", List.of("cel"))) + .getVal() + .booleanValue()) + .isFalse(); + + Ast literalMembershipAst = compile(untrustedEnv, "wordTarget in ['cel']"); + Prog untrustedLiteralMembership = + (Prog) untrustedEnv.program(literalMembershipAst, functions(membership)); + assertEstablishedRoot( + untrustedLiteralMembership, "EvalBinary", "literal membership replacement"); + assertThat(untrustedLiteralMembership.eval(Map.of("wordTarget", "cel")).getVal().booleanValue()) + .isFalse(); + + Ast mappedMembershipAst = compile(untrustedEnv, "wordTarget in words.map(value, value)"); + Prog untrustedMappedMembership = + (Prog) untrustedEnv.program(mappedMembershipAst, functions(membership)); + assertCurrentPlan(untrustedMappedMembership); + assertThat( + untrustedMappedMembership + .eval(Map.of("wordTarget", "cel", "words", List.of("cel"))) + .getVal() + .booleanValue()) + .isFalse(); + + Overload index = Overload.binary(Overloads.IndexList, (left, right) -> intOf(99L)); + Ast topLevelIndexAst = compile(untrustedEnv, "numbers[0]"); + Prog untrustedTopLevelIndex = (Prog) untrustedEnv.program(topLevelIndexAst, functions(index)); + assertCurrentPlan(untrustedTopLevelIndex); + assertThat(untrustedTopLevelIndex.interpretable.getClass().getSimpleName()) + .isNotEqualTo("NativeIsland"); + assertThat(untrustedTopLevelIndex.eval(Map.of("numbers", new long[] {1L})).getVal().intValue()) + .isEqualTo(1L); + + Ast indexAst = compile(untrustedEnv, "[x, 2][0]"); + Prog untrustedIndex = (Prog) untrustedEnv.program(indexAst, functions(index)); + assertEstablishedRoot(untrustedIndex, "EvalAttr", "integer literal index replacement"); + assertThat(untrustedIndex.eval(Map.of("x", 1L)).getVal().intValue()).isEqualTo(1L); + + Ast stringIndexAst = compile(untrustedEnv, "['cel'][0]"); + Prog untrustedStringIndex = (Prog) untrustedEnv.program(stringIndexAst, functions(index)); + assertEstablishedRoot(untrustedStringIndex, "EvalAttr", "string literal index replacement"); + assertThat(untrustedStringIndex.eval(Map.of()).getVal().value()).isEqualTo("cel"); + + Ast boolIndexAst = compile(untrustedEnv, "[true, false][0]"); + Prog untrustedBoolIndex = (Prog) untrustedEnv.program(boolIndexAst, functions(index)); + assertEstablishedRoot(untrustedBoolIndex, "EvalAttr", "boolean literal index replacement"); + assertThat(untrustedBoolIndex.eval(Map.of()).getVal().booleanValue()).isTrue(); + + Ast mapIndexAst = compile(untrustedEnv, "numbers.map(value, value + target)[0]"); + Prog untrustedMapIndex = (Prog) untrustedEnv.program(mapIndexAst, functions(index)); + assertCurrentPlan(untrustedMapIndex); + + Overload size = Overload.unary(Overloads.SizeList, ignored -> intOf(99L)); + Ast sizeAst = compile(untrustedEnv, "size([x, 2])"); + Prog untrustedSize = (Prog) untrustedEnv.program(sizeAst, functions(size)); + assertEstablishedRoot(untrustedSize, "EvalUnary", "literal global size replacement"); + assertThat(untrustedSize.eval(Map.of("x", 1L)).getVal().intValue()).isEqualTo(99L); + + Ast filterSizeAst = compile(untrustedEnv, "size(numbers.filter(value, value == target))"); + Prog untrustedFilterSize = (Prog) untrustedEnv.program(filterSizeAst, functions(size)); + assertCurrentPlan(untrustedFilterSize); + + Ast mapSizeAst = compile(untrustedEnv, "size(numbers.map(value, value + target))"); + Prog untrustedMapSize = (Prog) untrustedEnv.program(mapSizeAst, functions(size)); + assertCurrentPlan(untrustedMapSize); + + Ast stringSizeAst = compile(untrustedEnv, "size(['cel'])"); + Prog untrustedStringSize = (Prog) untrustedEnv.program(stringSizeAst, functions(size)); + assertEstablishedRoot( + untrustedStringSize, "EvalUnary", "string literal global size replacement"); + assertThat(untrustedStringSize.eval(Map.of()).getVal().intValue()).isEqualTo(99L); + + Ast boolSizeAst = compile(untrustedEnv, "size([true, false])"); + Prog untrustedBoolSize = (Prog) untrustedEnv.program(boolSizeAst, functions(size)); + assertEstablishedRoot( + untrustedBoolSize, "EvalUnary", "boolean literal global size replacement"); + assertThat(untrustedBoolSize.eval(Map.of()).getVal().intValue()).isEqualTo(99L); + + Overload receiverSize = Overload.unary(Overloads.SizeListInst, ignored -> intOf(99L)); + Ast receiverSizeAst = compile(untrustedEnv, "[x, 2].size()"); + Prog untrustedReceiverSize = + (Prog) untrustedEnv.program(receiverSizeAst, functions(receiverSize)); + assertEstablishedRoot(untrustedReceiverSize, "EvalUnary", "literal receiver size replacement"); + assertThat(untrustedReceiverSize.eval(Map.of("x", 1L)).getVal().intValue()).isEqualTo(99L); + + Ast filterReceiverSizeAst = + compile(untrustedEnv, "numbers.filter(value, value == target).size()"); + Prog untrustedFilterReceiverSize = + (Prog) untrustedEnv.program(filterReceiverSizeAst, functions(receiverSize)); + assertCurrentPlan(untrustedFilterReceiverSize); + + Ast mapReceiverSizeAst = compile(untrustedEnv, "numbers.map(value, value + target).size()"); + Prog untrustedMapReceiverSize = + (Prog) untrustedEnv.program(mapReceiverSizeAst, functions(receiverSize)); + assertCurrentPlan(untrustedMapReceiverSize); + + Ast stringReceiverSizeAst = compile(untrustedEnv, "['cel'].size()"); + Prog untrustedStringReceiverSize = + (Prog) untrustedEnv.program(stringReceiverSizeAst, functions(receiverSize)); + assertEstablishedRoot( + untrustedStringReceiverSize, "EvalUnary", "string literal receiver size replacement"); + assertThat(untrustedStringReceiverSize.eval(Map.of()).getVal().intValue()).isEqualTo(99L); + + Ast boolReceiverSizeAst = compile(untrustedEnv, "[true, false].size()"); + Prog untrustedBoolReceiverSize = + (Prog) untrustedEnv.program(boolReceiverSizeAst, functions(receiverSize)); + assertEstablishedRoot( + untrustedBoolReceiverSize, "EvalUnary", "boolean literal receiver size replacement"); + assertThat(untrustedBoolReceiverSize.eval(Map.of()).getVal().intValue()).isEqualTo(99L); + } + + @Test + void customLibraryWithoutNativeDescriptorRemainsOnTheCurrentEvaluator() { + Env customEnv = + newEnv(Library.Lib(new IncrementLibrary()), declarations(Decls.newVar("x", Decls.Int))); + Prog program = (Prog) customEnv.program(compile(customEnv, "increment(x) + 1")); + + assertCurrentPlan(program); + assertThat(program.eval(Map.of("x", 40L)).getVal().intValue()).isEqualTo(42L); + } + + @Test + void preservesPartialActivationUnknowns() { + Ast ast = compile("x + 1"); + Prog nativeProgram = (Prog) env.program(ast); + Prog interpreterProgram = + (Prog) env.program(ast, evalOptions(OptPartialEval, OptDisableNativeEval)); + Object vars = partialVars(Map.of("x", 41L), attributePattern("x")); + + assertEquivalent(nativeProgram.eval(vars).getVal(), interpreterProgram.eval(vars).getVal()); + } + + @Test + void usesDefaultVariablesThroughTheExistingActivationHierarchy() { + Ast ast = compile("x + y"); + Prog nativeProgram = (Prog) env.program(ast, globals(Map.of("x", 40L))); + Prog interpreterProgram = + (Prog) env.program(ast, globals(Map.of("x", 40L)), evalOptions(OptDisableNativeEval)); + + assertIntegratedPlan(nativeProgram, "x + y"); + assertEquivalent( + nativeProgram.eval(Map.of("y", 2L)).getVal(), + interpreterProgram.eval(Map.of("y", 2L)).getVal()); + } + + @Test + void resolvesEachIdentifierAtTheSamePoint() { + Programs programs = programs("x + x * x"); + AtomicInteger nativeResolutions = new AtomicInteger(); + AtomicInteger interpreterResolutions = new AtomicInteger(); + + Val nativeResult = programs.nativeProgram.eval(countingActivation(nativeResolutions)).getVal(); + Val interpreterResult = + programs.interpreterProgram.eval(countingActivation(interpreterResolutions)).getVal(); + + assertEquivalent(nativeResult, interpreterResult); + assertThat(nativeResolutions).hasValue(interpreterResolutions.get()).hasValue(3); + } + + @Test + void fallsBackWhenAnyUnsupportedProgramFeatureOrExpressionIsPresent() { + Ast supported = compile("x + 1"); + Prog decorated = (Prog) env.program(supported, customDecorator(interpretable -> interpretable)); + Prog nativeDisabled = (Prog) env.program(supported, evalOptions(OptDisableNativeEval)); + Prog optimized = (Prog) env.program(supported, evalOptions(OptOptimize)); + Prog unsupported = (Prog) env.program(compile("[x, y]")); + Prog unchecked = (Prog) env.program(env.parse("x + 1").getAst()); + + assertCurrentPlan(decorated); + assertCurrentPlan(nativeDisabled); + assertIntegratedPlan(optimized, "x + 1 with OptOptimize"); + assertCurrentPlan(unsupported); + assertCurrentPlan(unchecked); + } + + @Test + void immutablePlanCanBeEvaluatedConcurrently() throws Exception { + Prog program = (Prog) env.program(compile("(x + 1) * (y - 2)")); + assertIntegratedPlan(program, "(x + 1) * (y - 2)"); + ExecutorService executor = Executors.newFixedThreadPool(4); + try { + List> results = new ArrayList<>(); + for (int i = 0; i < 200; i++) { + long value = i; + results.add( + executor.submit( + () -> program.eval(Map.of("x", value, "y", value + 4L)).getVal().intValue())); + } + for (int i = 0; i < results.size(); i++) { + assertThat(results.get(i).get(5, SECONDS)).isEqualTo((long) (i + 1) * (i + 2)); + } + } finally { + executor.shutdownNow(); + assertThat(executor.awaitTermination(5, SECONDS)).isTrue(); + } + } + + private Programs programs(String expression) { + return programs(env, expression); + } + + private static Programs programs(Env env, String expression) { + Ast ast = compile(env, expression); + Prog nativeProgram = (Prog) env.program(ast); + Prog interpreterProgram = (Prog) env.program(ast, evalOptions(OptDisableNativeEval)); + Interpretable nativePlan = assertIntegratedPlan(nativeProgram, expression); + assertThat(nativePlan.id()).as(expression).isEqualTo(ast.getExpr().getId()); + assertCurrentPlan(interpreterProgram, expression); + assertThat(nativeProgram.cost()).as(expression).isEqualTo(interpreterProgram.cost()); + return new Programs(nativeProgram, interpreterProgram); + } + + private void assertExpression(String expression, Object input) { + Programs programs = programs(expression); + assertEquivalent( + programs.nativeProgram.eval(input).getVal(), + programs.interpreterProgram.eval(input).getVal()); + assertThat(programs.nativeProgram.cost()).isEqualTo(programs.interpreterProgram.cost()); + } + + private static Interpretable undecoratedCurrent(Prog nativeProgram, Ast ast) { + return ((Prog) nativeProgram.e.program(ast, evalOptions(OptDisableNativeEval))).interpretable; + } + + private static Interpretable assertIntegratedPlan(Prog program, String description) { + assertThat(program.interpretable.getClass().getSimpleName()) + .as(description) + .isEqualTo("NativeIsland"); + return program.interpretable; + } + + private static void assertCurrentPlan(Prog program) { + assertThat(program.interpretable).isNotNull(); + assertThat(program.interpretable.getClass().getSimpleName()).isNotEqualTo("NativeIsland"); + } + + private static void assertCurrentPlan(Prog program, String description) { + assertThat(program.interpretable).as(description).isNotNull(); + assertThat(program.interpretable.getClass().getSimpleName()) + .as(description) + .isNotEqualTo("NativeIsland"); + } + + private static void assertEstablishedRoot( + Prog program, String expectedSimpleName, String description) { + assertThat(program.interpretable).as(description).isNotNull(); + assertThat(program.interpretable.getClass().getSimpleName()) + .as(description) + .isEqualTo(expectedSimpleName); + } + + private static void assertExpression(Env env, String expression, Object input) { + Programs programs = programs(env, expression); + assertEquivalent( + programs.nativeProgram.eval(input).getVal(), + programs.interpreterProgram.eval(input).getVal()); + assertThat(programs.nativeProgram.cost()).isEqualTo(programs.interpreterProgram.cost()); + } + + private void assertResolutionCounts( + String expression, Map values, Map expectedCounts) { + Programs programs = programs(expression); + Map nativeCounts = new java.util.HashMap<>(); + Map interpreterCounts = new java.util.HashMap<>(); + + Val nativeResult = + programs.nativeProgram.eval(countingActivation(values, nativeCounts)).getVal(); + Val interpreterResult = + programs.interpreterProgram.eval(countingActivation(values, interpreterCounts)).getVal(); + + assertEquivalent(nativeResult, interpreterResult); + for (Map.Entry expected : expectedCounts.entrySet()) { + assertThat(count(nativeCounts, expected.getKey())).isEqualTo(expected.getValue()); + assertThat(count(interpreterCounts, expected.getKey())).isEqualTo(expected.getValue()); + } + } + + private static int count(Map counts, String name) { + AtomicInteger count = counts.get(name); + return count != null ? count.get() : 0; + } + + private static ActivationFunction countingActivation( + Map values, Map counts) { + return name -> { + counts.computeIfAbsent(name, ignored -> new AtomicInteger()).incrementAndGet(); + return values.getOrDefault(name, ActivationFunction.ABSENT); + }; + } + + private static ActivationFunction activation(Map values) { + return name -> values.getOrDefault(name, ActivationFunction.ABSENT); + } + + private static ActivationFunction orderedActivation( + Map values, List order) { + return name -> { + order.add(name); + return values.getOrDefault(name, ActivationFunction.ABSENT); + }; + } + + private static ActivationFunction orderedMembershipActivation(List order) { + return name -> { + order.add(name); + return switch (name) { + case "wordTarget" -> newErr("needle"); + case "words" -> newErr("list"); + default -> ActivationFunction.ABSENT; + }; + }; + } + + @SuppressWarnings("SameParameterValue") + private static ActivationFunction orderedMappedStringMembershipActivation( + List order, Object needle, Object words) { + return name -> { + order.add(name); + return switch (name) { + case "wordTarget" -> needle; + case "words" -> words; + case "s" -> "!"; + default -> ActivationFunction.ABSENT; + }; + }; + } + + @SuppressWarnings("SameParameterValue") + private static ActivationFunction orderedBoolListActivation( + List order, Object b, Object c) { + return name -> { + order.add(name); + return switch (name) { + case "b" -> b; + case "c" -> c; + case "x" -> 1L; + default -> ActivationFunction.ABSENT; + }; + }; + } + + private static ActivationFunction orderedIntListActivation(List order, boolean error) { + return name -> { + order.add(name); + return switch (name) { + case "x" -> error ? newErr("first") : 1L; + case "y" -> 2L; + default -> ActivationFunction.ABSENT; + }; + }; + } + + @SuppressWarnings("SameParameterValue") + private static ActivationFunction orderedDoubleListActivation( + List order, Object d, Object e) { + return name -> { + order.add(name); + return switch (name) { + case "d" -> d; + case "e" -> e; + default -> ActivationFunction.ABSENT; + }; + }; + } + + private static ActivationFunction orderedStringListLiteralMembershipActivation( + List order, Object needle, Object first, Object second) { + return name -> { + order.add(name); + return switch (name) { + case "wordTarget" -> needle; + case "s" -> first; + case "key" -> second; + default -> ActivationFunction.ABSENT; + }; + }; + } + + private Ast compile(String expression) { + return compile(env, expression); + } + + private static Ast compile(Env env, String expression) { + AstIssuesTuple result = env.compile(expression); + assertThat(result.hasIssues()).as(expression).isFalse(); + return result.getAst(); + } + + private static ActivationFunction countingActivation(AtomicInteger counter) { + return name -> { + counter.incrementAndGet(); + return name.equals("x") ? 3L : ActivationFunction.ABSENT; + }; + } + + private static void assertEquivalent(Val actual, Val expected) { + assertThat(actual.getClass()).isEqualTo(expected.getClass()); + assertThat(actual.type()).isSameAs(expected.type()); + assertThat(actual.toString()).isEqualTo(expected.toString()); + if (actual instanceof Err actualError && expected instanceof Err expectedError) { + assertThat(actualError.hasCause()).isEqualTo(expectedError.hasCause()); + if (actualError.hasCause()) { + assertThat(actualError.getCause().getClass()) + .isEqualTo(expectedError.getCause().getClass()); + assertThat(actualError.getCause().getMessage()) + .isEqualTo(expectedError.getCause().getMessage()); + } + return; + } + if (actual.value() instanceof Double actualDouble + && expected.value() instanceof Double expectedDouble) { + assertThat(Double.doubleToRawLongBits(actualDouble)) + .isEqualTo(Double.doubleToRawLongBits(expectedDouble)); + } else { + assertThat(actual.value()).isEqualTo(expected.value()); + } + } + + private record Evaluation(String expression, Object input) { + @Override + @NonNull + public String toString() { + return expression; + } + } + + private static final class IncrementLibrary implements Library { + private static final String FUNCTION = "increment"; + private static final String OVERLOAD = "increment_int"; + + private final Overload implementation; + + private IncrementLibrary() { + this.implementation = Overload.unary(OVERLOAD, IncrementLibrary::increment); + } + + @Override + public List getCompileOptions() { + return List.of( + declarations( + Decls.newFunction( + FUNCTION, Decls.newOverload(OVERLOAD, List.of(Decls.Int), Decls.Int)))); + } + + @Override + public List getProgramOptions() { + return List.of(functions(implementation)); + } + + private static Val increment(Val value) { + return value instanceof IntT + ? intOf(value.intValue() + 1L) + : Err.maybeNoSuchOverloadErr(value); + } + } + + private record Programs(Prog nativeProgram, Prog interpreterProgram) {} +} diff --git a/core/src/test/java/org/projectnessie/cel/interpreter/BuiltInOptimizerTest.java b/core/src/test/java/org/projectnessie/cel/interpreter/BuiltInOptimizerTest.java new file mode 100644 index 000000000..877e60b38 --- /dev/null +++ b/core/src/test/java/org/projectnessie/cel/interpreter/BuiltInOptimizerTest.java @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2026 The Authors of CEL-Java + * + * 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 + * + * http://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 org.projectnessie.cel.interpreter; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.projectnessie.cel.common.types.IntT.intOf; +import static org.projectnessie.cel.common.types.StringT.stringOf; +import static org.projectnessie.cel.interpreter.Activation.emptyActivation; +import static org.projectnessie.cel.interpreter.Interpretable.newConstValue; + +import java.util.concurrent.atomic.AtomicInteger; +import org.junit.jupiter.api.Test; +import org.projectnessie.cel.common.types.Overloads; +import org.projectnessie.cel.common.types.pb.DefaultTypeAdapter; +import org.projectnessie.cel.common.types.ref.Val; +import org.projectnessie.cel.common.types.traits.Lister; +import org.projectnessie.cel.common.types.traits.Mapper; +import org.projectnessie.cel.interpreter.Interpretable.InterpretableConst; + +class BuiltInOptimizerTest { + + @Test + void publicFactoryReturnsThePackagePrivateSingleton() { + assertThat(Interpreter.optimize()).isSameAs(BuiltInOptimizer.INSTANCE); + assertThat(InterpretableDecorator.decOptimize()).isSameAs(BuiltInOptimizer.INSTANCE); + } + + @Test + void dispatchFoldsListsMapsAndConversions() { + Interpretable one = newConstValue(1, intOf(1)); + Interpretable two = newConstValue(2, intOf(2)); + + Interpretable list = + BuiltInOptimizer.INSTANCE.decorate( + new EvalList( + 3, new Interpretable[] {one, two}, new boolean[2], DefaultTypeAdapter.Instance)); + assertThat(list).isInstanceOf(InterpretableConst.class); + Lister listValue = (Lister) list.eval(emptyActivation()); + assertThat(listValue.nativeSize()).isEqualTo(2); + assertThat(listValue.nativeGetAt(0)).isEqualTo(intOf(1)); + assertThat(listValue.nativeGetAt(1)).isEqualTo(intOf(2)); + + Interpretable map = + BuiltInOptimizer.INSTANCE.decorate( + new EvalMap( + 4, + new Interpretable[] {newConstValue(5, stringOf("key"))}, + new Interpretable[] {one}, + new boolean[1], + DefaultTypeAdapter.Instance)); + assertThat(map).isInstanceOf(InterpretableConst.class); + assertThat(((Mapper) map.eval(emptyActivation())).get(stringOf("key"))).isEqualTo(intOf(1)); + + AtomicInteger evaluations = new AtomicInteger(); + Interpretable conversion = + BuiltInOptimizer.INSTANCE.decorate( + new EvalUnary( + 6, + "int", + Overloads.StringToInt, + newConstValue(7, stringOf("ignored")), + null, + ignored -> { + evaluations.incrementAndGet(); + return intOf(42); + })); + assertThat(conversion).isInstanceOf(InterpretableConst.class); + assertThat(conversion.eval(emptyActivation())).isEqualTo(intOf(42)); + assertThat(conversion.eval(emptyActivation())).isEqualTo(intOf(42)); + assertThat(evaluations).hasValue(1); + } + + @Test + void nonConstantInputsKeepTheirOriginalNodes() { + Interpretable dynamic = + new AbstractEval(1) { + @Override + public Val eval(Activation activation) { + return intOf(1); + } + }; + EvalList list = + new EvalList(2, new Interpretable[] {dynamic}, new boolean[1], DefaultTypeAdapter.Instance); + EvalMap map = + new EvalMap( + 3, + new Interpretable[] {newConstValue(4, stringOf("key"))}, + new Interpretable[] {dynamic}, + new boolean[1], + DefaultTypeAdapter.Instance); + + assertThat(BuiltInOptimizer.INSTANCE.decorate(list)).isSameAs(list); + assertThat(BuiltInOptimizer.INSTANCE.decorate(map)).isSameAs(map); + } +} diff --git a/core/src/test/java/org/projectnessie/cel/interpreter/ConstantSetMembershipSupportTest.java b/core/src/test/java/org/projectnessie/cel/interpreter/ConstantSetMembershipSupportTest.java new file mode 100644 index 000000000..05412022a --- /dev/null +++ b/core/src/test/java/org/projectnessie/cel/interpreter/ConstantSetMembershipSupportTest.java @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2026 The Authors of CEL-Java + * + * 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 + * + * http://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 org.projectnessie.cel.interpreter; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.projectnessie.cel.common.types.BoolT.False; +import static org.projectnessie.cel.common.types.BoolT.True; +import static org.projectnessie.cel.common.types.DoubleT.doubleOf; +import static org.projectnessie.cel.common.types.Err.newErr; +import static org.projectnessie.cel.common.types.StringT.stringOf; +import static org.projectnessie.cel.common.types.UintT.uintOf; +import static org.projectnessie.cel.common.types.UnknownT.unknownOf; + +import java.util.Set; +import org.junit.jupiter.api.Test; +import org.projectnessie.cel.common.types.DoubleT; +import org.projectnessie.cel.common.types.UintT; +import org.projectnessie.cel.common.types.ref.Val; + +class ConstantSetMembershipSupportTest { + + @Test + void preservesErrorUnknownEmptyAndTypeCheckOrder() { + Val error = newErr("carried"); + Val unknown = unknownOf(7L); + Set values = Set.of(stringOf("value")); + + assertThat(evaluate(error, "string", values)).isSameAs(error); + assertThat(evaluate(unknown, "string", values)).isSameAs(unknown); + assertThat(evaluate(uintOf(1), null, Set.of())).isSameAs(False); + assertThat(evaluate(uintOf(1), "string", values).type().typeName()).isEqualTo("error"); + } + + @Test + void delegatesExactEqualityAndHashingToTheStoredValSet() { + DoubleT nan = doubleOf(Double.NaN); + Set nanSet = Set.of(nan); + assertThat(evaluate(nan, "double", nanSet)).isSameAs(True); + assertThat(evaluate(doubleOf(Double.NaN), "double", nanSet)).isSameAs(False); + + for (double stored : new double[] {-0.0d, 0.0d}) { + Set zeroSet = Set.of(doubleOf(stored)); + for (double needle : new double[] {-0.0d, 0.0d}) { + Val needleValue = doubleOf(needle); + assertThat(evaluate(needleValue, "double", zeroSet).booleanValue()) + .isEqualTo(zeroSet.contains(needleValue)); + } + } + + UintT highBit = uintOf(Long.MIN_VALUE); + assertThat(evaluate(highBit, "uint", Set.of(highBit))).isSameAs(True); + } + + private static Val evaluate(Val needle, String typeName, Set values) { + return ConstantSetMembershipSupport.evaluate(needle, typeName, values); + } +} diff --git a/core/src/test/java/org/projectnessie/cel/interpreter/PlanningPolicyTest.java b/core/src/test/java/org/projectnessie/cel/interpreter/PlanningPolicyTest.java new file mode 100644 index 000000000..24906e4d8 --- /dev/null +++ b/core/src/test/java/org/projectnessie/cel/interpreter/PlanningPolicyTest.java @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2026 The Authors of CEL-Java + * + * 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 + * + * http://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 org.projectnessie.cel.interpreter; + +import static java.lang.reflect.Modifier.isPublic; +import static java.util.Collections.emptyMap; +import static org.assertj.core.api.Assertions.assertThat; +import static org.projectnessie.cel.common.containers.Container.defaultContainer; +import static org.projectnessie.cel.common.types.pb.ProtoTypeRegistry.newEmptyRegistry; +import static org.projectnessie.cel.interpreter.Activation.emptyActivation; +import static org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory; +import static org.projectnessie.cel.interpreter.Dispatcher.newDispatcher; +import static org.projectnessie.cel.interpreter.InterpretablePlanner.newPlanner; +import static org.projectnessie.cel.interpreter.InterpretablePlanner.newUncheckedPlanner; +import static org.projectnessie.cel.interpreter.Interpreter.newInterpreter; +import static org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter; + +import com.google.api.expr.v1alpha1.CheckedExpr; +import com.google.api.expr.v1alpha1.Constant; +import com.google.api.expr.v1alpha1.Expr; +import java.util.Map; +import org.junit.jupiter.api.Test; +import org.projectnessie.cel.checker.Decls; +import org.projectnessie.cel.common.types.pb.ProtoTypeRegistry; + +class PlanningPolicyTest { + private final ProtoTypeRegistry registry = newEmptyRegistry(); + private final Dispatcher dispatcher = newDispatcher(); + private final AttributeFactory attributes = + newAttributeFactory(defaultContainer, registry, registry); + private final Expr expression = + Expr.newBuilder().setId(1L).setConstExpr(Constant.newBuilder().setInt64Value(42L)).build(); + private final CheckedExpr checked = + CheckedExpr.newBuilder().setExpr(expression).putTypeMap(1L, Decls.Int).build(); + + @Test + void interpreterFactoriesApplyPermissionOnlyToUndecoratedCheckedPlans() { + ExprInterpreter oldFactory = + interpreter(newInterpreter(dispatcher, defaultContainer, registry, registry, attributes)); + ExprInterpreter disabled = + interpreter( + newInterpreter(dispatcher, defaultContainer, registry, registry, attributes, false)); + ExprInterpreter enabled = + interpreter( + newInterpreter(dispatcher, defaultContainer, registry, registry, attributes, true)); + ExprInterpreter standard = + interpreter(newStandardInterpreter(defaultContainer, registry, registry, attributes)); + + assertEstablished(oldFactory.checkedPlanner(checked)); + assertEstablished(disabled.checkedPlanner(checked)); + assertPermitted(enabled.checkedPlanner(checked)); + assertPermitted(enabled.checkedPlanner(emptyMap(), emptyMap())); + assertOptimized(enabled.checkedPlanner(checked, Interpreter.optimize())); + assertEstablished(disabled.checkedPlanner(checked, Interpreter.optimize())); + InterpretableDecorator wrapped = node -> Interpreter.optimize().decorate(node); + assertEstablished(enabled.checkedPlanner(checked, wrapped)); + assertEstablished( + enabled.checkedPlanner(checked, Interpreter.optimize(), Interpreter.optimize())); + assertEstablished(enabled.checkedPlanner(checked, plan -> plan)); + assertEstablished(enabled.checkedPlanner(emptyMap(), emptyMap(), plan -> plan)); + assertEstablished(enabled.uncheckedPlanner()); + assertEstablished(enabled.uncheckedPlanner(plan -> plan)); + assertEstablished(standard.checkedPlanner(checked)); + + assertThat(enabled.newInterpretable(checked).eval(emptyActivation()).intValue()).isEqualTo(42L); + Interpretable optimizedConstant = enabled.newInterpretable(checked, Interpreter.optimize()); + assertThat(optimizedConstant).isInstanceOf(NativeIntConst.class); + assertThat(optimizedConstant).isInstanceOf(Interpretable.InterpretableConst.class); + assertThat(enabled.newUncheckedInterpretable(expression).eval(emptyActivation()).intValue()) + .isEqualTo(42L); + } + + @Test + void publicPlannerFactoriesRemainEstablishedOnly() { + Planner checkedWrapper = + planner(newPlanner(dispatcher, registry, registry, attributes, defaultContainer, checked)); + Planner checkedMaps = + planner( + newPlanner( + dispatcher, registry, registry, attributes, defaultContainer, Map.of(), Map.of())); + Planner unchecked = + planner(newUncheckedPlanner(dispatcher, registry, registry, attributes, defaultContainer)); + + assertEstablished(checkedWrapper); + assertEstablished(checkedMaps); + assertEstablished(unchecked); + assertThat(checkedWrapper.plan(expression).eval(emptyActivation()).intValue()).isEqualTo(42L); + } + + @Test + void extractedImplementationsArePackagePrivate() { + assertThat(isPublic(ExprInterpreter.class.getModifiers())).isFalse(); + assertThat(isPublic(Planner.class.getModifiers())).isFalse(); + assertThat(isPublic(PlanningPolicy.class.getModifiers())).isFalse(); + } + + private static ExprInterpreter interpreter(Interpreter interpreter) { + assertThat(interpreter).isInstanceOf(ExprInterpreter.class); + return (ExprInterpreter) interpreter; + } + + private static Planner planner(InterpretablePlanner planner) { + assertThat(planner).isInstanceOf(Planner.class); + return (Planner) planner; + } + + private static void assertEstablished(Planner planner) { + assertThat(planner.policy()).isSameAs(PlanningPolicy.ESTABLISHED_ONLY); + } + + private static void assertPermitted(Planner planner) { + assertThat(planner.policy()).isSameAs(PlanningPolicy.NATIVE_SPECIALIZATION_PERMITTED); + } + + private static void assertOptimized(Planner planner) { + assertThat(planner.policy()).isSameAs(PlanningPolicy.NATIVE_OPTIMIZED); + } +} diff --git a/core/src/test/java/org/projectnessie/cel/interpreter/SetMembershipOptimizationTest.java b/core/src/test/java/org/projectnessie/cel/interpreter/SetMembershipOptimizationTest.java index e24aa5f2a..74cad898c 100644 --- a/core/src/test/java/org/projectnessie/cel/interpreter/SetMembershipOptimizationTest.java +++ b/core/src/test/java/org/projectnessie/cel/interpreter/SetMembershipOptimizationTest.java @@ -19,16 +19,20 @@ import static org.projectnessie.cel.common.types.BoolT.False; import static org.projectnessie.cel.common.types.Err.newErr; import static org.projectnessie.cel.common.types.IntT.intOf; +import static org.projectnessie.cel.common.types.IteratorT.javaIterator; import static org.projectnessie.cel.common.types.ListT.newValArrayList; import static org.projectnessie.cel.common.types.UnknownT.unknownOf; import static org.projectnessie.cel.interpreter.Activation.emptyActivation; import static org.projectnessie.cel.interpreter.Interpretable.newConstValue; +import java.lang.reflect.Proxy; +import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import org.junit.jupiter.api.Test; import org.projectnessie.cel.common.types.Overloads; import org.projectnessie.cel.common.types.pb.DefaultTypeAdapter; import org.projectnessie.cel.common.types.ref.Val; +import org.projectnessie.cel.common.types.traits.Lister; import org.projectnessie.cel.interpreter.Interpretable.InterpretableCall; class SetMembershipOptimizationTest { @@ -54,9 +58,32 @@ void optimizedMembershipPropagatesErrorAndUnknown() { .isSameAs(unknown); } + @Test + void inconsistentListSizeAndIteratorPreserveOriginalEvaluation() { + Lister inconsistentList = + (Lister) + Proxy.newProxyInstance( + Lister.class.getClassLoader(), + new Class[] {Lister.class}, + (proxy, method, args) -> { + if (method.getName().equals("size")) { + return intOf(1); + } + if (method.getName().equals("iterator")) { + return javaIterator(DefaultTypeAdapter.Instance, List.of().iterator()); + } + throw new UnsupportedOperationException(method.getName()); + }); + Interpretable lhs = newConstValue(1, intOf(1)); + Interpretable rhs = newConstValue(2, inconsistentList); + MembershipCall original = new MembershipCall(lhs, rhs); + + assertThat(BuiltInOptimizer.maybeOptimizeSetMembership(original, original)).isSameAs(original); + } + private static Interpretable optimize(Interpretable lhs, Val[] values) { Interpretable rhs = newConstValue(2, newValArrayList(DefaultTypeAdapter.Instance, values)); - return InterpretableDecorator.maybeOptimizeSetMembership( + return BuiltInOptimizer.maybeOptimizeSetMembership( new MembershipCall(lhs, rhs), new MembershipCall(lhs, rhs)); } diff --git a/core/src/test/java/org/projectnessie/cel/interpreter/StandardNativeOverloadCatalogTest.java b/core/src/test/java/org/projectnessie/cel/interpreter/StandardNativeOverloadCatalogTest.java new file mode 100644 index 000000000..438115ce0 --- /dev/null +++ b/core/src/test/java/org/projectnessie/cel/interpreter/StandardNativeOverloadCatalogTest.java @@ -0,0 +1,166 @@ +/* + * Copyright (C) 2026 The Authors of CEL-Java + * + * 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 + * + * http://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 org.projectnessie.cel.interpreter; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.projectnessie.cel.common.containers.Container.defaultContainer; +import static org.projectnessie.cel.common.types.IntT.intOf; +import static org.projectnessie.cel.common.types.Overloads.AddDouble; +import static org.projectnessie.cel.common.types.Overloads.AddInt64; +import static org.projectnessie.cel.common.types.Overloads.AddList; +import static org.projectnessie.cel.common.types.Overloads.AddString; +import static org.projectnessie.cel.common.types.Overloads.InList; +import static org.projectnessie.cel.common.types.Overloads.InMap; +import static org.projectnessie.cel.common.types.Overloads.IndexList; +import static org.projectnessie.cel.common.types.Overloads.IndexMap; +import static org.projectnessie.cel.common.types.Overloads.SizeList; +import static org.projectnessie.cel.common.types.Overloads.SizeListInst; +import static org.projectnessie.cel.common.types.Overloads.SizeMap; +import static org.projectnessie.cel.common.types.Overloads.SizeMapInst; +import static org.projectnessie.cel.common.types.pb.ProtoTypeRegistry.newEmptyRegistry; +import static org.projectnessie.cel.interpreter.Activation.emptyActivation; +import static org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory; +import static org.projectnessie.cel.interpreter.Dispatcher.newDispatcher; + +import com.google.api.expr.v1alpha1.Constant; +import com.google.api.expr.v1alpha1.Expr; +import com.google.api.expr.v1alpha1.Reference; +import java.util.Arrays; +import java.util.Map; +import org.junit.jupiter.api.Test; +import org.projectnessie.cel.common.operators.Operator; +import org.projectnessie.cel.common.types.pb.ProtoTypeRegistry; +import org.projectnessie.cel.interpreter.Planner.ResolvedFunction; +import org.projectnessie.cel.interpreter.functions.Overload; + +class StandardNativeOverloadCatalogTest { + private static final long CALL_ID = 100L; + + @Test + void exactImplementationCanOwnMultipleCheckedDescriptors() { + Overload add = standardImplementation(Operator.Add.id); + + assertThat(StandardNativeOverloadCatalog.descriptors(add)) + .extracting(NativeOverloadDescriptor::overloadId) + .containsExactly(AddInt64, AddDouble, AddString, AddList); + assertThat(StandardNativeOverloadCatalog.find(add, Operator.Add.id, AddInt64)).isNotNull(); + assertThat(StandardNativeOverloadCatalog.find(add, Operator.Add.id, AddDouble)).isNotNull(); + assertThat(StandardNativeOverloadCatalog.find(add, "wrong", AddInt64)).isNull(); + assertThat(StandardNativeOverloadCatalog.find(add, Operator.Add.id, "wrong")).isNull(); + } + + @Test + void aggregateDescriptorsRetainExactFunctionAndImplementationProvenance() { + Overload index = standardImplementation(Operator.Index.id); + Overload in = standardImplementation(Operator.In.id); + Overload size = standardImplementation(org.projectnessie.cel.common.types.Overloads.Size); + + assertThat(StandardNativeOverloadCatalog.descriptors(index)) + .extracting(NativeOverloadDescriptor::overloadId) + .containsExactly(IndexList, IndexMap); + assertThat(StandardNativeOverloadCatalog.descriptors(in)) + .extracting(NativeOverloadDescriptor::overloadId) + .containsExactly(InList, InMap); + assertThat(StandardNativeOverloadCatalog.descriptors(size)) + .extracting(NativeOverloadDescriptor::overloadId) + .containsExactly(SizeList, SizeListInst, SizeMap, SizeMapInst); + + Overload replacement = Overload.binary(Operator.Index, (left, right) -> intOf(99L)); + assertThat(StandardNativeOverloadCatalog.find(replacement, Operator.Index.id, IndexList)) + .isNull(); + assertThat(StandardNativeOverloadCatalog.find(index, Operator.In.id, IndexList)).isNull(); + } + + @Test + void sameNameReplacementIsNotStandardNativeProvenance() { + Overload replacement = Overload.binary(Operator.Add, (left, right) -> intOf(99L)); + + assertThat(StandardNativeOverloadCatalog.descriptors(replacement)).isEmpty(); + assertThat(StandardNativeOverloadCatalog.find(replacement, Operator.Add.id, AddInt64)).isNull(); + } + + @Test + void resolvedCallCarriesExactImplementationAndCheckedDescriptorOnlyWhenPermitted() { + Overload add = standardImplementation(Operator.Add.id); + Dispatcher dispatcher = newDispatcher(); + dispatcher.add(Overload.standardOverloads()); + + ResolvedFunction permitted = planner(dispatcher, true).resolveFunction(addExpression()); + assertThat(permitted.implementation).isSameAs(add); + assertThat(permitted.fnName).isEqualTo(Operator.Add.id); + assertThat(permitted.overloadId).isEqualTo(AddInt64); + assertThat(permitted.nativeDescriptor()) + .isSameAs(StandardNativeOverloadCatalog.find(add, Operator.Add.id, AddInt64)); + + Planner established = planner(dispatcher, false); + ResolvedFunction disabled = established.resolveFunction(addExpression()); + assertThat(disabled.implementation).isSameAs(add); + assertThat(disabled.nativeDescriptor()).isNull(); + assertThat(established.plan(addExpression()).eval(emptyActivation())).isEqualTo(intOf(3L)); + } + + @Test + void replacementRemainsTheEstablishedImplementationButIsNotNativeEligible() { + Overload standard = standardImplementation(Operator.Add.id); + Overload replacement = Overload.binary(AddInt64, (left, right) -> intOf(99L)); + Dispatcher dispatcher = newDispatcher(); + dispatcher.add(standard, replacement); + Planner planner = planner(dispatcher, true); + + ResolvedFunction resolved = planner.resolveFunction(addExpression()); + assertThat(resolved.implementation).isSameAs(replacement); + assertThat(resolved.nativeDescriptor()).isNull(); + assertThat(planner.plan(addExpression()).eval(emptyActivation())).isEqualTo(intOf(99L)); + } + + private static Planner planner(Dispatcher dispatcher, boolean nativePlanningPermitted) { + ProtoTypeRegistry registry = newEmptyRegistry(); + return new Planner( + dispatcher, + registry, + registry, + newAttributeFactory(defaultContainer, registry, registry), + defaultContainer, + Map.of(CALL_ID, Reference.newBuilder().addOverloadId(AddInt64).build()), + Map.of(), + PlanningPolicy.nativeSpecialization(nativePlanningPermitted), + new InterpretableDecorator[0]); + } + + private static Expr addExpression() { + return Expr.newBuilder() + .setId(CALL_ID) + .setCallExpr( + Expr.Call.newBuilder() + .setFunction(Operator.Add.id) + .addArgs( + Expr.newBuilder() + .setId(CALL_ID + 1) + .setConstExpr(Constant.newBuilder().setInt64Value(1L))) + .addArgs( + Expr.newBuilder() + .setId(CALL_ID + 2) + .setConstExpr(Constant.newBuilder().setInt64Value(2L)))) + .build(); + } + + private static Overload standardImplementation(String function) { + return Arrays.stream(Overload.standardOverloads()) + .filter(overload -> overload.operator.equals(function)) + .findFirst() + .orElseThrow(); + } +} diff --git a/core/src/test/java/org/projectnessie/cel/interpreter/functions/OverloadTest.java b/core/src/test/java/org/projectnessie/cel/interpreter/functions/OverloadTest.java new file mode 100644 index 000000000..6c4f88410 --- /dev/null +++ b/core/src/test/java/org/projectnessie/cel/interpreter/functions/OverloadTest.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2026 The Authors of CEL-Java + * + * 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 + * + * http://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 org.projectnessie.cel.interpreter.functions; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Arrays; +import org.junit.jupiter.api.Test; + +class OverloadTest { + @Test + void standardOverloadsHaveStableEntriesInCallerOwnedArrays() { + Overload[] first = Overload.standardOverloads(); + Overload[] second = Overload.standardOverloads(); + + assertThat(first).isNotSameAs(second).isNotEmpty().hasSameSizeAs(second); + for (int i = 0; i < first.length; i++) { + assertThat(first[i]).isSameAs(second[i]); + } + + Arrays.fill(first, null); + Overload[] third = Overload.standardOverloads(); + assertThat(third).doesNotContainNull(); + for (int i = 0; i < third.length; i++) { + assertThat(third[i]).isSameAs(second[i]); + } + } +} diff --git a/core/src/test/java/org/projectnessie/cel/publicapi/InterpreterPublicApiCompileTest.java b/core/src/test/java/org/projectnessie/cel/publicapi/InterpreterPublicApiCompileTest.java index bf2c9f202..b07d8fb71 100644 --- a/core/src/test/java/org/projectnessie/cel/publicapi/InterpreterPublicApiCompileTest.java +++ b/core/src/test/java/org/projectnessie/cel/publicapi/InterpreterPublicApiCompileTest.java @@ -73,6 +73,8 @@ void retainedContractsAndFactoriesRemainUsableOutsideInterpreterPackage() { Interpreter.newStandardInterpreter(container, registry, registry, attributes); Interpreter configured = Interpreter.newInterpreter(extended, container, registry, registry, attributes); + Interpreter configuredNative = + Interpreter.newInterpreter(extended, container, registry, registry, attributes, true); InterpretableDecorator optimizer = Interpreter.optimize(); InterpretablePlanner checked = InterpretablePlanner.newPlanner( @@ -89,6 +91,7 @@ void retainedContractsAndFactoriesRemainUsableOutsideInterpreterPackage() { assertThat(constant.value()).isEqualTo(intOf(1L)); assertThat(standard).isNotNull(); assertThat(configured).isNotNull(); + assertThat(configuredNative).isNotNull(); assertThat(optimizer).isNotNull(); assertThat(checked).isNotNull(); assertThat(checkedMaps).isNotNull(); diff --git a/jackson/src/main/java/org/projectnessie/cel/types/jackson/JacksonRegistry.java b/jackson/src/main/java/org/projectnessie/cel/types/jackson/JacksonRegistry.java index c276f48a7..bcee17911 100644 --- a/jackson/src/main/java/org/projectnessie/cel/types/jackson/JacksonRegistry.java +++ b/jackson/src/main/java/org/projectnessie/cel/types/jackson/JacksonRegistry.java @@ -26,6 +26,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.projectnessie.cel.common.types.ref.FieldType; +import org.projectnessie.cel.common.types.ref.StandardScalarTypeAdapter; import org.projectnessie.cel.common.types.ref.Type; import org.projectnessie.cel.common.types.ref.TypeAdapterSupport; import org.projectnessie.cel.common.types.ref.TypeRegistry; @@ -38,7 +39,7 @@ * therefore returning Jackson objects from CEL expressions is not possible/implemented and results * in {@link UnsupportedOperationException}s. */ -public final class JacksonRegistry implements TypeRegistry { +public final class JacksonRegistry implements TypeRegistry, StandardScalarTypeAdapter { final ObjectMapper objectMapper; private final SerializerProvider serializationProvider; private final TypeFactory typeFactory; diff --git a/jackson3/src/main/java/org/projectnessie/cel/types/jackson3/Jackson3Registry.java b/jackson3/src/main/java/org/projectnessie/cel/types/jackson3/Jackson3Registry.java index 6faa36006..2707a6c01 100644 --- a/jackson3/src/main/java/org/projectnessie/cel/types/jackson3/Jackson3Registry.java +++ b/jackson3/src/main/java/org/projectnessie/cel/types/jackson3/Jackson3Registry.java @@ -20,6 +20,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.projectnessie.cel.common.types.ref.FieldType; +import org.projectnessie.cel.common.types.ref.StandardScalarTypeAdapter; import org.projectnessie.cel.common.types.ref.Type; import org.projectnessie.cel.common.types.ref.TypeAdapterSupport; import org.projectnessie.cel.common.types.ref.TypeRegistry; @@ -40,7 +41,7 @@ * therefore returning Jackson objects from CEL expressions is not possible/implemented and results * in {@link UnsupportedOperationException}s. */ -public final class Jackson3Registry implements TypeRegistry { +public final class Jackson3Registry implements TypeRegistry, StandardScalarTypeAdapter { final ObjectMapper objectMapper; private final SerializationContextExt serializationContextExt; private final TypeFactory typeFactory; diff --git a/tools/src/main/java/org/projectnessie/cel/tools/ScriptHost.java b/tools/src/main/java/org/projectnessie/cel/tools/ScriptHost.java index 848315950..7a6f5645a 100644 --- a/tools/src/main/java/org/projectnessie/cel/tools/ScriptHost.java +++ b/tools/src/main/java/org/projectnessie/cel/tools/ScriptHost.java @@ -27,7 +27,6 @@ import com.google.api.expr.v1alpha1.Decl; import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; import org.projectnessie.cel.Ast; import org.projectnessie.cel.Env; import org.projectnessie.cel.Env.AstIssuesTuple; @@ -120,7 +119,7 @@ public Script build() throws ScriptCreateException { if (container != null) { envOptions.add(container(container)); } - envOptions.addAll(libraries.stream().map(Library::Lib).collect(Collectors.toList())); + envOptions.addAll(libraries.stream().map(Library::Lib).toList()); Env env = newCustomEnv(registry, envOptions); @@ -134,6 +133,7 @@ public Script build() throws ScriptCreateException { if (astIss.hasIssues()) { throw new ScriptCreateException("check failed", astIss.getIssues()); } + ast = astIss.getAst(); List programOptions = new ArrayList<>(); if (!disableOptimize) { @@ -158,8 +158,11 @@ private Builder() {} private TypeRegistry registry; /** - * Call to instruct the built {@link ScriptHost} to disable script optimizations. + * Disables {@link EvalOption#OptOptimize} for scripts built by the resulting host. * + *

This does not disable planner-selected native evaluation. + * + * @return this builder * @see EvalOption#OptOptimize */ public Builder disableOptimize() { diff --git a/tools/src/test/java/org/projectnessie/cel/tools/ScriptHostTest.java b/tools/src/test/java/org/projectnessie/cel/tools/ScriptHostTest.java index a8e7f6985..c4632a6eb 100644 --- a/tools/src/test/java/org/projectnessie/cel/tools/ScriptHostTest.java +++ b/tools/src/test/java/org/projectnessie/cel/tools/ScriptHostTest.java @@ -27,6 +27,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; import org.junit.jupiter.api.Test; import org.projectnessie.cel.EnvOption; import org.projectnessie.cel.Library; @@ -35,6 +36,7 @@ import org.projectnessie.cel.common.types.IntT; import org.projectnessie.cel.common.types.ListT; import org.projectnessie.cel.common.types.MapT; +import org.projectnessie.cel.common.types.Overloads; import org.projectnessie.cel.common.types.StringT; import org.projectnessie.cel.common.types.ref.Val; import org.projectnessie.cel.interpreter.ActivationFunction; @@ -124,6 +126,40 @@ void executeObjectStillConvertsToNativeResult() throws Exception { assertThat(result).isEqualTo(Arrays.asList(1L, 2L, 3L)); } + @Test + void optimizationFoldsConstantConversionAtBuildWhileDisableOptimizeDefersIt() throws Exception { + AtomicInteger optimizedCalls = new AtomicInteger(); + Script optimized = + ScriptHost.newBuilder() + .build() + .buildScript("int(\"ignored\")") + .withLibraries(new CountingStringToIntLibrary(optimizedCalls)) + .build(); + + assertThat(optimizedCalls).hasValue(1); + assertThat(optimized.executeWithActivation(Integer.class, Collections.emptyMap())) + .isEqualTo(42); + assertThat(optimized.executeWithActivation(Integer.class, Collections.emptyMap())) + .isEqualTo(42); + assertThat(optimizedCalls).hasValue(1); + + AtomicInteger unoptimizedCalls = new AtomicInteger(); + Script unoptimized = + ScriptHost.newBuilder() + .disableOptimize() + .build() + .buildScript("int(\"ignored\")") + .withLibraries(new CountingStringToIntLibrary(unoptimizedCalls)) + .build(); + + assertThat(unoptimizedCalls).hasValue(0); + assertThat(unoptimized.executeWithActivation(Integer.class, Collections.emptyMap())) + .isEqualTo(42); + assertThat(unoptimized.executeWithActivation(Integer.class, Collections.emptyMap())) + .isEqualTo(42); + assertThat(unoptimizedCalls).hasValue(2); + } + @Test void execFail() throws Exception { ScriptHost scriptHost = ScriptHost.newBuilder().build(); @@ -423,6 +459,31 @@ private static boolean grants(Script script, Map arguments) { } } + private static final class CountingStringToIntLibrary implements Library { + private final AtomicInteger calls; + + private CountingStringToIntLibrary(AtomicInteger calls) { + this.calls = calls; + } + + @Override + public List getCompileOptions() { + return Collections.emptyList(); + } + + @Override + public List getProgramOptions() { + return Collections.singletonList( + ProgramOption.functions( + Overload.unary( + Overloads.StringToInt, + ignored -> { + calls.incrementAndGet(); + return IntT.intOf(42); + }))); + } + } + private static final class AttributeLibrary implements Library { private final String environment; From e4ed978abe3307172ee39eb4801a407ec30b4720 Mon Sep 17 00:00:00 2001 From: Robert Stupp Date: Wed, 11 Nov 2026 11:02:32 +0100 Subject: [PATCH 2/2] Fix optimized membership and retain public optimizer helpers --- .../ConstantSetMembershipNodes.java | 19 +++++-- .../EstablishedDecoratorNodes.java | 13 +---- .../interpreter/InterpretableDecorator.java | 21 ++++++++ .../ConstantSetMembershipSupportTest.java | 18 ++++--- .../SetMembershipOptimizationTest.java | 52 +++++++++++++++++++ .../InterpreterPublicApiCompileTest.java | 33 ++++++++++++ .../cel/tools/ScriptHostTest.java | 13 +++++ 7 files changed, 146 insertions(+), 23 deletions(-) diff --git a/core/src/main/java/org/projectnessie/cel/interpreter/ConstantSetMembershipNodes.java b/core/src/main/java/org/projectnessie/cel/interpreter/ConstantSetMembershipNodes.java index b448a7f2e..8625c79b1 100644 --- a/core/src/main/java/org/projectnessie/cel/interpreter/ConstantSetMembershipNodes.java +++ b/core/src/main/java/org/projectnessie/cel/interpreter/ConstantSetMembershipNodes.java @@ -17,11 +17,10 @@ import static org.projectnessie.cel.common.types.BoolT.False; import static org.projectnessie.cel.common.types.BoolT.True; -import static org.projectnessie.cel.common.types.Err.noSuchOverload; import static org.projectnessie.cel.common.types.Util.isUnknownOrError; import java.util.Set; -import org.projectnessie.cel.common.operators.Operator; +import org.projectnessie.cel.common.types.DoubleT; import org.projectnessie.cel.common.types.ref.Val; /** Shared post-evaluation semantics for optimized constant-set membership. */ @@ -35,9 +34,19 @@ static Val evaluate(Val needle, String needleTypeName, Set values) { if (values.isEmpty()) { return False; } - if (!needle.type().typeName().equals(needleTypeName)) { - return noSuchOverload(null, Operator.In.id, needle); + // Set.contains() accepts reference identity, but CEL NaN never equals even itself. + if (needle instanceof DoubleT && Double.isNaN(needle.doubleValue())) { + return False; + } + if (needle.type().typeName().equals(needleTypeName)) { + return values.contains(needle) ? True : False; + } + // CEL equality permits cross-type numeric matches without requiring matching Java hashes. + for (Val value : values) { + if (needle.equal(value) == True) { + return True; + } } - return values.contains(needle) ? True : False; + return False; } } diff --git a/core/src/main/java/org/projectnessie/cel/interpreter/EstablishedDecoratorNodes.java b/core/src/main/java/org/projectnessie/cel/interpreter/EstablishedDecoratorNodes.java index f2a79161c..c70a62200 100644 --- a/core/src/main/java/org/projectnessie/cel/interpreter/EstablishedDecoratorNodes.java +++ b/core/src/main/java/org/projectnessie/cel/interpreter/EstablishedDecoratorNodes.java @@ -21,7 +21,6 @@ import static org.projectnessie.cel.common.types.Err.newErr; import static org.projectnessie.cel.common.types.Err.noSuchOverload; import static org.projectnessie.cel.common.types.UnknownT.isUnknown; -import static org.projectnessie.cel.common.types.Util.isUnknownOrError; import static org.projectnessie.cel.interpreter.Coster.Cost.estimateCost; import static org.projectnessie.cel.interpreter.Interpretable.calExhaustiveBinaryOpsCost; @@ -56,17 +55,7 @@ final class EvalSetMembership extends AbstractEval implements Coster { /** Eval implements the Interpretable interface method. */ @Override public Val eval(org.projectnessie.cel.interpreter.Activation ctx) { - Val val = arg.eval(ctx); - if (isUnknownOrError(val)) { - return val; - } - if (valueSet.isEmpty()) { - return False; - } - if (!val.type().typeName().equals(argTypeName)) { - return noSuchOverload(null, Operator.In.id, val); - } - return valueSet.contains(val) ? True : False; + return ConstantSetMembershipSupport.evaluate(arg.eval(ctx), argTypeName, valueSet); } /** Cost implements the Coster interface method. */ diff --git a/core/src/main/java/org/projectnessie/cel/interpreter/InterpretableDecorator.java b/core/src/main/java/org/projectnessie/cel/interpreter/InterpretableDecorator.java index e9eae3c3d..03d5da40e 100644 --- a/core/src/main/java/org/projectnessie/cel/interpreter/InterpretableDecorator.java +++ b/core/src/main/java/org/projectnessie/cel/interpreter/InterpretableDecorator.java @@ -17,6 +17,7 @@ import org.projectnessie.cel.common.types.ref.Val; import org.projectnessie.cel.interpreter.Interpretable.InterpretableAttribute; +import org.projectnessie.cel.interpreter.Interpretable.InterpretableCall; import org.projectnessie.cel.interpreter.Interpretable.InterpretableConst; /** @@ -107,4 +108,24 @@ static InterpretableDecorator decDisableShortcircuits() { static InterpretableDecorator decOptimize() { return BuiltInOptimizer.INSTANCE; } + + /** Folds a unary call with a constant argument when supported. */ + static Interpretable maybeOptimizeConstUnary(Interpretable i, InterpretableCall call) { + return BuiltInOptimizer.maybeOptimizeConstUnary(i, call); + } + + /** Folds a list literal whose elements are constant. */ + static Interpretable maybeBuildListLiteral(Interpretable i, EvalList list) { + return BuiltInOptimizer.maybeBuildListLiteral(i, list); + } + + /** Folds a map literal whose keys and values are constant. */ + static Interpretable maybeBuildMapLiteral(Interpretable i, EvalMap map) { + return BuiltInOptimizer.maybeBuildMapLiteral(i, map); + } + + /** Optimizes membership in an eligible constant list. */ + static Interpretable maybeOptimizeSetMembership(Interpretable i, InterpretableCall inList) { + return BuiltInOptimizer.maybeOptimizeSetMembership(i, inList); + } } diff --git a/core/src/test/java/org/projectnessie/cel/interpreter/ConstantSetMembershipSupportTest.java b/core/src/test/java/org/projectnessie/cel/interpreter/ConstantSetMembershipSupportTest.java index 05412022a..dff4adce0 100644 --- a/core/src/test/java/org/projectnessie/cel/interpreter/ConstantSetMembershipSupportTest.java +++ b/core/src/test/java/org/projectnessie/cel/interpreter/ConstantSetMembershipSupportTest.java @@ -20,6 +20,7 @@ import static org.projectnessie.cel.common.types.BoolT.True; import static org.projectnessie.cel.common.types.DoubleT.doubleOf; import static org.projectnessie.cel.common.types.Err.newErr; +import static org.projectnessie.cel.common.types.IntT.intOf; import static org.projectnessie.cel.common.types.StringT.stringOf; import static org.projectnessie.cel.common.types.UintT.uintOf; import static org.projectnessie.cel.common.types.UnknownT.unknownOf; @@ -33,7 +34,7 @@ class ConstantSetMembershipSupportTest { @Test - void preservesErrorUnknownEmptyAndTypeCheckOrder() { + void preservesErrorUnknownAndEmptySetSemantics() { Val error = newErr("carried"); Val unknown = unknownOf(7L); Set values = Set.of(stringOf("value")); @@ -41,25 +42,30 @@ void preservesErrorUnknownEmptyAndTypeCheckOrder() { assertThat(evaluate(error, "string", values)).isSameAs(error); assertThat(evaluate(unknown, "string", values)).isSameAs(unknown); assertThat(evaluate(uintOf(1), null, Set.of())).isSameAs(False); - assertThat(evaluate(uintOf(1), "string", values).type().typeName()).isEqualTo("error"); + assertThat(evaluate(uintOf(1), "string", values)).isSameAs(False); } @Test - void delegatesExactEqualityAndHashingToTheStoredValSet() { + void preservesCelNumericEquality() { DoubleT nan = doubleOf(Double.NaN); Set nanSet = Set.of(nan); - assertThat(evaluate(nan, "double", nanSet)).isSameAs(True); + assertThat(evaluate(nan, "double", nanSet)).isSameAs(False); assertThat(evaluate(doubleOf(Double.NaN), "double", nanSet)).isSameAs(False); for (double stored : new double[] {-0.0d, 0.0d}) { Set zeroSet = Set.of(doubleOf(stored)); for (double needle : new double[] {-0.0d, 0.0d}) { Val needleValue = doubleOf(needle); - assertThat(evaluate(needleValue, "double", zeroSet).booleanValue()) - .isEqualTo(zeroSet.contains(needleValue)); + assertThat(evaluate(needleValue, "double", zeroSet).booleanValue()).isTrue(); } } + assertThat(evaluate(doubleOf(1.0), "int", Set.of(intOf(1)))).isSameAs(True); + assertThat(evaluate(intOf(1), "double", Set.of(doubleOf(1.0)))).isSameAs(True); + assertThat(evaluate(uintOf(1), "int", Set.of(intOf(1)))).isSameAs(True); + assertThat(evaluate(intOf(-1), "uint", Set.of(uintOf(-1)))).isSameAs(False); + assertThat(evaluate(doubleOf(1.5), "int", Set.of(intOf(1)))).isSameAs(False); + UintT highBit = uintOf(Long.MIN_VALUE); assertThat(evaluate(highBit, "uint", Set.of(highBit))).isSameAs(True); } diff --git a/core/src/test/java/org/projectnessie/cel/interpreter/SetMembershipOptimizationTest.java b/core/src/test/java/org/projectnessie/cel/interpreter/SetMembershipOptimizationTest.java index 74cad898c..469399845 100644 --- a/core/src/test/java/org/projectnessie/cel/interpreter/SetMembershipOptimizationTest.java +++ b/core/src/test/java/org/projectnessie/cel/interpreter/SetMembershipOptimizationTest.java @@ -27,9 +27,20 @@ import java.lang.reflect.Proxy; import java.util.List; +import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.projectnessie.cel.Ast; +import org.projectnessie.cel.Env; +import org.projectnessie.cel.EnvOption; +import org.projectnessie.cel.EvalOption; +import org.projectnessie.cel.ProgramOption; +import org.projectnessie.cel.checker.Decls; +import org.projectnessie.cel.common.types.DoubleT; import org.projectnessie.cel.common.types.Overloads; +import org.projectnessie.cel.common.types.UintT; import org.projectnessie.cel.common.types.pb.DefaultTypeAdapter; import org.projectnessie.cel.common.types.ref.Val; import org.projectnessie.cel.common.types.traits.Lister; @@ -81,6 +92,47 @@ void inconsistentListSizeAndIteratorPreserveOriginalEvaluation() { assertThat(BuiltInOptimizer.maybeOptimizeSetMembership(original, original)).isSameAs(original); } + @ParameterizedTest + @ValueSource( + strings = { + "x in [1]", + "x in [1u]", + "x in [1.0]", + "x in ['1']", + "x in [double('NaN')]", + "x in [0.0]", + "x in []" + }) + void optimizedMembershipMatchesListEquality(String expression) { + Env env = Env.newEnv(EnvOption.declarations(Decls.newVar("x", Decls.Dyn))); + var compiled = env.compile(expression); + assertThat(compiled.hasIssues()).isFalse(); + Ast ast = compiled.getAst(); + var plain = env.program(ast, ProgramOption.evalOptions(EvalOption.OptDisableNativeEval)); + var optimized = env.program(ast, ProgramOption.evalOptions(EvalOption.OptOptimize)); + var establishedOptimized = + env.program( + ast, + ProgramOption.evalOptions(EvalOption.OptOptimize, EvalOption.OptDisableNativeEval)); + for (Object value : + List.of(1L, 1.0, UintT.uintOf(1), "1", -1L, 1.5, -0.0, 0.0, DoubleT.doubleOf(Double.NaN))) { + Object input = Map.of("x", value); + Val expected = plain.eval(input).getVal(); + assertThat(optimized.eval(input).getVal()) + .as(expression + " with " + value) + .isEqualTo(expected); + assertThat(establishedOptimized.eval(input).getVal()).isEqualTo(expected); + } + } + + @Test + void crossTypeMembershipEvaluatesLeftOperandOnce() { + AtomicInteger evaluations = new AtomicInteger(); + Interpretable lhs = countingValue(DoubleT.doubleOf(1), evaluations); + assertThat(optimize(lhs, new Val[] {intOf(1)}).eval(emptyActivation()).booleanValue()).isTrue(); + assertThat(evaluations).hasValue(1); + } + private static Interpretable optimize(Interpretable lhs, Val[] values) { Interpretable rhs = newConstValue(2, newValArrayList(DefaultTypeAdapter.Instance, values)); return BuiltInOptimizer.maybeOptimizeSetMembership( diff --git a/core/src/test/java/org/projectnessie/cel/publicapi/InterpreterPublicApiCompileTest.java b/core/src/test/java/org/projectnessie/cel/publicapi/InterpreterPublicApiCompileTest.java index b07d8fb71..7a0b32745 100644 --- a/core/src/test/java/org/projectnessie/cel/publicapi/InterpreterPublicApiCompileTest.java +++ b/core/src/test/java/org/projectnessie/cel/publicapi/InterpreterPublicApiCompileTest.java @@ -22,6 +22,8 @@ import com.google.api.expr.v1alpha1.CheckedExpr; import java.util.Map; import org.junit.jupiter.api.Test; +import org.projectnessie.cel.Env; +import org.projectnessie.cel.ProgramOption; import org.projectnessie.cel.common.containers.Container; import org.projectnessie.cel.common.types.pb.ProtoTypeRegistry; import org.projectnessie.cel.interpreter.Activation; @@ -35,6 +37,7 @@ import org.projectnessie.cel.interpreter.Coster.Cost; import org.projectnessie.cel.interpreter.Dispatcher; import org.projectnessie.cel.interpreter.Interpretable; +import org.projectnessie.cel.interpreter.Interpretable.InterpretableCall; import org.projectnessie.cel.interpreter.Interpretable.InterpretableConst; import org.projectnessie.cel.interpreter.InterpretableDecorator; import org.projectnessie.cel.interpreter.InterpretablePlanner; @@ -48,6 +51,36 @@ */ class InterpreterPublicApiCompileTest { + @Test + void optimizerHelpersRemainCallableOutsideInterpreterPackage() { + Env env = Env.newEnv(); + var conversion = env.compile("int('42')").getAst(); + var membership = env.compile("1 in [1]").getAst(); + InterpretableDecorator foldUnary = + node -> + node instanceof InterpretableCall call + ? InterpretableDecorator.maybeOptimizeConstUnary(node, call) + : node; + InterpretableDecorator foldMembership = + node -> { + Interpretable folded = Interpreter.optimize().decorate(node); + return node instanceof InterpretableCall call + ? InterpretableDecorator.maybeOptimizeSetMembership(folded, call) + : folded; + }; + assertThat( + env.program(conversion, ProgramOption.customDecorator(foldUnary)) + .eval(emptyMap()) + .getVal()) + .isEqualTo(intOf(42)); + assertThat( + env.program(membership, ProgramOption.customDecorator(foldMembership)) + .eval(emptyMap()) + .getVal() + .booleanValue()) + .isTrue(); + } + @Test void retainedContractsAndFactoriesRemainUsableOutsideInterpreterPackage() { ProtoTypeRegistry registry = ProtoTypeRegistry.newEmptyRegistry(); diff --git a/tools/src/test/java/org/projectnessie/cel/tools/ScriptHostTest.java b/tools/src/test/java/org/projectnessie/cel/tools/ScriptHostTest.java index c4632a6eb..88856f4dd 100644 --- a/tools/src/test/java/org/projectnessie/cel/tools/ScriptHostTest.java +++ b/tools/src/test/java/org/projectnessie/cel/tools/ScriptHostTest.java @@ -50,6 +50,19 @@ class ScriptHostTest { + " && resource.name.startsWith(\"projects/_/buckets/example/objects/reports/\")" + " && request.time < timestamp(\"2026-08-01T00:00:00Z\")"; + @Test + void checkedPlanningPreservesCrossTypeListMembership() throws Exception { + for (String expression : List.of("dyn(1.0) in [1]", "dyn(1) in [1.0]", "dyn(1u) in [1]")) { + Script optimized = ScriptHost.newBuilder().build().buildScript(expression).build(); + Script unoptimized = + ScriptHost.newBuilder().disableOptimize().build().buildScript(expression).build(); + assertThat(optimized.execute(Boolean.class, Map.of())).isTrue(); + assertThat(unoptimized.execute(Boolean.class, Map.of())).isTrue(); + } + Script mismatch = ScriptHost.newBuilder().build().buildScript("dyn('1') in [1]").build(); + assertThat(mismatch.execute(Boolean.class, Map.of())).isFalse(); + } + @Test void basic() throws Exception { ScriptHost scriptHost = ScriptHost.newBuilder().build();