8000 covering index wins late materialization by iurii-i-popov · Pull Request #10673 · arangodb/arangodb · GitHub
[go: up one dir, main page]

Skip to content

covering index wins late materialization #10673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
subquery uses document in not calc nodes
  • Loading branch information
Yuriy Popov committed Dec 12, 2019
commit 37c662a45d633627fd2ad730e71ceb64e0f1e23f
55 changes: 26 additions & 29 deletions arangod/Aql/IResearchViewOptimizerRules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "IResearchViewOptimizerRules.h"

#include "Aql/CalculationNodeVarFinder.h"
#include "Aql/ClusterNodes.h"
#include "Aql/Condition.h"
#include "Aql/ExecutionNode.h"
Expand All @@ -31,7 +32,6 @@
#include "Aql/Function.h"
#include "Aql/IResearchViewNode.h"
#include "Aql/LateMaterializedOptimizerRulesCommon.h"
#include "Aql/NodeFinder.h"
#include "Aql/Optimizer.h"
#include "Aql/OptimizerRule.h"
#include "Aql/Query.h"
Expand Down Expand Up @@ -369,13 +369,10 @@ void lateDocumentMaterializationArangoSearchRule(Optimizer* opt,
}
break;
case ExecutionNode::REMOTE:
// REMOTE node is a blocker - we do not want to make materialization calls across cluster!
// REMOTE node is a blocker - we do not want to make materialization calls across cluster!
// Moreover we pass raw collection pointer - this must not cross process border!
if (sortNode != nullptr) {
// this limit node affects only closest sort, if this sort is invalid
// we need to check other limit node
stopSearch = true;
sortNode = nullptr;
}
break;
default: // make clang happy
Expand All @@ -385,10 +382,8 @@ void lateDocumentMaterializationArangoSearchRule(Optimizer* opt,
::arangodb::containers::HashSet<Variable const*> currentUsedVars;
current->getVariablesUsedHere(currentUsedVars);
if (currentUsedVars.find(&viewNode.outVariable()) != currentUsedVars.end()) {
// Currently only calculation and subquery nodes expected to use a loop variable.
// We successfully replace all references to the loop variable.
// However if some other node types will begin to use the loop variable
// assertion below will be triggered and this rule should be updated.
// currently only calculation nodes expected to use a loop variable with attributes
// we successfully replace all references to the loop variable
auto invalid = true;
switch (type) {
case ExecutionNode::CALCULATION: {
Expand All @@ -405,45 +400,47 @@ void lateDocumentMaterializationArangoSearchRule(Optimizer* opt,
::arangodb::containers::SmallVector<ExecutionNode*>::allocator_type::arena_type sa;
::arangodb::containers::SmallVector<ExecutionNode*> subqueryCalcNodes{sa};
// find calculation nodes in the plan of a subquery
NodeFinder<ExecutionNode::NodeType> finder(ExecutionNode::CALCULATION, subqueryCalcNodes, true);
subquery->walk(finder);
for (auto scn : subqueryCalcNodes) {
TRI_ASSERT(scn->getType() == ExecutionNode::CALCULATION);
currentUsedVars.clear();
scn->getVariablesUsedHere(currentUsedVars);
if (currentUsedVars.find(&viewNode.outVariable()) != currentUsedVars.end()) {
auto calcNode = ExecutionNode::castTo<CalculationNode*>(scn);
if (viewNodeState.canVariablesBeReplaced(calcNode)) {
calcNodes.emplace_back(calcNode);
invalid = false;
} else {
invalid = true;
break;
CalculationNodeVarFinder finder(&viewNode.outVariable(), subqueryCalcNodes);
invalid = subquery->walk(finder);
if (!invalid) { // if the finder did not stop
for (auto scn : subqueryCalcNodes) {
TRI_ASSERT(scn->getType() == ExecutionNode::CALCULATION);
currentUsedVars.clear();
scn->getVariablesUsedHere(currentUsedVars);
if (currentUsedVars.find(&viewNode.outVariable()) != currentUsedVars.end()) {
auto calcNode = ExecutionNode::castTo<CalculationNode*>(scn);
if (viewNodeState.canVariablesBeReplaced(calcNode)) {
calcNodes.emplace_back(calcNode);
invalid = false;
} else {
invalid = true;
break;
}
}
}
}
break;
}
default:
TRI_ASSERT(false);
break;
}
if (invalid) {
if (sortNode != nullptr) {
// we have a doc body used before selected SortNode. Forget it, let`s look for better sort to use
sortNode = nullptr;
// this limit node affects only closest sort, if this sort is invalid
// we need to check other limit node
// we have a doc body used before selected SortNode
// forget it, let`s look for better sort to use
stopSearch = true;
} else {
// we are between limit and sort nodes.
// we are between limit and sort nodes
// late materialization could still be applied but we must insert MATERIALIZE node after sort not after limit
stickToSortNode = true;
}
}
}
}
if (stopSearch) {
// this limit node affects only closest sort if this sort is invalid
// we need to check other limit node
sortNode = nullptr;
break;
}
current = current->getFirstDependency(); // inspect next node
Expand Down
73 changes: 38 additions & 35 deletions arangod/Aql/IndexNodeOptimizerRules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
////////////////////////////////////////////////////////////////////////////////

#include "Aql/Ast.h"
#include "Aql/CalculationNodeVarFinder.h"
#include "Aql/Collection.h"
#include "Aql/Condition.h"
#include "Aql/Expression.h"< 8000 /td>
#include "Aql/IndexNode.h"
#include "Aql/LateMaterializedOptimizerRulesCommon.h"
#include "Aql/NodeFinder.h"
#include "Aql/Optimizer.h"
#include "IndexNodeOptimizerRules.h"
#include "Basics/AttributeNameParser.h"
Expand Down Expand Up @@ -73,26 +73,17 @@ namespace {

void processCalculationNode(IndexNode const* indexNode, CalculationNode* calculationNode,
std::vector<latematerialized::NodeWithAttrs>& nodesToChange,
bool NoSortNode, TRI_idx_iid_t& commonIndexId, bool& stickToSortNode, bool& stopSearch) {
TRI_idx_iid_t& commonIndexId, bool& invalid) {
auto astNode = calculationNode->expression()->nodeForModification();
latematerialized::NodeWithAttrs node;
node.node = calculationNode;
// find attributes referenced to index node out variable
if (!latematerialized::getReferencedAttributes(astNode, indexNode->outVariable(), node)) {
// is not safe for optimization
stopSearch = true;
invalid = true;
} else if (!node.attrs.empty()) {
if (!attributesMatch(commonIndexId, indexNode, node)) {
// the node uses attributes which is not in index
if (NoSortNode) {
// we are between limit and sort nodes.
// late materialization could still be applied but we must insert MATERIALIZE node after sort not after limit
stickToSortNode = true;
} else {
// this limit node affects only closest sort, if this sort is invalid
// we need to check other limit node
stopSearch = true;
}
invalid = true;
} else {
nodesToChange.emplace_back(std::move(node));
}
Expand Down Expand Up @@ -136,6 +127,7 @@ void arangodb::aql::lateDocumentMaterializationRule(Optimizer* opt,
std::vector<latematerialized::NodeWithAttrs> nodesToChange;
TRI_idx_iid_t commonIndexId = 0; // use one index only
while (current != loop) {
auto invalid = false;
auto type = current->getType();
switch (type) {
case ExecutionNode::SORT:
Expand All @@ -145,7 +137,7 @@ void arangodb::aql::lateDocumentMaterializationRule(Optimizer* opt,
break;
case ExecutionNode::CALCULATION:
processCalculationNode(indexNode, ExecutionNode::castTo<CalculationNode*>(current), nodesToChange,
nullptr == sortNode, commonIndexId, stickToSortNode, stopSearch);
commonIndexId, invalid);
break;
case ExecutionNode::REMOTE:
// REMOTE node is a blocker - we do not want to make materialization calls across cluster!
Expand All @@ -156,47 +148,58 @@ void arangodb::aql::lateDocumentMaterializationRule(Optimizer* opt,
default: // make clang happy
break;
}
// Currently only calculation and subquery nodes expected to use a loop variable.
// We successfully replaced all references to the loop variable.
// However if some other node types will begin to use the loop variable
// assertion below will be triggered and this rule should be updated.
if (!stopSearch && type != ExecutionNode::CALCULATION) {
// currently only calculation nodes expected to use a loop variable with attributes
// we successfully replace all references to the loop variable
if (!(stopSearch || invalid) && type != ExecutionNode::CALCULATION) {
arangodb::containers::HashSet<Variable const*> currentUsedVars;
current->getVariablesUsedHere(currentUsedVars);
if (currentUsedVars.find(indexNode->outVariable()) != currentUsedVars.end()) {
invalid = true;
if (ExecutionNode::SUBQUERY == type) {
auto subqueryNode = ExecutionNode::castTo<SubqueryNode*>(current);
auto subquery = subqueryNode->getSubquery();
arangodb::containers::SmallVector<ExecutionNode*>::allocator_type::arena_type sa;
arangodb::containers::SmallVector<ExecutionNode*> subqueryCalcNodes{sa};
// find calculation nodes in the plan of a subquery
NodeFinder<ExecutionNode::NodeType> finder(ExecutionNode::CALCULATION, subqueryCalcNodes, true);
subquery->walk(finder);
for (auto scn : subqueryCalcNodes) {
TRI_ASSERT(scn->getType() == ExecutionNode::CALCULATION);
currentUsedVars.clear();
scn->getVariablesUsedHere(currentUsedVars);
if (currentUsedVars.find(indexNode->outVariable()) != currentUsedVars.end()) {
processCalculationNode(indexNode, ExecutionNode::castTo<CalculationNode*>(scn), nodesToChange,
nullptr == sortNode, commonIndexId, stickToSortNode, stopSearch);
if (stopSearch) {
break;
CalculationNodeVarFinder finder(indexNode->outVariable(), subqueryCalcNodes);
6D40 invalid = subquery->walk(finder);
if (!invalid) { // if the finder did not stop
for (auto scn : subqueryCalcNodes) {
TRI_ASSERT(scn->getType() == ExecutionNode::CALCULATION);
currentUsedVars.clear();
scn->getVariablesUsedHere(currentUsedVars);
if (currentUsedVars.find(indexNode->outVariable()) != currentUsedVars.end()) {
processCalculationNode(indexNode, ExecutionNode::castTo<CalculationNode*>(scn), nodesToChange,
commonIndexId, invalid);
if (invalid) {
break;
}
}
}
}
} else {
TRI_ASSERT(false);
stopSearch = true;
}
}
}
if (invalid) {
TRI_ASSERT(!stopSearch);
if (sortNode != nullptr) {
// we have a doc body used before selected SortNode
// forget it, let`s look for better sort to use
stopSearch = true;
} else {
// we are between limit and sort nodes
// late materialization could still be applied but we must insert MATERIALIZE node after sort not after limit
stickToSortNode = true;
}
}
if (stopSearch) {
// we have a doc body used before selected SortNode. Forget it, let`s look for better sort to use
// this limit node affects only closest sort if this sort is invalid
// we need to check other limit node
sortNode = nullptr;
nodesToChange.clear();
break;
}
current = current->getFirstDependency(); // inspect next node
current = current->getFirstDependency(); // inspect next node
}
if (sortNode && !nodesToChange.empty()) {
auto ast = plan->getAst();
Expand Down
1 change: 1 addition & 0 deletions arangod/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ set(LIB_ARANGO_AQL_SOURCES
Aql/MultiDependencySingleRowFetcher.cpp
Aql/NoResultsExecutor.cpp
Aql/NodeFinder.cpp
Aql/CalculationNodeVarFinder.cpp
Aql/Optimizer.cpp
Aql/OptimizerRules.cpp
Aql/OptimizerRulesCluster.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ function lateDocumentMaterializationArangoSearch2RuleTestSuite () {
let plan = AQL_EXPLAIN(query).plan;
assertEqual(-1, plan.rules.indexOf(ruleName));
},
testNotAppliedDueToUpdateDoc() {
let query = "FOR d IN " + svn + " SEARCH d.value IN [1, 2, 11, 12] UPDATE d IN " + cn + " SORT d.foo DESC LIMIT 10 RETURN d";
let plan = AQL_EXPLAIN(query).plan;
assertEqual(-1, plan.rules.indexOf(ruleName));
},
testNotAppliedDueToSubqueryWithDocumentAccess() {
let query = "FOR d IN " + svn + " SEARCH d.value IN [1, 2, 11, 12] " +
"LET a = NOOPT(d.foo) " +
Expand All @@ -116,6 +121,14 @@ function lateDocumentMaterializationArangoSearch2RuleTestSuite () {
let plan = AQL_EXPLAIN(query).plan;
assertEqual(-1, plan.rules.indexOf(ruleName));
},
testNotAppliedDueToSubqueryWithReturnDocument() {
let query = "FOR d IN " + svn + " SEARCH d.value IN [1, 2, 11, 12] " +
"LET a = NOOPT(d.foo) " +
"LET e = SUM(FOR c IN " + vn + " LET p = NOOPT(CONCAT(d.foo, c.foo)) RETURN d) " +
"SORT CONCAT(a, e) LIMIT 10 RETURN d";
let plan = AQL_EXPLAIN(query).plan;
assertEqual(-1, plan.rules.indexOf(ruleName));
},
testQueryResultsWithSubqueryWithDocumentAccessByAttribute() {
let query = "FOR d IN " + svn + " SEARCH d.value IN [1, 2, 11, 12] " +
"LET a = NOOPT(d.foo) " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@ function lateDocumentMaterializationRuleTestSuite () {
assertEqual(-1, plan.rules.indexOf(ruleName));
}
},
testNotAppliedDueToUpdateDoc() {
for (i = 0; i < numOfCollectionIndexes; ++i) {
let query = "FOR d IN " + collectionNames[i] + " FILTER d.obj.a == 'a_val' UPDATE d IN " + collectionNames[i] + " SORT d.obj.c LIMIT 10 RETURN d";
let plan = AQL_EXPLAIN(query).plan;
assertEqual(-1, plan.rules.indexOf(ruleName));
}
},
testNotAppliedDueToExpansion() {
for (i = 0; i < numOfCollectionIndexes; ++i) {
for (j = 0; j < numOfExpCollections; ++j) {
Expand Down Expand Up @@ -227,6 +234,16 @@ function lateDocumentMaterializationRuleTestSuite () {
assertEqual(-1, plan.rules.indexOf(ruleName));
}
},
testNotAppliedDueToSubqueryWithReturnDocument() {
for (i = 0; i < numOfCollectionIndexes; ++i) {
let query = "FOR d IN " + collectionNames[i] + " FILTER d.obj.a == 'a_val' " +
"LET a = NOOPT(d.obj.b) " +
"LET e = SUM(FOR c IN " + collectionNames[(i + 1) % numOfCollectionIndexes] + " LET p = NOOPT(CONCAT(d.obj.a, c.obj.a)) RETURN d) " +
"SORT CONCAT(a, e) LIMIT 10 RETURN d";
let plan = AQL_EXPLAIN(query).plan;
assertEqual(-1, plan.rules.indexOf(ruleName));
}
},
testQueryResultsWithSubqueryWithDocumentAccessByAttribute() {
for (i = 0; i < numOfCollectionIndexes; ++i) {
let query = "FOR d IN " + collectionNames[i] + " FILTER d.obj.a == 'a_val' " +
Expand Down
0