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

refactoring: make "add_select" Plan method.

Also fix some documetation style
parent 873cbeeb
No related branches found
No related tags found
1 merge request!1414sbroad import
......@@ -191,35 +191,6 @@ impl Relational {
}
}
/// New `Selection` constructor
///
/// # Errors
/// Returns `QueryPlannerError`:
/// - filter expression is not boolean
/// - child node is not relational
/// - child output tuple is not valid
pub fn new_select(
plan: &mut Plan,
child: usize,
filter: usize,
) -> Result<Self, QueryPlannerError> {
if let Node::Expression(Expression::Bool { .. }) = plan.get_node(filter)? {
} else {
return Err(QueryPlannerError::InvalidBool);
}
let id = plan.nodes.next_id();
let children: Vec<usize> = vec![child];
let output = plan.add_output_row(id, &children, &[0], &[])?;
Ok(Relational::Selection {
children,
filter,
id,
output,
})
}
/// New `UnionAll` constructor.
///
/// # Errors
......@@ -290,7 +261,7 @@ impl Plan {
/// Add a scan node.
///
/// # Errors
/// Returns `QueryPlannerError` when when relation is invalid.
/// - relation is invalid
pub fn add_scan(&mut self, table: &str) -> Result<usize, QueryPlannerError> {
let logical_id = self.nodes.next_id();
let nodes = &mut self.nodes;
......@@ -325,10 +296,9 @@ impl Plan {
// TODO: we need a more flexible projection constructor (constants, etc)
/// New `Projection` constructor.
/// Add projection node.
///
/// # Errors
/// Returns `QueryPlannerError`:
/// - child node is not relational
/// - child output tuple is invalid
/// - column name do not match the ones in the child output tuple
......@@ -348,6 +318,32 @@ impl Plan {
};
Ok(self.nodes.push(Node::Relational(proj)))
}
/// Add selection node
///
/// # Errors
/// - filter expression is not boolean
/// - child node is not relational
/// - child output tuple is not valid
pub fn add_select(&mut self, child: usize, filter: usize) -> Result<usize, QueryPlannerError> {
if let Node::Expression(Expression::Bool { .. }) = self.get_node(filter)? {
} else {
return Err(QueryPlannerError::InvalidBool);
}
let id = self.nodes.next_id();
let children: Vec<usize> = vec![child];
let output = self.add_output_row(id, &children, &[0], &[])?;
let select = Relational::Selection {
children,
filter,
id,
output,
};
Ok(self.nodes.push(Node::Relational(select)))
}
}
#[cfg(test)]
......
......@@ -155,18 +155,18 @@ fn selection() {
let gt_id = plan.nodes.add_bool(a_id, Bool::Gt, const_id).unwrap();
// Correct Selection operator
Relational::new_select(&mut plan, scan_id, gt_id).unwrap();
plan.add_select(scan_id, gt_id).unwrap();
// Non-boolean filter
assert_eq!(
QueryPlannerError::InvalidBool,
Relational::new_select(&mut plan, scan_id, const_id).unwrap_err()
plan.add_select(scan_id, const_id).unwrap_err()
);
// Non-relational child
assert_eq!(
QueryPlannerError::InvalidNode,
Relational::new_select(&mut plan, const_id, gt_id).unwrap_err()
plan.add_select(const_id, gt_id).unwrap_err()
);
}
......
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