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.
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
OPTIONALpatterns when?testNodeis left bound in both cases and you are not doing anything else (and theex:current_value rdfs:value ?value .there is redundant too):