Skip to content

Commit

Permalink
Add plan flows for JOIN. Add 'self managed' graph nodes. (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpschorr authored Jun 5, 2023
1 parent 024620a commit 8599593
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
13 changes: 13 additions & 0 deletions partiql-eval/src/eval/evaluable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,22 @@ macro_rules! take_input {
};
}

/// Whether an [`Evaluable`] takes input from the plan graph or manages its own iteration.
pub enum EvalType {
SelfManaged,
GraphManaged,
}

/// `Evaluable` represents each evaluation operator in the evaluation plan as an evaluable entity.
pub trait Evaluable: Debug {
fn evaluate(&mut self, ctx: &dyn EvalContext) -> Value;
fn update_input(&mut self, input: Value, branch_num: u8);
fn get_vars(&self) -> Option<&[String]> {
None
}
fn eval_type(&self) -> EvalType {
EvalType::GraphManaged
}
}

/// Represents an evaluation `Scan` operator; `Scan` operator scans the given bindings from its
Expand Down Expand Up @@ -305,6 +314,10 @@ impl Evaluable for EvalJoin {
fn update_input(&mut self, input: Value, _branch_num: u8) {
self.input = Some(input);
}

fn eval_type(&self) -> EvalType {
EvalType::SelfManaged
}
}

/// An SQL aggregation function call that has been rewritten to be evaluated with the `GROUP BY`
Expand Down
54 changes: 33 additions & 21 deletions partiql-eval/src/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use petgraph::graph::NodeIndex;
use crate::error::{EvalErr, EvaluationError};
use petgraph::visit::EdgeRef;

use crate::eval::evaluable::Evaluable;
use crate::eval::evaluable::{EvalType, Evaluable};

pub mod evaluable;
pub mod expr;
Expand Down Expand Up @@ -77,32 +77,44 @@ impl EvalPlan {

let mut result = None;
for idx in ops.into_iter() {
result = Some(self.get_node(idx)?.evaluate(&*ctx));

// return on first evaluation error
if ctx.has_errors() {
return Err(EvalErr {
errors: ctx.errors(),
});
}

let destinations: Vec<(usize, (u8, NodeIndex))> = self
.plan_graph()
.edges_directed(idx, Outgoing)
.map(|e| (*e.weight(), e.target()))
.enumerate()
.collect_vec();
let branches = destinations.len();
for (i, (branch_num, dst_id)) in destinations {
let res = if i == branches - 1 {
result.take()
} else {
result.clone()
};

let res =
res.ok_or_else(|| err_illegal_state("Error in retrieving source value"))?;
self.get_node(dst_id)?.update_input(res, branch_num);

// Some evaluables (i.e., `JOIN`) manage their own inputs
let graph_managed = destinations.is_empty()
|| destinations.iter().any(|(_, (_, dest_idx))| {
matches!(
self.get_node(*dest_idx).map(|d| d.eval_type()),
Ok(EvalType::GraphManaged)
)
});
if graph_managed {
let src = self.get_node(idx)?;
result = Some(src.evaluate(&*ctx));

// return on first evaluation error
if ctx.has_errors() {
return Err(EvalErr {
errors: ctx.errors(),
});
}

let num_destinations = destinations.len();
for (i, (branch_num, dst_id)) in destinations {
let res = if i == num_destinations - 1 {
result.take()
} else {
result.clone()
};

let res =
res.ok_or_else(|| err_illegal_state("Error in retrieving source value"))?;
self.get_node(dst_id)?.update_input(res, branch_num);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions partiql-logical-planner/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,8 @@ impl<'a, 'ast> Visitor<'ast> for AstToLogical<'a> {
right,
});
let join = self.plan.add_operator(join);
self.plan.add_flow_with_branch_num(lid, join, 0);
self.plan.add_flow_with_branch_num(rid, join, 1);
self.push_bexpr(join);
Traverse::Continue
}
Expand Down

2 comments on commit 8599593

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'PartiQL (rust) Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 8599593 Previous: 024620a Ratio
numbers 175 ns/iter (± 8) 106 ns/iter (± 0) 1.65
parse-simple 1043 ns/iter (± 36) 695 ns/iter (± 10) 1.50
parse-complex 34103 ns/iter (± 1355) 22174 ns/iter (± 41) 1.54
parse-complex-fexpr 54291 ns/iter (± 22163) 35008 ns/iter (± 112) 1.55

This comment was automatically generated by workflow using github-action-benchmark.

CC: @jpschorr @jpschorr @partiql

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PartiQL (rust) Benchmark

Benchmark suite Current: 8599593 Previous: 024620a Ratio
parse-1 7744 ns/iter (± 459) 6028 ns/iter (± 44) 1.28
parse-15 74508 ns/iter (± 3109) 57453 ns/iter (± 68) 1.30
parse-30 145739 ns/iter (± 6524) 115181 ns/iter (± 708) 1.27
compile-1 6283 ns/iter (± 272) 5096 ns/iter (± 49) 1.23
compile-15 48984 ns/iter (± 2206) 36466 ns/iter (± 43) 1.34
compile-30 101284 ns/iter (± 4182) 74352 ns/iter (± 65) 1.36
plan-1 27872 ns/iter (± 2440) 20578 ns/iter (± 647) 1.35
plan-15 526660 ns/iter (± 33205) 367561 ns/iter (± 1287) 1.43
plan-30 1071481 ns/iter (± 64031) 745146 ns/iter (± 1620) 1.44
eval-1 27906859 ns/iter (± 1342101) 20161461 ns/iter (± 271804) 1.38
eval-15 151754110 ns/iter (± 4065915) 121602615 ns/iter (± 304646) 1.25
eval-30 286354827 ns/iter (± 6707477) 238276133 ns/iter (± 3529775) 1.20
join 17884 ns/iter (± 560) 14337 ns/iter (± 34) 1.25
simple 8557 ns/iter (± 329) 7189 ns/iter (± 11) 1.19
simple-no 792 ns/iter (± 34) 704 ns/iter (± 11) 1.13
numbers 175 ns/iter (± 8) 106 ns/iter (± 0) 1.65
parse-simple 1043 ns/iter (± 36) 695 ns/iter (± 10) 1.50
parse-ion 4023 ns/iter (± 226) 2728 ns/iter (± 36) 1.47
parse-group 12741 ns/iter (± 391) 8671 ns/iter (± 18) 1.47
parse-complex 34103 ns/iter (± 1355) 22174 ns/iter (± 41) 1.54
parse-complex-fexpr 54291 ns/iter (± 22163) 35008 ns/iter (± 112) 1.55

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.