Skip to content

Commit 9650d55

Browse files
GH-4920 When sending the remote size query make sure we don't send null,
or RDF4J nill
1 parent 6bd8fff commit 9650d55

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import org.eclipse.rdf4j.model.impl.DynamicModelFactory;
4343
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
4444
import org.eclipse.rdf4j.model.util.Literals;
45+
import org.eclipse.rdf4j.model.vocabulary.RDF4J;
46+
import org.eclipse.rdf4j.model.vocabulary.SESAME;
4547
import org.eclipse.rdf4j.query.BindingSet;
4648
import org.eclipse.rdf4j.query.BooleanQuery;
4749
import org.eclipse.rdf4j.query.GraphQuery;
@@ -313,12 +315,13 @@ public long size(Resource... contexts) throws RepositoryException {
313315
String sizeAsTupleQuery(Resource... contexts) {
314316
String query = COUNT_EVERYTHING;
315317
if (contexts != null && isQuadMode()) {
316-
if (contexts.length == 1 && contexts[0].isIRI()) {
317-
query = "SELECT (COUNT(*) AS ?count) WHERE { GRAPH <" + ((IRI) contexts[0]).stringValue()
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()
318321
+ "> { ?s ?p ?o}}";
319322
} else if (contexts.length > 0) {
320323
String graphs = Arrays.stream(contexts)
321-
.filter(Resource::isIRI)
324+
.filter(SPARQLConnection::isExposableGraphIri)
322325
.map(Resource::stringValue)
323326
.map(s -> "FROM <" + s + ">")
324327
.collect(Collectors.joining(" "));
@@ -328,6 +331,17 @@ String sizeAsTupleQuery(Resource... contexts) {
328331
return query;
329332
}
330333

334+
/**
335+
* For the sparql protocol a context must be an IRI However we can't send out the RDF4j intenral default graph IRIs
336+
*
337+
* @param resource to test if it can be the IRI for a named graph
338+
* @return true if it the input can be a foreign named graph.
339+
*/
340+
private static boolean isExposableGraphIri(Resource resource) {
341+
// We use the instanceof test to avoid any issue with a null pointer.
342+
return resource instanceof IRI && RDF4J.NIL != resource && SESAME.NIL != resource;
343+
}
344+
331345
@Override
332346
public RepositoryResult<Statement> getStatements(Resource subj, IRI pred, Value obj, boolean includeInferred,
333347
Resource... contexts) throws RepositoryException {

core/repository/sparql/src/test/java/org/eclipse/rdf4j/repository/sparql/SPARQLConnectionTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import static org.assertj.core.api.Assertions.assertThat;
1414
import static org.eclipse.rdf4j.model.util.Values.iri;
1515
import static org.junit.jupiter.api.Assertions.assertEquals;
16+
import static org.junit.jupiter.api.Assertions.assertFalse;
1617
import static org.junit.jupiter.api.Assertions.assertNotNull;
1718
import static org.mockito.ArgumentMatchers.any;
1819
import static org.mockito.ArgumentMatchers.anyBoolean;
@@ -32,6 +33,7 @@
3233
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
3334
import org.eclipse.rdf4j.model.vocabulary.FOAF;
3435
import org.eclipse.rdf4j.model.vocabulary.RDF;
36+
import org.eclipse.rdf4j.model.vocabulary.RDF4J;
3537
import org.eclipse.rdf4j.model.vocabulary.RDFS;
3638
import org.eclipse.rdf4j.query.impl.MapBindingSet;
3739
import org.eclipse.rdf4j.query.impl.SimpleBinding;
@@ -132,6 +134,16 @@ public void testSizeQuery() throws Exception {
132134
sizeAsTupleQuery = subject.sizeAsTupleQuery(vf.createIRI("urn:g1"), vf.createBNode());
133135
query = new SPARQLParserFactory().getParser().parseQuery(sizeAsTupleQuery, "http://example.org/");
134136
assertNotNull(query);
137+
138+
sizeAsTupleQuery = subject.sizeAsTupleQuery(RDF4J.NIL);
139+
query = new SPARQLParserFactory().getParser().parseQuery(sizeAsTupleQuery, "http://example.org/");
140+
assertNotNull(query);
141+
assertFalse(sizeAsTupleQuery.contains("nil"));
142+
143+
sizeAsTupleQuery = subject.sizeAsTupleQuery(null);
144+
query = new SPARQLParserFactory().getParser().parseQuery(sizeAsTupleQuery, "http://example.org/");
145+
146+
assertNotNull(query);
135147
}
136148

137149
@Test

0 commit comments

Comments
 (0)