Removes global ordering of blank node and literals

It does not corresponds to anything meaningful
pull/41/head
Tpt 4 years ago
parent 71016ce475
commit 827667e495
  1. 2
      lib/src/model/blank_node.rs
  2. 4
      lib/src/model/literal.rs
  3. 8
      lib/src/model/triple.rs
  4. 33
      lib/src/sparql/algebra.rs
  5. 33
      lib/src/sparql/parser.rs
  6. 16
      lib/src/sparql/plan_builder.rs

@ -14,7 +14,7 @@ use std::str;
/// The default string formatter is returning a N-Triples, Turtle and SPARQL compatible representation.
/// `BlankNode::default().to_string()` should return something like `_:00112233445566778899aabbccddeeff`
///
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)]
pub struct BlankNode {
id: u128,
str: [u8; 32],

@ -32,10 +32,10 @@ use std::option::Option;
/// );
/// # Result::<(), LanguageTagParseError>::Ok(())
/// ```
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub struct Literal(LiteralContent);
#[derive(PartialEq, Eq, Ord, PartialOrd, Debug, Clone, Hash)]
#[derive(PartialEq, Eq, Debug, Clone, Hash)]
enum LiteralContent {
String(String),
LanguageTaggedString { value: String, language: String },

@ -5,7 +5,7 @@ use rio_api::model as rio;
use std::fmt;
/// The union of [IRIs](https://www.w3.org/TR/rdf11-concepts/#dfn-iri) and [blank nodes](https://www.w3.org/TR/rdf11-concepts/#dfn-blank-node).
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum NamedOrBlankNode {
NamedNode(NamedNode),
BlankNode(BlankNode),
@ -59,7 +59,7 @@ impl<'a> From<&'a NamedOrBlankNode> for rio::NamedOrBlankNode<'a> {
/// An RDF [term](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-term)
/// It is the union of [IRIs](https://www.w3.org/TR/rdf11-concepts/#dfn-iri), [blank nodes](https://www.w3.org/TR/rdf11-concepts/#dfn-blank-node) and [literals](https://www.w3.org/TR/rdf11-concepts/#dfn-literal).
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum Term {
NamedNode(NamedNode),
BlankNode(BlankNode),
@ -140,7 +140,7 @@ impl<'a> From<&'a Term> for rio::Term<'a> {
}
/// A [RDF triple](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple)
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub struct Triple {
subject: NamedOrBlankNode,
predicate: NamedNode,
@ -219,7 +219,7 @@ impl<'a> From<&'a Triple> for rio::Triple<'a> {
}
/// A [triple](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple) in a [RDF dataset](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-dataset)
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub struct Quad {
subject: NamedOrBlankNode,
predicate: NamedNode,

@ -4,12 +4,11 @@ use crate::model::*;
use crate::sparql::model::*;
use oxiri::Iri;
use rio_api::model as rio;
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::fmt;
use std::ops::Add;
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum NamedNodeOrVariable {
NamedNode(NamedNode),
Variable(Variable),
@ -36,7 +35,7 @@ impl From<Variable> for NamedNodeOrVariable {
}
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum TermOrVariable {
Term(Term),
Variable(Variable),
@ -90,7 +89,7 @@ impl From<NamedNodeOrVariable> for TermOrVariable {
}
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub struct StaticBindings {
variables: Vec<Variable>,
values: Vec<Vec<Option<Term>>>,
@ -127,7 +126,7 @@ impl Default for StaticBindings {
}
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub struct TriplePattern {
pub subject: TermOrVariable,
pub predicate: NamedNodeOrVariable,
@ -154,7 +153,7 @@ impl fmt::Display for TriplePattern {
}
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum PropertyPath {
PredicatePath(NamedNode),
InversePath(Box<PropertyPath>),
@ -228,7 +227,7 @@ impl From<NamedNode> for PropertyPath {
}
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub struct PathPattern {
pub subject: TermOrVariable,
pub path: PropertyPath,
@ -269,7 +268,7 @@ impl<'a> fmt::Display for SparqlPathPattern<'a> {
}
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum TripleOrPathPattern {
Triple(TriplePattern),
Path(PathPattern),
@ -323,7 +322,7 @@ impl<'a> fmt::Display for SparqlTripleOrPathPattern<'a> {
}
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum Expression {
NamedNode(NamedNode),
Literal(Literal),
@ -526,7 +525,7 @@ impl<'a> fmt::Display for SparqlExpression<'a> {
}
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum Function {
Str,
Lang,
@ -637,7 +636,7 @@ impl fmt::Display for Function {
}
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum GraphPattern {
BGP(Vec<TripleOrPathPattern>),
Join(Box<GraphPattern>, Box<GraphPattern>),
@ -648,7 +647,7 @@ pub enum GraphPattern {
Extend(Box<GraphPattern>, Variable, Expression),
Minus(Box<GraphPattern>, Box<GraphPattern>),
Service(NamedNodeOrVariable, Box<GraphPattern>, bool),
AggregateJoin(GroupPattern, BTreeMap<Aggregation, Variable>),
AggregateJoin(GroupPattern, Vec<(Aggregation, Variable)>),
Data(StaticBindings),
OrderBy(Box<GraphPattern>, Vec<OrderComparator>),
Project(Box<GraphPattern>, Vec<Variable>),
@ -810,7 +809,7 @@ impl GraphPattern {
GraphPattern::Minus(a, _) => a.add_visible_variables(vars),
GraphPattern::Service(_, p, _) => p.add_visible_variables(vars),
GraphPattern::AggregateJoin(_, a) => {
for v in a.values() {
for (_, v) in a {
vars.insert(v);
}
}
@ -1020,7 +1019,7 @@ impl<'a> fmt::Display for SparqlGraphRootPattern<'a> {
}
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub struct GroupPattern(pub Vec<Variable>, pub Box<GraphPattern>);
impl fmt::Display for GroupPattern {
@ -1049,7 +1048,7 @@ fn build_sparql_select_arguments(args: &[Variable]) -> String {
}
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum Aggregation {
Count(Option<Box<Expression>>, bool),
Sum(Box<Expression>, bool),
@ -1222,7 +1221,7 @@ fn fmt_str(value: &str) -> rio::Literal<'_> {
rio::Literal::Simple { value }
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum OrderComparator {
Asc(Expression),
Desc(Expression),
@ -1254,7 +1253,7 @@ impl<'a> fmt::Display for SparqlOrderComparator<'a> {
}
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash, Default)]
#[derive(Eq, PartialEq, Debug, Clone, Hash, Default)]
pub struct DatasetSpec {
pub default: Vec<NamedNode>,
pub named: Vec<NamedNode>,

@ -8,8 +8,7 @@ use peg::error::ParseError;
use peg::parser;
use peg::str::LineCol;
use std::borrow::Cow;
use std::collections::HashMap;
use std::collections::{BTreeMap, BTreeSet};
use std::collections::{HashMap, HashSet};
use std::error::Error;
use std::str::Chars;
use std::str::FromStr;
@ -39,8 +38,8 @@ impl Query {
None
},
namespaces: HashMap::default(),
bnodes_map: BTreeMap::default(),
used_bnodes: BTreeSet::default(),
bnodes_map: HashMap::default(),
used_bnodes: HashSet::default(),
aggregations: Vec::default(),
};
@ -197,7 +196,7 @@ impl<F, T: From<F>> From<FocusedTriplePattern<F>> for FocusedTripleOrPathPattern
}
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
enum PartialGraphPattern {
Optional(GraphPattern, Option<Expression>),
Minus(GraphPattern),
@ -278,7 +277,7 @@ fn build_select(
let mut p = wher;
//GROUP BY
let aggregations = state.aggregations.pop().unwrap_or_else(BTreeMap::default);
let aggregations = state.aggregations.pop().unwrap_or_else(Vec::default);
if group.is_none() && !aggregations.is_empty() {
let const_variable = Variable::new_random();
group = Some((
@ -357,9 +356,9 @@ enum Either<L, R> {
pub struct ParserState {
base_iri: Option<Iri<String>>,
namespaces: HashMap<String, String>,
bnodes_map: BTreeMap<String, BlankNode>,
used_bnodes: BTreeSet<String>,
aggregations: Vec<BTreeMap<Aggregation, Variable>>,
bnodes_map: HashMap<String, BlankNode>,
used_bnodes: HashSet<String>,
aggregations: Vec<Vec<(Aggregation, Variable)>>,
}
impl ParserState {
@ -376,11 +375,15 @@ impl ParserState {
.aggregations
.last_mut()
.ok_or_else(|| "Unexpected aggregate")?;
Ok(aggregations.get(&agg).cloned().unwrap_or_else(|| {
let new_var = Variable::new_random();
aggregations.insert(agg, new_var.clone());
new_var
}))
Ok(aggregations
.iter()
.find_map(|(a, v)| if a == &agg { Some(v) } else { None })
.cloned()
.unwrap_or_else(|| {
let new_var = Variable::new_random();
aggregations.push((agg, new_var.clone()));
new_var
}))
}
}
@ -634,7 +637,7 @@ parser! {
}
}
rule Selection_init() = {
state.aggregations.push(BTreeMap::default())
state.aggregations.push(Vec::default())
}
rule SelectClause_option() -> SelectionOption =
i("DISTINCT") { SelectionOption::Distinct } /

@ -5,7 +5,7 @@ use crate::sparql::plan::*;
use crate::store::numeric_encoder::{Encoder, ENCODED_DEFAULT_GRAPH};
use crate::Error;
use crate::Result;
use std::collections::BTreeSet;
use std::collections::{BTreeSet, HashSet};
pub struct PlanBuilder<E: Encoder> {
encoder: E,
@ -46,7 +46,7 @@ impl<E: Encoder> PlanBuilder<E> {
let left = self.build_for_graph_pattern(a, variables, graph_name)?;
let right = self.build_for_graph_pattern(b, variables, graph_name)?;
let mut possible_problem_vars = BTreeSet::new();
let mut possible_problem_vars = ::new();
self.add_left_join_problematic_variables(&right, &mut possible_problem_vars);
//We add the extra filter if needed
@ -997,8 +997,8 @@ fn slice_key<T: Eq>(slice: &[T], element: &T) -> Option<usize> {
}
fn sort_bgp(p: &[TripleOrPathPattern]) -> Vec<&TripleOrPathPattern> {
let mut assigned_variables = BTreeSet::default();
let mut assigned_blank_nodes = BTreeSet::default();
let mut assigned_variables = HashSet::default();
let mut assigned_blank_nodes = HashSet::default();
let mut new_p: Vec<_> = p.iter().collect();
for i in 0..new_p.len() {
@ -1015,8 +1015,8 @@ fn sort_bgp(p: &[TripleOrPathPattern]) -> Vec<&TripleOrPathPattern> {
fn count_pattern_binds(
pattern: &TripleOrPathPattern,
assigned_variables: &BTreeSet<&Variable>,
assigned_blank_nodes: &BTreeSet<&BlankNode>,
assigned_variables: &HashSet<&Variable>,
assigned_blank_nodes: &HashSet<&BlankNode>,
) -> u8 {
let mut count = 12;
if let TermOrVariable::Variable(v) = pattern.subject() {
@ -1057,8 +1057,8 @@ fn count_pattern_binds(
fn add_pattern_variables<'a>(
pattern: &'a TripleOrPathPattern,
variables: &mut BTreeSet<&'a Variable>,
blank_nodes: &mut BTreeSet<&'a BlankNode>,
variables: &mut HashSet<&'a Variable>,
blank_nodes: &mut HashSet<&'a BlankNode>,
) {
if let TermOrVariable::Variable(v) = pattern.subject() {
variables.insert(v);

Loading…
Cancel
Save