Skip to content

Bind expressions before optimizing - #10002

Merged
gatesn merged 1 commit into
developfrom
mk/bind-before-optimize
Sep 25, 2026
Merged

gatesn merged 1 commit into
developfrom
mk/bind-before-optimize

Conversation

@mhk197

@mhk197 mhk197 commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Supersedes #9514.

Optimization now runs only on BoundExpressions: 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. Remove unused unbound expression analysis and traversal helpers #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_nulls 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.

@codspeed

codspeed Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 9.04%

⚠️ 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.

⚠️ 3 benchmarks measured no execution time

Nothing ran under measurement, usually because the compiler removed the code under test. These results are not comparable, so they count as unchanged.

Preventing compiler optimizations

⚡ 2 improved benchmarks
❌ 7 regressed benchmarks
✅ 2119 untouched benchmarks
⏩ 437 skipped benchmarks1
🗄️ 1 archived benchmark run2

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Simulation optimize_lookup_predicate[ids=256, shape=in_list] 43 µs 79.9 µs -46.19%
❌ Simulation optimize_lookup_predicate[ids=16, shape=in_list] 43 µs 51.7 µs -16.79%
❌ Simulation optimize_lookup_predicate[ids=128, shape=in_list] 43.1 µs 50.8 µs -15.25%
❌ Simulation optimize_lookup_predicate[ids=1, shape=balanced_or] 42.5 µs 50 µs -15.01%
❌ Simulation optimize_lookup_predicate[ids=64, shape=in_list] 43.3 µs 50.7 µs -14.63%
❌ Simulation optimize_lookup_predicate[ids=192, shape=in_list] 43.2 µs 50.3 µs -14.21%
❌ Simulation optimize_lookup_predicate[ids=16, shape=balanced_or] 77.6 µs 87.1 µs -10.94%
⚡ Simulation optimize_lookup_predicate[ids=1, shape=in_list] 136.6 µs 74.5 µs +83.39%
⚡ WallTime dbp_assemble_kernel_avx512[(I128, 32768)] 18.8 µs 17 µs +10.52%
⚠️ Simulation fixed_16_advancing_ptr_safe[100] < 1 ns < 1 ns N/A
⚠️ Simulation preverify_advancing_ptr_unchecked[1000] < 1 ns < 1 ns N/A
⚠️ Simulation preverify_advancing_ptr_unchecked[10000] < 1 ns < 1 ns N/A

Tip

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


Comparing mk/bind-before-optimize (059e584) with develop (0fea4a3)

Open in CodSpeed

Footnotes

  1. 437 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. ↩

  2. 1 benchmark was run, but is now archived. If it was deleted in another branch, consider rebasing to remove it from the report. Instead if it was added back, click here to restore it. ↩

@mhk197
mhk197 added this pull request to stack #10003 September 23, 2026 14:25
@mhk197 mhk197 added changelog/chore A trivial change changelog/break A breaking API change and removed changelog/chore A trivial change labels Sep 23, 2026
@github-actions
github-actions Bot temporarily deployed to docs-preview/pr-10002 September 23, 2026 14:27 Inactive
@mhk197
mhk197 force-pushed the mk/bind-before-optimize branch from db9209a to c35e8ed Compare September 23, 2026 14:39
@github-actions
github-actions Bot temporarily deployed to docs-preview/pr-10002 September 23, 2026 14:49 Inactive
@mhk197
mhk197 removed this pull request from stack #10003 September 23, 2026 15:50
@mhk197
mhk197 changed the base branch from mk/remove-unused-unbound-expressions to develop September 23, 2026 15:50
@mhk197
mhk197 marked this pull request as ready for review September 23, 2026 15:58
@github-actions
github-actions Bot temporarily deployed to docs-preview/pr-10002 September 23, 2026 16:01 Inactive
@mhk197
mhk197 force-pushed the mk/bind-before-optimize branch from c35e8ed to 605db12 Compare September 23, 2026 16:06
@github-actions
github-actions Bot temporarily deployed to docs-preview/pr-10002 September 23, 2026 16:16 Inactive

@joseph-isaacs joseph-isaacs left a comment

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.

i though we want simplify untyped too?

let mut changed = false;

// Try simplify_untyped
if let Some(simplified) = expr.simplify_untyped_node()? {

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.

so we don't have simplfiy untyped?

}

