Skip to content

Commit a01fab9

Browse files
GH-4920 Make the logic that distinguises between counting from the remote default graph, or a dataset clearer
1 parent 9650d55 commit a01fab9

1 file changed

Lines changed: 17 additions & 10 deletions

File tree

core/repository/sparql/src/main/java/org/eclipse/rdf4j/repository/sparql/SPARQLConnection.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -313,33 +313,40 @@ public long size(Resource... contexts) throws RepositoryException {
313313
}
314314

315315
String sizeAsTupleQuery(Resource... contexts) {
316-
String query = COUNT_EVERYTHING;
317-
if (contexts != null && isQuadMode()) {
318-
if (contexts.length == 1 && isExposableGraphIri(contexts[0])) { // in case the context is null we want the
319-
// default graph.
320-
query = "SELECT (COUNT(*) AS ?count) WHERE { GRAPH <" + contexts[0].stringValue()
316+
317+
// in case the context is null we want the
318+
// default graph of the remote store i.e. ask without graph/from.
319+
if (contexts != null && isQuadMode() && contexts.length > 0) {
320+
// this is an optimization for the case that we can use a GRAPH instead of a FROM.
321+
if (contexts.length == 1 && isExposableGraphIri(contexts[0])) {
322+
return "SELECT (COUNT(*) AS ?count) WHERE { GRAPH <" + contexts[0].stringValue()
321323
+ "> { ?s ?p ?o}}";
322-
} else if (contexts.length > 0) {
324+
} else {
325+
// If we had an default graph setting that is sesame/rdf4j specific
326+
// we must drop it before sending it over the wire. Otherwise
327+
// gather up the given contexts and send them as a from clauses
328+
// to make the matching dataset.
323329
String graphs = Arrays.stream(contexts)
324330
.filter(SPARQLConnection::isExposableGraphIri)
325331
.map(Resource::stringValue)
326332
.map(s -> "FROM <" + s + ">")
327333
.collect(Collectors.joining(" "));
328-
query = "SELECT (COUNT(*) AS ?count) " + graphs + "WHERE { ?s ?p ?o}";
334+
return "SELECT (COUNT(*) AS ?count) " + graphs + "WHERE { ?s ?p ?o}";
329335
}
336+
} else {
337+
return COUNT_EVERYTHING;
330338
}
331-
return query;
332339
}
333340

334341
/**
335-
* For the sparql protocol a context must be an IRI However we can't send out the RDF4j intenral default graph IRIs
342+
* For the sparql protocol a context must be an IRI However we can't send out the RDF4j internal default graph IRIs
336343
*
337344
* @param resource to test if it can be the IRI for a named graph
338345
* @return true if it the input can be a foreign named graph.
339346
*/
340347
private static boolean isExposableGraphIri(Resource resource) {
341348
// We use the instanceof test to avoid any issue with a null pointer.
342-
return resource instanceof IRI && RDF4J.NIL != resource && SESAME.NIL != resource;
349+
return resource instanceof IRI && !RDF4J.NIL.equals(resource) && !SESAME.NIL.equals(resource);
343350
}
344351

345352
@Override

0 commit comments

Comments
 (0)