devanshu0987 commented on code in PR #20099:
URL: https://github.com/apache/datafusion/pull/20099#discussion_r2754526208


##########
datafusion/functions/src/math/floor.rs:
##########
@@ -310,9 +351,45 @@ fn int_preimage_bounds<I: CheckedAdd + One + Copy>(n: I) 
-> Option<(I, I)> {
     Some((n, upper))
 }
 
+/// Compute preimage bounds for floor function on decimal types.
+/// For floor(x) = n, the preimage is [n, n+1).
+/// Returns None if:
+/// - The value has a fractional part (floor always returns integers)
+/// - Adding 1 would overflow
+fn decimal_preimage_bounds<D: DecimalType>(
+    value: D::Native,
+    precision: u8,
+    scale: i8,
+) -> Option<(D::Native, D::Native)>
+where
+    D::Native: DecimalCast + ArrowNativeTypeOp + std::ops::Rem<Output = 
D::Native>,
+{
+    // Use rescale_decimal to compute "1" at target scale (avoids manual pow)
+    // Convert integer 1 (scale=0) to the target scale
+    let one_scaled: D::Native = rescale_decimal::<D, D>(
+        D::Native::ONE, // value = 1
+        1,              // input_precision = 1
+        0,              // input_scale = 0 (integer)
+        precision,      // output_precision
+        scale,          // output_scale
+    )?;
+
+    // floor always returns an integer, so if value has a fractional part, 
there's no solution
+    // Check: value % one_scaled != 0 means fractional part exists
+    if scale > 0 && value % one_scaled != D::Native::ZERO {

Review Comment:
   Hi, the logic here come from the idea that `floor(x) = 1.3` has no pre-image 
solution. We do not want to optimize it via this.
   
   floor(x) will always return a floor data type but it will actually be an 
integer.
   
   ```
       // floor always returns an integer, so if n has a fractional part, 
there's no solution
       if n.fract() != F::zero() {
           return None;
       }
   ```
   
   Same logic is carried over here for Decimal.
   
   `scale <= 0` is effectively an integer and hence only possibility is to 
check for overflow which we check in the next line.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to