Skip to content

Apache DataFusion SQL Query Engine

Return None for cardinality overflow (#22309)

jj.leec48e9936033f
diff --git a/datafusion/expr-common/src/interval_arithmetic.rs b/datafusion/expr-common/src/interval_arithmetic.rs
index e2f8198c92..51858be538 100644
--- a/datafusion/expr-common/src/interval_arithmetic.rs
+++ b/datafusion/expr-common/src/interval_arithmetic.rs
@@ -944,7 +944,7 @@ impl Interval {
// Cardinality calculations are not implemented for this data type yet:
None
}
- .map(|result| result + 1)
+ .and_then(|result| result.checked_add(1))
}
/// Reflects an [`Interval`] around the point zero.
@@ -4157,7 +4157,22 @@ mod tests {
ScalarValue::TimestampNanosecond(Some(2_000_000_000), None),
)?;
assert_eq!(interval.cardinality().unwrap(), 1_000_000_001);
+ Ok(())
+ }
+
+ #[test]
+ fn test_cardinality_full_integer_range_does_not_overflow() -> Result<()> {
+ let interval = Interval::try_new(
+ ScalarValue::Int64(Some(i64::MIN)),
+ ScalarValue::Int64(Some(i64::MAX)),
+ )?;
+ assert_eq!(interval.cardinality(), None);
+ let interval = Interval::try_new(
+ ScalarValue::UInt64(Some(0)),
+ ScalarValue::UInt64(Some(u64::MAX)),
+ )?;
+ assert_eq!(interval.cardinality(), None);
Ok(())
}
diff --git a/datafusion/sqllogictest/test_files/select.slt b/datafusion/sqllogictest/test_files/select.slt
index 3e97dc4588..762f5c1133 100644
--- a/datafusion/sqllogictest/test_files/select.slt
+++ b/datafusion/sqllogictest/test_files/select.slt
@@ -960,6 +960,12 @@ physical_plan
01)ProjectionExec: expr=[c1@0 >= 2 AND c1@0 <= 3 as select_between_data.c1 BETWEEN Int64(2) AND Int64(3)]
02)--DataSourceExec: partitions=1, partition_sizes=[1]
+# regression test: full i64 BETWEEN bounds should not overflow
+query I
+SELECT * FROM (VALUES (1)) AS t(x)
+WHERE x BETWEEN -9223372036854775808 AND 9223372036854775807
+----
+1
# TODO: query_get_indexed_field