2017-01-25 22:06:19 +00:00
|
|
|
// Copyright 2016 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.
|
|
|
|
|
|
|
|
/// ! This module defines the interface and implementation for parsing an EDN
|
|
|
|
/// ! input into a structured Datalog query.
|
|
|
|
/// !
|
|
|
|
/// ! The query types are defined in the `query` crate, because they
|
|
|
|
/// ! are shared between the parser (EDN -> query), the translator
|
|
|
|
/// ! (query -> SQL), and the executor (query, SQL -> running code).
|
|
|
|
/// !
|
|
|
|
/// ! The query input can be in two forms: a 'flat' human-oriented
|
|
|
|
/// ! sequence:
|
|
|
|
/// !
|
|
|
|
/// ! ```clojure
|
|
|
|
/// ! [:find ?y :in $ ?x :where [?x :foaf/knows ?y]]
|
|
|
|
/// ! ```
|
|
|
|
/// !
|
|
|
|
/// ! or a more programmatically generable map:
|
|
|
|
/// !
|
|
|
|
/// ! ```clojure
|
|
|
|
/// ! {:find [?y]
|
|
|
|
/// ! :in [$]
|
|
|
|
/// ! :where [[?x :foaf/knows ?y]]}
|
|
|
|
/// ! ```
|
|
|
|
/// !
|
|
|
|
/// ! We parse by expanding the array format into four parts, treating them as the four
|
|
|
|
/// ! parts of the map.
|
|
|
|
|
|
|
|
extern crate edn;
|
Extract partial storage abstraction; use error-chain throughout. Fixes #328. r=rnewman (#341)
* Pre: Drop unneeded tx0 from search results.
* Pre: Don't require a schema in some of the DB code.
The idea is to separate the transaction applying code, which is
schema-aware, from the concrete storage code, which is just concerned
with getting bits onto disk.
* Pre: Only reference Schema, not DB, in debug module.
This is part of a larger separation of the volatile PartitionMap,
which is modified every transaction, from the stable Schema, which is
infrequently modified.
* Pre: Fix indentation.
* Extract part of DB to new SchemaTypeChecking trait.
* Extract part of DB to new PartitionMapping trait.
* Pre: Don't expect :db.part/tx partition to advance when tx fails.
This fails right now, because we allocate tx IDs even when we shouldn't.
* Sketch a db interface without DB.
* Add ValueParseError; use error-chain in tx-parser.
This can be simplified when
https://github.com/Marwes/combine/issues/86 makes it to a published
release, but this unblocks us for now. This converts the `combine`
error type `ParseError<&'a [edn::Value]>` to a type with owned
`Vec<edn::Value>` collections, re-using `edn::Value::Vector` for
making them `Display`.
* Pre: Accept Borrow<Schema> instead of just &Schema in debug module.
This makes it easy to use Rc<Schema> or Arc<Schema> without inserting
&* sigils throughout the code.
* Use error-chain in query-parser.
There are a few things to point out here:
- the fine grained error types have been flattened into one crate-wide
error type; it's pretty easy to regain the granularity as needed.
- edn::ParseError is automatically lifted to
mentat_query_parser::errors::Error;
- we use mentat_parser_utils::ValueParser to maintain parsing error
information from `combine`.
* Patch up top-level.
* Review comment: Only `borrow()` once.
2017-02-24 23:32:41 +00:00
|
|
|
extern crate mentat_parser_utils;
|
2017-01-25 22:06:19 +00:00
|
|
|
extern crate mentat_query;
|
|
|
|
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
2017-02-03 02:32:00 +00:00
|
|
|
use self::mentat_query::{
|
|
|
|
FindQuery,
|
|
|
|
FromValue,
|
|
|
|
SrcVar,
|
|
|
|
Variable,
|
|
|
|
};
|
Extract partial storage abstraction; use error-chain throughout. Fixes #328. r=rnewman (#341)
* Pre: Drop unneeded tx0 from search results.
* Pre: Don't require a schema in some of the DB code.
The idea is to separate the transaction applying code, which is
schema-aware, from the concrete storage code, which is just concerned
with getting bits onto disk.
* Pre: Only reference Schema, not DB, in debug module.
This is part of a larger separation of the volatile PartitionMap,
which is modified every transaction, from the stable Schema, which is
infrequently modified.
* Pre: Fix indentation.
* Extract part of DB to new SchemaTypeChecking trait.
* Extract part of DB to new PartitionMapping trait.
* Pre: Don't expect :db.part/tx partition to advance when tx fails.
This fails right now, because we allocate tx IDs even when we shouldn't.
* Sketch a db interface without DB.
* Add ValueParseError; use error-chain in tx-parser.
This can be simplified when
https://github.com/Marwes/combine/issues/86 makes it to a published
release, but this unblocks us for now. This converts the `combine`
error type `ParseError<&'a [edn::Value]>` to a type with owned
`Vec<edn::Value>` collections, re-using `edn::Value::Vector` for
making them `Display`.
* Pre: Accept Borrow<Schema> instead of just &Schema in debug module.
This makes it easy to use Rc<Schema> or Arc<Schema> without inserting
&* sigils throughout the code.
* Use error-chain in query-parser.
There are a few things to point out here:
- the fine grained error types have been flattened into one crate-wide
error type; it's pretty easy to regain the granularity as needed.
- edn::ParseError is automatically lifted to
mentat_query_parser::errors::Error;
- we use mentat_parser_utils::ValueParser to maintain parsing error
information from `combine`.
* Patch up top-level.
* Review comment: Only `borrow()` once.
2017-02-24 23:32:41 +00:00
|
|
|
use self::mentat_parser_utils::ValueParseError;
|
|
|
|
|
2017-02-03 02:32:00 +00:00
|
|
|
use super::parse::{
|
2017-02-24 05:13:04 +00:00
|
|
|
Result,
|
|
|
|
ErrorKind,
|
2017-02-03 02:32:00 +00:00
|
|
|
QueryParseResult,
|
|
|
|
clause_seq_to_patterns,
|
|
|
|
};
|
|
|
|
|
|
|
|
use super::util::vec_to_keyword_map;
|
|
|
|
|
|
|
|
/// If the provided slice of EDN values are all variables as
|
|
|
|
/// defined by `value_to_variable`, return a `Vec` of `Variable`s.
|
|
|
|
/// Otherwise, return the unrecognized Value in a `NotAVariableError`.
|
Extract partial storage abstraction; use error-chain throughout. Fixes #328. r=rnewman (#341)
* Pre: Drop unneeded tx0 from search results.
* Pre: Don't require a schema in some of the DB code.
The idea is to separate the transaction applying code, which is
schema-aware, from the concrete storage code, which is just concerned
with getting bits onto disk.
* Pre: Only reference Schema, not DB, in debug module.
This is part of a larger separation of the volatile PartitionMap,
which is modified every transaction, from the stable Schema, which is
infrequently modified.
* Pre: Fix indentation.
* Extract part of DB to new SchemaTypeChecking trait.
* Extract part of DB to new PartitionMapping trait.
* Pre: Don't expect :db.part/tx partition to advance when tx fails.
This fails right now, because we allocate tx IDs even when we shouldn't.
* Sketch a db interface without DB.
* Add ValueParseError; use error-chain in tx-parser.
This can be simplified when
https://github.com/Marwes/combine/issues/86 makes it to a published
release, but this unblocks us for now. This converts the `combine`
error type `ParseError<&'a [edn::Value]>` to a type with owned
`Vec<edn::Value>` collections, re-using `edn::Value::Vector` for
making them `Display`.
* Pre: Accept Borrow<Schema> instead of just &Schema in debug module.
This makes it easy to use Rc<Schema> or Arc<Schema> without inserting
&* sigils throughout the code.
* Use error-chain in query-parser.
There are a few things to point out here:
- the fine grained error types have been flattened into one crate-wide
error type; it's pretty easy to regain the granularity as needed.
- edn::ParseError is automatically lifted to
mentat_query_parser::errors::Error;
- we use mentat_parser_utils::ValueParser to maintain parsing error
information from `combine`.
* Patch up top-level.
* Review comment: Only `borrow()` once.
2017-02-24 23:32:41 +00:00
|
|
|
fn values_to_variables(vals: &[edn::Value]) -> Result<Vec<Variable>> {
|
2017-02-03 02:32:00 +00:00
|
|
|
let mut out: Vec<Variable> = Vec::with_capacity(vals.len());
|
|
|
|
for v in vals {
|
|
|
|
if let Some(var) = Variable::from_value(v) {
|
|
|
|
out.push(var);
|
|
|
|
continue;
|
|
|
|
}
|
Extract partial storage abstraction; use error-chain throughout. Fixes #328. r=rnewman (#341)
* Pre: Drop unneeded tx0 from search results.
* Pre: Don't require a schema in some of the DB code.
The idea is to separate the transaction applying code, which is
schema-aware, from the concrete storage code, which is just concerned
with getting bits onto disk.
* Pre: Only reference Schema, not DB, in debug module.
This is part of a larger separation of the volatile PartitionMap,
which is modified every transaction, from the stable Schema, which is
infrequently modified.
* Pre: Fix indentation.
* Extract part of DB to new SchemaTypeChecking trait.
* Extract part of DB to new PartitionMapping trait.
* Pre: Don't expect :db.part/tx partition to advance when tx fails.
This fails right now, because we allocate tx IDs even when we shouldn't.
* Sketch a db interface without DB.
* Add ValueParseError; use error-chain in tx-parser.
This can be simplified when
https://github.com/Marwes/combine/issues/86 makes it to a published
release, but this unblocks us for now. This converts the `combine`
error type `ParseError<&'a [edn::Value]>` to a type with owned
`Vec<edn::Value>` collections, re-using `edn::Value::Vector` for
making them `Display`.
* Pre: Accept Borrow<Schema> instead of just &Schema in debug module.
This makes it easy to use Rc<Schema> or Arc<Schema> without inserting
&* sigils throughout the code.
* Use error-chain in query-parser.
There are a few things to point out here:
- the fine grained error types have been flattened into one crate-wide
error type; it's pretty easy to regain the granularity as needed.
- edn::ParseError is automatically lifted to
mentat_query_parser::errors::Error;
- we use mentat_parser_utils::ValueParser to maintain parsing error
information from `combine`.
* Patch up top-level.
* Review comment: Only `borrow()` once.
2017-02-24 23:32:41 +00:00
|
|
|
bail!(ErrorKind::NotAVariableError(v.clone()));
|
2017-02-03 02:32:00 +00:00
|
|
|
}
|
|
|
|
return Ok(out);
|
|
|
|
}
|
2017-01-25 22:06:19 +00:00
|
|
|
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
fn parse_find_parts(find: &[edn::Value],
|
|
|
|
ins: Option<&[edn::Value]>,
|
|
|
|
with: Option<&[edn::Value]>,
|
|
|
|
wheres: &[edn::Value])
|
|
|
|
-> QueryParseResult {
|
|
|
|
// :find must be an array of plain var symbols (?foo), pull expressions, and aggregates.
|
|
|
|
// For now we only support variables and the annotations necessary to declare which
|
|
|
|
// flavor of :find we want:
|
|
|
|
// ?x ?y ?z = FindRel
|
|
|
|
// [?x ...] = FindColl
|
|
|
|
// ?x . = FindScalar
|
|
|
|
// [?x ?y ?z] = FindTuple
|
|
|
|
//
|
|
|
|
// :in must be an array of sources ($), rules (%), and vars (?). For now we only support the
|
|
|
|
// default source. :in can be omitted, in which case the default is equivalent to `:in $`.
|
|
|
|
// TODO: process `ins`.
|
|
|
|
let source = SrcVar::DefaultSrc;
|
|
|
|
|
|
|
|
// :with is an array of variables. This is simple, so we don't use a parser.
|
2017-02-03 02:32:00 +00:00
|
|
|
let with_vars = if let Some(vals) = with {
|
|
|
|
values_to_variables(vals)?
|
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
};
|
|
|
|
|
2017-01-25 22:06:19 +00:00
|
|
|
// :wheres is a whole datastructure.
|
2017-02-03 02:32:00 +00:00
|
|
|
let where_clauses = clause_seq_to_patterns(wheres)?;
|
2017-01-25 22:06:19 +00:00
|
|
|
|
|
|
|
super::parse::find_seq_to_find_spec(find)
|
|
|
|
.map(|spec| {
|
|
|
|
FindQuery {
|
|
|
|
find_spec: spec,
|
|
|
|
default_source: source,
|
2017-02-03 02:32:00 +00:00
|
|
|
with: with_vars,
|
|
|
|
in_vars: vec!(), // TODO
|
|
|
|
in_sources: vec!(), // TODO
|
|
|
|
where_clauses: where_clauses,
|
2017-01-25 22:06:19 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_find_map(map: BTreeMap<edn::Keyword, Vec<edn::Value>>) -> QueryParseResult {
|
|
|
|
// Eagerly awaiting `const fn`.
|
|
|
|
let kw_find = edn::Keyword::new("find");
|
|
|
|
let kw_in = edn::Keyword::new("in");
|
|
|
|
let kw_with = edn::Keyword::new("with");
|
|
|
|
let kw_where = edn::Keyword::new("where");
|
|
|
|
|
|
|
|
// Oh, if only we had `guard`.
|
|
|
|
if let Some(find) = map.get(&kw_find) {
|
|
|
|
if let Some(wheres) = map.get(&kw_where) {
|
2017-02-24 05:13:04 +00:00
|
|
|
parse_find_parts(find,
|
|
|
|
map.get(&kw_in).map(|x| x.as_slice()),
|
|
|
|
map.get(&kw_with).map(|x| x.as_slice()),
|
|
|
|
wheres)
|
2017-01-25 22:06:19 +00:00
|
|
|
} else {
|
2017-02-24 05:13:04 +00:00
|
|
|
bail!(ErrorKind::MissingFieldError(kw_where))
|
2017-01-25 22:06:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-02-24 05:13:04 +00:00
|
|
|
bail!(ErrorKind::MissingFieldError(kw_find))
|
2017-01-25 22:06:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_find_edn_map(map: BTreeMap<edn::Value, edn::Value>) -> QueryParseResult {
|
|
|
|
// Every key must be a Keyword. Every value must be a Vec.
|
|
|
|
let mut m = BTreeMap::new();
|
|
|
|
|
|
|
|
if map.is_empty() {
|
|
|
|
return parse_find_map(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (k, v) in map {
|
|
|
|
if let edn::Value::Keyword(kw) = k {
|
|
|
|
if let edn::Value::Vector(vec) = v {
|
|
|
|
m.insert(kw, vec);
|
|
|
|
continue;
|
|
|
|
} else {
|
2017-02-24 05:13:04 +00:00
|
|
|
bail!(ErrorKind::InvalidInputError(v))
|
2017-01-25 22:06:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-02-24 05:13:04 +00:00
|
|
|
bail!(ErrorKind::InvalidInputError(k))
|
2017-01-25 22:06:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parse_find_map(m)
|
|
|
|
}
|
|
|
|
|
2017-02-13 18:30:02 +00:00
|
|
|
pub fn parse_find_string(string: &str) -> QueryParseResult {
|
|
|
|
let expr = edn::parse::value(string)?;
|
2017-02-14 15:43:32 +00:00
|
|
|
parse_find(expr.without_spans())
|
2017-02-13 18:30:02 +00:00
|
|
|
}
|
|
|
|
|
2017-01-25 22:06:19 +00:00
|
|
|
pub fn parse_find(expr: edn::Value) -> QueryParseResult {
|
|
|
|
// No `match` because scoping and use of `expr` in error handling is nuts.
|
|
|
|
if let edn::Value::Map(m) = expr {
|
|
|
|
return parse_find_edn_map(m);
|
|
|
|
}
|
|
|
|
if let edn::Value::Vector(ref v) = expr {
|
|
|
|
if let Some(m) = vec_to_keyword_map(v) {
|
|
|
|
return parse_find_map(m);
|
|
|
|
}
|
|
|
|
}
|
2017-02-24 05:13:04 +00:00
|
|
|
bail!(ErrorKind::InvalidInputError(expr))
|
2017-01-25 22:06:19 +00:00
|
|
|
}
|
2017-02-03 02:32:00 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test_parse {
|
|
|
|
extern crate edn;
|
|
|
|
|
|
|
|
use self::edn::{NamespacedKeyword, PlainSymbol};
|
2017-02-14 15:43:32 +00:00
|
|
|
use self::edn::types::Value;
|
2017-02-03 02:32:00 +00:00
|
|
|
use super::mentat_query::{
|
|
|
|
Element,
|
|
|
|
FindSpec,
|
|
|
|
Pattern,
|
|
|
|
PatternNonValuePlace,
|
|
|
|
PatternValuePlace,
|
|
|
|
SrcVar,
|
|
|
|
Variable,
|
|
|
|
WhereClause,
|
|
|
|
};
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
// TODO: when #224 lands, fix to_keyword to be variadic.
|
|
|
|
#[test]
|
|
|
|
fn test_parse_find() {
|
2017-02-14 15:43:32 +00:00
|
|
|
let truncated_input = edn::Value::Vector(vec![Value::from_keyword(None, "find")]);
|
2017-02-03 02:32:00 +00:00
|
|
|
assert!(parse_find(truncated_input).is_err());
|
|
|
|
|
|
|
|
let input = edn::Value::Vector(vec![
|
2017-02-14 15:43:32 +00:00
|
|
|
Value::from_keyword(None, "find"),
|
|
|
|
Value::from_symbol(None, "?x"),
|
|
|
|
Value::from_symbol(None, "?y"),
|
|
|
|
Value::from_keyword(None, "where"),
|
2017-02-03 02:32:00 +00:00
|
|
|
edn::Value::Vector(vec![
|
2017-02-14 15:43:32 +00:00
|
|
|
Value::from_symbol(None, "?x"),
|
|
|
|
Value::from_keyword("foo", "bar"),
|
|
|
|
Value::from_symbol(None, "?y"),
|
2017-02-03 02:32:00 +00:00
|
|
|
]),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let parsed = parse_find(input).unwrap();
|
|
|
|
if let FindSpec::FindRel(elems) = parsed.find_spec {
|
|
|
|
assert_eq!(2, elems.len());
|
|
|
|
assert_eq!(vec![
|
|
|
|
Element::Variable(Variable(edn::PlainSymbol::new("?x"))),
|
|
|
|
Element::Variable(Variable(edn::PlainSymbol::new("?y"))),
|
|
|
|
], elems);
|
|
|
|
} else {
|
|
|
|
panic!("Expected FindRel.");
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(SrcVar::DefaultSrc, parsed.default_source);
|
|
|
|
assert_eq!(parsed.where_clauses,
|
|
|
|
vec![
|
|
|
|
WhereClause::Pattern(Pattern {
|
|
|
|
source: None,
|
|
|
|
entity: PatternNonValuePlace::Variable(Variable(PlainSymbol::new("?x"))),
|
|
|
|
attribute: PatternNonValuePlace::Ident(NamespacedKeyword::new("foo", "bar")),
|
|
|
|
value: PatternValuePlace::Variable(Variable(PlainSymbol::new("?y"))),
|
|
|
|
tx: PatternNonValuePlace::Placeholder,
|
|
|
|
})]);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|