Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion datafusion/expr-common/src/interval_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(())
}

Expand Down
6 changes: 6 additions & 0 deletions datafusion/sqllogictest/test_files/select.slt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

query I
SELECT * FROM (VALUES (1)) AS t(x)
WHERE x BETWEEN -9223372036854775808 AND 9223372036854775807
----
1

# TODO: query_get_indexed_field

Expand Down
Loading