SPARQL OPTIONAL with BIND not working in RDFlib

46 Views Asked by At

I tried the following query and found that it works in rdf4j and not in rdflib. After doing some brute force, I found out that the query does not behave as expected only when there is a BIND within the OPTIONAL block. Is there any workaround for this?

PREFIX rdfs:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX ex:<http://example.org#>


CONSTRUCT {
          
    ex:node1 rdfs:value ?testNode .

}
WHERE{
    ex:current_value rdfs:value ?value .
    
    OPTIONAL { 
    ex:current_value rdfs:value ?value .
    FILTER(?value = ex:test1) .
   
    BIND(BNODE() as ?testNode) .

    }
    OPTIONAL {
    ex:current_value rdfs:value ?value .
    FILTER(?value != ex:test1) .
    BIND(rdfs:nil as ?testNode) .        
    }
}

So, technically, this query should yield a value with either ?testNode to be rdfs:nil or a blank node. e.g.

http://example.org#node1  http://www.w3.org/1999/02/22-rdf-syntax-ns#value http://www.w3.org/1999/02/22-rdf-syntax-ns#nil

In rdf4j, it behaves as expected (yielding rdfs:nil or a blank node depending on the value of ex:test1). But, in rdflib the OPTIONAL part gets skipped, so I don't get any result.

1

There are 1 best solutions below

0
IS4 On

I cannot explain why this behaves inconsistently as I cannot speak for the implementations, but I am also not surprised. The query is a bit overcomplicated; you don't need the individual OPTIONAL patterns when ?testNode is left bound in both cases and you are not doing anything else (and the ex:current_value rdfs:value ?value . there is redundant too):

PREFIX rdfs:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX ex:<http://example.org#>

CONSTRUCT {
    ex:node1 rdfs:value ?testNode .
}
WHERE{
    ex:current_value rdfs:value ?value .
    BIND((IF(?value = ex:test1), BNODE(), rdfs:nil) as ?testNode) .
}