Skip to content

Add native scalar planning and optimizer integration - #1034

Open
snazy wants to merge 4 commits into
projectnessie:mainfrom
snazy:native-scalar-planning
Open

snazy wants to merge 4 commits into
projectnessie:mainfrom
snazy:native-scalar-planning

Conversation

@snazy

@snazy snazy commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

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.

@snazy
snazy requested a review from XN137 November 11, 2026 07:28
if (astIss.hasIssues()) {
throw new ScriptCreateException("check failed", astIss.getIssues());
}
ast = astIss.getAst();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

codex finding:

  • [P2] Checked planning changes membership results in ScriptHost — ScriptHost.java:136. Using the checked AST enables constant-set optimization by default. For example, dyn(1.0) in [1] previously returned true through CEL numeric equality; it now reaches EvalSetMembership, whose type-name check rejects double versus int, causing ScriptExecutionException. The set optimization needs to preserve list-membership semantics before enabling it for existing scripts

};
}

static Interpretable maybeOptimizeConstUnary(Interpretable i, InterpretableCall call) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

codex finding

  • [P2] Optimizer extraction removes public methods — InterpretableDecorator.java:107–110. The removed maybeOptimizeConstUnary and maybeOptimizeSetMembership methods were implicitly public interface methods with public parameter types. Existing callers will fail compilation or encounter NoSuchMethodError. Keep forwarding methods delegating to BuiltInOptimizer to preserve compatibility.

if it used to be part of the public and we remove it intentionally, we might want to call it out in PR description

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.
@snazy
snazy force-pushed the native-scalar-planning branch from e91750b to e4ed978 Compare November 11, 2026 10:05
@snazy

snazy commented Nov 11, 2026

Copy link
Copy Markdown
Member Author

Good findings! Addressed in the commit.

@snazy
snazy requested a review from XN137 November 11, 2026 10:06
boolean selectTruthy = selectTruthy(activation);
try {
return selectTruthy ? truthy.evalBoolean(activation) : falsy.evalBoolean(activation);
} catch (ValueSignal valueSignal) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more codex finding:

  • [P2] Preserve exception handling in native conditionals — NativeScalarNodes.java:806–812. Native conditionals catch only ValueSignal, whereas the established EvalAttr converts all evaluation exceptions into CEL errors. With b=true and s=StringT.stringOf(null), (b ? s == 'value' : false) || true previously returns true: the conditional converts the comparison’s NullPointerException into an error, which || true suppresses. Native evaluation instead throws an internal-error exception. Preserve the exception conversion around both the condition and selected branch.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Fixed in the new commit

@snazy
snazy requested a review from XN137 November 19, 2026 08:53
Comment on lines +50 to +58
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;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

codex finding:

  • [P2] Native selectors enable partial evaluation without OptPartialEval — resolve() switches to the partial attribute whenever the input is a PartialActivation, regardless of program options. For checked attrs.answer == 1, supplying attrs.answer = 1 and an unknown pattern for that field now returns unknown by default, while adding OptDisableNativeEval or OptTrackState returns true. Evaluation results therefore depend on optimization or instrumentation settings. Preserve the configured attribute factory’s behavior and enable qualifier-pattern matching only with OptPartialEval.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Native selectors now use the program’s configured attribute factory. attrs.answer == 1 returns true by default, with optimization, and with state tracking. With OptPartialEval and a matching unknown pattern, it returns unknown.

@snazy
snazy requested a review from XN137 November 28, 2026 08:47
return null;
}
// An InList call always has a Lister right operand.
Lister list = (Lister) listConstant.value();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

codex finding:

BuiltInOptimizer.java:128 — Preserve CEL errors during membership folding

true || ({'k': 1} in [{'k': 1, string(b'k'): 2}]) should evaluate to true. If evaluated, the duplicate key produces a CEL error that || must absorb. Here the list folds to an Err, and the cast to Lister throws ClassCastException during ScriptHost.build(). The cast predates this PR, and switching ScriptHost to checked planning makes it reachable by default. Could we require the folded value to be a Lister before applying the rewrite?


@Override
public boolean evalBoolean(Activation activation) {
return NativeSupport.booleanValue(adapter, resolveRaw(activation));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

codex finding:

NativeScalarNodes.java:186 — Keep binding errors visible to ||

Consider x || true with x bound to a supplier that throws Err.ErrException. The previous optimized plan converted it to a CEL error at the lookup, so || returned true. The new lookup lets it escape to Prog.eval, which converts it after the whole expression has unwound. Could we convert Err.ErrException at the lookup so the logical operator can apply CEL’s error-absorption rule?

Expr expr, ResolvedFunction resolvedFunction, Interpretable[] arguments) {
Call call = expr.getCallExpr();
if (!policy.builtInOptimizationEnabled()
|| !resolvedFunction.overloadId.equals(Overloads.InList)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

codex finding:

Planner.java:503 — Honor the registered membership overload

ProgramOption.functions documents support for overriding built-ins. If an application registers Overloads.InList to return false, the checked expression 1 in [1] returns false with optimization disabled. This rewrite returns true without calling the registered implementation. The existing checked optimizer has the same gap. Could we require the resolved implementation to be the standard membership overload before applying the rewrite?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants