use of org.apache.hyracks.algebricks.core.algebra.operators.physical.SortGroupByPOperator in project asterixdb by apache.
the class PushGroupByIntoSortRule method rewritePost.
@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
ILogicalOperator op1 = opRef.getValue();
if (op1 == null) {
return false;
}
boolean changed = false;
for (Mutable<ILogicalOperator> childRef : op1.getInputs()) {
AbstractLogicalOperator op = (AbstractLogicalOperator) childRef.getValue();
if (op.getOperatorTag() == LogicalOperatorTag.GROUP) {
PhysicalOperatorTag opTag = op.getPhysicalOperator().getOperatorTag();
GroupByOperator groupByOperator = (GroupByOperator) op;
if (opTag == PhysicalOperatorTag.PRE_CLUSTERED_GROUP_BY) {
Mutable<ILogicalOperator> op2Ref = op.getInputs().get(0).getValue().getInputs().get(0);
AbstractLogicalOperator op2 = (AbstractLogicalOperator) op2Ref.getValue();
if (op2.getPhysicalOperator().getOperatorTag() == PhysicalOperatorTag.STABLE_SORT) {
AbstractStableSortPOperator sortPhysicalOperator = (AbstractStableSortPOperator) op2.getPhysicalOperator();
if (groupByOperator.getNestedPlans().size() != 1) {
//an aggregate and a nested-tuple-source.
continue;
}
ILogicalPlan p0 = groupByOperator.getNestedPlans().get(0);
if (p0.getRoots().size() != 1) {
//an aggregate and a nested-tuple-source.
continue;
}
Mutable<ILogicalOperator> r0 = p0.getRoots().get(0);
AbstractLogicalOperator r0Logical = (AbstractLogicalOperator) r0.getValue();
if (r0Logical.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
//we only rewrite aggregation function; do nothing for running aggregates
continue;
}
AggregateOperator aggOp = (AggregateOperator) r0.getValue();
AbstractLogicalOperator aggInputOp = (AbstractLogicalOperator) aggOp.getInputs().get(0).getValue();
if (aggInputOp.getOperatorTag() != LogicalOperatorTag.NESTEDTUPLESOURCE) {
continue;
}
boolean hasIntermediateAggregate = generateMergeAggregationExpressions(groupByOperator, context);
if (!hasIntermediateAggregate) {
continue;
}
//replace preclustered gby with sort gby
if (!groupByOperator.isGroupAll()) {
op.setPhysicalOperator(new SortGroupByPOperator(groupByOperator.getGroupByList(), context.getPhysicalOptimizationConfig().getMaxFramesExternalGroupBy(), sortPhysicalOperator.getSortColumns()));
}
// remove the stable sort operator
op.getInputs().clear();
op.getInputs().addAll(op2.getInputs());
changed = true;
}
}
continue;
} else {
continue;
}
}
return changed;
}
Aggregations