Search in sources :

Example 1 with StableSortPOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.physical.StableSortPOperator in project asterixdb by apache.

the class PushLimitIntoOrderByRule method pushLimitIntoOrder.

/**
     * Generate new ORDER operator that uses TopKSort module and replaces the old ORDER operator.
     */
private boolean pushLimitIntoOrder(Mutable<ILogicalOperator> opRef, Mutable<ILogicalOperator> opRef2, IOptimizationContext context) throws AlgebricksException {
    PhysicalOptimizationConfig physicalOptimizationConfig = context.getPhysicalOptimizationConfig();
    LimitOperator limitOp = (LimitOperator) opRef.getValue();
    OrderOperator orderOp = (OrderOperator) opRef2.getValue();
    long topK = -1;
    // We don't push-down LIMIT into in-memory sort.
    if (orderOp.getPhysicalOperator().getOperatorTag() != PhysicalOperatorTag.STABLE_SORT) {
        return false;
    }
    // Get the LIMIT constant
    if (limitOp.getMaxObjects().getValue().getExpressionTag() == LogicalExpressionTag.CONSTANT) {
        // Currently, we support LIMIT with a constant value.
        topK = AccessMethodUtils.getInt64Constant(limitOp.getMaxObjects());
        // since the original external sort's performance might be better.
        if (topK > Integer.MAX_VALUE) {
            return false;
        }
        if (topK < 0) {
            topK = 0;
        }
    } else {
        return false;
    }
    // Final topK will be applied through LIMIT.
    if (limitOp.getOffset().getValue() != null) {
        if (limitOp.getOffset().getValue().getExpressionTag() == LogicalExpressionTag.CONSTANT) {
            long offset = AccessMethodUtils.getInt64Constant(limitOp.getOffset());
            if (offset < 0) {
                offset = 0;
            }
            // Check the overflow case.
            if (offset >= Integer.MAX_VALUE - topK) {
                return false;
            }
            topK += offset;
        } else {
            return false;
        }
    }
    // Create the new ORDER operator, set the topK value, and replace the current one.
    OrderOperator newOrderOp = new OrderOperator(orderOp.getOrderExpressions(), (int) topK);
    newOrderOp.setPhysicalOperator(new StableSortPOperator(physicalOptimizationConfig.getMaxFramesExternalSort(), newOrderOp.getTopK()));
    newOrderOp.getInputs().addAll(orderOp.getInputs());
    newOrderOp.setExecutionMode(orderOp.getExecutionMode());
    newOrderOp.recomputeSchema();
    newOrderOp.computeDeliveredPhysicalProperties(context);
    opRef2.setValue(newOrderOp);
    context.computeAndSetTypeEnvironmentForOperator(newOrderOp);
    context.addToDontApplySet(this, limitOp);
    return true;
}
Also used : LimitOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.LimitOperator) OrderOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator) PhysicalOptimizationConfig(org.apache.hyracks.algebricks.core.rewriter.base.PhysicalOptimizationConfig) StableSortPOperator(org.apache.hyracks.algebricks.core.algebra.operators.physical.StableSortPOperator)

Example 2 with StableSortPOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.physical.StableSortPOperator in project asterixdb by apache.

the class PushNestedOrderByUnderPreSortedGroupByRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    if (op.getOperatorTag() != LogicalOperatorTag.GROUP) {
        return false;
    }
    if (op.getPhysicalOperator() == null) {
        return false;
    }
    AbstractPhysicalOperator pOp = (AbstractPhysicalOperator) op.getPhysicalOperator();
    if (pOp.getOperatorTag() != PhysicalOperatorTag.PRE_CLUSTERED_GROUP_BY) {
        return false;
    }
    GroupByOperator gby = (GroupByOperator) op;
    ILogicalPlan plan = gby.getNestedPlans().get(0);
    AbstractLogicalOperator op1 = (AbstractLogicalOperator) plan.getRoots().get(0).getValue();
    if (op1.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
        return false;
    }
    Mutable<ILogicalOperator> opRef2 = op1.getInputs().get(0);
    AbstractLogicalOperator op2 = (AbstractLogicalOperator) opRef2.getValue();
    if (op2.getOperatorTag() != LogicalOperatorTag.ORDER) {
        return false;
    }
    OrderOperator order1 = (OrderOperator) op2;
    if (!isIndependentFromChildren(order1)) {
        return false;
    }
    AbstractPhysicalOperator pOrder1 = (AbstractPhysicalOperator) op2.getPhysicalOperator();
    if (pOrder1.getOperatorTag() != PhysicalOperatorTag.STABLE_SORT && pOrder1.getOperatorTag() != PhysicalOperatorTag.IN_MEMORY_STABLE_SORT) {
        return false;
    }
    // StableSortPOperator sort1 = (StableSortPOperator) pOrder1;
    AbstractLogicalOperator op3 = (AbstractLogicalOperator) op.getInputs().get(0).getValue();
    if (op3.getOperatorTag() != LogicalOperatorTag.ORDER) {
        return false;
    }
    AbstractPhysicalOperator pOp3 = (AbstractPhysicalOperator) op3.getPhysicalOperator();
    if (pOp3.getOperatorTag() != PhysicalOperatorTag.STABLE_SORT) {
        return false;
    }
    OrderOperator order2 = (OrderOperator) op3;
    StableSortPOperator sort2 = (StableSortPOperator) pOp3;
    // int k = 0;
    for (Pair<IOrder, Mutable<ILogicalExpression>> oe : order1.getOrderExpressions()) {
        order2.getOrderExpressions().add(oe);
    // sortColumns[n2 + k] = sort1.getSortColumns()[k];
    // ++k;
    }
    // sort2.setSortColumns(sortColumns);
    sort2.computeDeliveredProperties(order2, null);
    // remove order1
    ILogicalOperator underOrder1 = order1.getInputs().get(0).getValue();
    opRef2.setValue(underOrder1);
    return true;
}
Also used : Mutable(org.apache.commons.lang3.mutable.Mutable) GroupByOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) IOrder(org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator.IOrder) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AbstractPhysicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.physical.AbstractPhysicalOperator) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) OrderOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator) StableSortPOperator(org.apache.hyracks.algebricks.core.algebra.operators.physical.StableSortPOperator)

