Skip to content

Commit

Permalink
feat: Coerce Interval + Interval expression
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Qyoun-ae <[email protected]>
  • Loading branch information
Nikita-str and MazterQyou committed Jun 26, 2024
1 parent 9a7820b commit 370f91f
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions datafusion/expr/src/binary_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ pub fn coerce_types(
Operator::Like | Operator::NotLike | Operator::ILike | Operator::NotILike => {
like_coercion(lhs_type, rhs_type)
}
Operator::Plus | Operator::Minus
if is_interval(lhs_type) && is_interval(rhs_type) =>
{
interval_add_coercion(lhs_type, rhs_type)
}
// for math expressions, the final value of the coercion is also the return type
// because coercion favours higher information types
Operator::Plus | Operator::Minus => {
Expand Down Expand Up @@ -769,6 +774,22 @@ fn null_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType> {
_ => None,
}
}
/// coercion rules for `DataType::Interval <op> DataType::Interval` case, where `<op>` in `{+, -}`
fn interval_add_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType> {
use arrow::datatypes::IntervalUnit::*;
match (lhs_type, rhs_type) {
(DataType::Interval(l_unit), DataType::Interval(r_unit)) => {
let coercion_unit = match (l_unit, r_unit) {
(MonthDayNano, _) | (_, MonthDayNano) => Some(MonthDayNano),
(DayTime, YearMonth) | (YearMonth, DayTime) => Some(MonthDayNano),
(l_unit, r_unit) if l_unit == r_unit => Some(l_unit.clone()),
_ => None,
};
Some(DataType::Interval(coercion_unit?))
}
_ => None,
}
}

#[cfg(test)]
mod tests {
Expand Down

0 comments on commit 370f91f

Please sign in to comment.