BlueBrain / nexus-forge

Building and Using Knowledge Graphs made easy
https://nexus-forge.readthedocs.io
GNU Lesser General Public License v3.0
38 stars 19 forks source link

Bug when using FILTER statement with IN operator in`forge.sparql` #242

Closed eugeniashurko closed 2 years ago

eugeniashurko commented 2 years ago

There is a bug when using FILTER (?x IN (...)) statement in SPARQL queries executed through forge.

Consider the following query:

SELECT ?trace ?mtype
WHERE {
  ?trace a <https://neuroshapes.org/SingleCellSimulationTrace> ;
           <https://neuroshapes.org/annotation>/<https://neuroshapes.org/hasBody> ?mtype .
  FILTER (?mtype IN  ( <http://uri.interlex.org/base/ilx_0381366>, <http://uri.interlex.org/base/ilx_0381367>)). 
}

When running this query with forge.sparql(query), an error "Bad request" is thrown (usually means an invalid query).

Indeed, when running forge.sparql(query, debug=True), the query that's sent to the Nexus is transformed into an erroneous one:

SELECT ?trace ?mtype
WHERE {
  ?trace a <https://neuroshapes.org/SingleCellSimulationTrace> ;
           <https://neuroshapes.org/annotation>/<https://neuroshapes.org/hasBody> ?mtype .
  FILTER (?mtype :IN  ( <http://uri.interlex.org/base/ilx_0381366>, <http://uri.interlex.org/base/ilx_0381367>)). 
}

(last line, ':' is added to IN)

eugeniashurko commented 2 years ago

My first guess would be: we need to add "in" to the global variable "SPARQL_CLAUSES" in kgforge.core.archetypes.store line 36

MFSY commented 2 years ago

Hi @eugeniashurko ,

If you have a valid SPARQL query (and it looks like you have) then you can set the forge.sparql(...) 'rewrite' argument to False to avoid any change to the provided query.

My first guess would be: we need to add "in" to the global variable "SPARQL_CLAUSES" in kgforge.core.archetypes.store line 36

Sure. The thing is it is difficult to take into account and know about all possible SPARQL clauses. That's why it is recommended to set 'rewrite' to False when having a FULL sparql query or use forge.search(...)

eugeniashurko commented 2 years ago

Ok, I will do that for now, but it's a bit annoying to need to specify all the prefixes and not to use the cool feature of 'light sparql' queries in forge. In my case, I cannot use forge search, as I need the full expressivity of sparql.

It would be great, if we could add "IN" to the list of the known statements in the next forge version.

eugeniashurko commented 2 years ago

Little UPD: rewrite seems to be not in the list of arguments of forge.search (kgforge version 0.7.0)

MFSY commented 2 years ago

Little UPD: rewrite seems to be not in the list of arguments of forge.search

It is part of forge.sparql(...)

eugeniashurko commented 2 years ago

Sorry, I meant forge.sparql (not there for some reason).

I get:

<action> wrapper
<error> TypeError: sparql() got an unexpected keyword argument 'rewrite'
MFSY commented 2 years ago

Not yet released I believe but in master.

eugeniashurko commented 2 years ago

Doesn't seem to be in the master either, but it's ok, I can wait until we merge from some PR or use from another branch. However, it would be really nice if you could also add "IN" while you are updating the sparql query builder.

MFSY commented 2 years ago

I will. See the Querying notebook: https://github.com/BlueBrain/nexus-forge/blob/master/examples/notebooks/getting-started/04%20-%20Querying.ipynb