fn optimize(&mut self, plan: &LogicalPlan) -> Result<LogicalPlan> {
        match plan {
            // Note: this code simply looks for the pattern of a Limit followed by a
            // Sort and replaces it by a TopK node. It does not handle many
            // edge cases (e.g multiple sort columns, sort ASC / DESC), etc.
            LogicalPlan::Limit { ref n, ref input } => {
                if let LogicalPlan::Sort {
                    ref expr,
                    ref input,
                } = **input
                {
                    if expr.len() == 1 {
                        // we found a sort with a single sort expr, replace with a a TopK
                        return Ok(LogicalPlan::Extension {
                            node: Arc::new(TopKPlanNode {
                                k: *n,
                                input: self.optimize(input.as_ref())?,
                                expr: expr[0].clone(),
                            }),
                        });
                    }
                }
            }
            // Due to the way explain is implemented, in order to get
            // explain functionality we need to explicitly handle it
            // here.
            LogicalPlan::Explain {
                verbose,
                plan,
                stringified_plans,
                schema,
            } => {
                return optimize_explain(
                    self,
                    *verbose,
                    &*plan,
                    stringified_plans,
                    &schema.as_ref().to_owned().into(),
                )
            }
            _ => {}
        }

        // If we didn't find the Limit/Sort combination, recurse as
        // normal and build the result.
        self.optimize_children(plan)
    }
// Example rewrite pass to insert a user defined LogicalPlanNode
Example rewrite pass to insert a user defined LogicalPlanNode
Example rewrite pass to insert a user defined LogicalPlanNode
fn optimize(&mut self, plan: &LogicalPlan) -> Result<LogicalPlan> {
        match plan {
            // Note: this code simply looks for the pattern of a Limit followed by a
            // Sort and replaces it by a TopK node. It does not handle many
            // edge cases (e.g multiple sort columns, sort ASC / DESC), etc.
            LogicalPlan::Limit { ref n, ref input } => {
                if let LogicalPlan::Sort {
                    ref expr,
                    ref input,
                } = **input
                {
                    if expr.len() == 1 {
                        // we found a sort with a single sort expr, replace with a a TopK
                        return Ok(LogicalPlan::Extension {
                            node: Arc::new(TopKPlanNode {
                                k: *n,
                                input: self.optimize(input.as_ref())?,
                                expr: expr[0].clone(),
                            }),
                        });
                    }
                }
            }
            // Due to the way explain is implemented, in order to get
            // explain functionality we need to explicitly handle it
            // here.
            LogicalPlan::Explain {
                verbose,
                plan,
                stringified_plans,
                schema,
            } => {
                return optimize_explain(
                    self,
                    *verbose,
                    &*plan,
                    stringified_plans,
                    &schema.as_ref().to_owned().into(),
                )
            }
            _ => {}
        }
        // If we didn't find the Limit/Sort combination, recurse as
        // normal and build the result.
        self.optimize_children(plan)
    }
fn optimize ( & mut self , plan : & LogicalPlan ) -> Result < LogicalPlan > { match plan { // Note: this code simply looks for the pattern of a Limit followed by a // Sort and replaces it by a TopK node. It does not handle many // edge cases (e.g multiple sort columns, sort ASC / DESC), etc. LogicalPlan :: Limit { ref n , ref input } => { if let LogicalPlan :: Sort { ref expr , ref input , } = * * input { if expr . len ( ) == 1 { // we found a sort with a single sort expr, replace with a a TopK return Ok ( LogicalPlan :: Extension { node : Arc :: new ( TopKPlanNode { k : * n , input : self . optimize ( input . as_ref ( ) ) ? , expr : expr [ 0 ] . clone ( ) , } ) , } ) ; } } } // Due to the way explain is implemented, in order to get // explain functionality we need to explicitly handle it // here. LogicalPlan :: Explain { verbose , plan , stringified_plans , schema , } => { return optimize_explain ( self , * verbose , & * plan , stringified_plans , & schema . as_ref ( ) . to_owned ( ) . into ( ) , ) } _ => { } } // If we didn't find the Limit/Sort combination, recurse as // normal and build the result. self . optimize_children ( plan ) }
Example rewrite pass to insert a user defined LogicalPlanNode
Example rewrite pass to insert a user defined LogicalPlanNode
&mut self
plan
&LogicalPlan
&mut self
plan
&LogicalPlan