8000 [3.7] Fix crash in optimizer rule remove-collect-variables by goedderz · Pull Request #14826 · arangodb/arangodb · GitHub
[go: up one dir, main page]

Skip to content

[3.7] Fix crash in optimizer rule remove-collect-variables #14826

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 4 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
v3.7.16 (XXXX-XX-XX)
--------------------

* Fix issue #14807: Fix crash during optimization of certain AQL queries during
the remove-collect-variables optimizer rule, when a COLLECT node without
output variables (this includes RETURN DISTINCT) occured in the plan.


v3.7.15 (2021-09-22)
--------------------

Expand Down
20 changes: 5 additions & 15 deletions arangod/Aql/OptimizerRules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,7 @@ void arangodb::aql::removeCollectVariablesRule(Optimizer* opt,
auto collectNode = ExecutionNode::castTo<CollectNode*>(n);
TRI_ASSERT(collectNode != nullptr);

auto const& varsUsedLater = n->getVarsUsedLater();
auto const& varsUsedLater = collectNode->getVarsUsedLater();
auto outVariable = collectNode->outVariable();

if (outVariable != nullptr &&
Expand Down Expand Up @@ -1294,27 +1294,17 @@ void arangodb::aql::removeCollectVariablesRule(Optimizer* opt,
}

} // end - expression exists
} else if (planNode->getType() == EN::COLLECT) {
auto innerCollectNode = ExecutionNode::castTo<CollectNode const*>(planNode);
if (innerCollectNode->hasOutVariable()) {
// We have the following situation:
//
// COLLECT v1 = doc._id INTO g1
// COLLECT v2 = doc._id INTO g2
//
searchVariables.push_back(innerCollectNode->outVariable());
} else {
// when we find another COLLECT, it will invalidate all
// previous variables in the scope
searchVariables.clear();
}
} else {
auto here = planNode->getVariableIdsUsedHere();
TRI_ASSERT(!searchVariables.empty());
if (here.find(searchVariables.back()->id) != here.end()) {
// the outVariable of the last collect should not be used by any following node directly
doOptimize = false;
break;
}
if (planNode->getType() == EN::COLLECT) {
break;
}
}

planNode = planNode->getFirstParent();
Expand Down
36 changes: 36 additions & 0 deletions tests/js/server/aql/aql-optimizer-rule-remove-collect-variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,42 @@ function optimizerRuleTestSuite () {
assertNotEqual(-1, explain.plan.rules.indexOf(ruleName));
},

// Regression test for https://github.com/arangodb/arangodb/issues/14807.
// This resulted in an invalid memory access in the removeCollectVariablesRule.
testIssue14807 : function () {
const query = `
for u in union([],[])
collect test = u.v into g
return Distinct {
Test: test,
Prop1: MIN(g[*].u.someProp),
Prop2: MAX(g[*].u.someProp)
}
`;

const expectedResults = [];

const results = AQL_EXECUTE(query);
assertEqual(expectedResults, results.json);
},

testCollectIntoBeforeCollectWithoutInto : function () {
const query = `
LET items = [{_id: 'ID'}]
FOR item1 IN items
COLLECT unused1 = item1._id INTO first
COLLECT unused2 = first[0].item1._id
RETURN unused2
`;
const expected = [ "ID" ];

let resultEnabled = AQL_EXECUTE(query, { }, paramEnabled).json;
assertEqual(expected, resultEnabled);

let explain = AQL_EXPLAIN(query, { }, paramEnabled);
assertNotEqual(-1, explain.plan.rules.indexOf(ruleName));
},

};
}

Expand Down
0