Skip to content

panic: interval analysis Interval::cardinality adds past u64::MAX for BETWEEN spanning full i64 range #22232

@Dandandan

Description

@Dandandan

Describe the bug

Optimizer interval analysis panics when a BETWEEN predicate spans the
full i64 range. Interval::cardinality finishes with
.map(|result| result + 1) and the + 1 overflows when the inner range
already equals u64::MAX.

To Reproduce

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

Actual behavior

thread 'main' panicked at datafusion/expr-common/src/interval_arithmetic.rs:968:23:
attempt to add with overflow

Call path: Interval::cardinality::{{closure}}cardinality_ratio
calculate_selectivityshrink_boundariesanalyze.

Expected behavior

Cardinality should saturate (e.g. u64::MAX) or return None, not panic.

Notes

Source: https://github.com/apache/datafusion/blob/53.1.0/datafusion/expr-common/src/interval_arithmetic.rs#L968

Related but distinct from closed #16736 (float subtract overflow in the
same module).

Found via LLM-guided fuzz testing on DataFusion 53.1.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions