8000 Feature/explain spliced subqueries by mchacki · Pull Request #10298 · arangodb/arangodb · GitHub
[go: up one dir, main page]

Skip to content

Feature/explain spliced subqueries #10298

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 3 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion arangod/Aql/OptimizerRules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7277,7 +7277,7 @@ void arangodb::aql::spliceSubqueriesRule(Optimizer* opt, std::unique_ptr<Executi
auto sq = ExecutionNode::castTo<SubqueryNode*>(n);

// insert a SubqueryStartNode before the SubqueryNode
SubqueryStartNode* start = new SubqueryStartNode(plan.get(), plan->nextId());
SubqueryStartNode* start = new SubqueryStartNode(plan.get(), plan->nextId(), sq->outVariable());
plan->registerNode(start);
plan->insertBefore(sq, start);

Expand Down
18 changes: 16 additions & 2 deletions arangod/Aql/SubqueryStartExecutionNode.cpp
10000
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ namespace aql {

SubqueryStartNode::SubqueryStartNode(ExecutionPlan* plan,
arangodb::velocypack::Slice const& base)
: ExecutionNode(plan, base) {}
: ExecutionNode(plan, base) {
// On purpose exclude the _subqueryOutVariable
}

CostEstimate SubqueryStartNode::estimateCost() const {
TRI_ASSERT(!_dependencies.empty());
Expand All @@ -53,6 +55,11 @@ CostEstimate SubqueryStartNode::estimateCost() const {
void SubqueryStartNode::toVelocyPackHelper(VPackBuilder& nodes, unsigned flags,
std::unordered_set<ExecutionNode const*>& seen) const {
ExecutionNode::toVelocyPackHelperGeneric(nodes, flags, seen);
// We need this for the Explainer
if (_subqueryOutVariable != nullptr) {
nodes.add(VPackValue("subqueryOutVariable"));
_subqueryOutVariable->toVelocyPack(nodes);
}
nodes.close();
}

Expand All @@ -70,17 +77,20 @@ std::unique_ptr<ExecutionBlock> SubqueryStartNode::createBlock(
getRegisterPlan()->nrRegs[previousNode->getDepth()],
getRegisterPlan()->nrRegs[getDepth()], getRegsToClear(),
calcRegsToKeep());
// On purpose exclude the _subqueryOutVariable
return std::make_unique<ExecutionBlockImpl<SubqueryStartExecutor>>(&engine, this,
std::move(infos));
}

ExecutionNode* SubqueryStartNode::clone(ExecutionPlan* plan, bool withDependencies,
bool withProperties) const {
auto c = std::make_unique<SubqueryStartNode>(plan, _id);
// On purpose exclude the _subqueryOutVariable
auto c = std::make_unique<SubqueryStartNode>(plan, _id, nullptr);
return cloneHelper(std::move(c), withDependencies, withProperties);
}

bool SubqueryStartNode::isEqualTo(ExecutionNode const& other) const {
// On purpose exclude the _subqueryOutVariable
try {
SubqueryStartNode const& p = dynamic_cast<SubqueryStartNode const&>(other);
return ExecutionNode::isEqualTo(p);
Expand All @@ -89,5 +99,9 @@ bool SubqueryStartNode::isEqualTo(ExecutionNode const& other) const {
}
}

Variable const* SubqueryStartNode::subqueryOutVariable() const {
return _subqueryOutVariable;
}

} // namespace aql
} // namespace arangodb
12 changes: 11 additions & 1 deletion arangod/Aql/SubqueryStartExecutionNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@
namespace arangodb {
namespace aql {

struct Variable;

class SubqueryStartNode : public ExecutionNode {
friend class ExecutionNode;
friend class ExecutionBlock;

public:
SubqueryStartNode(ExecutionPlan*, arangodb::velocypack::Slice const& base);
SubqueryStartNode(ExecutionPlan* plan, size_t id) : ExecutionNode(plan, id) {}
SubqueryStartNode(ExecutionPlan* plan, size_t id, Variable const* subqueryOutVariable)
: ExecutionNode(plan, id), _subqueryOutVariable(subqueryOutVariable) {}

CostEstimate estimateCost() const override final;

Expand All @@ -53,6 +56,13 @@ class SubqueryStartNode : public ExecutionNode {
bool withProperties) const override final;

bool isEqualTo(ExecutionNode const& other) const override final;

/// @brief This is only required for Explain output.
/// it has no practical usage other then to print this information during explain.
Variable const* subqueryOutVariable() const;

private:
Variable const* _subqueryOutVariable;
};

} // namespace aql
Expand Down
17 changes: 15 additions & 2 deletions js/common/modules/@arangodb/aql/explainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,11 @@ function processQuery(query, explain, planIndex) {
return variable(node.name);
};

var lateVariableCallback = function () {
require("internal").print("Called");
return (node) => variableName(node);
};

var buildExpression = function (node) {
var binaryOperator = function (node, name) {
var lhs = buildExpression(node.subNodes[0]);
Expand Down Expand Up @@ -1464,6 +1469,10 @@ function processQuery(query, explain, planIndex) {
return keyword('RETURN') + ' ' + variableName(node.inVariable);
case 'SubqueryNode':
return keyword('LET') + ' ' + variableName(node.outVariable) + ' = ... ' + annotation('/* ' + (node.isConst ? 'const ' : '') + 'subquery */');
case 'SubqueryStartNode':
return `${keyword('LET')} ${variableName(node.subqueryOutVariable)} = ( ${annotation(`/* subquery begin */`)}` ;
case 'SubqueryEndNode':
return `) ${annotation(`/* subquery end */`)}`;
case 'InsertNode': {
modificationFlags = node.modificationFlags;
let restrictString = '';
Expand Down Expand Up @@ -1653,7 +1662,7 @@ function processQuery(query, explain, planIndex) {
return 'unhandled node type (' + node.type + ')';
};

var level = 0, subqueries = [];
var level = 0, subqueries = [], subqueryCallbacks = [];
var indent = function (level, isRoot) {
return pad(1 + level + level) + (isRoot ? '* ' : '- ');
};
Expand All @@ -1662,9 +1671,12 @@ function processQuery(query, explain, planIndex) {
usedVariables = {};
currentNode = node.id;
isConst = true;
if (node.type === 'SubqueryNode') {
if (node.type === 'SubqueryNode' || node.type === 'SubqueryStartNode') {
subqueries.push(level);
}
if (node.type === 'SubqueryEndNode' && subqueries.length > 0) {
level = subqueries.pop();
}
};

var postHandle = function (node) {
Expand All @@ -1676,6 +1688,7 @@ function processQuery(query, explain, planIndex) {
'IndexRangeNode',
'IndexNode',
'TraversalNode',
'SubqueryStartNode',
'SubqueryNode'].indexOf(node.type) !== -1) {
level++;
} else if (isLeafNode && subqueries.length > 0) {
Expand Down
6 changes: 3 additions & 3 deletions tests/Aql/ExecutionNodeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ TEST_F(ExecutionNodeTest, start_node_velocypack_roundtrip) {
std::unique_ptr<SubqueryStartNode> node, nodeFromVPack;
std::unordered_set<ExecutionNode const*> seen{};

node = std::make_unique<SubqueryStartNode>(&plan, 0);
node = std::make_unique<SubqueryStartNode>(&plan, 0, nullptr);

builder.openArray();
node->toVelocyPackHelper(builder, ExecutionNode::SERIALIZE_DETAILS, seen);
Expand All @@ -75,8 +75,8 @@ TEST_F(ExecutionNodeTest, start_node_velocypack_roundtrip) {
TEST_F(ExecutionNodeTest, start_node_not_equal_different_id) {
std::unique_ptr<SubqueryStartNode> node1, node2;

node1 = std::make_unique<SubqueryStartNode>(&plan, 0);
node2 = std::make_unique<SubqueryStartNode>(&plan, 1);
node1 = std::make_unique<SubqueryStartNode>(&plan, 0, nullptr);
node2 = std::make_unique<SubqueryStartNode>(&plan, 1, nullptr);

ASSERT_FALSE(node1->isEqualTo(*node2));
}
Expand Down
0