Package io.substrait.expression
Class LambdaBuilder
java.lang.Object
io.substrait.expression.LambdaBuilder
Builds lambda expressions with build-time validation of parameter references.
Maintains a stack of lambda parameter scopes. Each call to lambda(java.util.List<io.substrait.type.Type>, java.util.function.Function<io.substrait.expression.LambdaBuilder.Scope, io.substrait.expression.Expression>) pushes parameters
onto the stack, builds the body via a callback, and pops. Nested lambdas simply call
lambda() again on the same builder.
The callback receives a LambdaBuilder.Scope handle for creating validated parameter references. The
correct stepsOut value is computed automatically from the stack.
LambdaBuilder lb = new LambdaBuilder();
// Simple: (x: i32) -> x
Expression.Lambda simple = lb.lambda(List.of(R.I32), params -> params.ref(0));
// Nested: (x: i32) -> (y: i64) -> add(x, y)
Expression.Lambda nested = lb.lambda(List.of(R.I32), outer ->
lb.lambda(List.of(R.I64), inner ->
add(outer.ref(0), inner.ref(0))
)
);
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassA handle to a particular lambda's parameter scope. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionlambda(List<Type> paramTypes, Function<LambdaBuilder.Scope, Expression> bodyFn) Builds a lambda expression.lambdaFromStruct(Type.Struct params, Supplier<Expression> bodyFn) Builds a lambda expression from a pre-built parameter struct.newParameterReference(int stepsOut, int paramIndex) Creates a validated field reference to a lambda parameter.resolveParams(int stepsOut) Resolves the parameter struct for a lambda at the given stepsOut from the current innermost scope.
-
Constructor Details
-
LambdaBuilder
public LambdaBuilder()
-
-
Method Details
-
lambda
public Expression.Lambda lambda(List<Type> paramTypes, Function<LambdaBuilder.Scope, Expression> bodyFn) Builds a lambda expression. The body function receives aLambdaBuilder.Scopefor creating validated parameter references. Nested lambdas are built by calling this method again inside the callback.- Parameters:
paramTypes- the lambda's parameter typesbodyFn- function that builds the lambda body given a scope handle- Returns:
- the constructed lambda expression
-
lambdaFromStruct
Builds a lambda expression from a pre-built parameter struct. Used by internal converters that already have a Type.Struct (e.g., during protobuf deserialization).- Parameters:
params- the lambda's parameter structbodyFn- function that builds the lambda body- Returns:
- the constructed lambda expression
-
resolveParams
Resolves the parameter struct for a lambda at the given stepsOut from the current innermost scope. Used by internal converters to validate lambda parameter references during deserialization.- Parameters:
stepsOut- number of lambda scopes to traverse outward (0 = current/innermost)- Returns:
- the parameter struct at the target scope level
- Throws:
IllegalArgumentException- if stepsOut exceeds the current nesting depth
-
newParameterReference
Creates a validated field reference to a lambda parameter. Validates that stepsOut is valid for the current lambda nesting context.- Parameters:
stepsOut- number of lambda scopes to traverse outward (0 = current/innermost)paramIndex- index of the parameter within the target lambda's parameter struct- Returns:
- a field reference to the specified lambda parameter
- Throws:
IllegalArgumentException- if stepsOut exceeds the current nesting depthIndexOutOfBoundsException- if paramIndex is out of bounds for the target lambda
-