alamb commented on a change in pull request #429: URL: https://github.com/apache/arrow-datafusion/pull/429#discussion_r660884744
########## File path: datafusion/src/physical_plan/expressions/lead_lag.rs ########## @@ -0,0 +1,180 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! Defines physical expression for `lead` and `lag` that can evaluated +//! at runtime during query execution + +use crate::error::{DataFusionError, Result}; +use crate::physical_plan::window_functions::PartitionEvaluator; +use crate::physical_plan::{window_functions::BuiltInWindowFunctionExpr, PhysicalExpr}; +use arrow::array::ArrayRef; +use arrow::compute::kernels::window::shift; +use arrow::datatypes::{DataType, Field}; +use arrow::record_batch::RecordBatch; +use std::any::Any; +use std::ops::Range; +use std::sync::Arc; + +/// window shift expression +#[derive(Debug)] +pub struct WindowShift { + name: String, + data_type: DataType, + shift_offset: i64, + expr: Arc<dyn PhysicalExpr>, +} + +/// lead() window function +pub fn lead( + name: String, + data_type: DataType, + expr: Arc<dyn PhysicalExpr>, +) -> WindowShift { + WindowShift { + name, + data_type, + shift_offset: -1, + expr, + } +} + +/// lag() window function +pub fn lag( + name: String, + data_type: DataType, + expr: Arc<dyn PhysicalExpr>, +) -> WindowShift { + WindowShift { + name, + data_type, + shift_offset: 1, + expr, + } +} + +impl BuiltInWindowFunctionExpr for WindowShift { + /// Return a reference to Any that can be used for downcasting + fn as_any(&self) -> &dyn Any { + self + } + + fn field(&self) -> Result<Field> { + let nullable = true; + Ok(Field::new(&self.name, self.data_type.clone(), nullable)) + } + + fn expressions(&self) -> Vec<Arc<dyn PhysicalExpr>> { + vec![self.expr.clone()] + } + + fn name(&self) -> &str { + &self.name + } + + fn create_evaluator( + &self, + batch: &RecordBatch, + ) -> Result<Box<dyn PartitionEvaluator>> { + let values = self + .expressions() + .iter() + .map(|e| e.evaluate(batch)) + .map(|r| r.map(|v| v.into_array(batch.num_rows()))) + .collect::<Result<Vec<_>>>()?; + Ok(Box::new(WindowShiftEvaluator { + shift_offset: self.shift_offset, + values, + })) + } +} + +pub(crate) struct WindowShiftEvaluator { + shift_offset: i64, + values: Vec<ArrayRef>, +} + +impl PartitionEvaluator for WindowShiftEvaluator { + fn evaluate_partition(&self, _partition: Range<usize>) -> Result<ArrayRef> { + let value = &self.values[0]; + shift(value.as_ref(), self.shift_offset).map_err(DataFusionError::ArrowError) Review comment: do you need to restrict the window to the partition bounds? If the input array had 10 rows in 2 partitions, wouldn't this code produce 2 output partitions of 10 rows each (rather than 2 output partitions of 5 rows each)? -- 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-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org