INCATools / ontology-access-kit

Ontology Access Kit: A python library and command line application for working with ontologies
https://incatools.github.io/ontology-access-kit/
Apache License 2.0
126 stars 29 forks source link

Use a SPARQL query builder in all sparql endpoint adapters #318

Open cmungall opened 2 years ago

cmungall commented 2 years ago

Currently we use a mixture of rdflib and sparqlwrapper in all sparql implementations (recall we also use sparql for local rdf files as well).

These provide extremely low-level interfaces and assume string construction of SPARQL queries, and leave it to the caller to avoid bobby tables style injections. Additionally, string manipulation of queries is an antipattern. E.g. this kind of thing:'SELECT {" ".join(select_vars)}' + where_clause

To circumvent this we have various helper classes and a simple SPARQL query object dataclasses, e.g

https://github.com/INCATools/ontology-access-kit/blob/56859dd247becea27cc4f469c28288daea0744f7/src/oaklib/implementations/sparql/sparql_query.py#L14-L23

However, query construction is still fairly low level.

We should use modern query builder patterns here. Options are:

  1. reuse an existing library
  2. generalize the existing helper classes into a separate standalone library that could be used by others

When I last looked into 1, I was disappointed by the current state of python sparql query builders, lots of abandonware. This seems surprising given the maturity of SPARQL. However, we should do another review before deciding on a course of action.

Note that the requirements here are for a query builder library. We have existing template libraries like sparqlfun, but we want to be able to do things like programmatically compose queries.

matentzn commented 2 years ago

@anitacaron cc

We should make sure that we only have one such solution in OAK.. Seems closely related to your work in SSSOM API.

anitacaron commented 2 years ago

I'm using the SparqlQuery class.