Skip to content

Commit

Permalink
Fix Duration vs Interval comparisons and Interval as LHS (#11876)
Browse files Browse the repository at this point in the history
* fix duration vs. interval and interval as LHS

* add more operators to "interval vs. duration comparison" slt tests
  • Loading branch information
samuelcolvin committed Aug 8, 2024
1 parent 0ce6d16 commit b9bf6c9
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
4 changes: 3 additions & 1 deletion datafusion/expr/src/type_coercion/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,9 @@ fn temporal_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataTyp
use arrow::datatypes::TimeUnit::*;

match (lhs_type, rhs_type) {
(Interval(_), Interval(_)) => Some(Interval(MonthDayNano)),
(Interval(_) | Duration(_), Interval(_) | Duration(_)) => {
Some(Interval(MonthDayNano))
}
(Date64, Date32) | (Date32, Date64) => Some(Date64),
(Timestamp(_, None), Date64) | (Date64, Timestamp(_, None)) => {
Some(Timestamp(Nanosecond, None))
Expand Down
6 changes: 6 additions & 0 deletions datafusion/sql/src/expr/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
let df_op = match op {
BinaryOperator::Plus => Operator::Plus,
BinaryOperator::Minus => Operator::Minus,
BinaryOperator::Eq => Operator::Eq,
BinaryOperator::NotEq => Operator::NotEq,
BinaryOperator::Gt => Operator::Gt,
BinaryOperator::GtEq => Operator::GtEq,
BinaryOperator::Lt => Operator::Lt,
BinaryOperator::LtEq => Operator::LtEq,
_ => {
return not_impl_err!("Unsupported interval operator: {op:?}");
}
Expand Down
31 changes: 31 additions & 0 deletions datafusion/sql/tests/cases/plan_to_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,3 +549,34 @@ fn test_pretty_roundtrip() -> Result<()> {

Ok(())
}

fn sql_round_trip(query: &str, expect: &str) {
let statement = Parser::new(&GenericDialect {})
.try_with_sql(query)
.unwrap()
.parse_statement()
.unwrap();

let context = MockContextProvider::default();
let sql_to_rel = SqlToRel::new(&context);
let plan = sql_to_rel.sql_statement_to_plan(statement).unwrap();

let roundtrip_statement = plan_to_sql(&plan).unwrap();
assert_eq!(roundtrip_statement.to_string(), expect);
}

#[test]
fn test_interval_lhs_eq() {
sql_round_trip(
"select interval '2 seconds' = interval '2 seconds'",
"SELECT (INTERVAL '0 YEARS 0 MONS 0 DAYS 0 HOURS 0 MINS 2.000000000 SECS' = INTERVAL '0 YEARS 0 MONS 0 DAYS 0 HOURS 0 MINS 2.000000000 SECS')",
);
}

#[test]
fn test_interval_lhs_lt() {
sql_round_trip(
"select interval '2 seconds' < interval '2 seconds'",
"SELECT (INTERVAL '0 YEARS 0 MONS 0 DAYS 0 HOURS 0 MINS 2.000000000 SECS' < INTERVAL '0 YEARS 0 MONS 0 DAYS 0 HOURS 0 MINS 2.000000000 SECS')",
);
}
62 changes: 62 additions & 0 deletions datafusion/sqllogictest/test_files/timestamps.slt
Original file line number Diff line number Diff line change
Expand Up @@ -3109,6 +3109,68 @@ SELECT * FROM VALUES
2024-02-01T08:00:00Z
2023-12-31T23:00:00Z

# interval vs. duration comparison
query B
select (now() - now()) < interval '1 seconds';
----
true

query B
select (now() - now()) <= interval '1 seconds';
----
true

query B
select (now() - now()) = interval '0 seconds';
----
true

query B
select (now() - now()) != interval '1 seconds';
----
true

query B
select (now() - now()) > interval '-1 seconds';
----
true

query B
select (now() - now()) >= interval '-1 seconds';
----
true

query B
select arrow_cast(123, 'Duration(Nanosecond)') < interval '200 nanoseconds';
----
true

query B
select arrow_cast(123, 'Duration(Nanosecond)') < interval '100 nanoseconds';
----
false

query B
select arrow_cast(123, 'Duration(Nanosecond)') < interval '1 seconds';
----
true

query B
select interval '1 seconds' < arrow_cast(123, 'Duration(Nanosecond)')
----
false

# interval as LHS
query B
select interval '2 seconds' = interval '2 seconds';
----
true

query B
select interval '1 seconds' < interval '2 seconds';
----
true

statement ok
drop table t;

Expand Down

0 comments on commit b9bf6c9

Please sign in to comment.