0
votes

I would like to know if there is a way to obtain the SQL query starting from a SPARQL query.

For example, I have this SPARQL query:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX ub: <http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#>
SELECT ?X   
WHERE {
    ?X rdf:type ub:GraduateStudent .
    ?X ub:takesCourse <http://www.Department0.University0.edu/GraduateCourse0>
}

I would like to obtain something like this (the triples are stored in a table triples(subject, predicate, object):

SELECT subject
FROM triples
WHERE predicate = "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>"
AND object = "<http://swat.cse.lehigh.edu/onto/univ-bench.owl#GraduateStudent>"
AND subject IN (
    SELECT subject
    FROM triples
    WHERE predicate = "<http://swat.cse.lehigh.edu/onto/univ-bench.owl#takesCourse>"
    AND object = "<http://www.Department0.University0.edu/GraduateCourse0>"
)

or

SELECT subject
FROM triples
WHERE predicate = "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>"
AND object = "<http://swat.cse.lehigh.edu/onto/univ-bench.owl#GraduateStudent>"

INTERSECT

SELECT subject
FROM triples
WHERE predicate = "<http://swat.cse.lehigh.edu/onto/univ-bench.owl#takesCourse>"
AND object = "<http://www.Department0.University0.edu/GraduateCourse0>"

Thank you in advance!

1
that is impossible because this needs a specified SPARQL to SQL mapping - I mean, this all depends on how the data is modelled in terms of SQL tables which is totally open. For a simple SQL table with columns s,p,o it would be indeed trvial at least for BGPs - it's takes more effort for things like optional, etc. - start with the SPARQL algebra and then write the translation - UninformedUser
or just use a OBDA tool instead of reinventing the wheel - UninformedUser
@UninformedUser Once I managed to have the algebra, then what should be the next step? I mean, there is a tool or an algorithm to use? - dariocurr
I'm not aware of a dedicated tool. I know from some tools that they did it under the hood, i.e.e somewhere in their source code. Basically all approaches that want to bring SPARQL to some framework that does only have native SQL support usually rewrite their SPARQL query to SQL, but then it's just somewhere tightly integrated in the code, i.e. not really usable as a dedicated Java package. - UninformedUser
Clearly, traversing the algebra is simple, for Java you could use Apache Jena and traverse the algebra tree quite nicely and just add the SQL part. As I said before, for basic graph patterns this is easy, for each triple pattern you could generate a subquery, and in a post-processing step you could create the join clause for the shared variables. - UninformedUser

1 Answers

0
votes

Apache Jena used to provide SQL storage : see https://github.com/apache/jena/tree/main/jena-sdb on how to get the old code. It translates basic graph patterns using JOIN and OPTIONAL with LEFT JOIN.

It has its own database layout with RDF terms in a dictionary. There was also a simple scheme with strings and a single triples table - that is not efficient in practice. See also the very old "RDB" Jena storage which was in the pre-Apache project.

There are also various papers on the web discussing this topic.