Search in sources :

Example 6 with OrderOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator 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 7 with OrderOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator in project asterixdb by apache.

the class AccessMethodUtils method createPrimaryIndexUnnestMap.

public static AbstractUnnestMapOperator createPrimaryIndexUnnestMap(AbstractDataSourceOperator dataSourceOp, Dataset dataset, ARecordType recordType, ARecordType metaRecordType, ILogicalOperator inputOp, IOptimizationContext context, boolean sortPrimaryKeys, boolean retainInput, boolean retainNull, boolean requiresBroadcast) throws AlgebricksException {
    List<LogicalVariable> primaryKeyVars = AccessMethodUtils.getPrimaryKeyVarsFromSecondaryUnnestMap(dataset, inputOp);
    // Optionally add a sort on the primary-index keys before searching the primary index.
    OrderOperator order = null;
    if (sortPrimaryKeys) {
        order = new OrderOperator();
        for (LogicalVariable pkVar : primaryKeyVars) {
            Mutable<ILogicalExpression> vRef = new MutableObject<>(new VariableReferenceExpression(pkVar));
            order.getOrderExpressions().add(new Pair<>(OrderOperator.ASC_ORDER, vRef));
        }
        // The secondary-index search feeds into the sort.
        order.getInputs().add(new MutableObject<>(inputOp));
        order.setExecutionMode(ExecutionMode.LOCAL);
        context.computeAndSetTypeEnvironmentForOperator(order);
    }
    // The job gen parameters are transferred to the actual job gen via the UnnestMapOperator's function arguments.
    List<Mutable<ILogicalExpression>> primaryIndexFuncArgs = new ArrayList<>();
    BTreeJobGenParams jobGenParams = new BTreeJobGenParams(dataset.getDatasetName(), IndexType.BTREE, dataset.getDataverseName(), dataset.getDatasetName(), retainInput, requiresBroadcast);
    // Set low/high inclusive to true for a point lookup.
    jobGenParams.setLowKeyInclusive(true);
    jobGenParams.setHighKeyInclusive(true);
    jobGenParams.setLowKeyVarList(primaryKeyVars, 0, primaryKeyVars.size());
    jobGenParams.setHighKeyVarList(primaryKeyVars, 0, primaryKeyVars.size());
    jobGenParams.setIsEqCondition(true);
    jobGenParams.writeToFuncArgs(primaryIndexFuncArgs);
    // Variables and types coming out of the primary-index search.
    List<LogicalVariable> primaryIndexUnnestVars = new ArrayList<>();
    List<Object> primaryIndexOutputTypes = new ArrayList<>();
    // Append output variables/types generated by the primary-index search (not forwarded from input).
    primaryIndexUnnestVars.addAll(dataSourceOp.getVariables());
    appendPrimaryIndexTypes(dataset, recordType, metaRecordType, primaryIndexOutputTypes);
    // An index search is expressed as an unnest over an index-search function.
    IFunctionInfo primaryIndexSearch = FunctionUtil.getFunctionInfo(BuiltinFunctions.INDEX_SEARCH);
    AbstractFunctionCallExpression primaryIndexSearchFunc = new ScalarFunctionCallExpression(primaryIndexSearch, primaryIndexFuncArgs);
    // This is the operator that jobgen will be looking for. It contains an unnest function that has all necessary arguments to determine
    // which index to use, which variables contain the index-search keys, what is the original dataset, etc.
    AbstractUnnestMapOperator primaryIndexUnnestOp = null;
    if (retainNull) {
        if (retainInput) {
            primaryIndexUnnestOp = new LeftOuterUnnestMapOperator(primaryIndexUnnestVars, new MutableObject<ILogicalExpression>(primaryIndexSearchFunc), primaryIndexOutputTypes, retainInput);
        } else {
            // Left-outer-join without retainNull and retainInput doesn't make sense.
            throw new AlgebricksException("Left-outer-join should propagate all inputs from the outer branch.");
        }
    } else {
        primaryIndexUnnestOp = new UnnestMapOperator(primaryIndexUnnestVars, new MutableObject<ILogicalExpression>(primaryIndexSearchFunc), primaryIndexOutputTypes, retainInput);
    }
    // Fed by the order operator or the secondaryIndexUnnestOp.
    if (sortPrimaryKeys) {
        primaryIndexUnnestOp.getInputs().add(new MutableObject<ILogicalOperator>(order));
    } else {
        primaryIndexUnnestOp.getInputs().add(new MutableObject<>(inputOp));
    }
    context.computeAndSetTypeEnvironmentForOperator(primaryIndexUnnestOp);
    primaryIndexUnnestOp.setExecutionMode(ExecutionMode.PARTITIONED);
    return primaryIndexUnnestOp;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) IFunctionInfo(org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo) LeftOuterUnnestMapOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterUnnestMapOperator) UnnestMapOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestMapOperator) AbstractUnnestMapOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractUnnestMapOperator) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) OrderOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator) 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) AbstractUnnestMapOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractUnnestMapOperator) MutableObject(org.apache.commons.lang3.mutable.MutableObject) IAObject(org.apache.asterix.om.base.IAObject) LeftOuterUnnestMapOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterUnnestMapOperator) MutableObject(org.apache.commons.lang3.mutable.MutableObject) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)

