|
| 1 | +package io.substrait.expression; |
| 2 | + |
| 3 | +import io.substrait.type.Type; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
| 6 | +import java.util.function.Function; |
| 7 | + |
| 8 | +/** |
| 9 | + * Builds lambda expressions with build-time validation of parameter references. |
| 10 | + * |
| 11 | + * <p>Maintains a stack of lambda parameter scopes. Each call to {@link #lambda} pushes parameters |
| 12 | + * onto the stack, builds the body via a callback, and pops. Nested lambdas simply call {@code |
| 13 | + * lambda()} again on the same builder. |
| 14 | + * |
| 15 | + * <p>The callback receives a {@link Scope} handle for creating validated parameter references. The |
| 16 | + * correct {@code stepsOut} value is computed automatically from the stack. |
| 17 | + * |
| 18 | + * <pre>{@code |
| 19 | + * LambdaBuilder lb = new LambdaBuilder(); |
| 20 | + * |
| 21 | + * // Simple: (x: i32) -> x |
| 22 | + * Expression.Lambda simple = lb.lambda(List.of(R.I32), params -> params.ref(0)); |
| 23 | + * |
| 24 | + * // Nested: (x: i32) -> (y: i64) -> add(x, y) |
| 25 | + * Expression.Lambda nested = lb.lambda(List.of(R.I32), outer -> |
| 26 | + * lb.lambda(List.of(R.I64), inner -> |
| 27 | + * add(outer.ref(0), inner.ref(0)) |
| 28 | + * ) |
| 29 | + * ); |
| 30 | + * }</pre> |
| 31 | + */ |
| 32 | +public class LambdaBuilder { |
| 33 | + private final List<Type.Struct> lambdaContext = new ArrayList<>(); |
| 34 | + |
| 35 | + /** |
| 36 | + * Builds a lambda expression. The body function receives a {@link Scope} for creating validated |
| 37 | + * parameter references. Nested lambdas are built by calling this method again inside the |
| 38 | + * callback. |
| 39 | + * |
| 40 | + * @param paramTypes the lambda's parameter types |
| 41 | + * @param bodyFn function that builds the lambda body given a scope handle |
| 42 | + * @return the constructed lambda expression |
| 43 | + */ |
| 44 | + public Expression.Lambda lambda(List<Type> paramTypes, Function<Scope, Expression> bodyFn) { |
| 45 | + Type.Struct params = Type.Struct.builder().nullable(false).addAllFields(paramTypes).build(); |
| 46 | + pushLambdaContext(params); |
| 47 | + try { |
| 48 | + Scope scope = new Scope(params); |
| 49 | + Expression body = bodyFn.apply(scope); |
| 50 | + return ImmutableExpression.Lambda.builder().parameters(params).body(body).build(); |
| 51 | + } finally { |
| 52 | + popLambdaContext(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Builds a lambda expression from a pre-built parameter struct. Used by internal converters that |
| 58 | + * already have a Type.Struct (e.g., during protobuf deserialization). |
| 59 | + * |
| 60 | + * @param params the lambda's parameter struct |
| 61 | + * @param bodyFn function that builds the lambda body |
| 62 | + * @return the constructed lambda expression |
| 63 | + */ |
| 64 | + public Expression.Lambda lambdaFromStruct( |
| 65 | + Type.Struct params, java.util.function.Supplier<Expression> bodyFn) { |
| 66 | + pushLambdaContext(params); |
| 67 | + try { |
| 68 | + Expression body = bodyFn.get(); |
| 69 | + return ImmutableExpression.Lambda.builder().parameters(params).body(body).build(); |
| 70 | + } finally { |
| 71 | + popLambdaContext(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Resolves the parameter struct for a lambda at the given stepsOut from the current innermost |
| 77 | + * scope. Used by internal converters to validate lambda parameter references during |
| 78 | + * deserialization. |
| 79 | + * |
| 80 | + * @param stepsOut number of lambda scopes to traverse outward (0 = current/innermost) |
| 81 | + * @return the parameter struct at the target scope level |
| 82 | + * @throws IllegalArgumentException if stepsOut exceeds the current nesting depth |
| 83 | + */ |
| 84 | + public Type.Struct resolveParams(int stepsOut) { |
| 85 | + int targetDepth = lambdaContext.size() - stepsOut; |
| 86 | + if (targetDepth <= 0 || targetDepth > lambdaContext.size()) { |
| 87 | + throw new IllegalArgumentException( |
| 88 | + String.format( |
| 89 | + "Lambda parameter reference with stepsOut=%d is invalid (current depth: %d)", |
| 90 | + stepsOut, lambdaContext.size())); |
| 91 | + } |
| 92 | + return lambdaContext.get(targetDepth - 1); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Pushes a lambda's parameters onto the context stack. This makes the parameters available for |
| 97 | + * validation when building the lambda's body, and allows nested lambda parameter references to |
| 98 | + * correctly compute their stepsOut values. |
| 99 | + */ |
| 100 | + private void pushLambdaContext(Type.Struct params) { |
| 101 | + lambdaContext.add(params); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Pops the most recently pushed lambda parameters from the context stack. Called after a lambda's |
| 106 | + * body has been built, restoring the context to the enclosing lambda's scope. |
| 107 | + */ |
| 108 | + private void popLambdaContext() { |
| 109 | + lambdaContext.remove(lambdaContext.size() - 1); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * A handle to a particular lambda's parameter scope. Use {@link #ref} to create validated |
| 114 | + * parameter references. |
| 115 | + * |
| 116 | + * <p>Each Scope captures the depth of the lambdaContext stack at the time it was created. When |
| 117 | + * {@link #ref} is called, the Substrait {@code stepsOut} value is computed as the difference |
| 118 | + * between the current stack depth and the captured depth. This means the same Scope produces |
| 119 | + * different stepsOut values depending on the nesting level at the time of the call, which is what |
| 120 | + * allows outer.ref(0) to produce stepsOut=1 when called inside a nested lambda. |
| 121 | + */ |
| 122 | + public class Scope { |
| 123 | + private final Type.Struct params; |
| 124 | + private final int depth; |
| 125 | + |
| 126 | + private Scope(Type.Struct params) { |
| 127 | + this.params = params; |
| 128 | + this.depth = lambdaContext.size(); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Computes the number of lambda boundaries between this scope and the current innermost scope. |
| 133 | + * This value changes dynamically as nested lambdas are built. |
| 134 | + */ |
| 135 | + private int stepsOut() { |
| 136 | + return lambdaContext.size() - depth; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Creates a validated reference to a parameter of this lambda. |
| 141 | + * |
| 142 | + * @param paramIndex index of the parameter within this lambda's parameter struct |
| 143 | + * @return a {@link FieldReference} pointing to the specified parameter |
| 144 | + * @throws IndexOutOfBoundsException if paramIndex is out of bounds |
| 145 | + */ |
| 146 | + public FieldReference ref(int paramIndex) { |
| 147 | + return FieldReference.newLambdaParameterReference(paramIndex, params, stepsOut()); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments