Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add interval arithmetic for timestamp types #7758

Merged
merged 8 commits into from
Oct 9, 2023
Merged
Changes from 1 commit
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
166 changes: 166 additions & 0 deletions datafusion/physical-expr/src/intervals/cp_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::intervals::interval_aritmetic::{apply_operator, Interval};
use crate::utils::{build_dag, ExprTreeNode};
use crate::PhysicalExpr;

use arrow::compute::CastOptions;
use arrow_schema::DataType;
use datafusion_common::{DataFusionError, Result, ScalarValue};
use datafusion_expr::type_coercion::binary::get_result_type;
Expand Down Expand Up @@ -305,6 +306,17 @@ pub fn propagate_comparison(
) -> Result<(Option<Interval>, Option<Interval>)> {
let left_type = left_child.get_datatype()?;
let right_type = right_child.get_datatype()?;
if let (DataType::Timestamp(..), DataType::Timestamp(..)) = (&left_type, &right_type)
{
return propagate_comparison_timestamp(
op,
left_child,
right_child,
&left_type,
&right_type,
);
}

let parent = comparison_operator_target(&left_type, op, &right_type)?;
match (&left_type, &right_type) {
// We can not compare a Duration type with a time interval type
Expand Down Expand Up @@ -681,13 +693,41 @@ pub fn propagate_comparison_to_time_interval_at_right(
}
}

/// Propagate the constraints arising from comparison operators on
/// timestamp data types. Arithmetic on timestamps is treated specially,
/// for example subtracting two timestamps results in duration rather
/// than another timestamp. To work around this all values are converted
/// to int64 before applying the proagation operation. The results are
/// converted back to their original types before being returned.
fn propagate_comparison_timestamp(
op: &Operator,
left_child: &Interval,
right_child: &Interval,
left_type: &DataType,
right_type: &DataType,
) -> Result<(Option<Interval>, Option<Interval>)> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should this function have some additional constraint checks on the two timestamp types? Perhaps to ensure the two timestamps are actually comparable? I couldn't think of any to add, but maybe someone else can.

let cast_options = CastOptions::default();
let left_child_i64 = left_child.cast_to(&DataType::Int64, &cast_options)?;
let right_child_i64 = right_child.cast_to(&DataType::Int64, &cast_options)?;
let (left_result_i64, right_result_i64) =
propagate_comparison(op, &left_child_i64, &right_child_i64)?;
let left_result = left_result_i64
.map(|interval| interval.cast_to(left_type, &cast_options))
.transpose()?;
let right_result = right_result_i64
.map(|interval| interval.cast_to(right_type, &cast_options))
.transpose()?;
Ok((left_result, right_result))
}

#[cfg(test)]
mod tests {
use super::*;
use itertools::Itertools;

use crate::expressions::{BinaryExpr, Column};
use crate::intervals::test_utils::gen_conjunctive_numerical_expr;
use arrow::datatypes::TimeUnit;
use datafusion_common::ScalarValue;
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
Expand Down Expand Up @@ -1414,4 +1454,130 @@ mod tests {

Ok(())
}

#[test]
fn test_propagate_comparison() {
let left = Interval::new(
IntervalBound::make_unbounded(DataType::Int64).unwrap(),
IntervalBound::make_unbounded(DataType::Int64).unwrap(),
);
let right = Interval::new(
IntervalBound::new(ScalarValue::Int64(Some(1000)), false),
IntervalBound::new(ScalarValue::Int64(Some(1000)), false),
);
assert_eq!(
(
Some(Interval::new(
IntervalBound::make_unbounded(DataType::Int64).unwrap(),
IntervalBound::new(ScalarValue::Int64(Some(1000)), true)
)),
Some(Interval::new(
IntervalBound::new(ScalarValue::Int64(Some(1000)), false),
IntervalBound::new(ScalarValue::Int64(Some(1000)), false)
)),
),
propagate_comparison(&Operator::Lt, &left, &right).unwrap()
);

let left = Interval::new(
IntervalBound::make_unbounded(DataType::Timestamp(
TimeUnit::Nanosecond,
None,
))
.unwrap(),
IntervalBound::make_unbounded(DataType::Timestamp(
TimeUnit::Nanosecond,
None,
))
.unwrap(),
);
let right = Interval::new(
IntervalBound::new(ScalarValue::TimestampNanosecond(Some(1000), None), false),
IntervalBound::new(ScalarValue::TimestampNanosecond(Some(1000), None), false),
);
assert_eq!(
(
Some(Interval::new(
IntervalBound::make_unbounded(DataType::Timestamp(
TimeUnit::Nanosecond,
None
))
.unwrap(),
IntervalBound::new(
ScalarValue::TimestampNanosecond(Some(1000), None),
true
)
)),
Some(Interval::new(
IntervalBound::new(
ScalarValue::TimestampNanosecond(Some(1000), None),
false
),
IntervalBound::new(
ScalarValue::TimestampNanosecond(Some(1000), None),
false
)
)),
),
propagate_comparison(&Operator::Lt, &left, &right).unwrap()
);

let left = Interval::new(
IntervalBound::make_unbounded(DataType::Timestamp(
TimeUnit::Nanosecond,
Some("+05:00".into()),
))
.unwrap(),
IntervalBound::make_unbounded(DataType::Timestamp(
TimeUnit::Nanosecond,
Some("+05:00".into()),
))
.unwrap(),
);
let right = Interval::new(
IntervalBound::new(
ScalarValue::TimestampNanosecond(Some(1000), Some("+05:00".into())),
false,
),
IntervalBound::new(
ScalarValue::TimestampNanosecond(Some(1000), Some("+05:00".into())),
false,
),
);
assert_eq!(
(
Some(Interval::new(
IntervalBound::make_unbounded(DataType::Timestamp(
TimeUnit::Nanosecond,
Some("+05:00".into()),
))
.unwrap(),
IntervalBound::new(
ScalarValue::TimestampNanosecond(
Some(1000),
Some("+05:00".into())
),
true
)
)),
Some(Interval::new(
IntervalBound::new(
ScalarValue::TimestampNanosecond(
Some(1000),
Some("+05:00".into())
),
false
),
IntervalBound::new(
ScalarValue::TimestampNanosecond(
Some(1000),
Some("+05:00".into())
),
false
)
)),
),
propagate_comparison(&Operator::Lt, &left, &right).unwrap()
);
}
}