← Back to Pull requests

API: Update Expressions to Support New Expression Spec #18629

Open
danielcweeks opened this pull request with 3137 lines diff across 35 files
View on GitHub
danielcweeks danielcweeks commented on Oct 24, 2026 00:51 UTC
Author

This PR is still __WIP / DRAFT__

This PR includes the changes necessary to update the class hierarchy for expressions to support the new expressions spec.

The goal is to

  • introduce the new ValueExpression side of the expressions with both unbound and bound variants

  • update the existing predicate expressions to support the new grammar while still preserving the existing API surface area for any predicates used for filtering/metrics evaluation
  • As part of this change, the existing Expression interface is promoted to be a super interface of ValueExpression and Predicate. This is largely for implementation purposes where the spec allows for either an EXPR or PREDICATE (like function arguments).

    A high-level overview of bound/unbound is:

    <img width="667" height="188" alt="image" src="https://github.com/user-attachments/assets/1bb6fe20-e86f-46bd-86c1-67eb3438ec5d" />

    A a quick overview of the legacy vs. new interfaces for predicates is:

    <img width="1021" height="188" alt="image" src="https://github.com/user-attachments/assets/62272163-792d-4c72-a0f6-14f3e7cad594" />

    AI Disclosure:

  • Claude (various models)
  • Diff: 3137 lines (+3032 / -105) across 35 files Reviewer: huaxingao Review duration: 54890m
    All Vouch review confidence checks passed
    Residual risk 0.11 = Change Risk (0.79) × (1 − Review Confidence (0.85)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.79 · 3137 lines diff across 35 files
    0.79
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.91 · 5 substantive comment(s)
    0.91
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    85%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.400
    Mass File Touch mass_file_touch
    0.080
    Real PR Conversation Timeline
    12 items
    huaxingao huaxingao commented on code on Nov 03, 2026 01:08 UTC
    Code Review
    api/src/main/java/org/apache/iceberg/expressions/UnboundPredicate.java:1
    @@ -123,6 +206,51 @@ public Expression bind(StructType struct, boolean caseSensitive) {
         return bindLiteralOperation(bound);
       }
     
    +  /**
    +   * Bind this UnboundPredicate, using a function binder to resolve function applications.
    +   *
    +   * @param struct The {@link StructType struct type} to resolve references by name.
    +   * @param caseSensitive A boolean flag to control whether the bind should enforce case
    +   *     sensitivity.
    +   * @param functionBinder a {@link FunctionBinder} used to resolve functions
    +   * @return an {@link Expression}
    +   * @throws ValidationException if literals do not match bound references, or if comparison on
    +   *     expression is invalid
    +   */
    +  public Expression bind(StructType struct, boolean caseSensitive, FunctionBinder functionBinder) {
    +    Preconditions.checkNotNull(functionBinder, "Function binder cannot be null");
    +    if (term() != null) {
    +      return bind(struct, caseSensitive);
    +    }
    +
    +    BoundExpression<T> boundLeft = bindOperand(left, struct, caseSensitive, functionBinder);

    Should the new bind also check and convert types, like the old bind does?

    For example, if x is a long:

  • x < 5 binds to a LongLiteral.

  • f(x) < 5, where f returns long: the 5 stays an int (IntegerLiteral). It is not converted to long.
  • Also, upper(name) == 5 binds without error, but the old form name == 5 gives a ValidationException.

    huaxingao huaxingao commented on code on Nov 03, 2026 01:35 UTC
    Code Review
    api/src/main/java/org/apache/iceberg/expressions/FunctionReference.java:1
    @@ -0,0 +1,43 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you 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.apache.iceberg.expressions;
    +
    +import org.apache.iceberg.catalog.CatalogObjectIdentifier;
    +
    +public class FunctionReference {

    Expression is Serializable, but FunctionReference, BoundFunctionReference, and CatalogObjectIdentifier are not. Should these classes implement Serializable? Spark and Flink serialize filters, so a predicate with a function would fail with NotSerializableException.

    RussellSpitzer RussellSpitzer commented on code on Nov 03, 2026 22:56 UTC
    Code Review
    api/src/main/java/org/apache/iceberg/expressions/BoundFunctionReference.java:1
    @@ -0,0 +1,64 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you 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.apache.iceberg.expressions;
    +
    +import org.apache.iceberg.catalog.CatalogObjectIdentifier;
    +import org.apache.iceberg.types.Type;
    +
    +public class BoundFunctionReference<T> {
    +  private final String catalog;
    +  private final CatalogObjectIdentifier identifier;
    +  private final Type type;
    +
    +  /**
    +   * Represents a specific function implementation that returns a specific type. What arguments the

    Nit: Awkward sentence construction, could do

    "Arguments the function accepts are not directly exposed as implementations ...."

    RussellSpitzer RussellSpitzer commented on Nov 04, 2026 21:59 UTC
    Contributor

    I'm not sure I like the old classes absorbing the functionality from the new ValueExpressions things. It seems to be leading to a lot of branching where the classes have to behave differently based on their state and is going to push some errors into runtime that would otherwise be obvious.

    I think it may make sense to have a parallel set of classes (UnboundValuePredicate and BoundValuePredicate) which represent the new hierarchy and live in parallel to the legacy classes (BoundSet, BoundLieteral, BoundUnary).

    <img width="1171" height="869" alt="image" src="https://github.com/user-attachments/assets/89fc9f49-c4c6-4b7f-977d-5458138c889f" />

    We could still expose Left(), right() in the older classes maybe through a shared interface?

    I'm doing some prototyping to see what the damage actually is in this approach

    danielcweeks danielcweeks commented on Nov 04, 2026 22:10 UTC
    Author

    I'm not sure I like the old classes absorbing the functionality from the new ValueExpressions things. It seems to be leading to a lot of branching where the classes have to behave differently based on their state and is going to push some errors into runtime that would otherwise be obvious.

    I don't think this is necessarily true. Anywhere a expression is used currently remains the same and everything should continue to work (parsing/visitors/evaluators/etc). Anything that is parsed into the new expression grammar would work with the new value/predicate representation.

    I think it may make sense to have a parallel set of classes (UnboundValuePredicate and BoundValuePredicate) which represent the new hierarchy and live in parallel to the legacy classes (BoundSet, BoundLieteral, BoundUnary).

    The issue here is we would have to separate grammar structures and potentially need to support both or translate between the two.

    > I'm doing some prototyping to see what the damage actually is in this approach

    rdblue rdblue commented on code on Nov 30, 2026 20:19 UTC
    Code Review
    api/src/main/java/org/apache/iceberg/expressions/Binder.java:1
    @@ -48,16 +48,101 @@ private Binder() {}
        * <p>The expression cannot contain references that are already bound, or an {@link
        * IllegalStateException} will be thrown.
        *
    +   * <p>A value expression is bound as by {@link #bind(StructType, ValueExpression, boolean)}.
    +   *
        * @param struct The {@link StructType struct type} to resolve references by name.
        * @param expr An {@link Expression expression} to rewrite with bound references.
        * @param caseSensitive A boolean flag to control whether the bind should enforce case
        *     sensitivity.
        * @return the expression rewritten with bound references
        * @throws ValidationException if literals do not match bound references
        * @throws IllegalStateException if any references are already bound
    +   * @throws UnsupportedOperationException if the expression calls a function outside {@code
    +   *     iceberg_functions}; use {@link #bind(StructType, Expression, boolean, FunctionBinder)} to
    +   *     resolve other functions
        */
       public static Expression bind(StructType struct, Expression expr, boolean caseSensitive) {
    -    return ExpressionVisitors.visit(expr, new BindVisitor(struct, caseSensitive));
    +    return bind(struct, expr, caseSensitive, IcebergFunctions.INSTANCE);
    +  }
    +
    +  /**
    +   * Replaces all unbound references with bound references to fields in the given struct, using a
    +   * function binder to resolve function calls.

    "and" uses a function binder to resolve function calls?

    Will iceberg_functions also be resolved automatically or is the function binder responsible for that?

    rdblue rdblue commented on code on Nov 30, 2026 20:20 UTC
    Code Review
    api/src/main/java/org/apache/iceberg/expressions/Binder.java:1
    @@ -48,16 +48,101 @@ private Binder() {}
        * <p>The expression cannot contain references that are already bound, or an {@link
        * IllegalStateException} will be thrown.
        *
    +   * <p>A value expression is bound as by {@link #bind(StructType, ValueExpression, boolean)}.
    +   *
        * @param struct The {@link StructType struct type} to resolve references by name.
        * @param expr An {@link Expression expression} to rewrite with bound references.
        * @param caseSensitive A boolean flag to control whether the bind should enforce case
        *     sensitivity.
        * @return the expression rewritten with bound references
        * @throws ValidationException if literals do not match bound references
        * @throws IllegalStateException if any references are already bound
    +   * @throws UnsupportedOperationException if the expression calls a function outside {@code
    +   *     iceberg_functions}; use {@link #bind(StructType, Expression, boolean, FunctionBinder)} to
    +   *     resolve other functions
        */
       public static Expression bind(StructType struct, Expression expr, boolean caseSensitive) {
    -    return ExpressionVisitors.visit(expr, new BindVisitor(struct, caseSensitive));
    +    return bind(struct, expr, caseSensitive, IcebergFunctions.INSTANCE);
    +  }
    +
    +  /**
    +   * Replaces all unbound references with bound references to fields in the given struct, using a
    +   * function binder to resolve function calls.
    +   *
    +   * <p>Behaves like {@link #bind(StructType, Expression, boolean)} for everything other than
    +   * function calls.
    +   *
    +   * @param struct The {@link StructType struct type} to resolve references by name.
    +   * @param expr An {@link Expression expression} to rewrite with bound references.
    +   * @param caseSensitive A boolean flag to control whether the bind should enforce case
    +   *     sensitivity.
    +   * @param functionBinder a {@link FunctionBinder} used to resolve functions
    +   * @return the expression rewritten with bound references
    +   * @throws ValidationException if literals do not match bound references
    +   * @throws IllegalStateException if any references are already bound
    +   */
    +  public static Expression bind(

    I would make this package-private until we want to expose FunctionBinder.

    rdblue rdblue commented on code on Nov 30, 2026 20:57 UTC
    Code Review
    api/src/main/java/org/apache/iceberg/expressions/Expression.java:1
    @@ -124,7 +124,10 @@ public Operation flipLR() {
       }
     
       /** Returns the operation for an expression node. */
    -  Operation op();
    +  default Operation op() {

    What is the downside to having an op for expression types? The addition of an APPLY operation makes sense to me, and I don't think it would be confusing to have ID_REF, NAMED_REF, and LITERAL as well.

    The operation here allows us to collapse implementations into the same class (for instance, Predicate with multiple operations) and to be able to dispatch calls in visitors without going through a series of instanceof checks. Those uses are still valid so I don't think I would make this change.

    rdblue rdblue commented on code on Nov 30, 2026 21:04 UTC
    Code Review
    api/src/main/java/org/apache/iceberg/expressions/Literal.java:1
    @@ -74,6 +75,22 @@ static Literal<BigDecimal> of(BigDecimal value) {
       /** Returns the value wrapped by this literal. */
       T value();
     
    +  /**
    +   * Returns the Iceberg type of this literal's value.
    +   *
    +   * @return the type of this literal
    +   * @throws UnsupportedOperationException if this literal does not have a type
    +   */
    +  @Override
    +  default Type type() {
    +    throw new UnsupportedOperationException(getClass().getSimpleName() + " does not have a type");

    Looks like the only literals that don't have a type are AboveMax and BelowMin. That's reasonable because those are not exposed outside of the package and are used for multiple types.

    rdblue rdblue commented on code on Nov 30, 2026 21:26 UTC
    Code Review
    api/src/main/java/org/apache/iceberg/expressions/UnboundPredicate.java:1
    @@ -45,30 +47,71 @@ public class UnboundPredicate<T> extends Predicate<T, UnboundTerm<T>>
       UnboundPredicate(Operation op, UnboundTerm<T> term) {
         super(op, term);
         this.literals = null;
    +    this.left = null;

    UnboundTerm is a ValueExpression, so I would expect this to set left to term and treat term like a ValueExpression in all cases. Otherwise, it looks like this keeps the existing term handling separate, which doesn't seem right.

    rdblue rdblue commented on code on Nov 30, 2026 21:54 UTC
    Code Review
    api/src/main/java/org/apache/iceberg/expressions/Predicate.java:1
    @@ -30,12 +30,30 @@ public abstract class Predicate<T, C extends Term> implements Expression {
         this.term = term;
       }
     
    +  Predicate(Operation op) {
    +    this.op = op;
    +    this.term = null;
    +  }
    +
       @Override
       public Operation op() {
         return op;
       }
     
    +  /**
    +   * Returns the term tested by this predicate.
    +   *
    +   * <p>Returns null when the predicate's operands are value expressions rather than a term.
    +   */
       public C term() {
         return term;
       }
    +
    +  C requireTerm() {
    +    if (term == null) {
    +      throw new UnsupportedOperationException("Predicate does not have a term: " + this);
    +    }
    +
    +    return term;

    We typically avoid validations that are used inline like this.

    I am also not sure why this is needed. It looks like the intended behavior is to make term and ref throw an exception when they are accessed, but then this is called from BoundPredicate#eval and BoundPredicate#test, both of which do not require a term since a predicate could be created using a ValueExpression instead.

    I think that BoundPredicate (and probably Predicate) needs to be updated so to use a term replacement (maybe child) natively, then implement term when possible.

    Reviewers
    Assignees
    danielcweeks danielcweeks
    Labels
    risk: low confidence: 85.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 78.5% (HIGH) · Higher = larger blast radius
    78.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 85.5% · Lower = rushed or shallow review
    85.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 11.4% · Safe to merge
    11.4%
    Repository