Apache DataFusion SQL Query Engine
fix: type coercion ignore prevent `Interval - Date` (#6177)
jakevin9031c2b8c7d1
diff --git a/datafusion/core/tests/sqllogictests/test_files/timestamps.slt b/datafusion/core/tests/sqllogictests/test_files/timestamps.sltindex 84405b0cf1..16b1e20e8f 100644--- a/datafusion/core/tests/sqllogictests/test_files/timestamps.slt+++ b/datafusion/core/tests/sqllogictests/test_files/timestamps.slt@@ -1031,7 +1031,7 @@ SELECT '2000-01-01T00:00:00'::timestamp - '2010-01-01T00:00:00'::timestamp;0 years 0 mons -3653 days 0 hours 0 mins 0.000000000 secs# Interval - Timestamp => error-statement error DataFusion error: Error during planning: Interval\(MonthDayNano\) - Timestamp\(Nanosecond, None\) can't be evaluated because there isn't a common type to coerce the types to+statement error DataFusion error: Error during planning: interval can't subtract timestamp/dateSELECT i - ts1 from FOO;diff --git a/datafusion/core/tests/sqllogictests/test_files/type_coercion.slt b/datafusion/core/tests/sqllogictests/test_files/type_coercion.sltindex f39d1caab1..41bb541eac 100644--- a/datafusion/core/tests/sqllogictests/test_files/type_coercion.slt+++ b/datafusion/core/tests/sqllogictests/test_files/type_coercion.slt@@ -29,3 +29,25 @@ query Bselect 's' is not distinct from arrow_cast('s', 'LargeUtf8');----true++# date - interval+query D+select '2023-05-01'::date - interval '1 month';+----+2023-04-01++# timestamp - interval+query P+SELECT '2023-05-01 12:30:00'::timestamp - interval '1 month';+----+2023-04-01T12:30:00++# TODO: https://github.com/apache/arrow-datafusion/issues/6180+# interval - date+query error DataFusion error: This feature is not implemented: Unsupported interval argument\. Expected string literal, got: BinaryOp \{ left: Value\(SingleQuotedString\("1 month"\)\), op: Minus, right: Cast \{ expr: Value\(SingleQuotedString\("2023\-05\-01"\)\), data_type: Date \} \}+select interval '1 month' - '2023-05-01'::date;++# TODO: https://github.com/apache/arrow-datafusion/issues/6180+# interval - timestamp+query error DataFusion error: This feature is not implemented: Unsupported interval argument\. Expected string literal, got: BinaryOp \{ left: Value\(SingleQuotedString\("1 month"\)\), op: Minus, right: Cast \{ expr: Value\(SingleQuotedString\("2023\-05\-01 12:30:00"\)\), data_type: Timestamp\(None, None\) \} \}+SELECT interval '1 month' - '2023-05-01 12:30:00'::timestamp;diff --git a/datafusion/expr/src/type_coercion/binary.rs b/datafusion/expr/src/type_coercion/binary.rsindex b3d3f3d5cb..3d88491c68 100644--- a/datafusion/expr/src/type_coercion/binary.rs+++ b/datafusion/expr/src/type_coercion/binary.rs@@ -17,15 +17,19 @@//! Coercion rules for matching argument types for binary operators-use crate::type_coercion::{is_date, is_decimal, is_interval, is_numeric, is_timestamp};-use crate::Operator;use arrow::compute::can_cast_types;use arrow::datatypes::{DataType, IntervalUnit, TimeUnit, DECIMAL128_MAX_PRECISION, DECIMAL128_MAX_SCALE,};+use datafusion_common::DataFusionError;use datafusion_common::Result;+use crate::type_coercion::{+ is_datetime, is_decimal, is_interval, is_numeric, is_timestamp,+};+use crate::Operator;+/// Returns the return type of a binary operator or an error when the binary operator cannot/// perform the computation between the argument's types, even after type coercion.///@@ -129,11 +133,10 @@ pub fn coerce_types(| Operator::BitwiseShiftLeft => bitwise_coercion(lhs_type, rhs_type),Operator::And | Operator::Or => match (lhs_type, rhs_type) {// logical binary boolean operators can only be evaluated in bools or nulls- (DataType::Boolean, DataType::Boolean) => Some(DataType::Boolean),- (DataType::Null, DataType::Null) => Some(DataType::Boolean),- (DataType::Boolean, DataType::Null) | (DataType::Null, DataType::Boolean) => {- Some(DataType::Boolean)- }+ (DataType::Boolean, DataType::Boolean)+ | (DataType::Null, DataType::Null)+ | (DataType::Boolean, DataType::Null)+ | (DataType::Null, DataType::Boolean) => Some(DataType::Boolean),_ => None,},// logical comparison operators have their own rules, and always return a boolean@@ -147,16 +150,16 @@ pub fn coerce_types(| Operator::IsNotDistinctFrom => comparison_coercion(lhs_type, rhs_type),// interval - timestamp is an erroneous case, cannot coerce a typeOperator::Plus | Operator::Minus- if (is_date(lhs_type)- || is_date(rhs_type)- || is_timestamp(lhs_type)- || is_timestamp(rhs_type)+ if is_datetime(lhs_type)+ || is_datetime(rhs_type)|| is_interval(lhs_type)- || is_interval(rhs_type))- && (!is_interval(lhs_type)- || !is_timestamp(rhs_type)- || *op != Operator::Minus) =>+ || is_interval(rhs_type) =>{+ if is_interval(lhs_type) && is_datetime(rhs_type) && *op == Operator::Minus {+ return Err(DataFusionError::Plan(+ "interval can't subtract timestamp/date".to_string(),+ ));+ }temporal_add_sub_coercion(lhs_type, rhs_type, op)}// for math expressions, the final value of the coercion is also the return type@@ -289,12 +292,8 @@ pub fn temporal_add_sub_coercion() -> Option<DataType> {match (lhs_type, rhs_type, op) {// if an interval is being added/subtracted from a date/timestamp, return the date/timestamp data type- (lhs, rhs, _) if is_interval(lhs) && (is_date(rhs) || is_timestamp(rhs)) => {- Some(rhs.clone())- }- (lhs, rhs, _) if is_interval(rhs) && (is_date(lhs) || is_timestamp(lhs)) => {- Some(lhs.clone())- }+ (lhs, rhs, _) if is_interval(lhs) && is_datetime(rhs) => Some(rhs.clone()),+ (lhs, rhs, _) if is_interval(rhs) && is_datetime(lhs) => Some(lhs.clone()),// if two timestamps are being subtracted, check their time units and return the corresponding interval data type(lhs, rhs, Operator::Minus) if is_timestamp(lhs) && is_timestamp(rhs) => {handle_timestamp_minus(lhs, rhs)@@ -303,10 +302,7 @@ pub fn temporal_add_sub_coercion((lhs, rhs, _) if is_interval(lhs) && is_interval(rhs) => {handle_interval_addition(lhs, rhs)}- (lhs, rhs, Operator::Minus)- if (is_date(lhs) || is_timestamp(lhs))- && (is_date(rhs) || is_timestamp(rhs)) =>- {+ (lhs, rhs, Operator::Minus) if is_datetime(lhs) && is_datetime(rhs) => {temporal_coercion(lhs, rhs)}// return None if no coercion is possible@@ -887,13 +883,16 @@ fn null_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType> {#[cfg(test)]mod tests {- use super::*;- use crate::Operator;use arrow::datatypes::DataType;+use datafusion_common::assert_contains;use datafusion_common::DataFusionError;use datafusion_common::Result;+ use crate::Operator;++ use super::*;+#[test]fn test_coercion_error() -> Result<()> {let result_type =diff --git a/datafusion/expr/src/type_coercion/mod.rs b/datafusion/expr/src/type_coercion/mod.rsindex 4a1d695dea..fa78e8903e 100644--- a/datafusion/expr/src/type_coercion/mod.rs+++ b/datafusion/expr/src/type_coercion/mod.rs@@ -76,9 +76,12 @@ pub fn is_interval(dt: &DataType) -> bool {matches!(dt, DataType::Interval(_))}-/// Determine whether the given data type `dt` is a `Date`.-pub fn is_date(dt: &DataType) -> bool {- matches!(dt, DataType::Date32 | DataType::Date64)+/// Determine whether the given data type `dt` is a `Date` or `Timestamp`.+pub fn is_datetime(dt: &DataType) -> bool {+ matches!(+ dt,+ DataType::Date32 | DataType::Date64 | DataType::Timestamp(_, _)+ )}/// Determine whether the given data type `dt` is a `Utf8` or `LargeUtf8`.diff --git a/datafusion/optimizer/src/analyzer/type_coercion.rs b/datafusion/optimizer/src/analyzer/type_coercion.rsindex 7a346b5b9e..04e6f09d66 100644--- a/datafusion/optimizer/src/analyzer/type_coercion.rs+++ b/datafusion/optimizer/src/analyzer/type_coercion.rs@@ -34,9 +34,7 @@ use datafusion_expr::type_coercion::functions::data_types;use datafusion_expr::type_coercion::other::{get_coerce_type_for_case_expression, get_coerce_type_for_list,};-use datafusion_expr::type_coercion::{- is_date, is_numeric, is_timestamp, is_utf8_or_large_utf8,-};+use datafusion_expr::type_coercion::{is_datetime, is_numeric, is_utf8_or_large_utf8};use datafusion_expr::utils::from_plan;use datafusion_expr::{aggregate_function, function, is_false, is_not_false, is_not_true, is_not_unknown,@@ -549,7 +547,7 @@ fn coerce_window_frame(if let Some(col_type) = current_types.first() {if is_numeric(col_type) || is_utf8_or_large_utf8(col_type) {col_type- } else if is_timestamp(col_type) || is_date(col_type) {+ } else if is_datetime(col_type) {&DataType::Interval(IntervalUnit::MonthDayNano)} else {return Err(DataFusionError::Internal(format!(