You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "milevin (via GitHub)" <gi...@apache.org> on 2024/12/12 08:20:17 UTC
[PR] Support binary temporal arithmetic with integers [datafusion]
milevin opened a new pull request, #13741:
URL: https://github.com/apache/datafusion/pull/13741
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
Closes #12342
## Rationale for this change
```
> ./target/debug/datafusion-cli -c "select 4 + to_date('1970-01-03');"
DataFusion CLI v43.0.0
+----------------------------------------+
| Int64(4) + to_date(Utf8("1970-01-03")) |
+----------------------------------------+
| 1970-01-07 |
+----------------------------------------+
1 row(s) fetched.
Elapsed 0.018 seconds.
> ./target/debug/datafusion-cli -c "select to_date('1970-01-03') - 2;"
DataFusion CLI v43.0.0
+----------------------------------------+
| to_date(Utf8("1970-01-03")) - Int64(2) |
+----------------------------------------+
| 1970-01-01 |
+----------------------------------------+
1 row(s) fetched.
Elapsed 0.006 seconds.
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Support binary temporal arithmetic with integers [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #13741:
URL: https://github.com/apache/datafusion/pull/13741#issuecomment-2542008198
FYI @jonahgao in case you would like to weigh in here
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Support binary temporal arithmetic with integers [datafusion]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #13741:
URL: https://github.com/apache/datafusion/pull/13741#issuecomment-2735135388
Thank you for your contribution. Unfortunately, this pull request is stale because it has been open 60 days with no activity. Please remove the stale label or comment or this will be closed in 7 days.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Support binary temporal arithmetic with integers [datafusion]
Posted by "milevin (via GitHub)" <gi...@apache.org>.
milevin commented on code in PR #13741:
URL: https://github.com/apache/datafusion/pull/13741#discussion_r1885383953
##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -453,6 +455,26 @@ impl ExprSchemable for Expr {
}
_ => Ok(Expr::Cast(Cast::new(Box::new(self), cast_to_type.clone()))),
}
+ } else if matches!(
+ (&this_type, cast_to_type),
+ (DataType::Int32 | DataType::Int64, DataType::Interval(_))
+ ) {
+ // Convert integer (days) to the corresponding DayTime interval
+ match self {
+ Expr::Literal(ScalarValue::Int32(Some(days))) => {
+ Ok(Expr::Literal(ScalarValue::IntervalDayTime(Some(
+ arrow_buffer::IntervalDayTime::new(days, 0),
+ ))))
+ }
+ Expr::Literal(ScalarValue::Int64(Some(days))) => {
+ Ok(Expr::Literal(ScalarValue::IntervalDayTime(Some(
+ arrow_buffer::IntervalDayTime::new(days as i32, 0),
+ ))))
+ }
+ _ => plan_err!(
+ "Cannot automatically convert {this_type:?} to {cast_to_type:?}"
+ ),
+ }
Review Comment:
Yes @jonahgao something like that is the approach! I've pinged the make_interval issue:
https://github.com/apache/datafusion/issues/6951
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Support binary temporal arithmetic with integers [datafusion]
Posted by "findepi (via GitHub)" <gi...@apache.org>.
findepi commented on code in PR #13741:
URL: https://github.com/apache/datafusion/pull/13741#discussion_r1881988070
##########
datafusion/expr-common/src/type_coercion/binary.rs:
##########
@@ -186,6 +186,12 @@ fn signature(lhs: &DataType, op: &Operator, rhs: &DataType) -> Result<Signature>
} else if let Some(numeric) = mathematics_numerical_coercion(lhs, rhs) {
// Numeric arithmetic, e.g. Int32 + Int32
Ok(Signature::uniform(numeric))
+ } else if let Some((new_lhs, new_rhs, ret)) = resolve_ints_to_intervals(lhs, rhs) {
Review Comment:
we're in `Plus | Minus | Multiply | Divide | Modulo => {` branch
but the new logic should apply
- to `+` symmetrically
- to `date - int`, but likely not to `int - date` (at least per PostgreSQL)
- not at all to other operators
so what about passing the operator to the new function and doing the work for `+` and `-` (so that code already identifies asymmetric treatment)
##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -453,6 +455,26 @@ impl ExprSchemable for Expr {
}
_ => Ok(Expr::Cast(Cast::new(Box::new(self), cast_to_type.clone()))),
}
+ } else if matches!(
+ (&this_type, cast_to_type),
+ (DataType::Int32 | DataType::Int64, DataType::Interval(_))
+ ) {
+ // Convert integer (days) to the corresponding DayTime interval
+ match self {
+ Expr::Literal(ScalarValue::Int32(Some(days))) => {
+ Ok(Expr::Literal(ScalarValue::IntervalDayTime(Some(
+ arrow_buffer::IntervalDayTime::new(days, 0),
+ ))))
+ }
+ Expr::Literal(ScalarValue::Int64(Some(days))) => {
+ Ok(Expr::Literal(ScalarValue::IntervalDayTime(Some(
+ arrow_buffer::IntervalDayTime::new(days as i32, 0),
+ ))))
+ }
+ _ => plan_err!(
+ "Cannot automatically convert {this_type:?} to {cast_to_type:?}"
+ ),
+ }
Review Comment:
if a Cast is supposed to work, it should work regardless how it's weaved into the plan.
`Expr.cast_to` should not have any additional logic (other than checks) vs creating `Expr::Cast` directly.
However, we should first decide whether integer should be castable to interval at all.
It's not castable in PostgreSQL
```
postgres=# SELECT 10::interval;
ERROR: cannot cast type integer to interval
LINE 1: SELECT 10::interval;
^
```
so I am inclined it should not be castable in DF either.
Which means, the `date + int` cannot be converted (desugared) to `date + interval` using ordinary cast expression (as such shouldn't exist), but it can be converted (desugared) by some other means (eg with a custom conversion function or combination of expressions).
Alternatively, we defer `date + int` until the execution, eg somewhere in https://github.com/apache/datafusion/blob/cc43766104cf66b8dbf431cbf6bdf25eda6ba274/datafusion/physical-expr/src/expressions/binary.rs#L301
but i'd prefer to desugar earlier and avoid separate code path in the `BinaryExpr` physical expr
##########
datafusion/expr-common/src/type_coercion/binary.rs:
##########
@@ -1449,6 +1455,26 @@ fn null_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType> {
}
}
+/// Resolves integer types to interval types for temporal arithmetic
+fn resolve_ints_to_intervals(
+ lhs: &DataType,
+ rhs: &DataType,
+) -> Option<(DataType, DataType, DataType)> {
+ use arrow::datatypes::DataType::*;
+ use arrow::datatypes::IntervalUnit::*;
+
+ match (lhs, rhs) {
+ // Handle integer + temporal types cases
+ (Int32 | Int64, rhs) if rhs.is_temporal() => {
Review Comment:
it should be `date + int`, not `any_temporal + int`:
```
postgres=# select now()::date + 1;
?column?
------------
2024-12-13
(1 row)
postgres=# select now()::time + 1;
ERROR: operator does not exist: time without time zone + integer
LINE 1: select now()::time + 1;
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
postgres=# select now()::timestamp + 1;
ERROR: operator does not exist: timestamp without time zone + integer
LINE 1: select now()::timestamp + 1;
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
postgres=# select now()::timestamptz + 1;
ERROR: operator does not exist: timestamp with time zone + integer
LINE 1: select now()::timestamptz + 1;
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
```
(i didn't check other `is_temporal` types: interval, duration)
##########
datafusion/expr-common/src/type_coercion/binary.rs:
##########
@@ -1607,6 +1629,18 @@ mod tests {
}};
}
+ /// Test coercion rules for assymetric binary operators
+ ///
+ /// Applies coercion rules for `$LHS_TYPE $OP $RHS_TYPE` and asserts that the
+ /// the result types are `$RESULT_TYPE1` and `$RESULT_TYPE2` respectively
+ macro_rules! test_coercion_assymetric_binary_rule {
Review Comment:
i assumed so. But also -- macros aren't "better functions", they come with downsides, so if something is expressible as a function, it should.
##########
datafusion/expr-common/src/type_coercion/binary.rs:
##########
@@ -1607,6 +1629,18 @@ mod tests {
}};
}
+ /// Test coercion rules for assymetric binary operators
+ ///
+ /// Applies coercion rules for `$LHS_TYPE $OP $RHS_TYPE` and asserts that the
+ /// the result types are `$RESULT_TYPE1` and `$RESULT_TYPE2` respectively
+ macro_rules! test_coercion_assymetric_binary_rule {
+ ($LHS_TYPE:expr, $RHS_TYPE:expr, $OP:expr, $RESULT_TYPE1:expr, $RESULT_TYPE2:expr) => {{
+ let (lhs, rhs) = get_input_types(&$LHS_TYPE, &$OP, &$RHS_TYPE)?;
+ assert_eq!(lhs, $RESULT_TYPE1);
+ assert_eq!(rhs, $RESULT_TYPE2);
Review Comment:
You could have this
```rust
let (a, b) = get_input_types(&$LHS_TYPE, &$OP, &$RHS_TYPE)?;
assert_eq!(a, $RESULT_TYPE1);
assert_eq!(b, $RESULT_TYPE2);
// test symmetry
let (b, a) = get_input_types(&$RHS_TYPE, &$OP, &$LHS_TYPE)?;
assert_eq!(a, $RESULT_TYPE1);
assert_eq!(b, $RESULT_TYPE2);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Support binary temporal arithmetic with integers [datafusion]
Posted by "findepi (via GitHub)" <gi...@apache.org>.
findepi commented on code in PR #13741:
URL: https://github.com/apache/datafusion/pull/13741#discussion_r1881583324
##########
datafusion/expr-common/src/type_coercion/binary.rs:
##########
@@ -1449,6 +1455,22 @@ fn null_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType> {
}
}
+/// Resolves integer types to interval types for temporal arithmetic
+fn resolve_ints_to_intervals(
+ lhs: &DataType,
+ rhs: &DataType,
+) -> Option<(DataType, DataType, DataType)> {
+ use arrow::datatypes::DataType::*;
+ use arrow::datatypes::IntervalUnit::*;
+
+ match (lhs, rhs) {
+ // Handle integer + interval cases
+ (Int32 | Int64, _) => Some((Interval(DayTime), rhs.clone(), rhs.clone())),
Review Comment:
should this check rhs type to be something specific?
##########
datafusion/expr-common/src/type_coercion/binary.rs:
##########
@@ -1449,6 +1455,22 @@ fn null_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType> {
}
}
+/// Resolves integer types to interval types for temporal arithmetic
+fn resolve_ints_to_intervals(
+ lhs: &DataType,
+ rhs: &DataType,
+) -> Option<(DataType, DataType, DataType)> {
+ use arrow::datatypes::DataType::*;
+ use arrow::datatypes::IntervalUnit::*;
+
+ match (lhs, rhs) {
+ // Handle integer + interval cases
Review Comment:
"+ interval" or "+ date" ?
##########
datafusion/expr-common/src/type_coercion/binary.rs:
##########
@@ -1607,6 +1629,18 @@ mod tests {
}};
}
+ /// Test coercion rules for assymetric binary operators
+ ///
+ /// Applies coercion rules for `$LHS_TYPE $OP $RHS_TYPE` and asserts that the
+ /// the result types are `$RESULT_TYPE1` and `$RESULT_TYPE2` respectively
+ macro_rules! test_coercion_assymetric_binary_rule {
+ ($LHS_TYPE:expr, $RHS_TYPE:expr, $OP:expr, $RESULT_TYPE1:expr, $RESULT_TYPE2:expr) => {{
+ let (lhs, rhs) = get_input_types(&$LHS_TYPE, &$OP, &$RHS_TYPE)?;
+ assert_eq!(lhs, $RESULT_TYPE1);
+ assert_eq!(rhs, $RESULT_TYPE2);
Review Comment:
for `+` operator symmetry is expected, so the assert function could do this for us
##########
datafusion/expr-common/src/type_coercion/binary.rs:
##########
@@ -1607,6 +1629,18 @@ mod tests {
}};
}
+ /// Test coercion rules for assymetric binary operators
+ ///
+ /// Applies coercion rules for `$LHS_TYPE $OP $RHS_TYPE` and asserts that the
+ /// the result types are `$RESULT_TYPE1` and `$RESULT_TYPE2` respectively
+ macro_rules! test_coercion_assymetric_binary_rule {
Review Comment:
What's the benefit of this being a macro? Could this be ordinary function?
##########
datafusion/expr-common/src/type_coercion/binary.rs:
##########
@@ -1869,6 +1905,51 @@ mod tests {
Operator::Multiply,
DataType::Float64
);
+
+ // Test integer to interval coercion for temporal arithmetic
+ test_coercion_assymetric_binary_rule!(
+ DataType::Int32,
+ DataType::Timestamp(TimeUnit::Nanosecond, None),
+ Operator::Plus,
+ DataType::Interval(IntervalUnit::DayTime),
+ DataType::Timestamp(TimeUnit::Nanosecond, None)
+ );
+ test_coercion_assymetric_binary_rule!(
+ DataType::Timestamp(TimeUnit::Nanosecond, None),
+ DataType::Int64,
+ Operator::Plus,
+ DataType::Timestamp(TimeUnit::Nanosecond, None),
+ DataType::Interval(IntervalUnit::DayTime)
+ );
+ test_coercion_assymetric_binary_rule!(
+ DataType::Int32,
+ DataType::Date32,
Review Comment:
do we also have Date32 + Int32 test (in that order)?
##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -453,6 +455,26 @@ impl ExprSchemable for Expr {
}
_ => Ok(Expr::Cast(Cast::new(Box::new(self), cast_to_type.clone()))),
}
+ } else if matches!(
+ (&this_type, cast_to_type),
+ (DataType::Int32 | DataType::Int64, DataType::Interval(_))
+ ) {
+ // Convert integer (days) to the corresponding DayTime interval
+ match self {
+ Expr::Literal(ScalarValue::Int32(Some(days))) => {
+ Ok(Expr::Literal(ScalarValue::IntervalDayTime(Some(
+ arrow_buffer::IntervalDayTime::new(days, 0),
+ ))))
+ }
+ Expr::Literal(ScalarValue::Int64(Some(days))) => {
+ Ok(Expr::Literal(ScalarValue::IntervalDayTime(Some(
+ arrow_buffer::IntervalDayTime::new(days as i32, 0),
+ ))))
+ }
+ _ => plan_err!(
+ "Cannot automatically convert {this_type:?} to {cast_to_type:?}"
+ ),
+ }
Review Comment:
This works for literals only and is immediate.
For other types all the function does is create cast expression. Why would we want to special-case here?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Support binary temporal arithmetic with integers [datafusion]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #13741: Support binary temporal arithmetic with integers
URL: https://github.com/apache/datafusion/pull/13741
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Support binary temporal arithmetic with integers [datafusion]
Posted by "milevin (via GitHub)" <gi...@apache.org>.
milevin commented on code in PR #13741:
URL: https://github.com/apache/datafusion/pull/13741#discussion_r1881600889
##########
datafusion/expr-common/src/type_coercion/binary.rs:
##########
@@ -1607,6 +1629,18 @@ mod tests {
}};
}
+ /// Test coercion rules for assymetric binary operators
+ ///
+ /// Applies coercion rules for `$LHS_TYPE $OP $RHS_TYPE` and asserts that the
+ /// the result types are `$RESULT_TYPE1` and `$RESULT_TYPE2` respectively
+ macro_rules! test_coercion_assymetric_binary_rule {
+ ($LHS_TYPE:expr, $RHS_TYPE:expr, $OP:expr, $RESULT_TYPE1:expr, $RESULT_TYPE2:expr) => {{
+ let (lhs, rhs) = get_input_types(&$LHS_TYPE, &$OP, &$RHS_TYPE)?;
+ assert_eq!(lhs, $RESULT_TYPE1);
+ assert_eq!(rhs, $RESULT_TYPE2);
Review Comment:
The operator is symmetric, but how we cast the left and right types is not symmetric. Hence I needed an assymetric test for this.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Support binary temporal arithmetic with integers [datafusion]
Posted by "milevin (via GitHub)" <gi...@apache.org>.
milevin commented on code in PR #13741:
URL: https://github.com/apache/datafusion/pull/13741#discussion_r1881601707
##########
datafusion/expr-common/src/type_coercion/binary.rs:
##########
@@ -1869,6 +1905,51 @@ mod tests {
Operator::Multiply,
DataType::Float64
);
+
+ // Test integer to interval coercion for temporal arithmetic
+ test_coercion_assymetric_binary_rule!(
+ DataType::Int32,
+ DataType::Timestamp(TimeUnit::Nanosecond, None),
+ Operator::Plus,
+ DataType::Interval(IntervalUnit::DayTime),
+ DataType::Timestamp(TimeUnit::Nanosecond, None)
+ );
+ test_coercion_assymetric_binary_rule!(
+ DataType::Timestamp(TimeUnit::Nanosecond, None),
+ DataType::Int64,
+ Operator::Plus,
+ DataType::Timestamp(TimeUnit::Nanosecond, None),
+ DataType::Interval(IntervalUnit::DayTime)
+ );
+ test_coercion_assymetric_binary_rule!(
+ DataType::Int32,
+ DataType::Date32,
Review Comment:
Probably not. Could be done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Support binary temporal arithmetic with integers [datafusion]
Posted by "jonahgao (via GitHub)" <gi...@apache.org>.
jonahgao commented on code in PR #13741:
URL: https://github.com/apache/datafusion/pull/13741#discussion_r1885119711
##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -453,6 +455,26 @@ impl ExprSchemable for Expr {
}
_ => Ok(Expr::Cast(Cast::new(Box::new(self), cast_to_type.clone()))),
}
+ } else if matches!(
+ (&this_type, cast_to_type),
+ (DataType::Int32 | DataType::Int64, DataType::Interval(_))
+ ) {
+ // Convert integer (days) to the corresponding DayTime interval
+ match self {
+ Expr::Literal(ScalarValue::Int32(Some(days))) => {
+ Ok(Expr::Literal(ScalarValue::IntervalDayTime(Some(
+ arrow_buffer::IntervalDayTime::new(days, 0),
+ ))))
+ }
+ Expr::Literal(ScalarValue::Int64(Some(days))) => {
+ Ok(Expr::Literal(ScalarValue::IntervalDayTime(Some(
+ arrow_buffer::IntervalDayTime::new(days as i32, 0),
+ ))))
+ }
+ _ => plan_err!(
+ "Cannot automatically convert {this_type:?} to {cast_to_type:?}"
+ ),
+ }
Review Comment:
Perhaps we require a new scalar function, `make_interval`, which can convert integers into intervals.
Then, similar to [`make_array`](https://github.com/apache/datafusion/blob/bfabd48de2e0deccbd8ce595936ad24f18baca65/datafusion/functions-nested/src/planner.rs#L101), perform transformations within the SQL planner.
Transform `4 + to_date('1970-01-03')` to `make_interval(4) + to_date('1970-01-03')` inside `plan_binary_op`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Support binary temporal arithmetic with integers [datafusion]
Posted by "milevin (via GitHub)" <gi...@apache.org>.
milevin commented on code in PR #13741:
URL: https://github.com/apache/datafusion/pull/13741#discussion_r1881606531
##########
datafusion/expr-common/src/type_coercion/binary.rs:
##########
@@ -1449,6 +1455,22 @@ fn null_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType> {
}
}
+/// Resolves integer types to interval types for temporal arithmetic
+fn resolve_ints_to_intervals(
+ lhs: &DataType,
+ rhs: &DataType,
+) -> Option<(DataType, DataType, DataType)> {
+ use arrow::datatypes::DataType::*;
+ use arrow::datatypes::IntervalUnit::*;
+
+ match (lhs, rhs) {
+ // Handle integer + interval cases
+ (Int32 | Int64, _) => Some((Interval(DayTime), rhs.clone(), rhs.clone())),
Review Comment:
I think so. I'll fix it
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Support binary temporal arithmetic with integers [datafusion]
Posted by "milevin (via GitHub)" <gi...@apache.org>.
milevin commented on code in PR #13741:
URL: https://github.com/apache/datafusion/pull/13741#discussion_r1881598760
##########
datafusion/expr-common/src/type_coercion/binary.rs:
##########
@@ -1607,6 +1629,18 @@ mod tests {
}};
}
+ /// Test coercion rules for assymetric binary operators
+ ///
+ /// Applies coercion rules for `$LHS_TYPE $OP $RHS_TYPE` and asserts that the
+ /// the result types are `$RESULT_TYPE1` and `$RESULT_TYPE2` respectively
+ macro_rules! test_coercion_assymetric_binary_rule {
Review Comment:
I don't know -- I just copied how the rest is done here, via macros too. This macro is a clone of test_coercion_binary_rule macro.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Support binary temporal arithmetic with integers [datafusion]
Posted by "milevin (via GitHub)" <gi...@apache.org>.
milevin commented on code in PR #13741:
URL: https://github.com/apache/datafusion/pull/13741#discussion_r1881596978
##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -453,6 +455,26 @@ impl ExprSchemable for Expr {
}
_ => Ok(Expr::Cast(Cast::new(Box::new(self), cast_to_type.clone()))),
}
+ } else if matches!(
+ (&this_type, cast_to_type),
+ (DataType::Int32 | DataType::Int64, DataType::Interval(_))
+ ) {
+ // Convert integer (days) to the corresponding DayTime interval
+ match self {
+ Expr::Literal(ScalarValue::Int32(Some(days))) => {
+ Ok(Expr::Literal(ScalarValue::IntervalDayTime(Some(
+ arrow_buffer::IntervalDayTime::new(days, 0),
+ ))))
+ }
+ Expr::Literal(ScalarValue::Int64(Some(days))) => {
+ Ok(Expr::Literal(ScalarValue::IntervalDayTime(Some(
+ arrow_buffer::IntervalDayTime::new(days as i32, 0),
+ ))))
+ }
+ _ => plan_err!(
+ "Cannot automatically convert {this_type:?} to {cast_to_type:?}"
+ ),
+ }
Review Comment:
What's the alternative? You can't cast an integer to an interval in arrow.
You are right; it only works for literals. Do you have thoughts on how to make it work for non also?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Support binary temporal arithmetic with integers [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #13741:
URL: https://github.com/apache/datafusion/pull/13741#issuecomment-2597024287
What is the status of this PR? Are we waiting on upstream changes in arrow-rs?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org