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. /// The default string formatter is returning a N-Triples, Turtle and SPARQL compatible representation.
/// `BlankNode::default().to_string()` should return something like `_:00112233445566778899aabbccddeeff` /// `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 { pub struct BlankNode {
id: u128, id: u128,
str: [u8; 32], str: [u8; 32],

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

@ -5,7 +5,7 @@ use rio_api::model as rio;
use std::fmt; 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). /// 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 { pub enum NamedOrBlankNode {
NamedNode(NamedNode), NamedNode(NamedNode),
BlankNode(BlankNode), 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) /// 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). /// 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 { pub enum Term {
NamedNode(NamedNode), NamedNode(NamedNode),
BlankNode(BlankNode), 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) /// 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 { pub struct Triple {
subject: NamedOrBlankNode, subject: NamedOrBlankNode,
predicate: NamedNode, 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) /// 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 { pub struct Quad {
subject: NamedOrBlankNode, subject: NamedOrBlankNode,
predicate: NamedNode, predicate: NamedNode,

@ -4,12 +4,11 @@ use crate::model::*;
use crate::sparql::model::*; use crate::sparql::model::*;
use oxiri::Iri; use oxiri::Iri;
use rio_api::model as rio; use rio_api::model as rio;
use std::collections::BTreeMap;
use std::collections::BTreeSet; use std::collections::BTreeSet;
use std::fmt; use std::fmt;
use std::ops::Add; use std::ops::Add;
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)] #[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum NamedNodeOrVariable { pub enum NamedNodeOrVariable {
NamedNode(NamedNode), NamedNode(NamedNode),
Variable(Variable), 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 { pub enum TermOrVariable {
Term(Term), Term(Term),
Variable(Variable), 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 { pub struct StaticBindings {
variables: Vec<Variable>, variables: Vec<Variable>,
values: Vec<Vec<Option<Term>>>, 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 struct TriplePattern {
pub subject: TermOrVariable, pub subject: TermOrVariable,
pub predicate: NamedNodeOrVariable, 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 { pub enum PropertyPath {
PredicatePath(NamedNode), PredicatePath(NamedNode),
InversePath(Box<PropertyPath>), 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 struct PathPattern {
pub subject: TermOrVariable, pub subject: TermOrVariable,
pub path: PropertyPath, 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 { pub enum TripleOrPathPattern {
Triple(TriplePattern), Triple(TriplePattern),
Path(PathPattern), 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 { pub enum Expression {
NamedNode(NamedNode), NamedNode(NamedNode),
Literal(Literal), 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 { pub enum Function {
Str, Str,
Lang, 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 { pub enum GraphPattern {
BGP(Vec<TripleOrPathPattern>), BGP(Vec<TripleOrPathPattern>),
Join(Box<GraphPattern>, Box<GraphPattern>), Join(Box<GraphPattern>, Box<GraphPattern>),
@ -648,7 +647,7 @@ pub enum GraphPattern {
Extend(Box<GraphPattern>, Variable, Expression), Extend(Box<GraphPattern>, Variable, Expression),
Minus(Box<GraphPattern>, Box<GraphPattern>), Minus(Box<GraphPattern>, Box<GraphPattern>),
Service(NamedNodeOrVariable, Box<GraphPattern>, bool), Service(NamedNodeOrVariable, Box<GraphPattern>, bool),
AggregateJoin(GroupPattern, BTreeMap<Aggregation, Variable>), AggregateJoin(GroupPattern, Vec<(Aggregation, Variable)>),
Data(StaticBindings), Data(StaticBindings),
OrderBy(Box<GraphPattern>, Vec<OrderComparator>), OrderBy(Box<GraphPattern>, Vec<OrderComparator>),
Project(Box<GraphPattern>, Vec<Variable>), Project(Box<GraphPattern>, Vec<Variable>),
@ -810,7 +809,7 @@ impl GraphPattern {
GraphPattern::Minus(a, _) => a.add_visible_variables(vars), GraphPattern::Minus(a, _) => a.add_visible_variables(vars),
GraphPattern::Service(_, p, _) => p.add_visible_variables(vars), GraphPattern::Service(_, p, _) => p.add_visible_variables(vars),
GraphPattern::AggregateJoin(_, a) => { GraphPattern::AggregateJoin(_, a) => {
for v in a.values() { for (_, v) in a {
vars.insert(v); 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>); pub struct GroupPattern(pub Vec<Variable>, pub Box<GraphPattern>);
impl fmt::Display for GroupPattern { 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 { pub enum Aggregation {
Count(Option<Box<Expression>>, bool), Count(Option<Box<Expression>>, bool),
Sum(Box<Expression>, bool), Sum(Box<Expression>, bool),
@ -1222,7 +1221,7 @@ fn fmt_str(value: &str) -> rio::Literal<'_> {
rio::Literal::Simple { value } rio::Literal::Simple { value }
} }
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)] #[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum OrderComparator { pub enum OrderComparator {
Asc(Expression), Asc(Expression),
Desc(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 struct DatasetSpec {
pub default: Vec<NamedNode>, pub default: Vec<NamedNode>,
pub named: Vec<NamedNode>, pub named: Vec<NamedNode>,

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

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

Loading…
Cancel
Save