Pre: Move query-projectors/errors and aggregates into query-projector-traits

This commit is contained in:
Grisha Kruglov 2018-08-08 12:37:25 -07:00
parent f7111c799d
commit 9444559f6c
20 changed files with 117 additions and 43 deletions

View file

@ -67,6 +67,9 @@ path = "query-algebrizer-traits"
[dependencies.mentat_query_projector] [dependencies.mentat_query_projector]
path = "query-projector" path = "query-projector"
[dependencies.query_projector_traits]
path = "query-projector-traits"
[dependencies.mentat_query_pull] [dependencies.mentat_query_pull]
path = "query-pull" path = "query-pull"

View file

@ -0,0 +1,40 @@
[package]
name = "query_projector_traits"
version = "0.0.1"
workspace = ".."
[lib]
name = "query_projector_traits"
path = "lib.rs"
[dependencies]
failure = "0.1.1"
failure_derive = "0.1.1"
[dependencies.rusqlite]
version = "0.13"
features = ["limits"]
[dependencies.edn]
path = "../edn"
[dependencies.core_traits]
path = "../core-traits"
[dependencies.db_traits]
path = "../db-traits"
[dependencies.query_pull_traits]
path = "../query-pull-traits"
[dependencies.mentat_query_algebrizer]
path = "../query-algebrizer"
[dependencies.mentat_query_sql]
path = "../query-sql"
[dev-dependencies.mentat_core]
path = "../core"
[dev-dependencies.mentat_query_projector]
path = "../query-projector"

View file