Example 8 with OrderOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator in project asterixdb by apache.

the class IsomorphismOperatorVisitor method visitOrderOperator.

@Override
public Boolean visitOrderOperator(OrderOperator op, ILogicalOperator arg) throws AlgebricksException {
    AbstractLogicalOperator aop = (AbstractLogicalOperator) arg;
    if (aop.getOperatorTag() != LogicalOperatorTag.ORDER) {
        return Boolean.FALSE;
    }
    OrderOperator orderOpArg = (OrderOperator) copyAndSubstituteVar(op, arg);
    boolean isomorphic = compareIOrderAndExpressions(op.getOrderExpressions(), orderOpArg.getOrderExpressions());
    return isomorphic;
}
Also used : AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) OrderOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator)

Example 9 with OrderOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator in project asterixdb by apache.

the class ExtractOrderExpressionsRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
    if (op1.getOperatorTag() != LogicalOperatorTag.ORDER) {
        return false;
    }
    if (context.checkIfInDontApplySet(this, op1)) {
        return false;
    }
    context.addToDontApplySet(this, op1);
    OrderOperator oo = (OrderOperator) op1;
    if (!orderHasComplexExpr(oo)) {
        return false;
    }
    Mutable<ILogicalOperator> opRef2 = oo.getInputs().get(0);
    for (Pair<IOrder, Mutable<ILogicalExpression>> orderPair : oo.getOrderExpressions()) {
        ILogicalExpression expr = orderPair.second.getValue();
        if (expr.getExpressionTag() != LogicalExpressionTag.VARIABLE && !AnalysisUtil.isAccessToFieldRecord(expr)) {
            LogicalVariable v = extractExprIntoAssignOpRef(expr, opRef2, context);
            orderPair.second.setValue(new VariableReferenceExpression(v));
        }
    }
    context.computeAndSetTypeEnvironmentForOperator(oo);
    return true;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) IOrder(org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator.IOrder) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) OrderOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator)

Example 10 with OrderOperator

use of org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator in project asterixdb by apache.

the class AccessMethodUtils method createExternalDataLookupUnnestMap.

