-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
GH-2187 keep parent reference in querymodelnode consistent
- Loading branch information
Showing
22 changed files
with
570 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...src/main/java/org/eclipse/rdf4j/query/algebra/evaluation/impl/ParentReferenceCleaner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Eclipse RDF4J contributors. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Distribution License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
*******************************************************************************/ | ||
package org.eclipse.rdf4j.query.algebra.evaluation.impl; | ||
|
||
import java.util.ArrayDeque; | ||
|
||
import org.eclipse.rdf4j.query.BindingSet; | ||
import org.eclipse.rdf4j.query.Dataset; | ||
import org.eclipse.rdf4j.query.algebra.QueryModelNode; | ||
import org.eclipse.rdf4j.query.algebra.TupleExpr; | ||
import org.eclipse.rdf4j.query.algebra.evaluation.QueryOptimizer; | ||
import org.eclipse.rdf4j.query.algebra.helpers.AbstractQueryModelVisitor; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Cleans up {@link QueryModelNode#getParentNode()} references that have become inconsistent with the actual algebra | ||
* tree structure due to optimization operations. Typically used at the very end of the optimization pipeline. | ||
* | ||
* @author Jeen Broekstra | ||
*/ | ||
public class ParentReferenceCleaner implements QueryOptimizer { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ParentReferenceCleaner.class); | ||
|
||
@Override | ||
public void optimize(TupleExpr tupleExpr, Dataset dataset, BindingSet bindings) { | ||
tupleExpr.visit(new ParentFixingVisitor()); | ||
} | ||
|
||
private class ParentFixingVisitor extends AbstractQueryModelVisitor<RuntimeException> { | ||
|
||
private final ArrayDeque<QueryModelNode> ancestors = new ArrayDeque<>(); | ||
|
||
@Override | ||
protected void meetNode(QueryModelNode node) throws RuntimeException { | ||
QueryModelNode expectedParent = ancestors.peekLast(); | ||
if (node.getParentNode() != expectedParent) { | ||
logger.debug("unexpected parent for node {}: {} (expected {})", node, node.getParentNode(), | ||
expectedParent); | ||
node.setParentNode(expectedParent); | ||
} | ||
|
||
ancestors.addLast(node); | ||
super.meetNode(node); | ||
ancestors.pollLast(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...aluation/src/test/java/org/eclipse/rdf4j/query/algebra/evaluation/QueryOptimizerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Eclipse RDF4J contributors. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Distribution License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
*******************************************************************************/ | ||
package org.eclipse.rdf4j.query.algebra.evaluation; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.ArrayDeque; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.eclipse.rdf4j.query.QueryLanguage; | ||
import org.eclipse.rdf4j.query.algebra.QueryModelNode; | ||
import org.eclipse.rdf4j.query.algebra.TupleExpr; | ||
import org.eclipse.rdf4j.query.algebra.helpers.AbstractQueryModelVisitor; | ||
import org.eclipse.rdf4j.query.impl.EmptyBindingSet; | ||
import org.eclipse.rdf4j.query.parser.ParsedTupleQuery; | ||
import org.eclipse.rdf4j.query.parser.QueryParserUtil; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public abstract class QueryOptimizerTest { | ||
|
||
@Test | ||
public void testParentReferencesConsistent_pathExpressions() { | ||
ParsedTupleQuery query = QueryParserUtil.parseTupleQuery(QueryLanguage.SPARQL, | ||
"select * where {?a ?b ?c. ?c <http://a>* ?d}", null); | ||
|
||
TupleExpr expr = query.getTupleExpr(); | ||
|
||
getOptimizer().optimize(expr, null, EmptyBindingSet.getInstance()); | ||
|
||
ParentCheckingVisitor checker = new ParentCheckingVisitor(); | ||
expr.visit(checker); | ||
|
||
assertThat(checker.getInconsistentNodes()).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void testParentReferencesConsistent_filter() { | ||
ParsedTupleQuery query = QueryParserUtil.parseTupleQuery(QueryLanguage.SPARQL, | ||
"select * where {?a ?b ?c. FILTER(?c = <urn:foo>) }", null); | ||
|
||
TupleExpr expr = query.getTupleExpr(); | ||
|
||
getOptimizer().optimize(expr, null, EmptyBindingSet.getInstance()); | ||
|
||
ParentCheckingVisitor checker = new ParentCheckingVisitor(); | ||
expr.visit(checker); | ||
|
||
assertThat(checker.getInconsistentNodes()).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void testParentReferencesConsistent_subselect() { | ||
ParsedTupleQuery query = QueryParserUtil.parseTupleQuery(QueryLanguage.SPARQL, | ||
"select * where {?a ?b ?c. { select ?a ?z where { ?a a ?z } }}", null); | ||
|
||
TupleExpr expr = query.getTupleExpr(); | ||
|
||
getOptimizer().optimize(expr, null, EmptyBindingSet.getInstance()); | ||
|
||
ParentCheckingVisitor checker = new ParentCheckingVisitor(); | ||
expr.visit(checker); | ||
|
||
assertThat(checker.getInconsistentNodes()).isEmpty(); | ||
} | ||
|
||
public abstract QueryOptimizer getOptimizer(); | ||
|
||
private class ParentCheckingVisitor extends AbstractQueryModelVisitor<RuntimeException> { | ||
private final ArrayDeque<QueryModelNode> ancestors = new ArrayDeque<>(); | ||
private List<QueryModelNode> inconsistentNodes = new ArrayList<>(); | ||
|
||
@Override | ||
protected void meetNode(QueryModelNode node) throws RuntimeException { | ||
QueryModelNode expectedParent = ancestors.peekLast(); | ||
if (node.getParentNode() != expectedParent) { | ||
inconsistentNodes.add(node); | ||
} | ||
|
||
ancestors.addLast(node); | ||
super.meetNode(node); | ||
ancestors.pollLast(); | ||
} | ||
|
||
public List<QueryModelNode> getInconsistentNodes() { | ||
return inconsistentNodes; | ||
} | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
...on/src/test/java/org/eclipse/rdf4j/query/algebra/evaluation/impl/BindingAssignerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Eclipse RDF4J contributors. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Distribution License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
*******************************************************************************/ | ||
package org.eclipse.rdf4j.query.algebra.evaluation.impl; | ||
|
||
import org.eclipse.rdf4j.query.algebra.evaluation.QueryOptimizerTest; | ||
|
||
public class BindingAssignerTest extends QueryOptimizerTest { | ||
|
||
@Override | ||
public BindingAssigner getOptimizer() { | ||
return new BindingAssigner(); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...n/src/test/java/org/eclipse/rdf4j/query/algebra/evaluation/impl/CompareOptimizerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Eclipse RDF4J contributors. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Distribution License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
*******************************************************************************/ | ||
package org.eclipse.rdf4j.query.algebra.evaluation.impl; | ||
|
||
import org.eclipse.rdf4j.query.algebra.evaluation.QueryOptimizerTest; | ||
|
||
/** | ||
* @author jeen | ||
* | ||
*/ | ||
public class CompareOptimizerTest extends QueryOptimizerTest { | ||
|
||
@Override | ||
public CompareOptimizer getOptimizer() { | ||
return new CompareOptimizer(); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...va/org/eclipse/rdf4j/query/algebra/evaluation/impl/ConjunctiveConstraintSplitterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Eclipse RDF4J contributors. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Distribution License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
*******************************************************************************/ | ||
package org.eclipse.rdf4j.query.algebra.evaluation.impl; | ||
|
||
import org.eclipse.rdf4j.query.algebra.evaluation.QueryOptimizerTest; | ||
|
||
/** | ||
* @author jeen | ||
* | ||
*/ | ||
public class ConjunctiveConstraintSplitterTest extends QueryOptimizerTest { | ||
|
||
@Override | ||
public ConjunctiveConstraintSplitter getOptimizer() { | ||
return new ConjunctiveConstraintSplitter(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...a/org/eclipse/rdf4j/query/algebra/evaluation/impl/DisjunctiveConstraintOptimizerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Eclipse RDF4J contributors. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Distribution License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
*******************************************************************************/ | ||
package org.eclipse.rdf4j.query.algebra.evaluation.impl; | ||
|
||
import org.eclipse.rdf4j.query.algebra.evaluation.QueryOptimizerTest; | ||
|
||
/** | ||
* @author jeen | ||
* | ||
*/ | ||
public class DisjunctiveConstraintOptimizerTest extends QueryOptimizerTest { | ||
|
||
@Override | ||
public DisjunctiveConstraintOptimizer getOptimizer() { | ||
return new DisjunctiveConstraintOptimizer(); | ||
} | ||
|
||
} |
Oops, something went wrong.