Skip to content

Commit 1d47c5a

Browse files
authored
GH-4859 Add instead of remove QueryRoot for explaining optimized Sail queries (#4862)
2 parents fb2cab4 + cc3782c commit 1d47c5a

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

core/repository/sail/src/main/java/org/eclipse/rdf4j/repository/sail/SailQuery.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ public Explanation explain(Explanation.Level level) {
4343

4444
TupleExpr tupleExpr = getParsedQuery().getTupleExpr();
4545

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

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

5454
}

core/repository/sail/src/test/java/org/eclipse/rdf4j/repository/sail/SailRepositoryConnectionTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
*******************************************************************************/
1111
package org.eclipse.rdf4j.repository.sail;
1212

13+
import static org.assertj.core.api.Assertions.assertThat;
1314
import static org.mockito.ArgumentMatchers.any;
1415
import static org.mockito.ArgumentMatchers.anyBoolean;
16+
import static org.mockito.ArgumentMatchers.anyInt;
1517
import static org.mockito.ArgumentMatchers.eq;
1618
import static org.mockito.Mockito.mock;
1719
import static org.mockito.Mockito.verify;
@@ -24,7 +26,9 @@
2426
import org.eclipse.rdf4j.query.GraphQuery;
2527
import org.eclipse.rdf4j.query.Query;
2628
import org.eclipse.rdf4j.query.TupleQuery;
29+
import org.eclipse.rdf4j.query.algebra.QueryRoot;
2730
import org.eclipse.rdf4j.query.algebra.TupleExpr;
31+
import org.eclipse.rdf4j.query.explanation.Explanation;
2832
import org.eclipse.rdf4j.sail.SailConnection;
2933
import org.junit.jupiter.api.BeforeEach;
3034
import org.junit.jupiter.api.Test;
@@ -148,4 +152,21 @@ public void testPrepareBooleanQuery_bypassed() throws Exception {
148152
verify(sailConnection).evaluate(eq(expr), any(), any(), anyBoolean());
149153
}
150154

155+
@Test
156+
public void testExplainQuery() {
157+
TupleExpr expr = mock(TupleExpr.class);
158+
when(expr.clone()).thenReturn(expr);
159+
Explanation explanation = mock(Explanation.class);
160+
161+
when(sailConnection.prepareQuery(any(), any(), any(), any())).thenReturn(Optional.of(expr));
162+
when(sailConnection.explain(any(), any(TupleExpr.class), any(), any(), anyBoolean(), anyInt()))
163+
.thenReturn(explanation);
164+
165+
TupleQuery query = subject.prepareTupleQuery("SELECT * WHERE { ?s ?p ?o }");
166+
assertThat(query.explain(Explanation.Level.Unoptimized)).isEqualTo(explanation);
167+
168+
verify(sailConnection).explain(eq(Explanation.Level.Unoptimized), any(QueryRoot.class), any(), any(),
169+
anyBoolean(), anyInt());
170+
}
171+
151172
}

core/sail/memory/src/test/java/org/eclipse/rdf4j/sail/memory/QueryPlanRetrievalTest.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -919,15 +919,10 @@ public void testDotTimed() {
919919
@Test
920920
public void testWildcard() {
921921

922-
String expected = "Projection\n" +
923-
"╠══ ProjectionElemList\n" +
924-
"║ ProjectionElem \"a\"\n" +
925-
"║ ProjectionElem \"b\"\n" +
926-
"║ ProjectionElem \"c\"\n" +
927-
"╚══ StatementPattern (resultSizeEstimate=12)\n" +
928-
" s: Var (name=a)\n" +
929-
" p: Var (name=b)\n" +
930-
" o: Var (name=c)\n";
922+
String expected = "StatementPattern (resultSizeEstimate=12)\n" +
923+
" s: Var (name=a)\n" +
924+
" p: Var (name=b)\n" +
925+
" o: Var (name=c)\n";
931926
SailRepository sailRepository = new SailRepository(new MemoryStore());
932927
addData(sailRepository);
933928

0 commit comments

Comments
 (0)