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

refactoring: move "add_proj" to Plan methods

Also make a "add_bool" test fixup
parent 8db540f1
No related branches found
No related tags found
1 merge request!1414sbroad import
......@@ -23,9 +23,7 @@ fn proj_preserve_dist_key() {
plan.add_rel(t);
let scan_id = plan.add_scan("t").unwrap();
let proj = Relational::new_proj(&mut plan, scan_id, &["a", "b"]).unwrap();
let proj_id = plan.nodes.push(Node::Relational(proj));
let proj_id = plan.add_proj(scan_id, &["a", "b"]).unwrap();
plan.top = Some(proj_id);
......
......@@ -191,31 +191,6 @@ impl Relational {
}
}
// TODO: we need a more flexible projection constructor (constants, etc)
/// New `Projection` constructor.
///
/// # 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
pub fn new_proj(
plan: &mut Plan,
child: usize,
col_names: &[&str],
) -> Result<Self, QueryPlannerError> {
let id = plan.nodes.next_id();
let children: Vec<usize> = vec![child];
let output = plan.add_output_row(id, &children, &[0], col_names)?;
Ok(Relational::Projection {
children,
id,
output,
})
}
/// New `Selection` constructor
///
/// # Errors
......@@ -347,6 +322,32 @@ impl Plan {
}
Err(QueryPlannerError::InvalidRelation)
}
// TODO: we need a more flexible projection constructor (constants, etc)
/// New `Projection` constructor.
///
/// # 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
pub fn add_proj(
&mut self,
child: usize,
col_names: &[&str],
) -> Result<usize, QueryPlannerError> {
let id = self.nodes.next_id();
let children: Vec<usize> = vec![child];
let output = self.add_output_row(id, &children, &[0], col_names)?;
let proj = Relational::Projection {
children,
id,
output,
};
Ok(self.nodes.push(Node::Relational(proj)))
}
}
#[cfg(test)]
......
use super::*;
use crate::errors::QueryPlannerError;
use crate::ir::distribution::*;
use crate::ir::expression::*;
use crate::ir::relation::*;
use crate::ir::value::*;
use crate::ir::*;
......@@ -103,19 +102,19 @@ fn projection() {
// Invalid alias names in the output
assert_eq!(
QueryPlannerError::InvalidRow,
Relational::new_proj(&mut plan, scan_id, &["a", "e"]).unwrap_err()
plan.add_proj(scan_id, &["a", "e"]).unwrap_err()
);
// Expression node instead of relational one
assert_eq!(
QueryPlannerError::InvalidNode,
Relational::new_proj(&mut plan, 1, &["a"]).unwrap_err()
plan.add_proj(1, &["a"]).unwrap_err()
);
// Try to build projection from the invalid node
// Try to build projection from the non-existing node
assert_eq!(
QueryPlannerError::ValueOutOfRange,
Relational::new_proj(&mut plan, 42, &["a"]).unwrap_err()
plan.add_proj(42, &["a"]).unwrap_err()
);
}
......@@ -153,7 +152,7 @@ fn selection() {
let ref_a_id = plan.nodes.add_ref(scan_id + 1, Some(vec![0]), 0);
let a_id = plan.nodes.add_alias("a", ref_a_id).unwrap();
let const_id = plan.nodes.add_const(Value::number_from_str("10").unwrap());
let gt_id = plan.nodes.add_bool(a_id, Bool::Gt, const_id)?;
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();
......
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