Skip to content

feat(expr): fold cast of a literal to a literal in Cast simplify#8962

Merged
robert3005 merged 1 commit into
developfrom
nemo/fold-cast-of-literal
Jul 24, 2026
Merged

feat(expr): fold cast of a literal to a literal in Cast simplify#8962
robert3005 merged 1 commit into
developfrom
nemo/fold-cast-of-literal

Conversation

@HarukiMoriarty

@HarukiMoriarty HarukiMoriarty commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Motivation

Pruning is shape-sensitive: prune rules pattern-match bare expression nodes and silently decline anything wrapped in a cast. For example, GeoDistancePrune (vortex-geo/src/prune/distance.rs) requires the comparison's RHS to be a bare Literal, so a filter pushed as GeoDistance(a, b) <= cast(3.19dec as f64) scans every chunk while the equivalent GeoDistance(a, b) <= lit(3.19f64) prunes. Measured in SpatialBench Q1 at SF10: 83ms vs 19ms, purely from the literal being wrapped.

Producers currently work around this by hand-folding literals themselves (vortex-duckdb's from_bound_f64 in src/convert/expr.rs; spiral-geo's st_dwithin lowering). Since ScanBuilder already runs optimize_recursive on every pushed filter (vortex-layout/src/scan/scan_builder.rs), one generic fold rule fixes every producer at once.

The change

Implement simplify_untyped for Cast: if the child is a Literal, try Scalar::cast to the target dtype and replace the node with the folded literal.

Implement Cast::simplify_untyped so cast(lit(x) as T) folds to
lit(x cast to T) at plan time. A failing scalar cast (e.g. null to a
non-nullable dtype) leaves the expression unchanged so the error still
surfaces at execution time.

Prune rules pattern-match bare Literal nodes and silently decline
cast-wrapped ones, so filters pushed with a cast around the literal
(e.g. DuckDB decimal literals bound as cast(dec as f64)) scan every
chunk. Since ScanBuilder already runs optimize_recursive on every
pushed filter, this one fold fixes all producers at once.

Signed-off-by: Nemo Yu <zyu379@wisc.edu>
@HarukiMoriarty HarukiMoriarty added the changelog/performance A performance improvement label Jul 24, 2026

@robert3005 robert3005 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.

Really need to figure out that cast registry

@robert3005
robert3005 merged commit a12c310 into develop Jul 24, 2026
82 of 83 checks passed
@robert3005
robert3005 deleted the nemo/fold-cast-of-literal branch July 24, 2026 23:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog/performance A performance improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants