Jimexist commented on a change in pull request #687:
URL: https://github.com/apache/arrow-datafusion/pull/687#discussion_r664531250
##########
File path: datafusion/src/physical_plan/expressions/lead_lag.rs
##########
@@ -98,20 +106,58 @@ impl BuiltInWindowFunctionExpr for WindowShift {
Ok(Box::new(WindowShiftEvaluator {
shift_offset: self.shift_offset,
values,
+ default_value: self.default_value.clone(),
}))
}
}
pub(crate) struct WindowShiftEvaluator {
shift_offset: i64,
values: Vec<ArrayRef>,
+ default_value: Option<ScalarValue>,
+}
+
+fn shift_with_default_value(
+ array: &ArrayRef,
+ offset: i64,
+ value: ScalarValue,
+) -> Result<ArrayRef> {
+ use arrow::compute::concat;
+
+ let value_len = array.len() as i64;
+ if offset == 0 {
+ Ok(arrow::array::make_array(array.data_ref().clone()))
+ } else if offset == i64::MIN || offset.abs() >= value_len {
+ Ok(arrow::array::new_null_array(array.data_type(), array.len()))
+ } else {
+ let slice_offset = (-offset).clamp(0, value_len) as usize;
+ let length = array.len() - offset.abs() as usize;
+ let slice = array.slice(slice_offset, length);
+
+ // Generate array with remaining `null` items
+ let default_values_len = offset.abs() as usize;
+ let default_values: ArrayRef =
value.to_array_of_size(default_values_len);
+
+ // Concatenate both arrays, add nulls after if shift > 0 else before
+ if offset > 0 {
+ concat(&[default_values.as_ref(), slice.as_ref()])
+ .map_err(DataFusionError::ArrowError)
+ } else {
+ concat(&[slice.as_ref(), default_values.as_ref()])
+ .map_err(DataFusionError::ArrowError)
+ }
+ }
}
impl PartitionEvaluator for WindowShiftEvaluator {
fn evaluate_partition(&self, partition: Range<usize>) -> Result<ArrayRef> {
let value = &self.values[0];
let value = value.slice(partition.start, partition.end -
partition.start);
- shift(value.as_ref(),
self.shift_offset).map_err(DataFusionError::ArrowError)
+ if let Some(default_value) = self.default_value.clone() {
+ shift_with_default_value(&value, self.shift_offset, default_value)
Review comment:
in both branches you can just call this `shift_with_default_value`. if
the default value isn't present, you can unwrap or provide a null scalar value
--
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]