Skip to content

Instantly share code, notes, and snippets.

View klinovp's full-sized avatar

Pavel Klinov klinovp

  • Complexible
  • Heidelberg, Germany
View GitHub Profile
@klinovp
klinovp / sparql-var-replace.md
Created February 10, 2022 09:04
Variable replacement for query optimisation is tricky

One query example:

select * {
  ?s1 <urn:p> ?o1
  optional { ?s1 <urn:q> ?t1 }
  ?s2 <urn:r> ?o2
  optional { 
    ?s2 <urn:s> ?t2
    filter(?t1 = ?t2)
 }
@klinovp
klinovp / rdf-list-from-data.md
Last active October 7, 2021 07:34
Generate an RDF list from simple data

This query generates an RDF list from an RDF graph that is a linear path. To set up initial data:

stardog query test "insert data { :a :p :b . :b :p :c . :c :p :d }"

The list generation query:

construct {
  ?start rdf:first ?s ;
 rdf:rest ?end .
@klinovp
klinovp / sparql-pivot-stardog.md
Last active September 22, 2021 11:11
Example of SPARQL pivot in Stardog

This shows how one can pivot a SELECT result set in SPARQL to have columns which correspond to row values. The topic is hot in the SQL world so perhaps one day people will start wondering about it in SPARQL.

This query pivots the (type, number_of_instances) relation generated by the inner subquery. It uses Stardog-specific functions (:set and :index) but it's probably not too hard to adapt group_concat (just a little clumsier, left as an exercise for the reader :)

prefix : <tag:stardog:api:>
[ {
"@id" : {
"@id" : "http://api.stardog.com/Alice",
"http://api.stardog.com/supervises" : {
"@id" : "http://api.stardog.com/Bob"
}
},
"http://api.stardog.com/endedAt" : [ {
"@type" : "http://www.w3.org/2001/XMLSchema#integer",
"@value" : "2015"