Skip to content

Bind expressions before optimization - #9514

Closed
mhk197 wants to merge 6 commits into
developfrom
mk/bound-expression-optimize
Closed

mhk197 wants to merge 6 commits into
developfrom
mk/bound-expression-optimize

Conversation

@mhk197

@mhk197 mhk197 commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Instead of optimizing expressions and then binding, first bind and then optimize.

There are many benefits to this, including:

  • We only optimize well-typed expression trees (we dont waste work on invalid trees that fail at execution time). Rewrites cannot obscure typing errors.
  • We no longer have multiple return_dtype walks that recurse through the entire expression tree, and we dont need an optimizer-owned dtype cache (SimplifyCache).
  • Few APIs and duplicate rules
  • Preparation for lambdas and variables. Variables must be typed before optimization. The optimizer can treat lambda scopes as opaque.
  • We can recursively optimize ONCE rather than incrementally calling expression.optimize on construction of every tree node.

@codspeed

codspeed Bot commented Aug 20, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 9.77%

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

⚡ 1 improved benchmark
❌ 7 regressed benchmarks
✅ 1973 untouched benchmarks
⏩ 54 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Simulation optimize_lookup_predicate[ids=1, shape=balanced_or] 46.3 µs 59.5 µs -22.17%
❌ Simulation optimize_lookup_predicate[ids=64, shape=in_list] 44.5 µs 55.8 µs -20.37%
❌ Simulation optimize_lookup_predicate[ids=128, shape=in_list] 44.7 µs 55.6 µs -19.74%
❌ Simulation optimize_lookup_predicate[ids=256, shape=in_list] 45.8 µs 56.6 µs -19.02%
❌ Simulation optimize_lookup_predicate[ids=192, shape=in_list] 45.5 µs 55.8 µs -18.51%
❌ Simulation optimize_lookup_predicate[ids=16, shape=in_list] 44.7 µs 54.7 µs -18.21%
❌ Simulation optimize_lookup_predicate[ids=16, shape=balanced_or] 86.8 µs 100.8 µs -13.9%
⚡ Simulation optimize_lookup_predicate[ids=1, shape=in_list] 151.2 µs 79.6 µs +90.02%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing mk/bound-expression-optimize (b7074f4) with develop (e4b3421)

Open in CodSpeed

Footnotes

  1. 54 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@mhk197
mhk197 force-pushed the mk/bound-expression-optimize branch from b9734fa to 5e71312 Compare August 21, 2026 01:41
@mhk197 mhk197 added the changelog/chore A trivial change label Aug 21, 2026
@mhk197
mhk197 marked this pull request as ready for review August 24, 2026 13:38
@mhk197
mhk197 requested a review from joseph-isaacs August 24, 2026 13:39
// expression.
let mut conjuncts = conjuncts(&expr);
/// Look for `x >= a AND x < b` and replace it with a bound `between` expression.
pub(crate) fn find_between_bound(expr: BoundExpression) -> BoundExpression {

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.

do this on the unbound expression no?


Ok(PartitionedExpr {
root: root.optimize_recursive(&root_scope)?,
root,

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.

this is a change are we sure we want this.

Maybe we do?

@joseph-isaacs

Copy link
Copy Markdown
Contributor

Where do we run typed and untyped expression simplify (reductions)

mhk197 added 5 commits August 24, 2026 09:55
Signed-off-by: Matt Katz <mhkatz97@gmail.com>
Signed-off-by: Matt Katz <mhkatz97@gmail.com>
Signed-off-by: Matt Katz <mhkatz97@gmail.com>
Signed-off-by: Matt Katz <mhkatz97@gmail.com>
Signed-off-by: Matt Katz <mhkatz97@gmail.com>
@mhk197
mhk197 force-pushed the mk/bound-expression-optimize branch from 35cc910 to e4a42c7 Compare August 24, 2026 14:09
Signed-off-by: Matt Katz <mhkatz97@gmail.com>
@github-actions

Copy link
Copy Markdown
Contributor

This PR has been marked as stale because it has been open for 14 days with no activity. Please comment or remove the stale label if you wish to keep it active, otherwise it will be closed in 7 days

@github-actions github-actions Bot added the stale This PR is stale and will be auto-closed soon label Sep 10, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR was closed because it has been inactive for 7 days since being marked as stale.

@github-actions github-actions Bot closed this Sep 19, 2026
gatesn pushed a commit that referenced this pull request Sep 25, 2026
Supersedes #9514.

Optimization now runs only on `BoundExpression`s: callers bind first,
then optimize. This removes the optimizer-owned dtype cache, rules can
read dtypes straight off the tree, and it unblocks lambdas/variables,
which must be typed before optimization.

## Changes

- `BoundExpression::optimize` and `optimize_recursive` replace the
`Expression` versions. Traversal and the fixpoint loop are unchanged.
- `ScalarFnVTable::simplify` now takes a `&BoundExpression`, and
`simplify_untyped` is merged into it. `SimplifyCtx` and `SimplifyCache`
are removed.
- `ExpressionReduceNode` keeps its name and `reduce_expression`
dispatch, but now borrows a bound tree.
- Rules ported: binary, case_when, cast, fill_null, mask, select, zip,
and `find_between`. `GetItem::simplify_untyped` is removed because it
duplicated `GetItem::reduce`. If the rebuilt `Between` fails to
type-check, `find_between` now skips the rewrite instead of producing a
tree that fails to bind.
- Callers change from `expr.optimize_recursive(dtype)?.bind(dtype)` to
`expr.bind(dtype)?.optimize_recursive()`.
- The unbound `expr::transform::partition` is deleted. It was the last
user of the unbound optimizer and has no callers; layouts use
`bound_partition`. #9993 deletes it too, so whichever PR lands second
will have a trivial conflict.

## API changes

- `Expression::optimize(scope)` / `optimize_recursive(scope)` become
`BoundExpression::optimize()` / `optimize_recursive()`.
- `ScalarFnVTable::simplify(options, &BoundExpression)` replaces
`simplify(options, &Expression, &dyn SimplifyCtx)` and
`simplify_untyped`.
- `ExpressionReduceNode::new(&BoundExpression)` drops the scope
argument.
- `partition`, `partition_annotations` and `PartitionedExpr` are
removed.

## Behavior

I checked this with a differential corpus of 30,018 generated
expressions targeting every rule, comparing develop's `optimize + bind`
against the new `bind + optimize` and evaluating before/after on data:

- 28,878 produce identical output.
- 131 panicked on develop (`Mask` simplify called `vortex_expect` on a
null mask literal) and now fail cleanly at bind time.
- 941 have ill-typed inputs that develop accepted only because a rule
removed the ill-typed subtree before binding. They now error.
- 68 differ because of a develop bug. `SimplifyCache` keys dtypes by
`Expression`, and `Scalar` equality ignores nullability, so develop
reused the wrong cached dtype and dropped `fill_null`s it should have
kept.

No well-typed expression regressed.

Existing bugs found, not addressed here:
- The `CASE WHEN`→`fill_null` rewrite is unexecutable for utf8.
- `select([], nullable_struct)` drops the struct's validity.
- `CaseWhen` execution can return a result that is non-nullable when its
dtype is nullable.
- `and`/`or`/`zip` literal rules can change the result's nullability.

## Performance

The optimizer benchmarks bind outside the timed loop and measure only
`optimize_recursive`, the same work develop's benchmarks time. Local
medians:

| case | develop | this PR |
|---|---|---|
| `or_chain` 200 | 17.5 µs | 18.6 µs |
| `balanced_or` 64–256 ids | 6.2–23.0 µs | 6.1–23.5 µs |
| `balanced_or` 1 id | 0.79 µs | 0.94 µs |
| `in_list` | ~0.77 µs | ~0.95 µs |

Small predicates pay a constant ~0.15–0.2 µs in the optimizer, almost
all of it in `find_between`, which now type-checks the `and`/`Between`
nodes it rebuilds (0.56 µs vs 0.34 µs). Across the full `bind +
optimize` pipeline that callers run, the gap is ~0.3–0.45 µs, because
binding also types the comparisons `find_between` later replaces.
Closing it would need an unchecked `BoundExpression` constructor. Open
question.

## Testing

- `cargo nextest run -p vortex-array -p vortex-layout -p vortex-file -p
vortex -p vortex-datafusion -p vortex-duckdb`
- `cargo test --doc -p vortex -p vortex-array`
- `cargo clippy --all-targets --all-features -- -D warnings` on all
changed crates and `fuzz/file_io`
- `cargo check --workspace --all-targets`

Not run: Python tests. `vortex-cuda-ffi` was not type-checked because it
is compiled out on macOS.

Signed-off-by: Matt Katz <mhkatz97@gmail.com>
@robert3005
robert3005 deleted the mk/bound-expression-optimize branch November 9, 2026 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog/chore A trivial change stale This PR is stale and will be auto-closed soon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants