-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Build aggregate schema in Aggregate::try_new #3739
Changes from 6 commits
be8a593
118ae17
f2c8094
9a52b51
fb11590
1fbdf19
6797b31
7c1191b
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 |
---|---|---|
|
@@ -15,9 +15,12 @@ | |
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use crate::logical_plan::builder::validate_unique_names; | ||
use crate::logical_plan::display::{GraphvizVisitor, IndentVisitor}; | ||
use crate::logical_plan::extension::UserDefinedLogicalNode; | ||
use crate::utils::{exprlist_to_fields, grouping_set_expr_count}; | ||
use crate::utils::{ | ||
exprlist_to_fields, grouping_set_expr_count, grouping_set_to_exprlist, | ||
}; | ||
use crate::{Expr, TableProviderFilterPushDown, TableSource}; | ||
use arrow::datatypes::{DataType, Field, Schema, SchemaRef}; | ||
use datafusion_common::{plan_err, Column, DFSchema, DFSchemaRef, DataFusionError}; | ||
|
@@ -1334,10 +1337,28 @@ pub struct Aggregate { | |
} | ||
|
||
impl Aggregate { | ||
/// Create a new aggregate operator. | ||
pub fn try_new( | ||
input: Arc<LogicalPlan>, | ||
group_expr: Vec<Expr>, | ||
aggr_expr: Vec<Expr>, | ||
) -> datafusion_common::Result<Self> { | ||
let grouping_expr: Vec<Expr> = grouping_set_to_exprlist(group_expr.as_slice())?; | ||
let all_expr = grouping_expr.iter().chain(aggr_expr.iter()); | ||
validate_unique_names("Aggregations", all_expr.clone())?; | ||
let schema = DFSchema::new_with_metadata( | ||
exprlist_to_fields(all_expr, &input)?, | ||
input.schema().metadata().clone(), | ||
)?; | ||
Comment on lines
+1346
to
+1352
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. This is the code moved from |
||
Self::try_new_with_schema(input, group_expr, aggr_expr, Arc::new(schema)) | ||
} | ||
|
||
/// Create a new aggregate operator using the provided schema to avoid the overhead of | ||
/// building the schema again when the schema is already known. | ||
pub fn try_new_with_schema( | ||
andygrove marked this conversation as resolved.
Show resolved
Hide resolved
|
||
input: Arc<LogicalPlan>, | ||
group_expr: Vec<Expr>, | ||
aggr_expr: Vec<Expr>, | ||
schema: DFSchemaRef, | ||
) -> datafusion_common::Result<Self> { | ||
if group_expr.is_empty() && aggr_expr.is_empty() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -303,24 +303,22 @@ fn optimize_plan( | |
LogicalPlan::Aggregate(Aggregate { | ||
group_expr, | ||
aggr_expr, | ||
schema, | ||
input, | ||
.. | ||
}) => { | ||
// aggregate: | ||
// * remove any aggregate expression that is not required | ||
// * construct the new set of required columns | ||
|
||
// Find distinct group by exprs in the case where we have a grouping set | ||
let all_group_expr: Vec<Expr> = grouping_set_to_exprlist(group_expr)?; | ||
|
||
exprlist_to_columns(&all_group_expr, &mut new_required_columns)?; | ||
|
||
// Gather all columns needed for expressions in this Aggregate | ||
let mut new_aggr_expr = Vec::new(); | ||
aggr_expr.iter().try_for_each(|expr| { | ||
let name = &expr.name()?; | ||
let column = Column::from_name(name); | ||
|
||
if required_columns.contains(&column) { | ||
new_aggr_expr.push(expr.clone()); | ||
new_required_columns.insert(column); | ||
|
@@ -332,16 +330,6 @@ fn optimize_plan( | |
} | ||
})?; | ||
|
||
let new_schema = DFSchema::new_with_metadata( | ||
schema | ||
.fields() | ||
.iter() | ||
.filter(|x| new_required_columns.contains(&x.qualified_column())) | ||
.cloned() | ||
.collect(), | ||
schema.metadata().clone(), | ||
)?; | ||
Comment on lines
-335
to
-343
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. This code was problematic and is now replaced with the code used elsewhere 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. yes I agree having the caller have to specify (correctly) the aggregate schema is a recipe for disaster |
||
|
||
Ok(LogicalPlan::Aggregate(Aggregate::try_new( | ||
Arc::new(optimize_plan( | ||
_optimizer, | ||
|
@@ -352,7 +340,6 @@ fn optimize_plan( | |
)?), | ||
group_expr.clone(), | ||
new_aggr_expr, | ||
DFSchemaRef::new(new_schema), | ||
)?)) | ||
} | ||
// scans: | ||
|
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.
This code is moved into
Aggregate::try_new