Skip to content
Snippets Groups Projects
Commit 148d23ee authored by Denis Smirnov's avatar Denis Smirnov
Browse files

refactoring: reduce nested levels in Select::new()

parent ce6a4bb4
No related branches found
No related tags found
1 merge request!1414sbroad import
...@@ -220,62 +220,67 @@ impl Select { ...@@ -220,62 +220,67 @@ impl Select {
id: usize, id: usize,
) -> Result<Option<Select>, QueryPlannerError> { ) -> Result<Option<Select>, QueryPlannerError> {
let sn = sp.nodes.get_syntax_node(id)?; let sn = sp.nodes.get_syntax_node(id)?;
let pn = if let Some(node) = sp.get_plan_node(&sn.data)? { // Check if the node is a projection
node if let Some(Node::Relational(Relational::Projection { .. })) = sp.get_plan_node(&sn.data)? {
} else { } else {
return Ok(None); return Ok(None);
}
// Get the left node
let left_id = match sn.left {
Some(id) => id,
None => {
return Err(QueryPlannerError::CustomError(
"Projection can't be a leaf node".into(),
))
}
}; };
if let Node::Relational(Relational::Projection { .. }) = pn { let syntax_node_left = sp.nodes.get_syntax_node(left_id)?;
if let Some(lev_1) = sn.left { let plan_node_left = sp
let syntax_node_lev_1 = sp.nodes.get_syntax_node(lev_1)?; .get_plan_node(&syntax_node_left.data)?
let plan_node_lev_1 = sp .ok_or(QueryPlannerError::InvalidNode)?;
.get_plan_node(&syntax_node_lev_1.data)? match plan_node_left {
.ok_or(QueryPlannerError::InvalidNode)?; // Expecting projection over selection and scan
match plan_node_lev_1 { Node::Relational(Relational::Selection { .. }) => {
Node::Relational(Relational::Selection { .. }) => { // Get the next left node
if let Some(lev_2) = syntax_node_lev_1.left { let next_left_id = match syntax_node_left.left {
let syntax_node_lev_2 = sp.nodes.get_syntax_node(lev_2)?; Some(id) => id,
let plan_node_lev_2 = sp None => {
.get_plan_node(&syntax_node_lev_2.data)? return Err(QueryPlannerError::CustomError(
.ok_or(QueryPlannerError::InvalidNode)?; "Selection can't be a leaf node".into(),
if let Node::Relational( ))
Relational::ScanRelation { .. } | Relational::ScanSubQuery { .. },
) = plan_node_lev_2
{
Ok(Some(Select {
parent,
branch,
proj: id,
scan: lev_2,
select: Some(lev_1),
}))
} else {
Err(QueryPlannerError::InvalidPlan)
}
} else {
Err(QueryPlannerError::CustomError(
"Selection can't be a leaf node".into(),
))
}
} }
Node::Relational( };
Relational::ScanRelation { .. } | Relational::ScanSubQuery { .. }, // We expect that the next left node is a scan
) => Ok(Some(Select { let syntax_node_next_left = sp.nodes.get_syntax_node(next_left_id)?;
parent, let plan_node_next_left = sp
branch, .get_plan_node(&syntax_node_next_left.data)?
.ok_or(QueryPlannerError::InvalidNode)?;
if let Node::Relational(
Relational::ScanRelation { .. } | Relational::ScanSubQuery { .. },
) = plan_node_next_left
{
Ok(Some(Select {
parent: parent,
branch: branch,
proj: id, proj: id,
scan: lev_1, scan: next_left_id,
select: None, select: Some(left_id),
})), }))
_ => Err(QueryPlannerError::InvalidPlan), } else {
Err(QueryPlannerError::InvalidPlan)
} }
} else {
Err(QueryPlannerError::CustomError(
"Projection can't be a leaf node".into(),
))
} }
} else { // Expecting projection over scan
Ok(None) Node::Relational(Relational::ScanRelation { .. } | Relational::ScanSubQuery { .. }) => {
Ok(Some(Select {
parent: parent,
branch: branch,
proj: id,
scan: left_id,
select: None,
}))
}
_ => Err(QueryPlannerError::InvalidPlan),
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment