Skip to content

Commit

Permalink
Only apply optimization for transitive path
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTF committed Feb 26, 2025
1 parent 7c2edf7 commit 8ad0d88
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/engine/QueryPlanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2089,6 +2089,19 @@ QueryPlanner::SubtreePlan cloneWithNewTree(
newPlan._qet = std::move(newTree);
return newPlan;
}

// Check if a transitive path is somewhere in the tree. This is because the
// optimization with `Union` currently only makes sense if the transitive path
// is in the tree.
bool hasTransitivePathInTree(const Operation& operation) {
if (dynamic_cast<const TransitivePathBase*>(&operation)) {
return true;
}
return ql::ranges::any_of(
operation.getChildren(), [](const QueryExecutionTree* child) {
return hasTransitivePathInTree(*child->getRootOperation());
});
}
} // namespace

// _____________________________________________________________________________________________________________________
Expand All @@ -2105,7 +2118,7 @@ auto QueryPlanner::applyJoinDistributivelyToUnion(const SubtreePlan& a,
bool flipped) {
auto unionOperation =
std::dynamic_pointer_cast<Union>(thisPlan._qet->getRootOperation());
if (!unionOperation) {
if (!unionOperation || !hasTransitivePathInTree(*unionOperation)) {
return;
}

Expand Down
25 changes: 25 additions & 0 deletions test/QueryPlannerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3048,6 +3048,31 @@ TEST(QueryPlanner, testDistributiveJoinInUnion) {
qec, {4, 16, 64'000'000});
}

// _____________________________________________________________________________
TEST(QueryPlanner, ensurePlanningIsSkippedWhenNoTransitivePathIsPresent) {
auto qp = makeQueryPlanner();
{
auto query = SparqlParser::parseQuery(
"SELECT * WHERE { ?x <P31> ?o ."
"{ VALUES ?x { 1 } } UNION { VALUES ?x { 1 } }}");
auto plans = qp.createExecutionTrees(query);
ASSERT_EQ(plans.size(), 1);
EXPECT_TRUE(
std::dynamic_pointer_cast<Join>(plans.at(0)._qet->getRootOperation()));
}
{
auto query = SparqlParser::parseQuery(
"SELECT * WHERE { ?x <P31> ?o . "
"{ { VALUES ?x { 1 } } UNION { VALUES ?x { 1 } } } "
"UNION "
"{ { VALUES ?x { 1 } } UNION { VALUES ?x { 1 } } } }");
auto plans = qp.createExecutionTrees(query);
ASSERT_EQ(plans.size(), 1);
EXPECT_TRUE(
std::dynamic_pointer_cast<Join>(plans.at(0)._qet->getRootOperation()));
}
}

// _____________________________________________________________________________
TEST(QueryPlanner, testDistributiveJoinInUnionRecursive) {
auto* qec = ad_utility::testing::getQec(
Expand Down

0 comments on commit 8ad0d88

Please sign in to comment.