feat(expr): fold cast of a literal to a literal in Cast simplify#8962
Merged
Conversation
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>
robert3005
approved these changes
Jul 24, 2026
robert3005
left a comment
Contributor
There was a problem hiding this comment.
Really need to figure out that cast registry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 bareLiteral, so a filter pushed asGeoDistance(a, b) <= cast(3.19dec as f64)scans every chunk while the equivalentGeoDistance(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_f64insrc/convert/expr.rs; spiral-geo'sst_dwithinlowering). SinceScanBuilderalready runsoptimize_recursiveon every pushed filter (vortex-layout/src/scan/scan_builder.rs), one generic fold rule fixes every producer at once.The change
Implement
simplify_untypedforCast: if the child is aLiteral, tryScalar::castto the target dtype and replace the node with the folded literal.