Skip to content

feat: convert physical filter expressions to Iceberg predicates - #1

Closed
Perseus wants to merge 2 commits into
mainfrom
iceberg-dyn-filter/d1-predicate-converter
Closed

Perseus wants to merge 2 commits into
mainfrom
iceberg-dyn-filter/d1-predicate-converter

Conversation

@Perseus

@Perseus Perseus commented Oct 30, 2026 •

Copy link
Copy Markdown
Owner

Why

The Iceberg data sources (iceberg/src/lazy/data_source.rs, iceberg/src/eager/data_source.rs) currently reject every filter DataFusion pushes into them (PushedDown::No). As a result, dynamic filters, such as the min/max bounds and IN lists that a hash join builds from its build side, are never used to prune Iceberg scans. iceberg-rust's ArrowReader can already prune row groups, pages and rows with an iceberg::expr::Predicate. What's missing is a way to turn a DataFusion physical filter expression into such a predicate.

This PR adds that converter. Nothing calls it yet; follow-up PRs will wire it into the Iceberg data sources.

What it adds

iceberg/src/common/physical_expr_to_predicate.rs:

pub(crate) fn physical_expr_to_predicate(
    expr: &Arc<dyn PhysicalExpr>,
    input_schema: &arrow::datatypes::Schema, // schema `expr` is evaluated against
    schema: &iceberg::spec::Schema,          // Iceberg table schema
) -> Option<iceberg::expr::Predicate>

Conservative rule. The returned predicate matches every row for which expr could evaluate to true. It may match more rows, but never fewer. None means "no pruning possible". DataFusion still evaluates the original filter, so over-matching only costs pruning opportunity, never correctness.

Exact vs approximate. Internally, each converted subtree records whether it is exact (it matches exactly the same rows, with the same NULL semantics) or approximate (it over-matches).

  • AND keeps the children it can convert. If it drops any child, the result becomes approximate. If no child converts, the result is None.
  • OR converts only when both children convert exactly. An approximate branch is fine on its own, but combined with NOT above it, it could under-match.
  • NOT converts only when its child is exact, because negating an over-approximation gives an under-approximation.
  • Leaves (comparisons, null checks, IN lists, boolean literals) are exact when they convert.

Supported shapes

  • col op literal and literal op col for = != < <= > >=. The operator is swapped when the literal is on the left.
  • IS NULL / IS NOT NULL on a column.
  • col IN (...) → is_in. NULL items are skipped (they can never match), and an empty list becomes AlwaysFalse.
  • col NOT IN (...) → is_not_in, only when every item is a non-null, convertible literal. NOT IN with a NULL item never matches in DataFusion, so it isn't converted.
  • Literal true / false → AlwaysTrue / AlwaysFalse.
  • DynamicFilterPhysicalExpr is converted from its current value (current()), recursively. This is called per conversion, so updated filter values are picked up.

Everything else doesn't convert and is dropped when it appears under AND: casts, CASE, hash-table lookups, arithmetic, column-vs-column comparisons and unknown expressions. For a hash-join dynamic filter, the bounds and the IN list are therefore kept and the hash/CASE routing is dropped.

Columns. A Column resolves only when the input-schema field at column.index() exists and has the same name as the column, and that name exists in the Iceberg schema. The index is what DataFusion actually evaluates, so a name that disagrees with it is not trusted.

Literals. Mapped types: bool, int8/16/32 → int, int64 → long, float32/float64, utf8 / large utf8 / utf8view, date32, and timestamps (seconds and milliseconds converted exactly to microseconds with overflow checks; microseconds; nanoseconds; tz → timestamptz*). The datum's type must equal the Iceberg field's primitive type exactly. No widening or narrowing (for example, an int32 literal against a long column doesn't convert), so there's no risk of lossy coercion. Not converted:

  • NULL literals.
  • Float NaN, because NaN ordering semantics differ.
  • Float +0.0 / -0.0. DataFusion distinguishes the two, while Iceberg may treat them as equal (for example in IN sets), which could under-match.
  • Decimal, because iceberg-rust exposes no public constructor from an unscaled mantissa.
  • Binary and other types.

Testing

16 unit tests next to the implementation, using inline insta snapshots of the rendered predicate. They cover:

  • every comparison in both operand orders
  • literal type mapping and unsupported or mismatched types
  • unknown columns and non-column operands
  • column name/index mismatch
  • float zeros
  • AND partial drop
  • OR/NOT rejecting approximate children
  • null checks
  • IN and NOT IN, including NULL items
  • boolean literals
  • a nested dynamic filter before and after update()
  • a hash-join-style filter (bounds AND IN list AND a CASE routing expression)

Commands run:

  • cargo fmt --all
  • cargo clippy -p datafusion-distributed-iceberg --all-targets: no warnings in this crate
  • cargo test -p datafusion-distributed-iceberg: all pass

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.

1 participant