-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Implement preimage for floor function to enable predicate pushdown #20059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devanshu0987
wants to merge
3
commits into
apache:main
Choose a base branch
from
devanshu0987:floor-preimage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+240
−2
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,12 +25,15 @@ use arrow::datatypes::{ | |
| }; | ||
| use datafusion_common::{Result, ScalarValue, exec_err}; | ||
| use datafusion_expr::interval_arithmetic::Interval; | ||
| use datafusion_expr::preimage::PreimageResult; | ||
| use datafusion_expr::simplify::SimplifyContext; | ||
| use datafusion_expr::sort_properties::{ExprProperties, SortProperties}; | ||
| use datafusion_expr::{ | ||
| Coercion, ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature, | ||
| TypeSignature, TypeSignatureClass, Volatility, | ||
| Coercion, ColumnarValue, Documentation, Expr, ScalarFunctionArgs, ScalarUDFImpl, | ||
| Signature, TypeSignature, TypeSignatureClass, Volatility, | ||
| }; | ||
| use datafusion_macros::user_doc; | ||
| use num_traits::{CheckedAdd, Float, One}; | ||
|
|
||
| use super::decimal::{apply_decimal_op, floor_decimal_value}; | ||
|
|
||
|
|
@@ -200,7 +203,242 @@ impl ScalarUDFImpl for FloorFunc { | |
| Interval::make_unbounded(&data_type) | ||
| } | ||
|
|
||
| /// Compute the preimage for floor function. | ||
| /// | ||
| /// For `floor(x) = N`, the preimage is `x >= N AND x < N + 1` | ||
| /// because floor(x) = N for all x in [N, N+1). | ||
| /// | ||
| /// This enables predicate pushdown optimizations, transforming: | ||
| /// `floor(col) = 100` into `col >= 100 AND col < 101` | ||
| fn preimage( | ||
| &self, | ||
| args: &[Expr], | ||
| lit_expr: &Expr, | ||
| _info: &SimplifyContext, | ||
| ) -> Result<PreimageResult> { | ||
| // floor takes exactly one argument | ||
| if args.len() != 1 { | ||
| return Ok(PreimageResult::None); | ||
| } | ||
|
|
||
| let arg = args[0].clone(); | ||
|
|
||
| // Extract the literal value being compared to | ||
| let Expr::Literal(lit_value, _) = lit_expr else { | ||
| return Ok(PreimageResult::None); | ||
| }; | ||
|
|
||
| // Compute lower bound (N) and upper bound (N + 1) using helper functions | ||
| let Some((lower, upper)) = (match lit_value { | ||
| // Floating-point types | ||
| ScalarValue::Float64(Some(n)) => float_preimage_bounds(*n).map(|(lo, hi)| { | ||
| ( | ||
| ScalarValue::Float64(Some(lo)), | ||
| ScalarValue::Float64(Some(hi)), | ||
| ) | ||
| }), | ||
| ScalarValue::Float32(Some(n)) => float_preimage_bounds(*n).map(|(lo, hi)| { | ||
| ( | ||
| ScalarValue::Float32(Some(lo)), | ||
| ScalarValue::Float32(Some(hi)), | ||
| ) | ||
| }), | ||
|
|
||
| // Integer types | ||
| ScalarValue::Int8(Some(n)) => int_preimage_bounds(*n).map(|(lo, hi)| { | ||
| (ScalarValue::Int8(Some(lo)), ScalarValue::Int8(Some(hi))) | ||
| }), | ||
| ScalarValue::Int16(Some(n)) => int_preimage_bounds(*n).map(|(lo, hi)| { | ||
| (ScalarValue::Int16(Some(lo)), ScalarValue::Int16(Some(hi))) | ||
| }), | ||
| ScalarValue::Int32(Some(n)) => int_preimage_bounds(*n).map(|(lo, hi)| { | ||
| (ScalarValue::Int32(Some(lo)), ScalarValue::Int32(Some(hi))) | ||
| }), | ||
| ScalarValue::Int64(Some(n)) => int_preimage_bounds(*n).map(|(lo, hi)| { | ||
| (ScalarValue::Int64(Some(lo)), ScalarValue::Int64(Some(hi))) | ||
| }), | ||
|
|
||
| // Unsupported types | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. floor also supports decimal types. Should we add those here? |
||
| _ => None, | ||
| }) else { | ||
| return Ok(PreimageResult::None); | ||
| }; | ||
|
|
||
| Ok(PreimageResult::Range { | ||
| expr: arg, | ||
| interval: Box::new(Interval::try_new(lower, upper)?), | ||
| }) | ||
| } | ||
|
|
||
| fn documentation(&self) -> Option<&Documentation> { | ||
| self.doc() | ||
| } | ||
| } | ||
|
|
||
| // ============ Helper functions for preimage bounds ============ | ||
|
|
||
| /// Compute preimage bounds for floor function on floating-point types. | ||
| /// For floor(x) = n, the preimage is [n, n+1). | ||
| /// Returns None if the value is non-finite or would lose precision. | ||
| fn float_preimage_bounds<F: Float>(n: F) -> Option<(F, F)> { | ||
| let one = F::one(); | ||
| // Check for non-finite values (infinity, NaN) or precision loss at extreme values | ||
| if !n.is_finite() || n + one <= n { | ||
| return None; | ||
| } | ||
| Some((n, n + one)) | ||
| } | ||
|
|
||
| /// Compute preimage bounds for floor function on integer types. | ||
| /// For floor(x) = n, the preimage is [n, n+1). | ||
| /// Returns None if adding 1 would overflow. | ||
| fn int_preimage_bounds<I: CheckedAdd + One + Copy>(n: I) -> Option<(I, I)> { | ||
| let upper = n.checked_add(&I::one())?; | ||
| Some((n, upper)) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use datafusion_expr::col; | ||
|
|
||
| /// Helper to test valid preimage cases that should return a Range | ||
| fn assert_preimage_range( | ||
| input: ScalarValue, | ||
| expected_lower: ScalarValue, | ||
| expected_upper: ScalarValue, | ||
| ) { | ||
| let floor_func = FloorFunc::new(); | ||
| let args = vec![col("x")]; | ||
| let lit_expr = Expr::Literal(input.clone(), None); | ||
| let info = SimplifyContext::default(); | ||
|
|
||
| let result = floor_func.preimage(&args, &lit_expr, &info).unwrap(); | ||
|
|
||
| match result { | ||
| PreimageResult::Range { expr, interval } => { | ||
| assert_eq!(expr, col("x")); | ||
| assert_eq!(interval.lower().clone(), expected_lower); | ||
| assert_eq!(interval.upper().clone(), expected_upper); | ||
| } | ||
| PreimageResult::None => { | ||
| panic!("Expected Range, got None for input {input:?}") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Helper to test cases that should return None | ||
| fn assert_preimage_none(input: ScalarValue) { | ||
| let floor_func = FloorFunc::new(); | ||
| let args = vec![col("x")]; | ||
| let lit_expr = Expr::Literal(input.clone(), None); | ||
| let info = SimplifyContext::default(); | ||
|
|
||
| let result = floor_func.preimage(&args, &lit_expr, &info).unwrap(); | ||
| assert!( | ||
| matches!(result, PreimageResult::None), | ||
| "Expected None for input {input:?}" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_floor_preimage_valid_cases() { | ||
| // Float64 | ||
| assert_preimage_range( | ||
| ScalarValue::Float64(Some(100.0)), | ||
| ScalarValue::Float64(Some(100.0)), | ||
| ScalarValue::Float64(Some(101.0)), | ||
| ); | ||
| // Float32 | ||
| assert_preimage_range( | ||
| ScalarValue::Float32(Some(50.0)), | ||
| ScalarValue::Float32(Some(50.0)), | ||
| ScalarValue::Float32(Some(51.0)), | ||
| ); | ||
| // Int64 | ||
| assert_preimage_range( | ||
| ScalarValue::Int64(Some(42)), | ||
| ScalarValue::Int64(Some(42)), | ||
| ScalarValue::Int64(Some(43)), | ||
| ); | ||
| // Int32 | ||
| assert_preimage_range( | ||
| ScalarValue::Int32(Some(100)), | ||
| ScalarValue::Int32(Some(100)), | ||
| ScalarValue::Int32(Some(101)), | ||
| ); | ||
| // Negative values | ||
| assert_preimage_range( | ||
| ScalarValue::Float64(Some(-5.0)), | ||
| ScalarValue::Float64(Some(-5.0)), | ||
| ScalarValue::Float64(Some(-4.0)), | ||
| ); | ||
| // Zero | ||
| assert_preimage_range( | ||
| ScalarValue::Float64(Some(0.0)), | ||
| ScalarValue::Float64(Some(0.0)), | ||
| ScalarValue::Float64(Some(1.0)), | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_floor_preimage_integer_overflow() { | ||
devanshu0987 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // All integer types at MAX value should return None | ||
| assert_preimage_none(ScalarValue::Int64(Some(i64::MAX))); | ||
| assert_preimage_none(ScalarValue::Int32(Some(i32::MAX))); | ||
| assert_preimage_none(ScalarValue::Int16(Some(i16::MAX))); | ||
| assert_preimage_none(ScalarValue::Int8(Some(i8::MAX))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_floor_preimage_float_edge_cases() { | ||
| // Float64 edge cases | ||
| assert_preimage_none(ScalarValue::Float64(Some(f64::INFINITY))); | ||
| assert_preimage_none(ScalarValue::Float64(Some(f64::NEG_INFINITY))); | ||
| assert_preimage_none(ScalarValue::Float64(Some(f64::NAN))); | ||
| assert_preimage_none(ScalarValue::Float64(Some(f64::MAX))); // precision loss | ||
|
|
||
| // Float32 edge cases | ||
| assert_preimage_none(ScalarValue::Float32(Some(f32::INFINITY))); | ||
| assert_preimage_none(ScalarValue::Float32(Some(f32::NEG_INFINITY))); | ||
| assert_preimage_none(ScalarValue::Float32(Some(f32::NAN))); | ||
| assert_preimage_none(ScalarValue::Float32(Some(f32::MAX))); // precision loss | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_floor_preimage_null_values() { | ||
| assert_preimage_none(ScalarValue::Float64(None)); | ||
| assert_preimage_none(ScalarValue::Float32(None)); | ||
| assert_preimage_none(ScalarValue::Int64(None)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_floor_preimage_invalid_inputs() { | ||
| let floor_func = FloorFunc::new(); | ||
| let info = SimplifyContext::default(); | ||
|
|
||
| // Non-literal comparison value | ||
| let result = floor_func.preimage(&[col("x")], &col("y"), &info).unwrap(); | ||
| assert!( | ||
| matches!(result, PreimageResult::None), | ||
| "Expected None for non-literal" | ||
| ); | ||
|
|
||
| // Wrong argument count (too many) | ||
| let lit = Expr::Literal(ScalarValue::Float64(Some(100.0)), None); | ||
| let result = floor_func | ||
| .preimage(&[col("x"), col("y")], &lit, &info) | ||
| .unwrap(); | ||
| assert!( | ||
| matches!(result, PreimageResult::None), | ||
| "Expected None for wrong arg count" | ||
| ); | ||
|
|
||
| // Wrong argument count (zero) | ||
| let result = floor_func.preimage(&[], &lit, &info).unwrap(); | ||
| assert!( | ||
| matches!(result, PreimageResult::None), | ||
| "Expected None for zero args" | ||
| ); | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps its good to
debug_assert!here?