use rudb_catalog::{Catalog, QualifiedName};
use rudb_common::{Cancel, Memory, Result};
use rudb_functions::TableFunction;
use rudb_plan::{Node, NodeRef, Plan};
use crate::cancel::Guarded;
use crate::group::{Aggregate, Distinct};
use crate::join::{CrossProduct, Join};
use crate::operator::Operator;
use crate::setop::SetOp;
use crate::sort::Sort;
use crate::source::{Dummy, FileScan, Scan, Series, Values};
use crate::stream::{Filter, Limit, Project};
pub fn build<'a>(plan: &'a Plan, catalog: &'a Catalog) -> Result<Box<dyn Operator + 'a>> {
build_with(plan, catalog, &Cancel::new(), &Memory::unlimited())
}
pub fn build_with<'a>(
plan: &'a Plan,
catalog: &'a Catalog,
cancel: &Cancel,
memory: &Memory,
) -> Result<Box<dyn Operator + 'a>> {
node(plan, catalog, cancel, memory, plan.root())
}
fn node<'a>(
plan: &'a Plan,
catalog: &'a Catalog,
cancel: &Cancel,
memory: &Memory,
reference: NodeRef,
) -> Result<Box<dyn Operator + 'a>> {
let inner: Box<dyn Operator + 'a> = match *plan.node(reference) {
Node::Get { catalog: database, schema, table, index, columns, .. } => {
let name =
QualifiedName::new(plan.string(database), plan.string(schema), plan.string(table));
Box::new(Scan::new(plan, catalog.table(&name)?, index, columns)?)
}
Node::Dummy => Box::new(Dummy::new()),
Node::Values { index, columns, rows } => Box::new(Values::new(plan, index, columns, rows)?),
Node::TableFunction { index, function, args, columns } => {
match TableFunction::lookup(plan.string(function)) {
Some(function @ (TableFunction::ReadParquet | TableFunction::ReadCsv)) => {
Box::new(FileScan::new(plan, index, function, args, columns)?)
}
_ => Box::new(Series::new(plan, index, plan.string(function), args)?),
}
}
Node::Filter { input, predicate } => {
Box::new(Filter::new(plan, node(plan, catalog, cancel, memory, input)?, predicate)?)
}
Node::Project { input, index, exprs, names } => Box::new(Project::new(
plan,
node(plan, catalog, cancel, memory, input)?,
index,
exprs,
names,
)?),
Node::Aggregate { input, index, groups, aggregates } => Box::new(Aggregate::new(
plan,
node(plan, catalog, cancel, memory, input)?,
index,
groups,
aggregates,
memory,
)?),
Node::Sort { input, keys } => {
Box::new(Sort::new(plan, node(plan, catalog, cancel, memory, input)?, keys, memory))
}
Node::Limit { input, count, offset } => {
Box::new(Limit::new(node(plan, catalog, cancel, memory, input)?, count, offset))
}
Node::Distinct { input, on } => {
Box::new(Distinct::new(plan, node(plan, catalog, cancel, memory, input)?, on, memory))
}
Node::Join { left, right, kind, conditions } => Box::new(Join::new(
plan,
node(plan, catalog, cancel, memory, left)?,
node(plan, catalog, cancel, memory, right)?,
kind,
conditions,
memory,
)),
Node::CrossProduct { left, right } => Box::new(CrossProduct::new(
node(plan, catalog, cancel, memory, left)?,
node(plan, catalog, cancel, memory, right)?,
memory,
)),
Node::SetOp { left, right, kind, all, index } => Box::new(SetOp::new(
node(plan, catalog, cancel, memory, left)?,
node(plan, catalog, cancel, memory, right)?,
kind,
all,
index,
memory,
)),
};
Ok(Box::new(Guarded::new(inner, cancel.clone())))
}