stuartcarnie commented on code in PR #3034:
URL: https://github.com/apache/arrow-datafusion/pull/3034#discussion_r939760674
##########
datafusion/physical-expr/src/datetime_expressions.rs:
##########
@@ -502,6 +589,140 @@ mod tests {
});
}
+ #[test]
+ fn test_date_bin_single() {
+ use chrono::Duration;
+
+ let cases = vec![
+ (
+ (
+ Duration::minutes(15),
+ "2004-04-09T02:03:04.123456789Z",
+ "2001-01-01T00:00:00",
+ ),
+ "2004-04-09T02:00:00Z",
+ ),
+ (
+ (
+ Duration::minutes(15),
+ "2004-04-09T02:03:04.123456789Z",
+ "2001-01-01T00:02:30",
+ ),
+ "2004-04-09T02:02:30Z",
+ ),
+ (
+ (
+ Duration::minutes(15),
+ "2004-04-09T02:03:04.123456789Z",
+ "2005-01-01T00:02:30",
+ ),
+ "2004-04-09T02:02:30Z",
+ ),
+ ];
+
+ cases
+ .iter()
+ .for_each(|((stride, source, origin), expected)| {
+ let stride1 = stride.num_nanoseconds().unwrap();
+ let source1 = string_to_timestamp_nanos(source).unwrap();
+ let origin1 = string_to_timestamp_nanos(origin).unwrap();
+
+ let expected1 = string_to_timestamp_nanos(expected).unwrap();
+ let result = date_bin_single(stride1, source1, origin1);
+ assert_eq!(result, expected1, "{} = {}", source, expected);
+ })
+ }
+
+ #[test]
+ fn test_date_bin() {
+ let res = date_bin(&[
+ ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(1))),
+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1),
None)),
+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1),
None)),
+ ]);
+ assert!(res.is_ok());
+
+ let mut builder = TimestampNanosecondArray::builder(5);
+ builder.append_slice((1..6).collect::<Vec<i64>>().as_slice());
+ let timestamps = Arc::new(builder.finish());
+ let res = date_bin(&[
+ ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(1))),
+ ColumnarValue::Array(timestamps),
+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1),
None)),
+ ]);
+ assert!(res.is_ok());
+
+ //
+ // Fallible test cases
+ //
+
+ // invalid number of arguments
+ let res = date_bin(&[
+ ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(1))),
+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1),
None)),
+ ]);
+ assert!(matches!(
+ res,
+ Err(DataFusionError::Execution(x)) if x == "Expected three
arguments for DATE_BIN"
+ ));
Review Comment:
I much prefer your suggestion, thanks! The most important benefit is a test
failure will show the unexpected error message.
I couldn't use `unwrap_err`, as the outer `ColumnarValue` has to implement
the `Debug` trait, so I did the following:
```rust
res.err().unwrap()
```
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]