use crate::node::Node;
use crate::plan::Plan;
use crate::{NodeRef, PipelineRef};
#[derive(Debug, Clone)]
pub struct Pipelines {
of: Vec<Option<PipelineRef>>,
waits: Vec<Vec<PipelineRef>>,
}
impl Pipelines {
#[must_use]
pub fn of(plan: &Plan) -> Self {
let mut pipelines = Self { of: vec![None; plan.node_count()], waits: vec![Vec::new()] };
pipelines.walk(plan, plan.root(), ROOT);
pipelines
}
#[must_use]
pub fn len(&self) -> usize {
self.waits.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.waits.is_empty()
}
#[must_use]
pub fn pipeline(&self, node: NodeRef) -> PipelineRef {
self.of[node as usize].expect("a node under the root of the plan it was walked from")
}
#[must_use]
pub fn waits_for(&self, pipeline: PipelineRef) -> &[PipelineRef] {
&self.waits[pipeline as usize]
}
pub fn all(&self) -> impl Iterator<Item = PipelineRef> {
0..u32::try_from(self.waits.len()).unwrap_or(u32::MAX)
}
fn fresh(&mut self) -> PipelineRef {
self.waits.push(Vec::new());
u32::try_from(self.waits.len() - 1).unwrap_or(u32::MAX)
}
fn waits_on(&mut self, pipeline: PipelineRef, on: PipelineRef) {
self.waits[pipeline as usize].push(on);
}
fn walk(&mut self, plan: &Plan, node: NodeRef, pipeline: PipelineRef) {
match *plan.node(node) {
Node::Aggregate { input, .. }
| Node::Sort { input, .. }
| Node::TopN { input, .. }
| Node::Distinct { input, .. } => {
let below = self.fresh();
self.waits_on(pipeline, below);
self.of[node as usize] = Some(below);
self.walk(plan, input, below);
}
Node::Join { left, right, .. } | Node::SetOp { left, right, .. } => {
let first = self.fresh();
let second = self.fresh();
self.waits_on(second, first);
self.waits_on(pipeline, second);
self.of[node as usize] = Some(second);
self.walk(plan, right, first);
self.walk(plan, left, second);
}
Node::CrossProduct { left, right } => {
let aside = self.fresh();
self.waits_on(pipeline, aside);
self.of[node as usize] = Some(pipeline);
self.walk(plan, right, aside);
self.walk(plan, left, pipeline);
}
ref other => {
self.of[node as usize] = Some(pipeline);
for child in other.children().into_iter().flatten() {
self.walk(plan, child, pipeline);
}
}
}
}
}
const ROOT: PipelineRef = 0;
#[cfg(test)]
mod tests {
use super::Pipelines;
use crate::plan::Plan;
fn decomposed(text: &str) -> (Plan, Pipelines) {
let plan =
Plan::parse(text).unwrap_or_else(|error| panic!("{text} did not parse: {error}"));
let pipelines = Pipelines::of(&plan);
(plan, pipelines)
}
#[test]
fn a_plan_with_nothing_that_buffers_is_one_pipeline() {
let (plan, pipelines) = decomposed(concat!(
"Filter (#0.0::INTEGER > 1::INTEGER)::BOOLEAN\n",
" Get memory.main.t AS t #0 [a::INTEGER]\n",
));
assert_eq!(pipelines.len(), 1);
assert_eq!(pipelines.pipeline(plan.root()), 0);
assert!(pipelines.waits_for(0).is_empty());
}
#[test]
fn a_sort_ends_the_pipeline_below_it_and_the_one_above_waits() {
let (plan, pipelines) = decomposed(concat!(
"Sort [#0.0::INTEGER ASC NULLS LAST]\n",
" Get memory.main.t AS t #0 [a::INTEGER]\n",
));
assert_eq!(pipelines.len(), 2);
assert_eq!(pipelines.pipeline(plan.root()), 1, "the sort is the sink of the one below");
assert_eq!(pipelines.waits_for(0), [1]);
assert!(pipelines.waits_for(1).is_empty());
}
#[test]
fn a_join_is_two_pipelines_in_the_order_they_have_to_run() {
let (plan, pipelines) = decomposed(concat!(
"Join INNER on=[(#0.0::INTEGER = #1.0::INTEGER)::BOOLEAN]\n",
" Get memory.main.l AS l #0 [a::INTEGER]\n",
" Get memory.main.r AS r #1 [a::INTEGER]\n",
));
let [left, right] = plan.node(plan.root()).children();
assert_eq!(pipelines.len(), 3);
assert_eq!(pipelines.pipeline(right.unwrap()), 1, "the gathered side runs first");
assert_eq!(pipelines.pipeline(left.unwrap()), 2, "the probing side is the second");
assert_eq!(pipelines.pipeline(plan.root()), 2, "and the join is its sink");
assert_eq!(pipelines.waits_for(2), [1]);
assert_eq!(pipelines.waits_for(0), [2]);
}
#[test]
fn a_cross_product_keeps_its_left_side_where_it_was() {
let (plan, pipelines) = decomposed(concat!(
"CrossProduct\n",
" Get memory.main.l AS l #0 [a::INTEGER]\n",
" Get memory.main.r AS r #1 [a::INTEGER]\n",
));
let [left, right] = plan.node(plan.root()).children();
assert_eq!(pipelines.len(), 2);
assert_eq!(pipelines.pipeline(plan.root()), 0, "the product streams");
assert_eq!(pipelines.pipeline(left.unwrap()), 0, "and so does the side it streams");
assert_eq!(pipelines.pipeline(right.unwrap()), 1, "the side that is kept is its own");
assert_eq!(pipelines.waits_for(0), [1]);
}
#[test]
fn two_sorts_under_one_another_are_three_pipelines_in_a_line() {
let (plan, pipelines) = decomposed(concat!(
"Sort [#0.0::INTEGER ASC NULLS LAST]\n",
" Limit 10 offset 0\n",
" Sort [#0.0::INTEGER DESC NULLS FIRST]\n",
" Get memory.main.t AS t #0 [a::INTEGER]\n",
));
assert_eq!(pipelines.len(), 3);
assert_eq!(pipelines.pipeline(plan.root()), 1);
assert_eq!(pipelines.waits_for(0), [1]);
assert_eq!(pipelines.waits_for(1), [2]);
assert!(pipelines.waits_for(2).is_empty());
}
}