forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
AggregateExpr
, PhysicalExpr
and PhysicalSortExpr
to physic…
…al-expr-core (apache#9926) * move PhysicalExpr Signed-off-by: jayzhan211 <[email protected]> * cleanup Signed-off-by: jayzhan211 <[email protected]> * move physical sort Signed-off-by: jayzhan211 <[email protected]> * cleanup dependencies Signed-off-by: jayzhan211 <[email protected]> * add readme Signed-off-by: jayzhan211 <[email protected]> * disable doc test Signed-off-by: jayzhan211 <[email protected]> * move column Signed-off-by: jayzhan211 <[email protected]> * fmt Signed-off-by: jayzhan211 <[email protected]> * move aggregatexp Signed-off-by: jayzhan211 <[email protected]> * move other two utils Signed-off-by: jayzhan211 <[email protected]> * license Signed-off-by: jayzhan211 <[email protected]> * switch to ignore Signed-off-by: jayzhan211 <[email protected]> * move reverse order Signed-off-by: jayzhan211 <[email protected]> * rename to common Signed-off-by: jayzhan211 <[email protected]> * cleanup Signed-off-by: jayzhan211 <[email protected]> --------- Signed-off-by: jayzhan211 <[email protected]>
- Loading branch information
1 parent
701abf7
commit 1dd5354
Showing
23 changed files
with
896 additions
and
621 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,38 @@ | ||
# 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. | ||
|
||
[package] | ||
name = "datafusion-physical-expr-common" | ||
description = "Common functionality of physical expression for DataFusion query engine" | ||
keywords = ["arrow", "query", "sql"] | ||
readme = "README.md" | ||
version = { workspace = true } | ||
edition = { workspace = true } | ||
homepage = { workspace = true } | ||
repository = { workspace = true } | ||
license = { workspace = true } | ||
authors = { workspace = true } | ||
rust-version = { workspace = true } | ||
|
||
[lib] | ||
name = "datafusion_physical_expr_common" | ||
path = "src/lib.rs" | ||
|
||
[dependencies] | ||
arrow = { workspace = true } | ||
datafusion-common = { workspace = true, default-features = true } | ||
datafusion-expr = { workspace = true } |
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,27 @@ | ||
<!--- | ||
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. | ||
--> | ||
|
||
# DataFusion Core Physical Expressions | ||
|
||
[DataFusion][df] is an extensible query execution framework, written in Rust, that uses Apache Arrow as its in-memory format. | ||
|
||
This crate is a submodule of DataFusion that provides shared APIs for implementing | ||
physical expressions such as `PhysicalExpr` and `PhysicalSortExpr`. | ||
|
||
[df]: https://crates.io/crates/datafusion |
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,102 @@ | ||
// 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. | ||
|
||
pub mod utils; | ||
|
||
use std::any::Any; | ||
use std::fmt::Debug; | ||
use std::sync::Arc; | ||
|
||
use crate::physical_expr::PhysicalExpr; | ||
use crate::sort_expr::PhysicalSortExpr; | ||
|
||
use arrow::datatypes::Field; | ||
use datafusion_common::{not_impl_err, Result}; | ||
use datafusion_expr::{Accumulator, GroupsAccumulator}; | ||
|
||
/// An aggregate expression that: | ||
/// * knows its resulting field | ||
/// * knows how to create its accumulator | ||
/// * knows its accumulator's state's field | ||
/// * knows the expressions from whose its accumulator will receive values | ||
/// | ||
/// Any implementation of this trait also needs to implement the | ||
/// `PartialEq<dyn Any>` to allows comparing equality between the | ||
/// trait objects. | ||
pub trait AggregateExpr: Send + Sync + Debug + PartialEq<dyn Any> { | ||
/// Returns the aggregate expression as [`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>; | ||
|
||
/// the accumulator used to accumulate values from the expressions. | ||
/// the accumulator expects the same number of arguments as `expressions` and must | ||
/// return states with the same description as `state_fields` | ||
fn create_accumulator(&self) -> Result<Box<dyn Accumulator>>; | ||
|
||
/// the fields that encapsulate the Accumulator's state | ||
/// the number of fields here equals the number of states that the accumulator contains | ||
fn state_fields(&self) -> Result<Vec<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>>; | ||
|
||
/// Order by requirements for the aggregate function | ||
/// By default it is `None` (there is no requirement) | ||
/// Order-sensitive aggregators, such as `FIRST_VALUE(x ORDER BY y)` should implement this | ||
fn order_bys(&self) -> Option<&[PhysicalSortExpr]> { | ||
None | ||
} | ||
|
||
/// Human readable name such as `"MIN(c2)"`. The default | ||
/// implementation returns placeholder text. | ||
fn name(&self) -> &str { | ||
"AggregateExpr: default name" | ||
} | ||
|
||
/// If the aggregate expression has a specialized | ||
/// [`GroupsAccumulator`] implementation. If this returns true, | ||
/// `[Self::create_groups_accumulator`] will be called. | ||
fn groups_accumulator_supported(&self) -> bool { | ||
false | ||
} | ||
|
||
/// Return a specialized [`GroupsAccumulator`] that manages state | ||
/// for all groups. | ||
/// | ||
/// For maximum performance, a [`GroupsAccumulator`] should be | ||
/// implemented in addition to [`Accumulator`]. | ||
fn create_groups_accumulator(&self) -> Result<Box<dyn GroupsAccumulator>> { | ||
not_impl_err!("GroupsAccumulator hasn't been implemented for {self:?} yet") | ||
} | ||
|
||
/// Construct an expression that calculates the aggregate in reverse. | ||
/// Typically the "reverse" expression is itself (e.g. SUM, COUNT). | ||
/// For aggregates that do not support calculation in reverse, | ||
/// returns None (which is the default value). | ||
fn reverse_expr(&self) -> Option<Arc<dyn AggregateExpr>> { | ||
None | ||
} | ||
|
||
/// Creates accumulator implementation that supports retract | ||
fn create_sliding_accumulator(&self) -> Result<Box<dyn Accumulator>> { | ||
not_impl_err!("Retractable Accumulator hasn't been implemented for {self:?} yet") | ||
} | ||
} |
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,69 @@ | ||
// 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::{any::Any, sync::Arc}; | ||
|
||
use arrow::{ | ||
compute::SortOptions, | ||
datatypes::{DataType, Field}, | ||
}; | ||
|
||
use crate::sort_expr::PhysicalSortExpr; | ||
|
||
use super::AggregateExpr; | ||
|
||
/// Downcast a `Box<dyn AggregateExpr>` or `Arc<dyn AggregateExpr>` | ||
/// and return the inner trait object as [`Any`] so | ||
/// that it can be downcast to a specific implementation. | ||
/// | ||
/// This method is used when implementing the `PartialEq<dyn Any>` | ||
/// for [`AggregateExpr`] aggregation expressions and allows comparing the equality | ||
/// between the trait objects. | ||
pub fn down_cast_any_ref(any: &dyn Any) -> &dyn Any { | ||
if let Some(obj) = any.downcast_ref::<Arc<dyn AggregateExpr>>() { | ||
obj.as_any() | ||
} else if let Some(obj) = any.downcast_ref::<Box<dyn AggregateExpr>>() { | ||
obj.as_any() | ||
} else { | ||
any | ||
} | ||
} | ||
|
||
/// Construct corresponding fields for lexicographical ordering requirement expression | ||
pub fn ordering_fields( | ||
ordering_req: &[PhysicalSortExpr], | ||
// Data type of each expression in the ordering requirement | ||
data_types: &[DataType], | ||
) -> Vec<Field> { | ||
ordering_req | ||
.iter() | ||
.zip(data_types.iter()) | ||
.map(|(sort_expr, dtype)| { | ||
Field::new( | ||
sort_expr.expr.to_string().as_str(), | ||
dtype.clone(), | ||
// Multi partitions may be empty hence field should be nullable. | ||
true, | ||
) | ||
}) | ||
.collect() | ||
} | ||
|
||
/// Selects the sort option attribute from all the given `PhysicalSortExpr`s. | ||
pub fn get_sort_options(ordering_req: &[PhysicalSortExpr]) -> Vec<SortOptions> { | ||
ordering_req.iter().map(|item| item.options).collect() | ||
} |
Oops, something went wrong.