spareval/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
#![doc = include_str!("../README.md")]
#![doc(test(attr(deny(warnings))))]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![doc(html_favicon_url = "https://raw.githubusercontent.com/oxigraph/oxigraph/main/logo.svg")]
#![doc(html_logo_url = "https://raw.githubusercontent.com/oxigraph/oxigraph/main/logo.svg")]
mod dataset;
mod error;
mod eval;
mod model;
mod service;
#[cfg(feature = "rdf-star")]
pub use crate::dataset::ExpressionTriple;
pub use crate::dataset::{ExpressionTerm, InternalQuad, QueryableDataset};
pub use crate::error::QueryEvaluationError;
use crate::eval::{EvalNodeWithStats, SimpleEvaluator, Timer};
pub use crate::model::{QueryResults, QuerySolution, QuerySolutionIter, QueryTripleIter};
use crate::service::ServiceHandlerRegistry;
pub use crate::service::{DefaultServiceHandler, ServiceHandler};
use json_event_parser::{JsonEvent, ToWriteJsonWriter};
use oxrdf::{NamedNode, Term, Variable};
use oxsdatatypes::{DayTimeDuration, Float};
use spargebra::Query;
use sparopt::algebra::GraphPattern;
use sparopt::Optimizer;
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::Arc;
use std::{fmt, io};
/// Evaluates a query against a given [RDF dataset](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-dataset)
///
/// Note that this evaluator does not handle the `FROM` and `FROM NAMED` part of the query.
/// You must select the proper dataset before using this struct.
///
/// To adapt this software to work on your own RDF dataset, you need to implement the [`QueryableDataset`] trait.
///
/// ```
/// use oxrdf::{Dataset, GraphName, NamedNode, Quad};
/// use spareval::{QueryEvaluator, QueryResults};
/// use spargebra::Query;
///
/// let ex = NamedNode::new("http://example.com")?;
/// let dataset = Dataset::from_iter([Quad::new(
/// ex.clone(),
/// ex.clone(),
/// ex.clone(),
/// GraphName::DefaultGraph,
/// )]);
/// let query = Query::parse("SELECT * WHERE { ?s ?p ?o }", None)?;
/// let results = QueryEvaluator::new().execute(dataset, &query);
/// if let QueryResults::Solutions(solutions) = results? {
/// let solutions = solutions.collect::<Result<Vec<_>, _>>()?;
/// assert_eq!(solutions.len(), 1);
/// assert_eq!(solutions[0]["s"], ex.into());
/// }
/// # Result::<_, Box<dyn std::error::Error>>::Ok(())
/// ```
#[derive(Clone, Default)]
pub struct QueryEvaluator {
service_handler: ServiceHandlerRegistry,
custom_functions: CustomFunctionRegistry,
without_optimizations: bool,
run_stats: bool,
}
impl QueryEvaluator {
#[must_use]
#[inline]
pub fn new() -> Self {
Self::default()
}
pub fn execute(
&self,
dataset: impl QueryableDataset,
query: &Query,
) -> Result<QueryResults, QueryEvaluationError> {
self.explain(dataset, query).0
}
/// Executes a SPARQL query while substituting some variables with the given values.
///
/// Substitution follows [RDF-dev SEP-0007](https://github.com/w3c/sparql-dev/blob/main/SEP/SEP-0007/sep-0007.md).
///
/// ```
/// use oxrdf::{Dataset, GraphName, NamedNode, Quad, Variable};
/// use spareval::{QueryEvaluator, QueryResults};
/// use spargebra::Query;
///
/// let ex = NamedNode::new("http://example.com")?;
/// let dataset = Dataset::from_iter([Quad::new(
/// ex.clone(),
/// ex.clone(),
/// ex.clone(),
/// GraphName::DefaultGraph,
/// )]);
/// let query = Query::parse("SELECT * WHERE { ?s ?p ?o }", None)?;
/// let results = QueryEvaluator::new().execute_with_substituted_variables(
/// dataset,
/// &query,
/// [(Variable::new("s")?, ex.clone().into())],
/// );
/// if let QueryResults::Solutions(solutions) = results? {
/// let solutions = solutions.collect::<Result<Vec<_>, _>>()?;
/// assert_eq!(solutions.len(), 1);
/// assert_eq!(solutions[0]["s"], ex.into());
/// }
/// # Result::<_, Box<dyn std::error::Error>>::Ok(())
/// ```
pub fn execute_with_substituted_variables(
&self,
dataset: impl QueryableDataset,
query: &Query,
substitutions: impl IntoIterator<Item = (Variable, Term)>,
) -> Result<QueryResults, QueryEvaluationError> {
self.explain_with_substituted_variables(dataset, query, substitutions)
.0
}
pub fn explain(
&self,
dataset: impl QueryableDataset,
query: &Query,
) -> (Result<QueryResults, QueryEvaluationError>, QueryExplanation) {
self.explain_with_substituted_variables(dataset, query, [])
}
pub fn explain_with_substituted_variables(
&self,
dataset: impl QueryableDataset,
query: &Query,
substitutions: impl IntoIterator<Item = (Variable, Term)>,
) -> (Result<QueryResults, QueryEvaluationError>, QueryExplanation) {
let start_planning = Timer::now();
let (results, plan_node_with_stats, planning_duration) = match query {
Query::Select {
pattern, base_iri, ..
} => {
let mut pattern = GraphPattern::from(pattern);
if !self.without_optimizations {
pattern = Optimizer::optimize_graph_pattern(pattern);
}
let planning_duration = start_planning.elapsed();
let (results, explanation) = SimpleEvaluator::new(
dataset,
base_iri.clone().map(Rc::new),
Rc::new(self.service_handler.clone()),
Rc::new(self.custom_functions.clone()),
self.run_stats,
)
.evaluate_select(&pattern, substitutions);
(
results.map(QueryResults::Solutions),
explanation,
planning_duration,
)
}
Query::Ask {
pattern, base_iri, ..
} => {
let mut pattern = GraphPattern::from(pattern);
if !self.without_optimizations {
pattern = Optimizer::optimize_graph_pattern(pattern);
}
let planning_duration = start_planning.elapsed();
let (results, explanation) = SimpleEvaluator::new(
dataset,
base_iri.clone().map(Rc::new),
Rc::new(self.service_handler.clone()),
Rc::new(self.custom_functions.clone()),
self.run_stats,
)
.evaluate_ask(&pattern, substitutions);
(
results.map(QueryResults::Boolean),
explanation,
planning_duration,
)
}
Query::Construct {
template,
pattern,
base_iri,
..
} => {
let mut pattern = GraphPattern::from(pattern);
if !self.without_optimizations {
pattern = Optimizer::optimize_graph_pattern(pattern);
}
let planning_duration = start_planning.elapsed();
let (results, explanation) = SimpleEvaluator::new(
dataset,
base_iri.clone().map(Rc::new),
Rc::new(self.service_handler.clone()),
Rc::new(self.custom_functions.clone()),
self.run_stats,
)
.evaluate_construct(&pattern, template, substitutions);
(
results.map(QueryResults::Graph),
explanation,
planning_duration,
)
}
Query::Describe {
pattern, base_iri, ..
} => {
let mut pattern = GraphPattern::from(pattern);
if !self.without_optimizations {
pattern = Optimizer::optimize_graph_pattern(pattern);
}
let planning_duration = start_planning.elapsed();
let (results, explanation) = SimpleEvaluator::new(
dataset,
base_iri.clone().map(Rc::new),
Rc::new(self.service_handler.clone()),
Rc::new(self.custom_functions.clone()),
self.run_stats,
)
.evaluate_describe(&pattern, substitutions);
(
results.map(QueryResults::Graph),
explanation,
planning_duration,
)
}
};
let explanation = QueryExplanation {
inner: plan_node_with_stats,
with_stats: self.run_stats,
planning_duration,
};
(results, explanation)
}
/// Use a given [`ServiceHandler`] to execute [SPARQL 1.1 Federated Query](https://www.w3.org/TR/sparql11-federated-query/) SERVICE calls.
///
/// See [`ServiceHandler`] for an example.
#[inline]
#[must_use]
pub fn with_service_handler(
mut self,
service_name: impl Into<NamedNode>,
handler: impl ServiceHandler + 'static,
) -> Self {
self.service_handler = self
.service_handler
.with_handler(service_name.into(), handler);
self
}
/// Use a given [`DefaultServiceHandler`] to execute [SPARQL 1.1 Federated Query](https://www.w3.org/TR/sparql11-federated-query/) SERVICE calls if no explicit service handler is defined for the service.
///
/// See [`DefaultServiceHandler`] for an example.
#[inline]
#[must_use]
pub fn with_default_service_handler(
mut self,
handler: impl DefaultServiceHandler + 'static,
) -> Self {
self.service_handler = self.service_handler.with_default_handler(handler);
self
}
#[inline]
#[must_use]
pub fn has_default_service_handler(&self) -> bool {
self.service_handler.has_default_handler()
}
/// Adds a custom SPARQL evaluation function.
///
/// Example with a function serializing terms to N-Triples:
/// ```
/// use oxrdf::{Dataset, Literal, NamedNode};
/// use spareval::{QueryEvaluator, QueryResults};
/// use spargebra::Query;
///
/// let evaluator = QueryEvaluator::new().with_custom_function(
/// NamedNode::new("http://www.w3.org/ns/formats/N-Triples")?,
/// |args| args.get(0).map(|t| Literal::from(t.to_string()).into()),
/// );
/// let query = Query::parse(
/// "SELECT (<http://www.w3.org/ns/formats/N-Triples>(1) AS ?nt) WHERE {}",
/// None,
/// )?;
/// if let QueryResults::Solutions(mut solutions) = evaluator.execute(Dataset::new(), &query)? {
/// assert_eq!(
/// solutions.next().unwrap()?.get("nt"),
/// Some(&Literal::from("\"1\"^^<http://www.w3.org/2001/XMLSchema#integer>").into())
/// );
/// }
/// # Result::<_, Box<dyn std::error::Error>>::Ok(())
/// ```
#[inline]
#[must_use]
pub fn with_custom_function(
mut self,
name: NamedNode,
evaluator: impl Fn(&[Term]) -> Option<Term> + Send + Sync + 'static,
) -> Self {
self.custom_functions.insert(name, Arc::new(evaluator));
self
}
/// Disables query optimizations and runs the query as it is.
#[inline]
#[must_use]
pub fn without_optimizations(mut self) -> Self {
self.without_optimizations = true;
self
}
/// Compute statistics during evaluation and fills them in the explanation tree.
#[inline]
#[must_use]
pub fn compute_statistics(mut self) -> Self {
self.run_stats = true;
self
}
}
pub(crate) type CustomFunctionRegistry =
HashMap<NamedNode, Arc<dyn (Fn(&[Term]) -> Option<Term>) + Send + Sync>>;
/// The explanation of a query.
#[derive(Clone)]
pub struct QueryExplanation {
inner: Rc<EvalNodeWithStats>,
with_stats: bool,
planning_duration: Option<DayTimeDuration>,
}
impl QueryExplanation {
/// Writes the explanation as JSON.
pub fn write_in_json(&self, writer: impl io::Write) -> io::Result<()> {
let mut writer = ToWriteJsonWriter::new(writer);
writer.write_event(JsonEvent::StartObject)?;
if let Some(planning_duration) = self.planning_duration {
writer.write_event(JsonEvent::ObjectKey("planning duration in seconds".into()))?;
writer.write_event(JsonEvent::Number(
planning_duration.as_seconds().to_string().into(),
))?;
}
writer.write_event(JsonEvent::ObjectKey("plan".into()))?;
self.inner.json_node(&mut writer, self.with_stats)?;
writer.write_event(JsonEvent::EndObject)
}
}
impl fmt::Debug for QueryExplanation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut obj = f.debug_struct("QueryExplanation");
if let Some(planning_duration) = self.planning_duration {
obj.field(
"planning duration in seconds",
&f32::from(Float::from(planning_duration.as_seconds())),
);
}
obj.field("tree", &self.inner);
obj.finish_non_exhaustive()
}
}