use smallvec::SmallVec;
use super::PlanRef;
#[allow(rustdoc::private_intra_doc_links)]
pub trait PlanTreeNode {
fn children(&self) -> SmallVec<[PlanRef; 2]>;
fn clone_with_children(&self, children: &[PlanRef]) -> PlanRef;
}
pub trait PlanTreeNodeLeaf: Clone {}
pub trait PlanTreeNodeUnary {
fn child(&self) -> PlanRef;
#[must_use]
fn clone_with_child(&self, child: PlanRef) -> Self;
}
pub trait PlanTreeNodeBinary {
fn left(&self) -> PlanRef;
fn right(&self) -> PlanRef;
#[must_use]
fn clone_with_left_right(&self, left: PlanRef, right: PlanRef) -> Self;
}
macro_rules! impl_plan_tree_node_for_leaf {
($leaf_node_type:ident) => {
impl crate::v1::optimizer::plan_nodes::PlanTreeNode for $leaf_node_type {
fn children(
&self,
) -> smallvec::SmallVec<[crate::v1::optimizer::plan_nodes::PlanRef; 2]> {
smallvec::smallvec![]
}
fn clone_with_children(
&self,
children: &[crate::v1::optimizer::plan_nodes::PlanRef],
) -> crate::v1::optimizer::plan_nodes::PlanRef {
assert_eq!(children.len(), 0);
std::sync::Arc::new(self.clone())
}
}
};
}
pub(crate) use impl_plan_tree_node_for_leaf;
macro_rules! impl_plan_tree_node_for_unary {
($unary_node_type:ident) => {
impl crate::v1::optimizer::plan_nodes::PlanTreeNode for $unary_node_type {
fn children(
&self,
) -> smallvec::SmallVec<[crate::v1::optimizer::plan_nodes::PlanRef; 2]> {
smallvec::smallvec![self.child()]
}
fn clone_with_children(
&self,
children: &[crate::v1::optimizer::plan_nodes::PlanRef],
) -> crate::v1::optimizer::plan_nodes::PlanRef {
assert_eq!(children.len(), 1);
std::sync::Arc::new(self.clone_with_child(children[0].clone()))
}
}
};
}
pub(crate) use impl_plan_tree_node_for_unary;
macro_rules! impl_plan_tree_node_for_binary {
($binary_node_type:ident) => {
impl crate::v1::optimizer::plan_nodes::PlanTreeNode for $binary_node_type {
fn children(
&self,
) -> smallvec::SmallVec<[crate::v1::optimizer::plan_nodes::PlanRef; 2]> {
smallvec::smallvec![self.left(), self.right()]
}
fn clone_with_children(
&self,
children: &[crate::v1::optimizer::plan_nodes::PlanRef],
) -> crate::v1::optimizer::plan_nodes::PlanRef {
assert_eq!(children.len(), 2);
std::sync::Arc::new(
self.clone_with_left_right(children[0].clone(), children[1].clone()),
)
}
}
};
}
pub(crate) use impl_plan_tree_node_for_binary;