Example 3 with StableSortPOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.physical.StableSortPOperator in project asterixdb by apache.

the class EnforceStructuralPropertiesRule method enforceOrderProperties.

private Mutable<ILogicalOperator> enforceOrderProperties(List<LocalOrderProperty> oList, Mutable<ILogicalOperator> topOp, boolean isMicroOp, IOptimizationContext context) throws AlgebricksException {
    List<Pair<IOrder, Mutable<ILogicalExpression>>> oe = new LinkedList<>();
    for (LocalOrderProperty orderProperty : oList) {
        for (OrderColumn oc : orderProperty.getOrderColumns()) {
            IOrder ordType = (oc.getOrder() == OrderKind.ASC) ? OrderOperator.ASC_ORDER : OrderOperator.DESC_ORDER;
            Pair<IOrder, Mutable<ILogicalExpression>> pair = new Pair<>(ordType, new MutableObject<ILogicalExpression>(new VariableReferenceExpression(oc.getColumn())));
            oe.add(pair);
        }
    }
    OrderOperator oo = new OrderOperator(oe);
    oo.setExecutionMode(AbstractLogicalOperator.ExecutionMode.LOCAL);
    if (isMicroOp) {
        oo.setPhysicalOperator(new InMemoryStableSortPOperator());
    } else {
        oo.setPhysicalOperator(new StableSortPOperator(physicalOptimizationConfig.getMaxFramesExternalSort()));
    }
    oo.getInputs().add(topOp);
    context.computeAndSetTypeEnvironmentForOperator(oo);
    if (AlgebricksConfig.DEBUG) {
        AlgebricksConfig.ALGEBRICKS_LOGGER.fine(">>>> Added sort enforcer " + oo.getPhysicalOperator() + ".\n");
    }
    return new MutableObject<ILogicalOperator>(oo);
}
Also used : IOrder(org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator.IOrder) OrderColumn(org.apache.hyracks.algebricks.core.algebra.properties.OrderColumn) OrderOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator) InMemoryStableSortPOperator(org.apache.hyracks.algebricks.core.algebra.operators.physical.InMemoryStableSortPOperator) LinkedList(java.util.LinkedList) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) LocalOrderProperty(org.apache.hyracks.algebricks.core.algebra.properties.LocalOrderProperty) InMemoryStableSortPOperator(org.apache.hyracks.algebricks.core.algebra.operators.physical.InMemoryStableSortPOperator) AbstractStableSortPOperator(org.apache.hyracks.algebricks.core.algebra.operators.physical.AbstractStableSortPOperator) StableSortPOperator(org.apache.hyracks.algebricks.core.algebra.operators.physical.StableSortPOperator) Pair(org.apache.hyracks.algebricks.common.utils.Pair) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Aggregations

OrderOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator)3 StableSortPOperator (org.apache.hyracks.algebricks.core.algebra.operators.physical.StableSortPOperator)3 Mutable (org.apache.commons.lang3.mutable.Mutable)2 IOrder (org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator.IOrder)2 LinkedList (java.util.LinkedList)1 MutableObject (org.apache.commons.lang3.mutable.MutableObject)1 Pair (org.apache.hyracks.algebricks.common.utils.Pair)1 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)1 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)1 ILogicalPlan (org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)1 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)1 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)1 GroupByOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator)1 LimitOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.LimitOperator)1 AbstractPhysicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.physical.AbstractPhysicalOperator)1 AbstractStableSortPOperator (org.apache.hyracks.algebricks.core.algebra.operators.physical.AbstractStableSortPOperator)1 InMemoryStableSortPOperator (org.apache.hyracks.algebricks.core.algebra.operators.physical.InMemoryStableSortPOperator)1 LocalOrderProperty (org.apache.hyracks.algebricks.core.algebra.properties.LocalOrderProperty)1 OrderColumn (org.apache.hyracks.algebricks.core.algebra.properties.OrderColumn)1 PhysicalOptimizationConfig (org.apache.hyracks.algebricks.core.rewriter.base.PhysicalOptimizationConfig)1