{
// The coordinator's top limit graph fragment for a MP plan.
// If planning "order by ... limit", getNextSelectPlan()
// will have already added an order by to the coordinator frag.
// This is the only limit node in a SP plan
LimitPlanNode topLimit = m_parsedSelect.getLimitNodeTop();
/*
* TODO: allow push down limit with distinct (select distinct C from T limit 5)
* or distinct in aggregates.
*/
AbstractPlanNode sendNode = null;
// Whether or not we can push the limit node down
boolean canPushDown = ! m_parsedSelect.hasDistinct();
if (canPushDown) {
sendNode = checkLimitPushDownViability(root);
if (sendNode == null) {
canPushDown = false;
} else {
canPushDown = m_parsedSelect.m_limitCanPushdown;
}
}
if (m_parsedSelect.m_mvFixInfo.needed()) {
// Do not push down limit for mv based distributed query.
canPushDown = false;
}
/*
* Push down the limit plan node when possible even if offset is set. If
* the plan is for a partitioned table, do the push down. Otherwise,
* there is no need to do the push down work, the limit plan node will
* be run in the partition.
*/
if (canPushDown) {
/*
* For partitioned table, the pushed-down limit plan node has a limit based
* on the combined limit and offset, which may require an expression if either of these
* was not a hard-coded constant and didn't get parameterized.
* The top level limit plan node remains the same, with the original limit and offset values.
*/
LimitPlanNode distLimit = m_parsedSelect.getLimitNodeDist();
// Disconnect the distributed parts of the plan below the SEND node
AbstractPlanNode distributedPlan = sendNode.getChild(0);
distributedPlan.clearParents();
sendNode.clearChildren();
// If the distributed limit must be performed on ordered input,
// ensure the order of the data on each partition.
distributedPlan = handleOrderBy(distributedPlan);
if (isInlineLimitPlanNodePossible(distributedPlan)) {
// Inline the distributed limit.
distributedPlan.addInlinePlanNode(distLimit);
sendNode.addAndLinkChild(distributedPlan);
} else {
distLimit.addAndLinkChild(distributedPlan);
// Add the distributed work back to the plan
sendNode.addAndLinkChild(distLimit);
}
}
// In future, inline LIMIT for join, Receive