Adds OFFSET and LIMIT to the SPARQL algebra

pull/10/head
Tpt 7 years ago
parent 644499dee8
commit f3ab335ad3
  1. 4
      src/sparql/parser.rs
  2. 37
      src/sparql/sparql_grammar.rustpeg

@ -153,6 +153,7 @@ mod grammar {
wher: MultiSetPattern, wher: MultiSetPattern,
having: Option<Expression>, having: Option<Expression>,
order_by: Option<Vec<OrderComparator>>, order_by: Option<Vec<OrderComparator>>,
offset_limit: Option<(usize, Option<usize>)>,
values: Option<MultiSetPattern>, values: Option<MultiSetPattern>,
) -> ListPattern { ) -> ListPattern {
let mut p = wher; let mut p = wher;
@ -191,6 +192,9 @@ mod grammar {
SelectionOption::Reduced => m = ListPattern::Reduced(Box::new(m)), SelectionOption::Reduced => m = ListPattern::Reduced(Box::new(m)),
SelectionOption::Default => (), SelectionOption::Default => (),
} }
if let Some((offset, limit)) = offset_limit {
m = ListPattern::Slice(Box::new(m), offset, limit)
}
m m
} }

@ -4,6 +4,7 @@ use std::char;
use model::vocab::rdf; use model::vocab::rdf;
use model::vocab::xsd; use model::vocab::xsd;
use std::iter; use std::iter;
use std::str::FromStr;
#![arguments(state: &mut ParserState)] #![arguments(state: &mut ParserState)]
@ -40,13 +41,13 @@ PrefixDecl -> () = "PREFIX"i _ ns:PNAME_NS _ i:IRIREF {
SelectQuery -> Query = s:SelectClause _ d:DatasetClauses _ w:WhereClause _ GroupClause? _ h:HavingClause? _ o:OrderClause? _ l:LimitOffsetClauses? _ v:ValuesClause { //TODO: Modifier SelectQuery -> Query = s:SelectClause _ d:DatasetClauses _ w:WhereClause _ GroupClause? _ h:HavingClause? _ o:OrderClause? _ l:LimitOffsetClauses? _ v:ValuesClause { //TODO: Modifier
Query::SelectQuery { Query::SelectQuery {
dataset: d, dataset: d,
algebra: build_select(s, w, h, o, v) algebra: build_select(s, w, h, o, l, v)
} }
} }
//[8] //[8]
SubSelect -> MultiSetPattern = s:SelectClause _ w:WhereClause _ GroupClause? _ h:HavingClause? _ o:OrderClause? _ l:LimitOffsetClauses? _ v:ValuesClause { //TODO: Modifiers SubSelect -> MultiSetPattern = s:SelectClause _ w:WhereClause _ GroupClause? _ h:HavingClause? _ o:OrderClause? _ l:LimitOffsetClauses? _ v:ValuesClause { //TODO: Modifiers
build_select(s, w, h, o, v).into() build_select(s, w, h, o, l, v).into()
} }
//[9] //[9]
@ -69,17 +70,17 @@ SelectClause_member -> SelectionMember =
//[10] //[10]
ConstructQuery -> Query = ConstructQuery -> Query =
"CONSTRUCT"i _ c:ConstructTemplate _ d:DatasetClauses _ w:WhereClause _ GroupClause? _ h:HavingClause? _ o:OrderClause? _ LimitOffsetClauses? _ v:ValuesClause { "CONSTRUCT"i _ c:ConstructTemplate _ d:DatasetClauses _ w:WhereClause _ GroupClause? _ h:HavingClause? _ o:OrderClause? _ l:LimitOffsetClauses? _ v:ValuesClause {
Query::ConstructQuery { Query::ConstructQuery {
construct: c, construct: c,
dataset: d, dataset: d,
algebra: build_select(Selection { algebra: build_select(Selection {
option: SelectionOption::Reduced, option: SelectionOption::Reduced,
variables: None variables: None
}, w, h, o, v) }, w, h, o, l, v)
} }
} / } /
"CONSTRUCT"i _ d:DatasetClauses _ "WHERE"i _ '{' _ c:ConstructQuery_optional_triple_template _ '}' _ GroupClause? _ h:HavingClause? _ o:OrderClause? _ LimitOffsetClauses? _ v:ValuesClause { "CONSTRUCT"i _ d:DatasetClauses _ "WHERE"i _ '{' _ c:ConstructQuery_optional_triple_template _ '}' _ GroupClause? _ h:HavingClause? _ o:OrderClause? _ l:LimitOffsetClauses? _ v:ValuesClause {
Query::ConstructQuery { Query::ConstructQuery {
construct: c.clone(), construct: c.clone(),
dataset: d, dataset: d,
@ -87,7 +88,7 @@ ConstructQuery -> Query =
option: SelectionOption::Reduced, option: SelectionOption::Reduced,
variables: None variables: None
}, MultiSetPattern::BGP(c.into_iter().map(|p| PropertyPathPattern::from(p)).collect()).into(), }, MultiSetPattern::BGP(c.into_iter().map(|p| PropertyPathPattern::from(p)).collect()).into(),
h, o, v) h, o, l, v)
} }
} }
@ -95,16 +96,16 @@ ConstructQuery_optional_triple_template -> Vec<TriplePattern> = TriplesTemplate
//[11] //[11]
DescribeQuery -> Query = DescribeQuery -> Query =
"DESCRIBE"i _ '*' _ d:DatasetClauses w:WhereClause? _ GroupClause? _ h:HavingClause? _ o:OrderClause? _ LimitOffsetClauses? _ v:ValuesClause { "DESCRIBE"i _ '*' _ d:DatasetClauses w:WhereClause? _ GroupClause? _ h:HavingClause? _ o:OrderClause? _ l:LimitOffsetClauses? _ v:ValuesClause {
Query::DescribeQuery { Query::DescribeQuery {
dataset: d, dataset: d,
algebra: build_select(Selection { algebra: build_select(Selection {
option: SelectionOption::Reduced, option: SelectionOption::Reduced,
variables: None variables: None
}, w.unwrap_or_else(MultiSetPattern::default), h, o, v) }, w.unwrap_or_else(MultiSetPattern::default), h, o, l, v)
} }
} / } /
"DESCRIBE"i _ p:DescribeQuery_item+ _ d:DatasetClauses w:WhereClause? _ GroupClause? _ h:HavingClause? _ o:OrderClause? _ LimitOffsetClauses? _ v:ValuesClause { "DESCRIBE"i _ p:DescribeQuery_item+ _ d:DatasetClauses w:WhereClause? _ GroupClause? _ h:HavingClause? _ o:OrderClause? _ l:LimitOffsetClauses? _ v:ValuesClause {
Query::DescribeQuery { Query::DescribeQuery {
dataset: d, dataset: d,
algebra: build_select(Selection { algebra: build_select(Selection {
@ -113,19 +114,19 @@ DescribeQuery -> Query =
NamedNodeOrVariable::NamedNode(n) => SelectionMember::Expression(n.into(), Variable::default()), NamedNodeOrVariable::NamedNode(n) => SelectionMember::Expression(n.into(), Variable::default()),
NamedNodeOrVariable::Variable(v) => SelectionMember::Variable(v) NamedNodeOrVariable::Variable(v) => SelectionMember::Variable(v)
}).collect()) }).collect())
}, w.unwrap_or_else(MultiSetPattern::default), h, o, v) }, w.unwrap_or_else(MultiSetPattern::default), h, o, l, v)
} }
} }
DescribeQuery_item -> NamedNodeOrVariable = i:VarOrIri _ { i } DescribeQuery_item -> NamedNodeOrVariable = i:VarOrIri _ { i }
//[12] //[12]
AskQuery -> Query = "ASK"i _ d:DatasetClauses w:WhereClause _ GroupClause? _ h:HavingClause? _ o:OrderClause? _ LimitOffsetClauses? _ v:ValuesClause { AskQuery -> Query = "ASK"i _ d:DatasetClauses w:WhereClause _ GroupClause? _ h:HavingClause? _ o:OrderClause? _ l:LimitOffsetClauses? _ v:ValuesClause {
Query::AskQuery { Query::AskQuery {
dataset: d, dataset: d,
algebra: build_select(Selection { algebra: build_select(Selection {
option: SelectionOption::Reduced, option: SelectionOption::Reduced,
variables: None variables: None
}, w, h, o, v) }, w, h, o, l, v)
} }
} }
@ -179,13 +180,19 @@ OrderCondition -> OrderComparator =
v: Var { Expression::from(v).into() } v: Var { Expression::from(v).into() }
//[25] //[25]
LimitOffsetClauses -> () = LimitClause _ OffsetClause? / OffsetClause _ LimitClause? LimitOffsetClauses -> (usize, Option<usize>) =
l:LimitClause _ o:OffsetClause? { (o.unwrap_or(0), Some(l)) } /
o:OffsetClause _ l:LimitClause? { (o, l) }
//[26] //[26]
LimitClause -> () = "LIMIT"i _ INTEGER LimitClause -> usize = "LIMIT"i _ l:$(INTEGER) {?
usize::from_str(l).map_err(|_| "The query limit should be a non negative integer")
}
//[27] //[27]
OffsetClause -> () = "OFFSET"i _ INTEGER OffsetClause -> usize = "OFFSET"i _ o:$(INTEGER) {?
usize::from_str(o).map_err(|_| "The query offset should be a non negative integer")
}
//[28] //[28]
ValuesClause -> Option<MultiSetPattern> = ValuesClause -> Option<MultiSetPattern> =

Loading…
Cancel
Save