Skip to content

Commit

Permalink
Add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
jpschorr committed Feb 7, 2024
1 parent 3f04665 commit 94d502a
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 3 deletions.
7 changes: 5 additions & 2 deletions partiql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ partiql-logical = { path = "../partiql-logical" }
partiql-logical-planner = { path = "../partiql-logical-planner" }
partiql-eval = { path = "../partiql-eval" }

itertools = "0.10"
criterion = "0.4"

thiserror = "1.0"

itertools = "0.12"
criterion = "0.5"
rand = "0.8"

[[bench]]
Expand Down
111 changes: 110 additions & 1 deletion partiql/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,114 @@
#[cfg(test)]
mod tests {
use partiql_ast_passes::error::AstTransformationError;
use partiql_catalog::{Catalog, PartiqlCatalog};
use partiql_eval as eval;
use partiql_eval::env::basic::MapBindings;
use partiql_eval::error::{EvalErr, PlanErr};
use partiql_eval::eval::{EvalPlan, EvalResult, Evaluated};
use partiql_eval::plan::EvaluationMode;
use partiql_logical as logical;
use partiql_parser::{Parsed, ParserError, ParserResult};
use partiql_value::Value;
use thiserror::Error;

#[derive(Error, Debug)]
enum TestError<'a> {
#[error("Parse error: {0:?}")]
Parse(ParserError<'a>),
#[error("Lower error: {0:?}")]
Lower(AstTransformationError),
#[error("Plan error: {0:?}")]
Plan(PlanErr),
#[error("Evaluation error: {0:?}")]
Eval(EvalErr),
}

impl<'a> From<ParserError<'a>> for TestError<'a> {
fn from(err: ParserError<'a>) -> Self {
TestError::Parse(err)
}
}

impl From<AstTransformationError> for TestError<'_> {
fn from(err: AstTransformationError) -> Self {
TestError::Lower(err)
}
}

impl From<PlanErr> for TestError<'_> {
fn from(err: PlanErr) -> Self {
TestError::Plan(err)
}
}

impl From<EvalErr> for TestError<'_> {
fn from(err: EvalErr) -> Self {
TestError::Eval(err)
}
}

#[track_caller]
#[inline]
pub(crate) fn parse(statement: &str) -> ParserResult {
partiql_parser::Parser::default().parse(statement)
}

#[track_caller]
#[inline]
pub(crate) fn lower(
catalog: &dyn Catalog,
parsed: &Parsed,
) -> Result<logical::LogicalPlan<logical::BindingsOp>, AstTransformationError> {
let planner = partiql_logical_planner::LogicalPlanner::new(catalog);
planner.lower(parsed)
}

#[track_caller]
#[inline]
pub(crate) fn compile(
mode: EvaluationMode,
catalog: &dyn Catalog,
logical: logical::LogicalPlan<logical::BindingsOp>,
) -> Result<EvalPlan, PlanErr> {
let mut planner = eval::plan::EvaluatorPlanner::new(mode.into(), catalog);
planner.compile(&logical)
}

#[track_caller]
#[inline]
pub(crate) fn evaluate(mut plan: EvalPlan, bindings: MapBindings<Value>) -> EvalResult {
plan.execute_mut(bindings)
}

#[track_caller]
#[inline]
pub(crate) fn eval<'a>(
statement: &'a str,
mode: EvaluationMode,
) -> Result<Evaluated, TestError<'a>> {
let catalog = PartiqlCatalog::default();

let parsed = parse(statement)?;
let lowered = lower(&catalog, &parsed)?;
let bindings = Default::default();
let plan = compile(mode, &catalog, lowered)?;
Ok(evaluate(plan, bindings)?)
}

#[test]
fn todo() {}
fn todo() {
let query = "select foo, count(1) as n from
<<
{ 'foo': 'foo' },
{ 'foo': 'bar' },
{ 'foo': 'qux' },
{ 'foo': 'bar' },
{ 'foo': 'baz' },
{ 'foo': 'bar' },
{ 'foo': 'baz' }
>> group by foo order by n desc";

let res = eval(query, EvaluationMode::Permissive);
}
}

0 comments on commit 94d502a

Please sign in to comment.