Skip to content

Commit

Permalink
refactor(optimizer): refactor plan visitor with associated type (#12994)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzl25 authored Oct 23, 2023
1 parent 848bdda commit 7574a1b
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ impl PlanRewriter for ShareSourceRewriter {
}
}

impl PlanVisitor<()> for SourceCounter {
type DefaultBehavior = impl DefaultBehavior<()>;
impl PlanVisitor for SourceCounter {
type Result = ();

type DefaultBehavior = impl DefaultBehavior<Self::Result>;

fn default_behavior() -> Self::DefaultBehavior {
DefaultValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ impl CardinalityVisitor {
}
}

impl PlanVisitor<Cardinality> for CardinalityVisitor {
type DefaultBehavior = impl DefaultBehavior<Cardinality>;
impl PlanVisitor for CardinalityVisitor {
type Result = Cardinality;

type DefaultBehavior = impl DefaultBehavior<Self::Result>;

fn default_behavior() -> Self::DefaultBehavior {
// returns unknown cardinality for default behavior, which is always correct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ impl ExecutionModeDecider {
}
}

impl PlanVisitor<bool> for ExecutionModeDecider {
type DefaultBehavior = impl DefaultBehavior<bool>;
impl PlanVisitor for ExecutionModeDecider {
type Result = bool;

type DefaultBehavior = impl DefaultBehavior<Self::Result>;

fn default_behavior() -> Self::DefaultBehavior {
Merge(|a, b| a & b)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ macro_rules! visit_project {
};
}

impl PlanVisitor<Option<String>> for InputRefValidator {
type DefaultBehavior = impl DefaultBehavior<Option<String>>;
impl PlanVisitor for InputRefValidator {
type Result = Option<String>;

type DefaultBehavior = impl DefaultBehavior<Self::Result>;

visit_filter!(logical, batch, stream);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ use crate::optimizer::plan_visitor::PlanVisitor;

pub struct HasMaxOneRowApply();

impl PlanVisitor<bool> for HasMaxOneRowApply {
type DefaultBehavior = impl DefaultBehavior<bool>;
impl PlanVisitor for HasMaxOneRowApply {
type Result = bool;

type DefaultBehavior = impl DefaultBehavior<Self::Result>;

fn default_behavior() -> Self::DefaultBehavior {
Merge(|a, b| a | b)
Expand Down
16 changes: 9 additions & 7 deletions src/frontend/src/optimizer/plan_visitor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,15 @@ macro_rules! def_visitor {
($({ $convention:ident, $name:ident }),*) => {
/// The visitor for plan nodes. visit all inputs and return the ret value of the left most input,
/// and leaf node returns `R::default()`
pub trait PlanVisitor<R: Default> {
type DefaultBehavior: DefaultBehavior<R>;
pub trait PlanVisitor {
type Result: Default;
type DefaultBehavior: DefaultBehavior<Self::Result>;

/// The behavior for the default implementations of `visit_xxx`.
fn default_behavior() -> Self::DefaultBehavior;

paste! {
fn visit(&mut self, plan: PlanRef) -> R{
fn visit(&mut self, plan: PlanRef) -> Self::Result {
match plan.node_type() {
$(
PlanNodeType::[<$convention $name>] => self.[<visit_ $convention:snake _ $name:snake>](plan.downcast_ref::<[<$convention $name>]>().unwrap()),
Expand All @@ -97,7 +98,7 @@ macro_rules! def_visitor {

$(
#[doc = "Visit [`" [<$convention $name>] "`] , the function should visit the inputs."]
fn [<visit_ $convention:snake _ $name:snake>](&mut self, plan: &[<$convention $name>]) -> R {
fn [<visit_ $convention:snake _ $name:snake>](&mut self, plan: &[<$convention $name>]) -> Self::Result {
let results = plan.inputs().into_iter().map(|input| self.visit(input));
Self::default_behavior().apply(results)
}
Expand All @@ -121,17 +122,18 @@ macro_rules! impl_has_variant {
pred: P,
}

impl<P> PlanVisitor<bool> for HasWhere<P>
impl<P> PlanVisitor for HasWhere<P>
where
P: FnMut(&$variant) -> bool,
{
type DefaultBehavior = impl DefaultBehavior<bool>;
type Result = bool;
type DefaultBehavior = impl DefaultBehavior<Self::Result>;

fn default_behavior() -> Self::DefaultBehavior {
Merge(|a, b| a | b)
}

fn [<visit_ $variant:snake>](&mut self, node: &$variant) -> bool {
fn [<visit_ $variant:snake>](&mut self, node: &$variant) -> Self::Result {
(self.pred)(node)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ impl PlanCorrelatedIdFinder {
}
}

impl PlanVisitor<()> for PlanCorrelatedIdFinder {
impl PlanVisitor for PlanCorrelatedIdFinder {
/// `correlated_input_ref` can only appear in `LogicalProject`, `LogicalFilter`,
/// `LogicalJoin` or the `filter` clause of `PlanAggCall` of `LogicalAgg` now.
type DefaultBehavior = impl DefaultBehavior<()>;
type Result = ();

type DefaultBehavior = impl DefaultBehavior<Self::Result>;

fn default_behavior() -> Self::DefaultBehavior {
DefaultValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ impl RelationCollectorVisitor {
}
}

impl PlanVisitor<()> for RelationCollectorVisitor {
type DefaultBehavior = impl DefaultBehavior<()>;
impl PlanVisitor for RelationCollectorVisitor {
type Result = ();

type DefaultBehavior = impl DefaultBehavior<Self::Result>;

fn default_behavior() -> Self::DefaultBehavior {
DefaultValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ impl ShareParentCounter {
}
}

impl PlanVisitor<()> for ShareParentCounter {
type DefaultBehavior = impl DefaultBehavior<()>;
impl PlanVisitor for ShareParentCounter {
type Result = ();

type DefaultBehavior = impl DefaultBehavior<Self::Result>;

fn default_behavior() -> Self::DefaultBehavior {
DefaultValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ use crate::optimizer::plan_node;
/// eliminated trivially.
pub struct SideEffectVisitor;

impl PlanVisitor<bool> for SideEffectVisitor {
type DefaultBehavior = impl DefaultBehavior<bool>;
impl PlanVisitor for SideEffectVisitor {
type Result = bool;

type DefaultBehavior = impl DefaultBehavior<Self::Result>;

fn default_behavior() -> Self::DefaultBehavior {
Merge(|a, b| a | b)
Expand Down
6 changes: 4 additions & 2 deletions src/frontend/src/optimizer/plan_visitor/sys_table_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ impl SysTableVisitor {
}
}

impl PlanVisitor<bool> for SysTableVisitor {
type DefaultBehavior = impl DefaultBehavior<bool>;
impl PlanVisitor for SysTableVisitor {
type Result = bool;

type DefaultBehavior = impl DefaultBehavior<Self::Result>;

fn default_behavior() -> Self::DefaultBehavior {
Merge(|a, b| a | b)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ impl TemporalJoinValidator {
}
}

impl PlanVisitor<bool> for TemporalJoinValidator {
type DefaultBehavior = impl DefaultBehavior<bool>;
impl PlanVisitor for TemporalJoinValidator {
type Result = bool;

type DefaultBehavior = impl DefaultBehavior<Self::Result>;

fn default_behavior() -> Self::DefaultBehavior {
Merge(|a, b| a | b)
Expand Down

0 comments on commit 7574a1b

Please sign in to comment.