Skip to content

Apache DataFusion SQL Query Engine

Minor: Add comments and clearer constructors to `Interval` (#7526)

Andrew Lamb8e2a0e6e8fc5
diff --git a/datafusion/physical-expr/src/analysis.rs b/datafusion/physical-expr/src/analysis.rs
index 7d9163763e..d3fcdc11ad 100644
--- a/datafusion/physical-expr/src/analysis.rs
+++ b/datafusion/physical-expr/src/analysis.rs
@@ -91,13 +91,11 @@ impl ExprBoundaries {
Self {
column: Column::new(&col, index),
interval: Interval::new(
- IntervalBound::new(
+ IntervalBound::new_closed(
stats.min_value.clone().unwrap_or(ScalarValue::Null),
- false,
),
- IntervalBound::new(
+ IntervalBound::new_closed(
stats.max_value.clone().unwrap_or(ScalarValue::Null),
- false,
),
),
distinct_count: stats.distinct_count,
diff --git a/datafusion/physical-expr/src/intervals/cp_solver.rs b/datafusion/physical-expr/src/intervals/cp_solver.rs
index 6573242664..0a090636dc 100644
--- a/datafusion/physical-expr/src/intervals/cp_solver.rs
+++ b/datafusion/physical-expr/src/intervals/cp_solver.rs
@@ -188,8 +188,8 @@ impl ExprIntervalGraphNode {
if let Some(literal) = expr.as_any().downcast_ref::<Literal>() {
let value = literal.value();
let interval = Interval::new(
- IntervalBound::new(value.clone(), false),
- IntervalBound::new(value.clone(), false),
+ IntervalBound::new_closed(value.clone()),
+ IntervalBound::new_closed(value.clone()),
);
ExprIntervalGraphNode::new_with_interval(expr, interval)
} else {
@@ -279,13 +279,13 @@ fn comparison_operator_target(
let unbounded = IntervalBound::make_unbounded(&datatype)?;
let zero = ScalarValue::new_zero(&datatype)?;
Ok(match *op {
- Operator::GtEq => Interval::new(IntervalBound::new(zero, false), unbounded),
- Operator::Gt => Interval::new(IntervalBound::new(zero, true), unbounded),
- Operator::LtEq => Interval::new(unbounded, IntervalBound::new(zero, false)),
- Operator::Lt => Interval::new(unbounded, IntervalBound::new(zero, true)),
+ Operator::GtEq => Interval::new(IntervalBound::new_closed(zero), unbounded),
+ Operator::Gt => Interval::new(IntervalBound::new_open(zero), unbounded),
+ Operator::LtEq => Interval::new(unbounded, IntervalBound::new_closed(zero)),
+ Operator::Lt => Interval::new(unbounded, IntervalBound::new_open(zero)),
Operator::Eq => Interval::new(
- IntervalBound::new(zero.clone(), false),
- IntervalBound::new(zero, false),
+ IntervalBound::new_closed(zero.clone()),
+ IntervalBound::new_closed(zero),
),
_ => unreachable!(),
})
diff --git a/datafusion/physical-expr/src/intervals/interval_aritmetic.rs b/datafusion/physical-expr/src/intervals/interval_aritmetic.rs
index 37b0ad433b..3f72ef588c 100644
--- a/datafusion/physical-expr/src/intervals/interval_aritmetic.rs
+++ b/datafusion/physical-expr/src/intervals/interval_aritmetic.rs
@@ -32,12 +32,13 @@ use datafusion_common::{exec_err, internal_err, DataFusionError, Result, ScalarV
use datafusion_expr::type_coercion::binary::get_result_type;
use datafusion_expr::Operator;
-/// This type represents a single endpoint of an [`Interval`]. An endpoint can
-/// be open or closed, denoting whether the interval includes or excludes the
-/// endpoint itself.
+/// This type represents a single endpoint of an [`Interval`]. An
+/// endpoint can be open (does not include the endpoint) or closed
+/// (includes the endpoint).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IntervalBound {
pub value: ScalarValue,
+ /// If true, interval does not include `value`
pub open: bool,
}
@@ -47,6 +48,18 @@ impl IntervalBound {
IntervalBound { value, open }
}
+ /// Creates a new "open" interval (does not include the `value`
+ /// bound)
+ pub const fn new_open(value: ScalarValue) -> IntervalBound {
+ IntervalBound::new(value, true)
+ }
+
+ /// Creates a new "closed" interval (includes the `value`
+ /// bound)
+ pub const fn new_closed(value: ScalarValue) -> IntervalBound {
+ IntervalBound::new(value, false)
+ }
+
/// This convenience function creates an unbounded interval endpoint.
pub fn make_unbounded<T: Borrow<DataType>>(data_type: T) -> Result<Self> {
ScalarValue::try_from(data_type.borrow()).map(|v| IntervalBound::new(v, true))
@@ -336,7 +349,7 @@ impl Interval {
/// Decide if this interval is certainly equal to, possibly equal to,
/// or can't be equal to `other` by returning [true, true],
- /// [false, true] or [false, false] respectively.
+ /// [false, true] or [false, false] respectively.
pub(crate) fn equal<T: Borrow<Interval>>(&self, other: T) -> Interval {
let rhs = other.borrow();
let flags = if !self.lower.is_unbounded()
@@ -438,18 +451,18 @@ impl Interval {
}
pub const CERTAINLY_FALSE: Interval = Interval {
- lower: IntervalBound::new(ScalarValue::Boolean(Some(false)), false),
- upper: IntervalBound::new(ScalarValue::Boolean(Some(false)), false),
+ lower: IntervalBound::new_closed(ScalarValue::Boolean(Some(false))),
+ upper: IntervalBound::new_closed(ScalarValue::Boolean(Some(false))),
};
pub const UNCERTAIN: Interval = Interval {
- lower: IntervalBound::new(ScalarValue::Boolean(Some(false)), false),
- upper: IntervalBound::new(ScalarValue::Boolean(Some(true)), false),
+ lower: IntervalBound::new_closed(ScalarValue::Boolean(Some(false))),
+ upper: IntervalBound::new_closed(ScalarValue::Boolean(Some(true))),
};
pub const CERTAINLY_TRUE: Interval = Interval {
- lower: IntervalBound::new(ScalarValue::Boolean(Some(true)), false),
- upper: IntervalBound::new(ScalarValue::Boolean(Some(true)), false),
+ lower: IntervalBound::new_closed(ScalarValue::Boolean(Some(true))),
+ upper: IntervalBound::new_closed(ScalarValue::Boolean(Some(true))),
};
/// Returns the cardinality of this interval, which is the number of all