/// Simplify the expression without type information.
pub(crate) fn simplify_untyped(&self, expr: &Expression) -> VortexResult<Option<Expression>> {

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.

?

@mhk197

mhk197 commented Sep 24, 2026

Copy link
Copy Markdown
Contributor Author

@joseph-isaacs no functional difference now if we also have dtypes, but I can keep?

@mhk197
mhk197 force-pushed the mk/bind-before-optimize branch from 605db12 to 5c985da Compare September 24, 2026 23:13
@github-actions
github-actions Bot temporarily deployed to docs-preview/pr-10002 September 24, 2026 23:21 Inactive
@joseph-isaacs

Copy link
Copy Markdown
Contributor

I think we want this for optimising an expr before binding even thought (vortex internals never use it).

@joseph-isaacs

Copy link
Copy Markdown
Contributor

do we want to merge bind and optimize (optionally)

@mhk197

mhk197 commented Sep 25, 2026

Copy link
Copy Markdown
Contributor Author

@joseph-isaacs okay what we can do then is just keep the unbound expression optimizer but just never use it internally

@gatesn

gatesn commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

I think we should move strongly in this direction inside vortex core.

And add a "vortex-expr" crate to handle unbound expressions for our "user friendly" APIs from Python or other language bindings. Host engines should never need to use the unbound API. And vortex-array also shouldn't know about it. This crate can handle coercion rules, literals, and so on as this implicit behavior can allow subtle bugs to leak in if engines use it.

Signed-off-by: Matt Katz <mhkatz97@gmail.com>
@mhk197
mhk197 force-pushed the mk/bind-before-optimize branch from 5c985da to 059e584 Compare September 25, 2026 16:21
@gatesn
gatesn enabled auto-merge (squash) September 25, 2026 16:24
@github-actions
github-actions Bot temporarily deployed to docs-preview/pr-10002 September 25, 2026 16:33 Inactive
@gatesn
gatesn merged commit c4c1bbf into develop Sep 25, 2026
90 of 91 checks passed
@gatesn
gatesn deleted the mk/bind-before-optimize branch September 25, 2026 16:41
robert3005 pushed a commit that referenced this pull request Sep 27, 2026
Replace separate eligibility and conversion checks with a schema-aware
`try_convert`, preserve SQL null semantics for IN/NOT IN, and reject
pushdown where casts, short-circuit evaluation, or nullable struct access
could differ. Re-split filters after per-file schema adaptation so
unsupported conjuncts run as DataFusion residual filters before projection
and limit. Fix v2 filter pushdown after projection swapping, and rename
`ExpressionConvertor` to `ExpressionConverter`.

Squashed onto develop, porting the bind-before-optimize change from #10002.

Signed-off-by: Adam Gutglick <adam@spiraldb.com>
Signed-off-by: "Claude" <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013wUbDvL9BL68K3YceQiuZw
robert3005 added a commit that referenced this pull request Oct 11, 2026
Develop changed `opener.rs` after this branch removed it. Apply the same
changes to the morsel planner:

- Let `FilePruner` decide if pruning is useful (#9841).
- Share the physical Arrow schema of each file across partitions.
- Disable projection pushdown for projections with lambdas (#10215).
- Bind expressions before optimizing them (#10002).
- Explain why the limit applies after the filter (#8766).
- Use `ScanBuilder<ArrayRef>` and `RepeatedScan<ArrayRef>`, because
  develop keeps `ScanBuilder::map`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Robert Kruszewski <github@robertk.io>
robert3005 pushed a commit that referenced this pull request Oct 12, 2026
Replace separate eligibility and conversion checks with a schema-aware
`try_convert`, preserve SQL null semantics for IN/NOT IN, and reject
pushdown where casts, short-circuit evaluation, or nullable struct access
could differ. Re-split filters after per-file schema adaptation so
unsupported conjuncts run as DataFusion residual filters before projection
and limit. Fix v2 filter pushdown after projection swapping, and rename
`ExpressionConvertor` to `ExpressionConverter`.

Squashed onto develop, porting the bind-before-optimize change from #10002.

Signed-off-by: Adam Gutglick <adam@spiraldb.com>
Signed-off-by: "Claude" <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013wUbDvL9BL68K3YceQiuZw
Signed-off-by: Robert Kruszewski <github@robertk.io>

This branch was previously deployed

1 inactive deployment
docs-preview/pr-10002 — 059e5844 Deployed Sep 25, 2026 by github-actions[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog/break A breaking API change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants