alamb commented on code in PR #10405:
URL: https://github.com/apache/datafusion/pull/10405#discussion_r1592580823
##########
datafusion/optimizer/src/optimize_projections/mod.rs:
##########
@@ -102,101 +118,19 @@ impl OptimizerRule for OptimizeProjections {
/// - `Ok(Some(LogicalPlan))`: An optimized `LogicalPlan` without unnecessary
/// columns.
/// - `Ok(None)`: Signal that the given logical plan did not require any
change.
-/// - `Err(error)`: An error occured during the optimization process.
+/// - `Err(error)`: An error occurred during the optimization process.
fn optimize_projections(
- plan: &LogicalPlan,
+ plan: LogicalPlan,
config: &dyn OptimizerConfig,
indices: RequiredIndicies,
-) -> Result<Option<LogicalPlan>> {
- let child_required_indices: Vec<RequiredIndicies> = match plan {
- LogicalPlan::Sort(_)
- | LogicalPlan::Filter(_)
- | LogicalPlan::Repartition(_)
- | LogicalPlan::Unnest(_)
- | LogicalPlan::Union(_)
- | LogicalPlan::SubqueryAlias(_)
- | LogicalPlan::Distinct(Distinct::On(_)) => {
- // Pass index requirements from the parent as well as column
indices
- // that appear in this plan's expressions to its child. All these
- // operators benefit from "small" inputs, so the
projection_beneficial
- // flag is `true`.
- plan.inputs()
- .into_iter()
- .map(|input| {
- indices
- .clone()
- .with_projection_beneficial()
- .with_plan_exprs(plan, input.schema())
- })
- .collect::<Result<_>>()?
- }
- LogicalPlan::Limit(_) | LogicalPlan::Prepare(_) => {
- // Pass index requirements from the parent as well as column
indices
- // that appear in this plan's expressions to its child. These
operators
- // do not benefit from "small" inputs, so the projection_beneficial
- // flag is `false`.
- plan.inputs()
- .into_iter()
- .map(|input| indices.clone().with_plan_exprs(plan,
input.schema()))
- .collect::<Result<_>>()?
- }
- LogicalPlan::Copy(_)
- | LogicalPlan::Ddl(_)
- | LogicalPlan::Dml(_)
- | LogicalPlan::Explain(_)
- | LogicalPlan::Analyze(_)
- | LogicalPlan::Subquery(_)
- | LogicalPlan::Distinct(Distinct::All(_)) => {
- // These plans require all their fields, and their children should
- // be treated as final plans -- otherwise, we may have schema a
- // mismatch.
- // TODO: For some subquery variants (e.g. a subquery arising from
an
- // EXISTS expression), we may not need to require all
indices.
- plan.inputs()
- .into_iter()
- .map(RequiredIndicies::new_for_all_exprs)
- .collect()
- }
- LogicalPlan::Extension(extension) => {
- let Some(necessary_children_indices) =
- extension.node.necessary_children_exprs(indices.indices())
- else {
- // Requirements from parent cannot be routed down to user
defined logical plan safely
- return Ok(None);
- };
- let children = extension.node.inputs();
- if children.len() != necessary_children_indices.len() {
- return internal_err!("Inconsistent length between children and
necessary children indices. \
- Make sure `.necessary_children_exprs` implementation of the
`UserDefinedLogicalNode` is \
- consistent with actual children length for the node.");
- }
- children
- .into_iter()
- .zip(necessary_children_indices)
- .map(|(child, necessary_indices)| {
- RequiredIndicies::new_from_indices(necessary_indices)
- .with_plan_exprs(plan, child.schema())
- })
- .collect::<Result<Vec<_>>>()?
- }
- LogicalPlan::EmptyRelation(_)
- | LogicalPlan::RecursiveQuery(_)
- | LogicalPlan::Statement(_)
- | LogicalPlan::Values(_)
- | LogicalPlan::DescribeTable(_) => {
- // These operators have no inputs, so stop the optimization
process.
- return Ok(None);
- }
+) -> Result<Transformed<LogicalPlan>> {
+ // Recursively rewrite any nodes that may be able to avoid computation
given
+ // their parents' required indices.
+ match plan {
Review Comment:
The order of handling plan nodes changed so the ones that rewrite the `plan`
directly do so in the first `match` statement and those that compute required
indices do so in a second `match`
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]