-
Notifications
You must be signed in to change notification settings - Fork 226
[AURON #2177] Implement native support for lag window function #2199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -129,6 +129,7 @@ enum WindowFunction { | |
| ROW_NUMBER = 0; | ||
| RANK = 1; | ||
| DENSE_RANK = 2; | ||
| LAG = 3; | ||
| } | ||
|
|
||
| enum AggFunction { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| // 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. | ||
|
|
||
| use std::sync::Arc; | ||
|
|
||
| use arrow::{array::ArrayRef, datatypes::DataType, record_batch::RecordBatch}; | ||
| use datafusion::{ | ||
| common::{DataFusionError, Result, ScalarValue}, | ||
| physical_expr::PhysicalExprRef, | ||
| }; | ||
| use datafusion_ext_commons::arrow::cast::cast; | ||
|
|
||
| use crate::window::{WindowFunctionProcessor, window_context::WindowContext}; | ||
|
|
||
| pub struct LagProcessor { | ||
| children: Vec<PhysicalExprRef>, | ||
| } | ||
|
|
||
| impl LagProcessor { | ||
| pub fn new(children: Vec<PhysicalExprRef>) -> Self { | ||
| Self { children } | ||
| } | ||
| } | ||
|
|
||
| impl WindowFunctionProcessor for LagProcessor { | ||
| fn process_batch(&mut self, context: &WindowContext, batch: &RecordBatch) -> Result<ArrayRef> { | ||
| assert_eq!( | ||
| self.children.len(), | ||
| 3, | ||
| "lag expects input/offset/default children", | ||
| ); | ||
|
Comment on lines
+39
to
+43
|
||
|
|
||
| let input_values = self.children[0] | ||
| .evaluate(batch) | ||
| .and_then(|v| v.into_array(batch.num_rows()))?; | ||
|
|
||
| let offset_values = self.children[1] | ||
| .evaluate(batch) | ||
| .and_then(|v| v.into_array(batch.num_rows()))?; | ||
| let offset_values = if offset_values.data_type() == &DataType::Int32 { | ||
| offset_values | ||
| } else { | ||
| cast(&offset_values, &DataType::Int32)? | ||
| }; | ||
| let offset = match ScalarValue::try_from_array(&offset_values, 0)? { | ||
| ScalarValue::Int32(Some(offset)) => offset as i64, | ||
| other => { | ||
| return Err(DataFusionError::Execution(format!( | ||
| "lag offset must be a non-null foldable integer, got {other:?}", | ||
| ))); | ||
| } | ||
| }; | ||
|
|
||
|
Comment on lines
+45
to
+65
|
||
| let default_values = self.children[2] | ||
| .evaluate(batch) | ||
| .and_then(|v| v.into_array(batch.num_rows()))?; | ||
| let default_values = if default_values.data_type() == input_values.data_type() { | ||
| default_values | ||
| } else { | ||
| cast(&default_values, input_values.data_type())? | ||
| }; | ||
|
|
||
| let mut partition_starts = vec![0usize; batch.num_rows()]; | ||
| let mut partition_ends = vec![batch.num_rows(); batch.num_rows()]; | ||
| if context.has_partition() && batch.num_rows() > 0 { | ||
| let partition_rows = context.get_partition_rows(batch)?; | ||
| let mut partition_start = 0usize; | ||
| for row_idx in 1..=batch.num_rows() { | ||
| let is_boundary = row_idx == batch.num_rows() | ||
| || partition_rows.row(row_idx).as_ref() | ||
| != partition_rows.row(partition_start).as_ref(); | ||
| if is_boundary { | ||
| for idx in partition_start..row_idx { | ||
| partition_starts[idx] = partition_start; | ||
| partition_ends[idx] = row_idx; | ||
| } | ||
| partition_start = row_idx; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let mut output = Vec::with_capacity(batch.num_rows()); | ||
| for row_idx in 0..batch.num_rows() { | ||
| // lag looks backward: target is offset rows before current row | ||
| let target_idx = row_idx as i64 - offset; | ||
| let partition_start = partition_starts[row_idx] as i64; | ||
| let partition_end = partition_ends[row_idx] as i64; | ||
| let value = if target_idx >= partition_start && target_idx < partition_end { | ||
| ScalarValue::try_from_array(&input_values, target_idx as usize)? | ||
| } else { | ||
| ScalarValue::try_from_array(&default_values, row_idx)? | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I understand correctly, the row-by-row ScalarValue::try_from_array + ScalarValue::iter_to_array pattern creates N heap-allocated scalar objects. For large partitions that full-partition buffering implies, this is will become an issue. Can we use arrow::compute::take with a pre-computed indices array to gather values in O(1) allocations? |
||
| }; | ||
| output.push(value); | ||
| } | ||
|
|
||
| ScalarValue::iter_to_array(output) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LAG = 3collides withLEAD = 3, which is already on master. Two values sharing the same ordinal in one enum won't compile withoutallow_alias, so on rebase this needs a fresh number (8 is the next free one). Flagging because a textual merge can hide it — the conflict is on the comment/whitespace, not the= 3, so it can resolve cleanly and still break the build. (If lag ends up reusing theLEADpath per the other comment, this enum entry goes away entirely.)