Skip to content

Commit

Permalink
Add tests; fix lifetimes and dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
jpschorr committed Mar 6, 2024
1 parent 6c5aa29 commit 20d255c
Show file tree
Hide file tree
Showing 16 changed files with 336 additions and 60 deletions.
10 changes: 8 additions & 2 deletions extension/partiql-extension-ion-functions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use partiql_logical as logical;
use partiql_value::Value;
use std::borrow::Cow;

use partiql_catalog::context::SessionContext;
use std::error::Error;
use std::fmt::Debug;
use std::fs::File;
Expand Down Expand Up @@ -98,7 +99,11 @@ impl BaseTableFunctionInfo for ReadIonFunction {
pub(crate) struct EvalFnReadIon {}

impl BaseTableExpr for EvalFnReadIon {
fn evaluate(&self, args: &[Cow<Value>]) -> BaseTableExprResult {
fn evaluate<'c>(
&self,
args: &[Cow<Value>],
_ctx: &'c dyn SessionContext<'c>,
) -> BaseTableExprResult<'c> {
if let Some(arg1) = args.first() {
match arg1.as_ref() {
Value::String(path) => parse_ion_file(path),
Expand Down Expand Up @@ -155,9 +160,10 @@ fn parse_ion_buff<'a, I: 'a + ToIonDataSource>(input: I) -> BaseTableExprResult<
mod tests {
use super::*;

use partiql_catalog::context::SystemContext;
use partiql_catalog::{Catalog, Extension, PartiqlCatalog};
use partiql_eval::env::basic::MapBindings;
use partiql_eval::eval::{BasicContext, SystemContext};
use partiql_eval::eval::BasicContext;
use partiql_eval::plan::EvaluationMode;
use partiql_parser::{Parsed, ParserResult};
use partiql_value::{bag, tuple, DateTime, Value};
Expand Down
27 changes: 27 additions & 0 deletions partiql-catalog/src/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use partiql_value::{BindingsName, DateTime, Tuple, Value};
use std::any::Any;
use std::fmt::Debug;

pub trait Bindings<T>: Debug {
fn get(&self, name: &BindingsName) -> Option<&T>;
}

impl Bindings<Value> for Tuple {
fn get(&self, name: &BindingsName) -> Option<&Value> {
self.get(name)
}
}

#[derive(Debug)]
pub struct SystemContext {
pub now: DateTime,
}

/// Represents a session context that is used during evaluation of a plan.
pub trait SessionContext<'a>: Debug {
fn bindings(&self) -> &dyn Bindings<Value>;

fn system_context(&self) -> &SystemContext;

fn user_context(&self, name: &str) -> Option<&(dyn Any)>;
}
9 changes: 8 additions & 1 deletion partiql-catalog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use partiql_types::PartiqlType;
use partiql_value::Value;
use std::borrow::Cow;

use crate::context::SessionContext;
use std::collections::HashMap;
use std::error::Error;
use std::fmt::Debug;
Expand All @@ -13,6 +14,8 @@ use unicase::UniCase;

pub mod call_defs;

pub mod context;

pub trait Extension: Debug {
fn name(&self) -> String;
fn load(&self, catalog: &mut dyn Catalog) -> Result<(), Box<dyn Error>>;
Expand Down Expand Up @@ -49,7 +52,11 @@ pub type BaseTableExprResult<'a> =
Result<BaseTableExprResultValueIter<'a>, BaseTableExprResultError>;

pub trait BaseTableExpr: Debug {
fn evaluate(&self, args: &[Cow<Value>]) -> BaseTableExprResult;
fn evaluate<'c>(
&self,
args: &[Cow<Value>],
ctx: &'c dyn SessionContext<'c>,
) -> BaseTableExprResult<'c>;
}

pub trait BaseTableFunctionInfo: Debug {
Expand Down
7 changes: 3 additions & 4 deletions partiql-conformance-tests/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ use partiql_catalog::{Catalog, PartiqlCatalog};
use partiql_eval as eval;

use partiql_eval::error::{EvalErr, PlanErr};
use partiql_eval::eval::{
BasicContext, EvalContext, EvalPlan, EvalResult, Evaluated, SystemContext,
};
use partiql_eval::eval::{BasicContext, EvalContext, EvalPlan, EvalResult, Evaluated};
use partiql_logical as logical;
use partiql_parser::{Parsed, ParserError, ParserResult};
use partiql_value::DateTime;

use partiql_catalog::context::SystemContext;
use thiserror::Error;

mod test_value;
Expand Down Expand Up @@ -60,7 +59,7 @@ pub(crate) fn compile(

#[track_caller]
#[inline]
pub(crate) fn evaluate<'a, 'c>(mut plan: EvalPlan, ctx: &'c dyn EvalContext<'c>) -> EvalResult {
pub(crate) fn evaluate<'c>(mut plan: EvalPlan, ctx: &'c dyn EvalContext<'c>) -> EvalResult {
plan.execute_mut(ctx)
}

Expand Down
11 changes: 8 additions & 3 deletions partiql-eval/benches/bench_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ use std::borrow::Cow;
use std::time::Duration;

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use partiql_catalog::context::SystemContext;
use partiql_catalog::PartiqlCatalog;

use partiql_eval::env::basic::MapBindings;
use partiql_eval::eval::EvalPlan;
use partiql_eval::eval::{BasicContext, EvalPlan};
use partiql_eval::plan;
use partiql_eval::plan::EvaluationMode;
use partiql_logical as logical;
use partiql_logical::BindingsOp::{Project, ProjectAll};
use partiql_logical::{
BinaryOp, BindingsOp, JoinKind, LogicalPlan, PathComponent, ValueExpr, VarRefType,
};
use partiql_value::{bag, list, tuple, BindingsName, Value};
use partiql_value::{bag, list, tuple, BindingsName, DateTime, Value};

fn data() -> MapBindings<Value> {
let hr = tuple![(
Expand Down Expand Up @@ -134,7 +135,11 @@ fn eval_plan(logical: &LogicalPlan<BindingsOp>) -> EvalPlan {
}

fn evaluate(mut plan: EvalPlan, bindings: MapBindings<Value>) -> Value {
if let Ok(out) = plan.execute_mut(bindings) {
let sys = SystemContext {
now: DateTime::from_system_now_utc(),
};
let ctx = BasicContext::new(bindings, sys);
if let Ok(out) = plan.execute_mut(&ctx) {
out.result
} else {
Value::Missing
Expand Down
11 changes: 1 addition & 10 deletions partiql-eval/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
use partiql_catalog::context::Bindings;
use partiql_value::{BindingsName, Tuple, Value};
use std::fmt::Debug;
use unicase::UniCase;

pub trait Bindings<T>: Debug {
fn get(&self, name: &BindingsName) -> Option<&T>;
}

impl Bindings<Value> for Tuple {
fn get(&self, name: &BindingsName) -> Option<&Value> {
self.get(name)
}
}

pub mod basic {
use super::*;
use std::collections::HashMap;
Expand Down
2 changes: 1 addition & 1 deletion partiql-eval/src/eval/evaluable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub enum EvalType {

/// `Evaluable` represents each evaluation operator in the evaluation plan as an evaluable entity.
pub trait Evaluable: Debug {
fn evaluate<'a, 'c>(&mut self, ctx: &'c dyn EvalContext<'c>) -> Value;
fn evaluate<'c>(&mut self, ctx: &'c dyn EvalContext<'c>) -> Value;
fn update_input(&mut self, input: Value, branch_num: u8, ctx: &dyn EvalContext);
fn get_vars(&self) -> Option<&[String]> {
None
Expand Down
2 changes: 1 addition & 1 deletion partiql-eval/src/eval/expr/base_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl EvalExpr for EvalFnBaseTableExpr {
.iter()
.map(|arg| arg.evaluate(bindings, ctx))
.collect_vec();
let results = self.expr.evaluate(&args);
let results = self.expr.evaluate(&args, ctx.as_session());
let result = match results {
Ok(it) => {
let bag: Result<Bag, _> = it.collect();
Expand Down
3 changes: 1 addition & 2 deletions partiql-eval/src/eval/expr/path.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::env::Bindings;

pub use core::borrow::Borrow;

use crate::eval::expr::{BindError, BindEvalExpr, EvalExpr};
Expand All @@ -8,6 +6,7 @@ use crate::eval::EvalContext;
use partiql_value::Value::Missing;
use partiql_value::{BindingsName, Tuple, Value};

use partiql_catalog::context::Bindings;
use std::borrow::Cow;
use std::fmt::{Debug, Formatter};

Expand Down
40 changes: 23 additions & 17 deletions partiql-eval/src/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ use petgraph::dot::Dot;
use petgraph::prelude::StableGraph;
use petgraph::{Directed, Outgoing};

use partiql_value::{DateTime, Value};
use partiql_value::Value;

use crate::env::basic::MapBindings;
use crate::env::Bindings;

use petgraph::graph::NodeIndex;

use crate::error::{EvalErr, EvaluationError};
use partiql_catalog::context::{Bindings, SessionContext, SystemContext};
use petgraph::visit::EdgeRef;

use crate::eval::evaluable::{EvalType, Evaluable};
Expand Down Expand Up @@ -143,17 +143,9 @@ pub struct Evaluated {
pub result: Value,
}

#[derive(Debug)]
pub struct SystemContext {
pub now: DateTime,
}

/// Represents an evaluation context that is used during evaluation of a plan.
pub trait EvalContext<'a>: Debug {
fn bindings(&self) -> &dyn Bindings<Value>;

fn system_context(&self) -> &SystemContext;
fn user_context(&self, name: &str) -> Option<&'a (dyn Any + 'a)>;
pub trait EvalContext<'a>: SessionContext<'a> + Debug {
fn as_session(&'a self) -> &'a dyn SessionContext<'a>;

fn add_error(&self, error: EvaluationError);
fn has_errors(&self) -> bool;
Expand All @@ -180,8 +172,7 @@ impl<'a> BasicContext<'a> {
}
}
}

impl<'a> EvalContext<'a> for BasicContext<'a> {
impl<'a> SessionContext<'a> for BasicContext<'a> {
fn bindings(&self) -> &dyn Bindings<Value> {
&self.bindings
}
Expand All @@ -190,9 +181,14 @@ impl<'a> EvalContext<'a> for BasicContext<'a> {
&self.sys
}

fn user_context(&self, name: &str) -> Option<&'a (dyn Any + 'a)> {
fn user_context(&self, name: &str) -> Option<&(dyn Any)> {
self.user.get(name).copied()
}
}
impl<'a> EvalContext<'a> for BasicContext<'a> {
fn as_session(&'a self) -> &'a dyn SessionContext<'a> {
self
}

fn add_error(&self, error: EvaluationError) {
self.errors.borrow_mut().push(error)
Expand All @@ -219,16 +215,26 @@ impl<'a, 'c> NestedContext<'a, 'c> {
}
}

impl<'a, 'c> EvalContext<'a> for NestedContext<'a, 'c> {
impl<'a, 'c> SessionContext<'a> for NestedContext<'a, 'c> {
fn bindings(&self) -> &dyn Bindings<Value> {
&self.bindings
}

delegate! {
to self.parent {
fn system_context(&self) -> &SystemContext;
fn user_context(&self, name: &str) -> Option<&'a (dyn Any + 'a)>;
fn user_context(&self, name: &str) -> Option<& (dyn Any )>;
}
}
}

impl<'a, 'c> EvalContext<'a> for NestedContext<'a, 'c> {
fn as_session(&'a self) -> &'a dyn SessionContext<'a> {
self
}

delegate! {
to self.parent {
fn add_error(&self, error: EvaluationError);
fn has_errors(&self) -> bool;
fn errors(&self) -> Vec<EvaluationError>;
Expand Down
8 changes: 5 additions & 3 deletions partiql-eval/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ mod tests {

use crate::env::basic::MapBindings;
use crate::plan;
use partiql_catalog::context::SystemContext;
use partiql_catalog::PartiqlCatalog;
use rust_decimal_macros::dec;

use partiql_logical as logical;
use partiql_logical::BindingsOp::{Distinct, Project, ProjectAll, ProjectValue};

use crate::eval::{BasicContext, SystemContext};
use crate::eval::BasicContext;
use crate::plan::EvaluationMode;
use partiql_logical::{
BagExpr, BetweenExpr, BinaryOp, BindingsOp, CoalesceExpr, ExprQuery, IsTypeExpr, JoinKind,
Expand Down Expand Up @@ -2215,7 +2216,8 @@ mod tests {
mod clause_from {
use crate::eval::evaluable::{EvalScan, Evaluable};
use crate::eval::expr::{EvalGlobalVarRef, EvalPath, EvalPathComponent};
use crate::eval::{BasicContext, SystemContext};
use crate::eval::BasicContext;
use partiql_catalog::context::SystemContext;
use partiql_value::{bag, list, BindingsName, DateTime};

use super::*;
Expand Down Expand Up @@ -2354,7 +2356,7 @@ mod tests {

use crate::eval::evaluable::{EvalUnpivot, Evaluable};
use crate::eval::expr::EvalGlobalVarRef;
use crate::eval::{BasicContext, SystemContext};
use crate::eval::BasicContext;

use super::*;

Expand Down
3 changes: 2 additions & 1 deletion partiql-logical-planner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ impl<'c> LogicalPlanner<'c> {
mod tests {
use assert_matches::assert_matches;
use partiql_ast_passes::error::AstTransformationError;
use partiql_catalog::context::SystemContext;
use partiql_catalog::PartiqlCatalog;

use partiql_eval::env::basic::MapBindings;
use partiql_eval::eval::{BasicContext, SystemContext};
use partiql_eval::eval::BasicContext;

use partiql_eval::plan;
use partiql_eval::plan::EvaluationMode;
Expand Down
11 changes: 8 additions & 3 deletions partiql/benches/bench_agg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ use std::time::Duration;

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use itertools::Itertools;
use partiql_catalog::context::SystemContext;
use partiql_catalog::{Catalog, PartiqlCatalog};

use partiql_eval::env::basic::MapBindings;
use partiql_eval::eval::EvalPlan;
use partiql_eval::eval::{BasicContext, EvalPlan};
use partiql_eval::plan::{EvaluationMode, EvaluatorPlanner};
use partiql_logical::{BindingsOp, LogicalPlan};
use partiql_logical_planner::LogicalPlanner;

use partiql_parser::{Parser, ParserResult};
use partiql_value::{tuple, Bag, Value};
use partiql_value::{tuple, Bag, DateTime, Value};

fn numbers() -> impl Iterator<Item = Value> {
(0..1000i64).map(Value::from)
Expand Down Expand Up @@ -49,7 +50,11 @@ fn plan(catalog: &dyn Catalog, logical: &LogicalPlan<BindingsOp>) -> EvalPlan {
}
#[inline]
pub(crate) fn evaluate(mut eval: EvalPlan, bindings: MapBindings<Value>) -> Value {
if let Ok(out) = eval.execute_mut(bindings) {
let sys = SystemContext {
now: DateTime::from_system_now_utc(),
};
let ctx = BasicContext::new(bindings, sys);
if let Ok(out) = eval.execute_mut(&ctx) {
out.result
} else {
Value::Missing
Expand Down
Loading

0 comments on commit 20d255c

Please sign in to comment.