SPARQL
Query application tables with RDF vocabulary through the common GraphEngine.
Choose the data model
For a property graph, define an OntologyMapping that maps class and predicate IRIs to graph labels and properties. Use it with GraphEngine::sparql. The older MappedGraphEngine::sparql method remains available on the compatibility facade.
For explicit column, identity, and named-graph mappings, add RdfMapping rules to GraphMapping and call GraphEngine::sparql_query. The RDF guide covers ordinary tables, composite foreign keys, and updates. On mapped engines, existing ontology builders translate to these same rules; patterns do not require an explicit type root.
Define a vocabulary
This vocabulary describes the people and social relationships in the mapped tutorial:
use orchiddb::ir::plan::Direction;
use orchiddb::language::sparql::OntologyMapping;
let ontology = OntologyMapping::new()
.class("https://example.com/Person", "Person")
.property("https://example.com/name", "Person", "name")
.property("https://example.com/age", "Person", "age")
.relationship_between(
"https://example.com/follows", "FOLLOWS", Direction::Out,
"Person", "Person",
);
The ontology guide explains identity properties and the builder methods.
Select properties
PREFIX ex: <https://example.com/>
SELECT ?name ?age WHERE {
?person a ex:Person ; ex:name ?name ; ex:age ?age .
FILTER(?age >= 30)
}
ORDER BY ?name
The prefix abbreviates IRIs. a expresses an RDF type. Variables begin with ?, and FILTER applies a condition to the matched bindings.
Run the query through a mapped engine:
let result = graph.sparql(
"PREFIX ex: <https://example.com/> \
SELECT ?name WHERE { ?p a ex:Person ; ex:name ?name . } ORDER BY ?name",
ontology.clone(),
).await?;
result.returned.batch contains Arrow data, and result.returned.fields contains output binding names. The ontology convenience method returns legacy scalar columns. For typed RDF terms, use sparql_query(query, dataset); for Arrow RDF term metadata and execution statistics, use sparql_dataset(query, dataset).
Join relationships
PREFIX ex: <https://example.com/>
SELECT ?name WHERE {
?person a ex:Person ; ex:name "alice" ; ex:follows ?friend .
?friend ex:name ?name .
}
ORDER BY ?name
Shared variables connect patterns. The ontology resolves ex:follows to the mapped FOLLOWS relationship.
Shape a result
Use SELECT DISTINCT to remove repeated projected bindings. Use ORDER BY for result ordering and LIMIT with OFFSET for a result window.
PREFIX ex: <https://example.com/>
SELECT DISTINCT ?name WHERE {
?person a ex:Person ; ex:name ?name .
}
ORDER BY ?name
LIMIT 20
OFFSET 0
Query named graphs
RDF datasets can include a graph column. Use GRAPH to choose a named graph and FROM or FROM NAMED to define the active dataset for a query. See RDF datasets for the source mapping and examples.