stuartcarnie commented on code in PR #3034:
URL: https://github.com/apache/arrow-datafusion/pull/3034#discussion_r939754634
##########
datafusion/physical-expr/src/datetime_expressions.rs:
##########
@@ -275,6 +275,93 @@ pub fn date_trunc(args: &[ColumnarValue]) ->
Result<ColumnarValue> {
})
}
+fn date_bin_single(stride: i64, source: i64, origin: i64) -> i64 {
+ let time_diff = source - origin;
+ // distance to bin
+ let time_delta = time_diff - (time_diff % stride);
+
+ let time_delta = if time_diff < 0 && stride > 1 {
+ // The origin is later than the source timestamp, round down to the
previous bin
+ time_delta - stride
+ } else {
+ time_delta
+ };
+
+ origin + time_delta
+}
+
+/// DATE_BIN sql function
+pub fn date_bin(args: &[ColumnarValue]) -> Result<ColumnarValue> {
+ if args.len() != 3 {
+ return Err(DataFusionError::Execution(
+ "Expected three arguments for DATE_BIN".to_string(),
+ ));
+ }
+
+ let (stride, array, origin) = (&args[0], &args[1], &args[2]);
+
+ let stride = match stride {
+ ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(v))) => {
+ let (days, ms) = IntervalDayTimeType::to_parts(*v);
+ let nanos = (Duration::days(days as i64) +
Duration::milliseconds(ms as i64))
+ .num_nanoseconds();
+ match nanos {
+ Some(v) => v,
+ _ => {
+ return Err(DataFusionError::Execution(
+ "stride of DATE_BIN is too large".to_string(),
+ ))
+ }
+ }
+ }
+ _ => {
+ return Err(DataFusionError::Execution(
+ "stride of DATE_BIN is an invalid type".to_string(),
+ ))
+ }
Review Comment:
👍🏻 Done. I prefixed all the errors with the function name, `DATE_BIN ...`,
to make them read more consistently.
--
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]