-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move built in window expr and partition evaluator (#1865)
- Loading branch information
Showing
15 changed files
with
187 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
datafusion-physical-expr/src/window/built_in_window_function_expr.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// 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 super::partition_evaluator::PartitionEvaluator; | ||
use crate::PhysicalExpr; | ||
use arrow::datatypes::Field; | ||
use arrow::record_batch::RecordBatch; | ||
use datafusion_common::Result; | ||
use std::any::Any; | ||
use std::sync::Arc; | ||
|
||
/// A window expression that is a built-in window function. | ||
/// | ||
/// Note that unlike aggregation based window functions, built-in window functions normally ignore | ||
/// window frame spec, with the exception of first_value, last_value, and nth_value. | ||
pub trait BuiltInWindowFunctionExpr: Send + Sync + std::fmt::Debug { | ||
/// Returns the aggregate expression as [`Any`](std::any::Any) so that it can be | ||
/// downcast to a specific implementation. | ||
fn as_any(&self) -> &dyn Any; | ||
|
||
/// the field of the final result of this aggregation. | ||
fn field(&self) -> Result<Field>; | ||
|
||
/// expressions that are passed to the Accumulator. | ||
/// Single-column aggregations such as `sum` return a single value, others (e.g. `cov`) return many. | ||
fn expressions(&self) -> Vec<Arc<dyn PhysicalExpr>>; | ||
|
||
/// Human readable name such as `"MIN(c2)"` or `"RANK()"`. The default | ||
/// implementation returns placeholder text. | ||
fn name(&self) -> &str { | ||
"BuiltInWindowFunctionExpr: default name" | ||
} | ||
|
||
/// Create built-in window evaluator with a batch | ||
fn create_evaluator( | ||
&self, | ||
batch: &RecordBatch, | ||
) -> Result<Box<dyn PartitionEvaluator>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// 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. | ||
|
||
mod built_in_window_function_expr; | ||
mod partition_evaluator; | ||
mod window_expr; | ||
|
||
pub use built_in_window_function_expr::BuiltInWindowFunctionExpr; | ||
pub use partition_evaluator::find_ranges_in_range; | ||
pub use partition_evaluator::PartitionEvaluator; | ||
pub use window_expr::WindowExpr; |
84 changes: 84 additions & 0 deletions
84
datafusion-physical-expr/src/window/partition_evaluator.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// 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 arrow::array::ArrayRef; | ||
use datafusion_common::DataFusionError; | ||
use datafusion_common::Result; | ||
use std::ops::Range; | ||
|
||
/// Given a partition range, and the full list of sort partition points, given that the sort | ||
/// partition points are sorted using [partition columns..., order columns...], the split | ||
/// boundaries would align (what's sorted on [partition columns...] would definitely be sorted | ||
/// on finer columns), so this will use binary search to find ranges that are within the | ||
/// partition range and return the valid slice. | ||
pub fn find_ranges_in_range<'a>( | ||
partition_range: &Range<usize>, | ||
sort_partition_points: &'a [Range<usize>], | ||
) -> &'a [Range<usize>] { | ||
let start_idx = sort_partition_points | ||
.partition_point(|sort_range| sort_range.start < partition_range.start); | ||
let end_idx = start_idx | ||
+ sort_partition_points[start_idx..] | ||
.partition_point(|sort_range| sort_range.end <= partition_range.end); | ||
&sort_partition_points[start_idx..end_idx] | ||
} | ||
|
||
/// Partition evaluator | ||
pub trait PartitionEvaluator { | ||
/// Whether the evaluator should be evaluated with rank | ||
fn include_rank(&self) -> bool { | ||
false | ||
} | ||
|
||
/// evaluate the partition evaluator against the partitions | ||
fn evaluate(&self, partition_points: Vec<Range<usize>>) -> Result<Vec<ArrayRef>> { | ||
partition_points | ||
.into_iter() | ||
.map(|partition| self.evaluate_partition(partition)) | ||
.collect() | ||
} | ||
|
||
/// evaluate the partition evaluator against the partitions with rank information | ||
fn evaluate_with_rank( | ||
&self, | ||
partition_points: Vec<Range<usize>>, | ||
sort_partition_points: Vec<Range<usize>>, | ||
) -> Result<Vec<ArrayRef>> { | ||
partition_points | ||
.into_iter() | ||
.map(|partition| { | ||
let ranks_in_partition = | ||
find_ranges_in_range(&partition, &sort_partition_points); | ||
self.evaluate_partition_with_rank(partition, ranks_in_partition) | ||
}) | ||
.collect() | ||
} | ||
|
||
/// evaluate the partition evaluator against the partition | ||
fn evaluate_partition(&self, _partition: Range<usize>) -> Result<ArrayRef>; | ||
|
||
/// evaluate the partition evaluator against the partition but with rank | ||
fn evaluate_partition_with_rank( | ||
&self, | ||
_partition: Range<usize>, | ||
_ranks_in_partition: &[Range<usize>], | ||
) -> Result<ArrayRef> { | ||
Err(DataFusionError::NotImplemented( | ||
"evaluate_partition_with_rank is not implemented by default".into(), | ||
)) | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.