@ -47,7 +47,7 @@ pub enum SimpleAggregationOp {
} }
impl SimpleAggregationOp { impl SimpleAggregationOp {
pub(crate) fn to_sql(&self) -> &'static str { pub fn to_sql(&self) -> &'static str {
use self::SimpleAggregationOp::*; use self::SimpleAggregationOp::*;
match self { match self {
&Avg => "avg", &Avg => "avg",
@ -76,7 +76,7 @@ impl SimpleAggregationOp {
/// but invalid to take `Max` of `{Uuid, String}`. /// but invalid to take `Max` of `{Uuid, String}`.
/// ///
/// The returned type is the type of the result of the aggregation. /// The returned type is the type of the result of the aggregation.
pub(crate) fn is_applicable_to_types(&self, possibilities: ValueTypeSet) -> Result<ValueType> { pub fn is_applicable_to_types(&self, possibilities: ValueTypeSet) -> Result<ValueType> {
use self::SimpleAggregationOp::*; use self::SimpleAggregationOp::*;
if possibilities.is_empty() { if possibilities.is_empty() {
bail!(ProjectorError::CannotProjectImpossibleBinding(*self)) bail!(ProjectorError::CannotProjectImpossibleBinding(*self))
@ -110,7 +110,7 @@ impl SimpleAggregationOp {
&Max | &Min => { &Max | &Min => {
if possibilities.is_unit() { if possibilities.is_unit() {
use ValueType::*; use self::ValueType::*;
let the_type = possibilities.exemplar().expect("a type"); let the_type = possibilities.exemplar().expect("a type");
match the_type { match the_type {
// These types are numerically ordered. // These types are numerically ordered.
@ -147,17 +147,17 @@ impl SimpleAggregationOp {
} }
} }
pub(crate) struct SimpleAggregate { pub struct SimpleAggregate {
pub op: SimpleAggregationOp, pub op: SimpleAggregationOp,
pub var: Variable, pub var: Variable,
} }
impl SimpleAggregate { impl SimpleAggregate {
pub(crate) fn column_name(&self) -> Name { pub fn column_name(&self) -> Name {
format!("({} {})", self.op.to_sql(), self.var.name()) format!("({} {})", self.op.to_sql(), self.var.name())
} }
pub(crate) fn use_static_value(&self) -> bool { pub fn use_static_value(&self) -> bool {
use self::SimpleAggregationOp::*; use self::SimpleAggregationOp::*;
match self.op { match self.op {
Avg | Max | Min => true, Avg | Max | Min => true,
@ -166,7 +166,7 @@ impl SimpleAggregate {
} }
/// Return `true` if this aggregate can be `NULL` over 0 rows. /// Return `true` if this aggregate can be `NULL` over 0 rows.
pub(crate) fn is_nullable(&self) -> bool { pub fn is_nullable(&self) -> bool {
use self::SimpleAggregationOp::*; use self::SimpleAggregationOp::*;
match self.op { match self.op {
Avg | Max | Min => true, Avg | Max | Min => true,
@ -175,7 +175,7 @@ impl SimpleAggregate {
} }
} }
pub(crate) trait SimpleAggregation { pub trait SimpleAggregation {
fn to_simple(&self) -> Option<SimpleAggregate>; fn to_simple(&self) -> Option<SimpleAggregate>;
} }
@ -195,7 +195,7 @@ impl SimpleAggregation for Aggregate {
/// - The `ColumnOrExpression` to use in the query. This will always refer to other /// - The `ColumnOrExpression` to use in the query. This will always refer to other
/// variables by name; never to a datoms column. /// variables by name; never to a datoms column.
/// - The known type of that value. /// - The known type of that value.
pub(crate) fn projected_column_for_simple_aggregate(simple: &SimpleAggregate, cc: &ConjoiningClauses) -> Result<(ProjectedColumn, ValueType)> { pub fn projected_column_for_simple_aggregate(simple: &SimpleAggregate, cc: &ConjoiningClauses) -> Result<(ProjectedColumn, ValueType)> {
let known_types = cc.known_type_set(&simple.var); let known_types = cc.known_type_set(&simple.var);
let return_type = simple.op.is_applicable_to_types(known_types)?; let return_type = simple.op.is_applicable_to_types(known_types)?;
let projected_column_or_expression = let projected_column_or_expression =

View file

@ -0,0 +1,28 @@
// Copyright 2018 Mozilla
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
extern crate failure;
#[macro_use]
extern crate failure_derive;
extern crate rusqlite;
extern crate core_traits;
extern crate db_traits;
extern crate edn;
extern crate query_pull_traits;
// TODO we only want to import a *_traits here, this is a smell.
extern crate mentat_query_algebrizer;
extern crate mentat_query_sql;
#[macro_use]
pub mod errors;
pub mod aggregates;

View file

@ -13,6 +13,7 @@ extern crate mentat_core;
extern crate core_traits; extern crate core_traits;
extern crate mentat_query_algebrizer; extern crate mentat_query_algebrizer;
extern crate mentat_query_projector; extern crate mentat_query_projector;
extern crate query_projector_traits;
use core_traits::{ use core_traits::{
Entid, Entid,
@ -102,7 +103,7 @@ fn test_the_without_max_or_min() {
// … when we look at the projection list, we cannot reconcile the types. // … when we look at the projection list, we cannot reconcile the types.
let projection = query_projection(&schema, &algebrized); let projection = query_projection(&schema, &algebrized);
assert!(projection.is_err()); assert!(projection.is_err());
use ::mentat_query_projector::errors::{ use query_projector_traits::errors::{
ProjectorError, ProjectorError,
}; };
match projection.err().expect("expected failure") { match projection.err().expect("expected failure") {

View file

@ -5,7 +5,6 @@ workspace = ".."
[dependencies] [dependencies]
failure = "0.1.1" failure = "0.1.1"
failure_derive = "0.1.1"
indexmap = "1" indexmap = "1"
[dependencies.rusqlite] [dependencies.rusqlite]
@ -39,6 +38,9 @@ path = "../query-pull"
[dependencies.query_pull_traits] [dependencies.query_pull_traits]
path = "../query-pull-traits" path = "../query-pull-traits"
[dependencies.query_projector_traits]
path = "../query-projector-traits"
[dependencies.mentat_query_sql] [dependencies.mentat_query_sql]
path = "../query-sql" path = "../query-sql"

View file

@ -12,7 +12,7 @@ use core_traits::{
Binding, Binding,
}; };
use errors::{ use query_projector_traits::errors::{
ProjectorError, ProjectorError,
Result, Result,
}; };

View file

@ -10,8 +10,6 @@
extern crate failure; extern crate failure;
#[macro_use]
extern crate failure_derive;
extern crate indexmap; extern crate indexmap;
extern crate rusqlite; extern crate rusqlite;
@ -23,6 +21,8 @@ extern crate mentat_db; // For value conversion.
extern crate mentat_query_algebrizer; extern crate mentat_query_algebrizer;
extern crate mentat_query_pull; extern crate mentat_query_pull;
extern crate query_pull_traits; extern crate query_pull_traits;
#[macro_use]
extern crate query_projector_traits;
extern crate mentat_query_sql; extern crate mentat_query_sql;
extern crate mentat_sql; extern crate mentat_sql;
@ -42,7 +42,6 @@ use rusqlite::{
use core_traits::{ use core_traits::{
Binding, Binding,
TypedValue, TypedValue,
ValueType,
}; };
use mentat_core::{ use mentat_core::{
@ -75,10 +74,6 @@ use mentat_query_sql::{
Projection, Projection,
}; };
#[macro_use]
pub mod errors;
mod aggregates;
mod binding_tuple; mod binding_tuple;
pub use binding_tuple::{ pub use binding_tuple::{
BindingTuple, BindingTuple,
@ -88,10 +83,6 @@ mod projectors;
mod pull; mod pull;
mod relresult; mod relresult;
pub use aggregates::{
SimpleAggregationOp,
};
use project::{ use project::{
ProjectedElements, ProjectedElements,
project_elements, project_elements,
@ -122,7 +113,7 @@ pub use relresult::{
StructuredRelResult, StructuredRelResult,
}; };
pub use errors::{ use query_projector_traits::errors::{
ProjectorError, ProjectorError,
Result, Result,
}; };

View file

@ -52,12 +52,12 @@ use mentat_query_sql::{
ProjectedColumn, ProjectedColumn,
}; };
use aggregates::{ use query_projector_traits::aggregates::{
SimpleAggregation, SimpleAggregation,
projected_column_for_simple_aggregate, projected_column_for_simple_aggregate,
}; };
use errors::{ use query_projector_traits::errors::{
ProjectorError, ProjectorError,
Result, Result,
}; };
@ -313,7 +313,7 @@ pub(crate) fn project_elements<'a, I: IntoIterator<Item = &'a Element>>(
if let Some(simple) = a.to_simple() { if let Some(simple) = a.to_simple() {
aggregates = true; aggregates = true;
use aggregates::SimpleAggregationOp::*; use query_projector_traits::aggregates::SimpleAggregationOp::*;
match simple.op { match simple.op {
Max | Min => { Max | Min => {
min_max_count += 1; min_max_count += 1;

View file

@ -20,7 +20,7 @@ use ::{
rusqlite, rusqlite,
}; };
use ::errors::{ use query_projector_traits::errors::{
Result, Result,
}; };

View file

@ -16,7 +16,7 @@ use super::{
rusqlite, rusqlite,
}; };
use super::errors::{ use query_projector_traits::errors::{
Result, Result,
}; };

View file

@ -44,7 +44,7 @@ use ::pull::{
PullTemplate, PullTemplate,
}; };
use ::errors::{ use query_projector_traits::errors::{
Result, Result,
}; };

View file

@ -26,7 +26,7 @@ use ::{
rusqlite, rusqlite,
}; };
use ::errors::{ use query_projector_traits::errors::{
Result, Result,
}; };

View file

@ -33,7 +33,7 @@ use mentat_query_pull::{
Puller, Puller,
}; };
use errors::Result; use query_projector_traits::errors::Result;
use super::{ use super::{
Index, Index,

View file

@ -25,5 +25,8 @@ path = "../query-algebrizer"
[dependencies.mentat_query_projector] [dependencies.mentat_query_projector]
path = "../query-projector" path = "../query-projector"
[dependencies.query_projector_traits]
path = "../query-projector-traits"
[dependencies.mentat_query_sql] [dependencies.mentat_query_sql]
path = "../query-sql" path = "../query-sql"

View file

@ -15,6 +15,7 @@ extern crate core_traits;
extern crate mentat_core; extern crate mentat_core;
extern crate mentat_query_algebrizer; extern crate mentat_query_algebrizer;
extern crate mentat_query_projector; extern crate mentat_query_projector;
extern crate query_projector_traits;
extern crate mentat_query_sql; extern crate mentat_query_sql;
extern crate mentat_sql; extern crate mentat_sql;
@ -31,5 +32,5 @@ pub use translate::{
}; };
// query-translator could be folded into query-projector; for now, just type alias the errors. // query-translator could be folded into query-projector; for now, just type alias the errors.
pub type TranslatorError = mentat_query_projector::ProjectorError; pub type TranslatorError = query_projector_traits::errors::ProjectorError;
pub type Result<T> = std::result::Result<T, TranslatorError>; pub type Result<T> = std::result::Result<T, TranslatorError>;

View file

@ -30,7 +30,9 @@ use db_traits::errors::DbError;
use query_algebrizer_traits::errors::{ use query_algebrizer_traits::errors::{
AlgebrizerError, AlgebrizerError,
}; };
use mentat_query_projector; use query_projector_traits::errors::{
ProjectorError,
};
use query_pull_traits::errors::{ use query_pull_traits::errors::{
PullError, PullError,
}; };
@ -107,7 +109,7 @@ pub enum MentatError {
AlgebrizerError(#[cause] AlgebrizerError), AlgebrizerError(#[cause] AlgebrizerError),
#[fail(display = "{}", _0)] #[fail(display = "{}", _0)]
ProjectorError(#[cause] mentat_query_projector::ProjectorError), ProjectorError(#[cause] ProjectorError),
#[fail(display = "{}", _0)] #[fail(display = "{}", _0)]
PullError(#[cause] PullError), PullError(#[cause] PullError),
@ -150,8 +152,8 @@ impl From<AlgebrizerError> for MentatError {
} }
} }
impl From<mentat_query_projector::ProjectorError> for MentatError { impl From<ProjectorError> for MentatError {
fn from(error: mentat_query_projector::ProjectorError) -> MentatError { fn from(error: ProjectorError) -> MentatError {
MentatError::ProjectorError(error) MentatError::ProjectorError(error)
} }
} }

View file

@ -29,6 +29,7 @@ extern crate db_traits;
extern crate mentat_query_algebrizer; extern crate mentat_query_algebrizer;
extern crate query_algebrizer_traits; extern crate query_algebrizer_traits;
extern crate mentat_query_projector; extern crate mentat_query_projector;
extern crate query_projector_traits;
extern crate mentat_query_pull; extern crate mentat_query_pull;
extern crate query_pull_traits; extern crate query_pull_traits;
extern crate mentat_query_translator; extern crate mentat_query_translator;
@ -141,9 +142,11 @@ pub use edn::{
ToMillis, ToMillis,
}; };
pub use query_algebrizer_traits::errors::AlgebrizerError; pub use query_algebrizer_traits::errors::AlgebrizerError;
pub use query_projector_traits::errors::{
ProjectorError,
};
pub use mentat_query_projector::{ pub use mentat_query_projector::{
BindingTuple, BindingTuple,
ProjectorError,
}; };
pub use query_pull_traits::errors::PullError; pub use query_pull_traits::errors::PullError;
pub use mentat_sql::SQLError; pub use mentat_sql::SQLError;

View file

@ -19,7 +19,7 @@ extern crate mentat_db;
// TODO: when we switch to `failure`, make this more humane. // TODO: when we switch to `failure`, make this more humane.
extern crate query_algebrizer_traits; // For errors; extern crate query_algebrizer_traits; // For errors;
extern crate mentat_query_projector; // For errors. extern crate query_projector_traits; // For errors.
extern crate mentat_query_translator; // For errors. extern crate mentat_query_translator; // For errors.
use std::str::FromStr; use std::str::FromStr;
@ -40,7 +40,7 @@ use mentat_core::{
Uuid, Uuid,
}; };
use mentat_query_projector::{ use query_projector_traits::aggregates::{
SimpleAggregationOp, SimpleAggregationOp,
}; };
@ -587,7 +587,7 @@ fn test_aggregates_type_handling() {
let r = store.q_once(r#"[:find (sum ?v) . :where [_ _ ?v]]"#, None); let r = store.q_once(r#"[:find (sum ?v) . :where [_ _ ?v]]"#, None);
let all_types = ValueTypeSet::any(); let all_types = ValueTypeSet::any();
match r.expect_err("expected query to fail") { match r.expect_err("expected query to fail") {
MentatError::ProjectorError(::mentat_query_projector::errors::ProjectorError::CannotApplyAggregateOperationToTypes( MentatError::ProjectorError(::query_projector_traits::errors::ProjectorError::CannotApplyAggregateOperationToTypes(
SimpleAggregationOp::Sum, types)) => { SimpleAggregationOp::Sum, types)) => {
assert_eq!(types, all_types); assert_eq!(types, all_types);
}, },
@ -599,7 +599,7 @@ fn test_aggregates_type_handling() {
:where [_ _ ?v] [(type ?v :db.type/instant)]]"#, :where [_ _ ?v] [(type ?v :db.type/instant)]]"#,
None); None);
match r.expect_err("expected query to fail") { match r.expect_err("expected query to fail") {
MentatError::ProjectorError(::mentat_query_projector::errors::ProjectorError::CannotApplyAggregateOperationToTypes( MentatError::ProjectorError(::query_projector_traits::errors::ProjectorError::CannotApplyAggregateOperationToTypes(
SimpleAggregationOp::Sum, SimpleAggregationOp::Sum,
types)) => { types)) => {
assert_eq!(types, ValueTypeSet::of_one(ValueType::Instant)); assert_eq!(types, ValueTypeSet::of_one(ValueType::Instant));
@ -1341,7 +1341,7 @@ fn test_aggregation_implicit_grouping() {
[?person :foo/is-vegetarian true] [?person :foo/is-vegetarian true]
[?person :foo/name ?name]]"#, None); [?person :foo/name ?name]]"#, None);
match res.expect_err("expected query to fail") { match res.expect_err("expected query to fail") {
MentatError::ProjectorError(::mentat_query_projector::errors::ProjectorError::AmbiguousAggregates(mmc, cc)) => { MentatError::ProjectorError(::query_projector_traits::errors::ProjectorError::AmbiguousAggregates(mmc, cc)) => {
assert_eq!(mmc, 2); assert_eq!(mmc, 2);
assert_eq!(cc, 1); assert_eq!(cc, 1);
}, },