Fixes BIND variables validation

pull/171/head
Tpt 3 years ago
parent a7b48a0bda
commit f0bd89d77f
  1. 30
      spargebra/src/algebra.rs
  2. 17
      spargebra/src/parser.rs
  3. 4
      testsuite/tests/sparql.rs

@ -739,12 +739,30 @@ impl<'a> fmt::Display for SparqlGraphPattern<'a> {
path, path,
object, object,
} => write!(f, "{} {} {} .", subject, SparqlPropertyPath(path), object), } => write!(f, "{} {} {} .", subject, SparqlPropertyPath(path), object),
GraphPattern::Join { left, right } => write!( GraphPattern::Join { left, right } => {
f, if matches!(
"{} {}", right.as_ref(),
SparqlGraphPattern(&*left), GraphPattern::LeftJoin { .. }
SparqlGraphPattern(&*right) | GraphPattern::Minus { .. }
), | GraphPattern::Extend { .. }
| GraphPattern::Filter { .. }
) {
// The second block might be considered as a modification of the first one.
write!(
f,
"{} {{ {} }}",
SparqlGraphPattern(&*left),
SparqlGraphPattern(&*right)
)
} else {
write!(
f,
"{} {}",
SparqlGraphPattern(&*left),
SparqlGraphPattern(&*right)
)
}
}
GraphPattern::LeftJoin { left, right, expr } => { GraphPattern::LeftJoin { left, right, expr } => {
if let Some(expr) = expr { if let Some(expr) = expr {
write!( write!(

@ -1352,14 +1352,10 @@ parser! {
} }
//[54] //[54]
rule GroupGraphPatternSub() -> GraphPattern = a:TriplesBlock()? _ b:GroupGraphPatternSub_item()* { rule GroupGraphPatternSub() -> GraphPattern = a:TriplesBlock()? _ b:GroupGraphPatternSub_item()* {?
let mut p = a.map_or_else(Vec::default, |v| vec![PartialGraphPattern::Other(build_bgp(v))]);
for v in b {
p.extend(v)
}
let mut filter: Option<Expression> = None; let mut filter: Option<Expression> = None;
let mut g = GraphPattern::default(); let mut g = a.map_or_else(GraphPattern::default, build_bgp);
for e in p { for e in b.into_iter().flatten() {
match e { match e {
PartialGraphPattern::Optional(p, f) => { PartialGraphPattern::Optional(p, f) => {
g = GraphPattern::LeftJoin { left: Box::new(g), right: Box::new(p), expr: f } g = GraphPattern::LeftJoin { left: Box::new(g), right: Box::new(p), expr: f }
@ -1368,6 +1364,9 @@ parser! {
g = GraphPattern::Minus { left: Box::new(g), right: Box::new(p) } g = GraphPattern::Minus { left: Box::new(g), right: Box::new(p) }
} }
PartialGraphPattern::Bind(expr, var) => { PartialGraphPattern::Bind(expr, var) => {
if g.visible_variables().contains(&var) {
return Err("BIND is overriding an existing variable")
}
g = GraphPattern::Extend { inner: Box::new(g), var, expr } g = GraphPattern::Extend { inner: Box::new(g), var, expr }
} }
PartialGraphPattern::Filter(expr) => filter = Some(if let Some(f) = filter { PartialGraphPattern::Filter(expr) => filter = Some(if let Some(f) = filter {
@ -1379,11 +1378,11 @@ parser! {
} }
} }
if let Some(expr) = filter { Ok(if let Some(expr) = filter {
GraphPattern::Filter { expr, inner: Box::new(g) } GraphPattern::Filter { expr, inner: Box::new(g) }
} else { } else {
g g
} })
} }
rule GroupGraphPatternSub_item() -> Vec<PartialGraphPattern> = a:GraphPatternNotTriples() _ ("." _)? b:TriplesBlock()? _ { rule GroupGraphPatternSub_item() -> Vec<PartialGraphPattern> = a:GraphPatternNotTriples() _ ("." _)? b:TriplesBlock()? _ {
let mut result = vec![a]; let mut result = vec![a];

@ -69,10 +69,6 @@ fn sparql11_query_w3c_evaluation_testsuite() -> Result<()> {
run_testsuite( run_testsuite(
"http://www.w3.org/2009/sparql/docs/tests/data-sparql11/manifest-sparql11-query.ttl", "http://www.w3.org/2009/sparql/docs/tests/data-sparql11/manifest-sparql11-query.ttl",
vec![ vec![
//Bad SPARQL query that should be rejected by the parser
"http://www.w3.org/2009/sparql/docs/tests/data-sparql11/syntax-query/manifest#test_60",
"http://www.w3.org/2009/sparql/docs/tests/data-sparql11/syntax-query/manifest#test_61a",
"http://www.w3.org/2009/sparql/docs/tests/data-sparql11/syntax-query/manifest#test_62a",
//BNODE() scope is currently wrong //BNODE() scope is currently wrong
"http://www.w3.org/2009/sparql/docs/tests/data-sparql11/functions/manifest#bnode01", "http://www.w3.org/2009/sparql/docs/tests/data-sparql11/functions/manifest#bnode01",
//Property path with unbound graph name are not supported yet //Property path with unbound graph name are not supported yet

Loading…
Cancel
Save