You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "haohuaijin (via GitHub)" <gi...@apache.org> on 2026/09/13 09:40:29 UTC

Re: [PR] fix: prevent unsafe integer interval propagation [datafusion]

haohuaijin commented on code in PR #25234:
URL: https://github.com/apache/datafusion/pull/25234#discussion_r3999298250


##########
datafusion/expr-common/src/interval_arithmetic.rs:
##########
@@ -422,9 +422,43 @@ impl Interval {
         data_type: &DataType,
         cast_options: &CastOptions,
     ) -> Result<Self> {
+        let source_type = self.data_type();
+        // An unbounded integer endpoint still has a finite limit imposed by its
+        // type. Preserve that limit when widening so subsequent arithmetic can
+        // prove that it does not overflow the destination type.
+        use DataType::{Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64};
+        let widening_integer_cast = matches!(
+            (&source_type, data_type),
+            (Int8, Int16 | Int32 | Int64)
+                | (Int16, Int32 | Int64)
+                | (Int32, Int64)
+                | (UInt8, UInt16 | UInt32 | UInt64 | Int16 | Int32 | Int64)
+                | (UInt16, UInt32 | UInt64 | Int32 | Int64)
+                | (UInt32, UInt64 | Int64)
+        );
+        let lower = if widening_integer_cast && self.lower.is_null() {
+            get_extreme_value!(
+                MIN,
+                MIN_DECIMAL128_FOR_EACH_PRECISION,
+                MIN_DECIMAL256_FOR_EACH_PRECISION,
+                &source_type
+            )
+        } else {
+            self.lower.clone()
+        };
+        let upper = if widening_integer_cast && self.upper.is_null() {
+            get_extreme_value!(
+                MAX,
+                MAX_DECIMAL128_FOR_EACH_PRECISION,
+                MAX_DECIMAL256_FOR_EACH_PRECISION,
+                &source_type
+            )
+        } else {
+            self.upper.clone()
+        };

Review Comment:
   Preserve source type bounds during integer widening casts (e.g. `UInt32` to `Int64`), allowing overflow checks to recognize safe arithmetic and retain valid optimizations. like the test case in https://github.com/apache/datafusion/pull/25234/changes#diff-4e8dde04785dd86931aa4ccbab316d31d7a66eb6f1126721fcf15906919a8f98R240-R286



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org