public static UnnestMapOperator createExternalDataLookupUnnestMap(AbstractDataSourceOperator dataSourceOp, Dataset dataset, ARecordType recordType, ILogicalOperator inputOp, IOptimizationContext context, boolean retainInput, boolean retainNull) throws AlgebricksException {
    List<LogicalVariable> primaryKeyVars = AccessMethodUtils.getPrimaryKeyVarsFromSecondaryUnnestMap(dataset, inputOp);
    // add a sort on the RID fields before fetching external data.
    OrderOperator order = new OrderOperator();
    for (LogicalVariable pkVar : primaryKeyVars) {
        Mutable<ILogicalExpression> vRef = new MutableObject<>(new VariableReferenceExpression(pkVar));
        order.getOrderExpressions().add(new Pair<>(OrderOperator.ASC_ORDER, vRef));
    }
    // The secondary-index search feeds into the sort.
    order.getInputs().add(new MutableObject<>(inputOp));
    order.setExecutionMode(ExecutionMode.LOCAL);
    context.computeAndSetTypeEnvironmentForOperator(order);
    List<Mutable<ILogicalExpression>> externalLookupArgs = new ArrayList<>();
    //Add dataverse to the arguments
    AccessMethodUtils.addStringArg(dataset.getDataverseName(), externalLookupArgs);
    //Add dataset to the arguments
    AccessMethodUtils.addStringArg(dataset.getDatasetName(), externalLookupArgs);
    //Add PK vars to the arguments
    AccessMethodUtils.writeVarList(primaryKeyVars, externalLookupArgs);
    // Variables and types coming out of the external access.
    List<LogicalVariable> externalUnnestVars = new ArrayList<>();
    List<Object> outputTypes = new ArrayList<>();
    // Append output variables/types generated by the data scan (not forwarded from input).
    externalUnnestVars.addAll(dataSourceOp.getVariables());
    appendExternalRecTypes(dataset, recordType, outputTypes);
    IFunctionInfo externalLookup = FunctionUtil.getFunctionInfo(BuiltinFunctions.EXTERNAL_LOOKUP);
    AbstractFunctionCallExpression externalLookupFunc = new ScalarFunctionCallExpression(externalLookup, externalLookupArgs);
    UnnestMapOperator unnestOp = new UnnestMapOperator(externalUnnestVars, new MutableObject<ILogicalExpression>(externalLookupFunc), outputTypes, retainInput);
    // Fed by the order operator or the secondaryIndexUnnestOp.
    unnestOp.getInputs().add(new MutableObject<ILogicalOperator>(order));
    context.computeAndSetTypeEnvironmentForOperator(unnestOp);
    unnestOp.setExecutionMode(ExecutionMode.PARTITIONED);
    //set the physical operator
    DataSourceId dataSourceId = new DataSourceId(dataset.getDataverseName(), dataset.getDatasetName());
    unnestOp.setPhysicalOperator(new ExternalDataLookupPOperator(dataSourceId, dataset, recordType, primaryKeyVars, false, retainInput, retainNull));
    return unnestOp;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) IFunctionInfo(org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo) LeftOuterUnnestMapOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterUnnestMapOperator) UnnestMapOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestMapOperator) AbstractUnnestMapOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractUnnestMapOperator) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) OrderOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator) 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) ExternalDataLookupPOperator(org.apache.asterix.algebra.operators.physical.ExternalDataLookupPOperator) MutableObject(org.apache.commons.lang3.mutable.MutableObject) IAObject(org.apache.asterix.om.base.IAObject) DataSourceId(org.apache.asterix.metadata.declared.DataSourceId) MutableObject(org.apache.commons.lang3.mutable.MutableObject) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)

Aggregations

OrderOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator)17 Mutable (org.apache.commons.lang3.mutable.Mutable)14 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)12 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)11 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)10 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)10 IOrder (org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator.IOrder)9 ArrayList (java.util.ArrayList)8 MutableObject (org.apache.commons.lang3.mutable.MutableObject)8 Pair (org.apache.hyracks.algebricks.common.utils.Pair)8 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)4 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)4 ILogicalPlan (org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)3 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)3 GroupByOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator)3 HashSet (java.util.HashSet)2 List (java.util.List)2 IAObject (org.apache.asterix.om.base.IAObject)2 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)2 AggregateFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression)2