Skip to content

Commit

Permalink
GH-4920 When sending the remote size query make sure we don't send null,
Browse files Browse the repository at this point in the history
or RDF4J nill
  • Loading branch information
JervenBolleman committed May 11, 2024
1 parent 6bd8fff commit 9650d55
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import org.eclipse.rdf4j.model.impl.DynamicModelFactory;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.model.util.Literals;
import org.eclipse.rdf4j.model.vocabulary.RDF4J;
import org.eclipse.rdf4j.model.vocabulary.SESAME;
import org.eclipse.rdf4j.query.BindingSet;
import org.eclipse.rdf4j.query.BooleanQuery;
import org.eclipse.rdf4j.query.GraphQuery;
Expand Down Expand Up @@ -313,12 +315,13 @@ public long size(Resource... contexts) throws RepositoryException {
String sizeAsTupleQuery(Resource... contexts) {
String query = COUNT_EVERYTHING;
if (contexts != null && isQuadMode()) {
if (contexts.length == 1 && contexts[0].isIRI()) {
query = "SELECT (COUNT(*) AS ?count) WHERE { GRAPH <" + ((IRI) contexts[0]).stringValue()
if (contexts.length == 1 && isExposableGraphIri(contexts[0])) { // in case the context is null we want the
// default graph.
query = "SELECT (COUNT(*) AS ?count) WHERE { GRAPH <" + contexts[0].stringValue()
+ "> { ?s ?p ?o}}";
} else if (contexts.length > 0) {
String graphs = Arrays.stream(contexts)
.filter(Resource::isIRI)
.filter(SPARQLConnection::isExposableGraphIri)
.map(Resource::stringValue)
.map(s -> "FROM <" + s + ">")
.collect(Collectors.joining(" "));
Expand All @@ -328,6 +331,17 @@ String sizeAsTupleQuery(Resource... contexts) {
return query;
}

/**
* For the sparql protocol a context must be an IRI However we can't send out the RDF4j intenral default graph IRIs
*
* @param resource to test if it can be the IRI for a named graph
* @return true if it the input can be a foreign named graph.
*/
private static boolean isExposableGraphIri(Resource resource) {
// We use the instanceof test to avoid any issue with a null pointer.
return resource instanceof IRI && RDF4J.NIL != resource && SESAME.NIL != resource;
}

@Override
public RepositoryResult<Statement> getStatements(Resource subj, IRI pred, Value obj, boolean includeInferred,
Resource... contexts) throws RepositoryException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.rdf4j.model.util.Values.iri;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
Expand All @@ -32,6 +33,7 @@
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.model.vocabulary.FOAF;
import org.eclipse.rdf4j.model.vocabulary.RDF;
import org.eclipse.rdf4j.model.vocabulary.RDF4J;
import org.eclipse.rdf4j.model.vocabulary.RDFS;
import org.eclipse.rdf4j.query.impl.MapBindingSet;
import org.eclipse.rdf4j.query.impl.SimpleBinding;
Expand Down Expand Up @@ -132,6 +134,16 @@ public void testSizeQuery() throws Exception {
sizeAsTupleQuery = subject.sizeAsTupleQuery(vf.createIRI("urn:g1"), vf.createBNode());
query = new SPARQLParserFactory().getParser().parseQuery(sizeAsTupleQuery, "http://example.org/");
assertNotNull(query);

sizeAsTupleQuery = subject.sizeAsTupleQuery(RDF4J.NIL);
query = new SPARQLParserFactory().getParser().parseQuery(sizeAsTupleQuery, "http://example.org/");
assertNotNull(query);
assertFalse(sizeAsTupleQuery.contains("nil"));

sizeAsTupleQuery = subject.sizeAsTupleQuery(null);
query = new SPARQLParserFactory().getParser().parseQuery(sizeAsTupleQuery, "http://example.org/");

assertNotNull(query);
}

@Test
Expand Down

0 comments on commit 9650d55

Please sign in to comment.