Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 25 additions & 0 deletions datafusion/core/src/sql/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4033,6 +4033,10 @@ mod tests {
name: TableReference,
) -> Option<Arc<dyn TableProvider>> {
let schema = match name.table() {
"test" => Some(Schema::new(vec![
Field::new("t_date32", DataType::Date32, false),
Field::new("t_date64", DataType::Date64, false),
])),
"person" => Some(Schema::new(vec![
Field::new("id", DataType::UInt32, false),
Field::new("first_name", DataType::Utf8, false),
Expand Down Expand Up @@ -4158,4 +4162,25 @@ mod tests {
let result = logical_plan(sql).err().unwrap();
assert_eq!(expected, format!("{}", result));
}

#[test]
fn date_plus_interval_in_projection() {
let sql = "select t_date32 + interval '5 days' FROM test";
let expected = "Projection: #test.t_date32 + IntervalDayTime(\"21474836480\")\
\n TableScan: test projection=None";
quick_test(sql, expected);
}

#[test]
fn date_plus_interval_in_filter() {
let sql = "select t_date64 FROM test \
WHERE t_date64 \
BETWEEN cast('1999-12-31' as date) \
AND cast('1999-12-31' as date) + interval '30 days'";
let expected =
"Projection: #test.t_date64\
\n Filter: #test.t_date64 BETWEEN CAST(Utf8(\"1999-12-31\") AS Date32) AND CAST(Utf8(\"1999-12-31\") AS Date32) + IntervalDayTime(\"128849018880\")\
\n TableScan: test projection=None";
quick_test(sql, expected);
}
}
9 changes: 9 additions & 0 deletions datafusion/physical-expr/src/coercion_rule/binary_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ pub(crate) fn coerce_types(
}
// "like" operators operate on strings and always return a boolean
Operator::Like | Operator::NotLike => like_coercion(lhs_type, rhs_type),
// date +/- interval returns date
Operator::Plus | Operator::Minus
if (*lhs_type == DataType::Date32 || *lhs_type == DataType::Date64) =>
{
match rhs_type {
DataType::Interval(_) => Some(lhs_type.clone()),
_ => None,
}
}
// for math expressions, the final value of the coercion is also the return type
// because coercion favours higher information types
Operator::Plus
Expand Down