Skip to content

Commit

Permalink
GH-4859 Add instead of remove QueryRoot for explaining optimized Sail…
Browse files Browse the repository at this point in the history
… queries (#4862)
  • Loading branch information
hmottestad authored Jan 13, 2024
2 parents fb2cab4 + cc3782c commit 1d47c5a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public Explanation explain(Explanation.Level level) {

TupleExpr tupleExpr = getParsedQuery().getTupleExpr();

// That query has a root does not add to the explanation.
if (tupleExpr instanceof QueryRoot) {
tupleExpr = ((QueryRoot) tupleExpr).getArg();
if (!(tupleExpr instanceof QueryRoot)) {
// Add a dummy root node to the tuple expressions to allow optimizers to modify the actual root node
tupleExpr = new QueryRoot(tupleExpr);
}
SailConnection sailCon = getConnection().getSailConnection();

SailConnection sailCon = getConnection().getSailConnection();
return sailCon.explain(level, tupleExpr, getActiveDataset(), getBindings(), getIncludeInferred(), timeout);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
*******************************************************************************/
package org.eclipse.rdf4j.repository.sail;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
Expand All @@ -24,7 +26,9 @@
import org.eclipse.rdf4j.query.GraphQuery;
import org.eclipse.rdf4j.query.Query;
import org.eclipse.rdf4j.query.TupleQuery;
import org.eclipse.rdf4j.query.algebra.QueryRoot;
import org.eclipse.rdf4j.query.algebra.TupleExpr;
import org.eclipse.rdf4j.query.explanation.Explanation;
import org.eclipse.rdf4j.sail.SailConnection;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -148,4 +152,21 @@ public void testPrepareBooleanQuery_bypassed() throws Exception {
verify(sailConnection).evaluate(eq(expr), any(), any(), anyBoolean());
}

@Test
public void testExplainQuery() {
TupleExpr expr = mock(TupleExpr.class);
when(expr.clone()).thenReturn(expr);
Explanation explanation = mock(Explanation.class);

when(sailConnection.prepareQuery(any(), any(), any(), any())).thenReturn(Optional.of(expr));
when(sailConnection.explain(any(), any(TupleExpr.class), any(), any(), anyBoolean(), anyInt()))
.thenReturn(explanation);

TupleQuery query = subject.prepareTupleQuery("SELECT * WHERE { ?s ?p ?o }");
assertThat(query.explain(Explanation.Level.Unoptimized)).isEqualTo(explanation);

verify(sailConnection).explain(eq(Explanation.Level.Unoptimized), any(QueryRoot.class), any(), any(),
anyBoolean(), anyInt());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -919,15 +919,10 @@ public void testDotTimed() {
@Test
public void testWildcard() {

String expected = "Projection\n" +
"╠══ ProjectionElemList\n" +
"║ ProjectionElem \"a\"\n" +
"║ ProjectionElem \"b\"\n" +
"║ ProjectionElem \"c\"\n" +
"╚══ StatementPattern (resultSizeEstimate=12)\n" +
" s: Var (name=a)\n" +
" p: Var (name=b)\n" +
" o: Var (name=c)\n";
String expected = "StatementPattern (resultSizeEstimate=12)\n" +
" s: Var (name=a)\n" +
" p: Var (name=b)\n" +
" o: Var (name=c)\n";
SailRepository sailRepository = new SailRepository(new MemoryStore());
addData(sailRepository);

Expand Down

0 comments on commit 1d47c5a

Please sign in to comment.