Search in sources :

Example 51 with AssignOperator

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

the class IntroduceRandomPartitioningFeedComputationRule method rewritePre.

@Override
public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    ILogicalOperator op = opRef.getValue();
    if (!op.getOperatorTag().equals(LogicalOperatorTag.ASSIGN)) {
        return false;
    }
    ILogicalOperator opChild = op.getInputs().get(0).getValue();
    if (!opChild.getOperatorTag().equals(LogicalOperatorTag.DATASOURCESCAN)) {
        return false;
    }
    DataSourceScanOperator scanOp = (DataSourceScanOperator) opChild;
    DataSource dataSource = (DataSource) scanOp.getDataSource();
    if (dataSource.getDatasourceType() != DataSource.Type.FEED) {
        return false;
    }
    final FeedDataSource feedDataSource = (FeedDataSource) dataSource;
    FeedConnection feedConnection = feedDataSource.getFeedConnection();
    if (feedConnection.getAppliedFunctions() == null || feedConnection.getAppliedFunctions().size() == 0) {
        return false;
    }
    ExchangeOperator exchangeOp = new ExchangeOperator();
    INodeDomain domain = new INodeDomain() {

        @Override
        public boolean sameAs(INodeDomain domain) {
            return domain == this;
        }

        @Override
        public Integer cardinality() {
            return feedDataSource.getComputeCardinality();
        }
    };
    exchangeOp.setPhysicalOperator(new RandomPartitionExchangePOperator(domain));
    op.getInputs().get(0).setValue(exchangeOp);
    exchangeOp.getInputs().add(new MutableObject<ILogicalOperator>(scanOp));
    ExecutionMode em = ((AbstractLogicalOperator) scanOp).getExecutionMode();
    exchangeOp.setExecutionMode(em);
    exchangeOp.computeDeliveredPhysicalProperties(context);
    context.computeAndSetTypeEnvironmentForOperator(exchangeOp);
    AssignOperator assignOp = (AssignOperator) opRef.getValue();
    AssignPOperator assignPhyOp = (AssignPOperator) assignOp.getPhysicalOperator();
    assignPhyOp.setCardinalityConstraint(domain.cardinality());
    return true;
}
Also used : AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) FeedConnection(org.apache.asterix.metadata.entities.FeedConnection) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) RandomPartitionExchangePOperator(org.apache.hyracks.algebricks.core.algebra.operators.physical.RandomPartitionExchangePOperator) ExchangeOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.ExchangeOperator) INodeDomain(org.apache.hyracks.algebricks.core.algebra.properties.INodeDomain) ExecutionMode(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator.ExecutionMode) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) FeedDataSource(org.apache.asterix.metadata.declared.FeedDataSource) DataSource(org.apache.asterix.metadata.declared.DataSource) FeedDataSource(org.apache.asterix.metadata.declared.FeedDataSource) AssignPOperator(org.apache.hyracks.algebricks.core.algebra.operators.physical.AssignPOperator) DataSourceScanOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.DataSourceScanOperator)

Example 52 with AssignOperator

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

the class PushAggregateIntoNestedSubplanRule method collectVarsBottomUp.

private boolean collectVarsBottomUp(Mutable<ILogicalOperator> opRef, IOptimizationContext context, Map<LogicalVariable, Integer> nspListifyVarsCount, Map<LogicalVariable, AbstractOperatorWithNestedPlans> nspWithAgg, Map<LogicalVariable, Integer> nspAggVarToPlanIndex, Map<ILogicalExpression, ILogicalExpression> aggregateExprToVarExpr) throws AlgebricksException {
    AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
    context.addToDontApplySet(this, op1);
    boolean change = false;
    for (Mutable<ILogicalOperator> child : op1.getInputs()) {
        if (collectVarsBottomUp(child, context, nspListifyVarsCount, nspWithAgg, nspAggVarToPlanIndex, aggregateExprToVarExpr)) {
            change = true;
        }
    }
    Set<LogicalVariable> used = new HashSet<>();
    VariableUtilities.getUsedVariables(op1, used);
    switch(op1.getOperatorTag()) {
        case ASSIGN:
        case SELECT:
            boolean found = false;
            // Do some prefiltering: check if the Assign uses any nsp vars.
            for (LogicalVariable v : used) {
                if (nspListifyVarsCount.get(v) != null) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                break;
            }
            if (op1.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
                AssignOperator assign = (AssignOperator) op1;
                for (Mutable<ILogicalExpression> exprRef : assign.getExpressions()) {
                    Pair<Boolean, ILogicalExpression> p = extractAggFunctionsFromExpression(exprRef, nspWithAgg, aggregateExprToVarExpr, context);
                    if (p.first) {
                        change = true;
                        exprRef.setValue(p.second);
                    }
                }
            }
            if (op1.getOperatorTag() == LogicalOperatorTag.SELECT) {
                SelectOperator select = (SelectOperator) op1;
                Mutable<ILogicalExpression> exprRef = select.getCondition();
                Pair<Boolean, ILogicalExpression> p = extractAggFunctionsFromExpression(exprRef, nspWithAgg, aggregateExprToVarExpr, context);
                if (p.first) {
                    change = true;
                    exprRef.setValue(p.second);
                }
            }
            used.clear();
            VariableUtilities.getUsedVariables(op1, used);
            // increment the count for the ones which are still used
            for (LogicalVariable v : used) {
                Integer m = nspListifyVarsCount.get(v);
                if (m != null) {
                    nspListifyVarsCount.put(v, m + 1);
                }
            }
            break;
        case SUBPLAN:
            // Try to push the subplan into a group-by operator if possible.
            for (LogicalVariable v : used) {
                Integer m = nspListifyVarsCount.get(v);
                if (m != null) {
                    AbstractOperatorWithNestedPlans nspOp = nspWithAgg.get(v);
                    if (pushSubplanAsAggIntoNestedSubplan(opRef, nspOp, v, nspListifyVarsCount, nspWithAgg, nspAggVarToPlanIndex, context)) {
                        change = true;
                    } else {
                        nspListifyVarsCount.put(v, m + 1);
                    }
                }
            }
            if (!change) {
                // Collect aggregate variables for pushing aggregates into the subplan (if possible).
                collectAggregateVars(nspListifyVarsCount, nspWithAgg, nspAggVarToPlanIndex, (AbstractOperatorWithNestedPlans) op1);
            }
            break;
        case GROUP:
            // Collect aggregate variables for pushing aggregates into the nested subplan
            // of the group by operator (if possible).
            collectAggregateVars(nspListifyVarsCount, nspWithAgg, nspAggVarToPlanIndex, (AbstractOperatorWithNestedPlans) op1);
            break;
        default:
            for (LogicalVariable v : used) {
                Integer m = nspListifyVarsCount.get(v);
                if (m != null) {
                    nspListifyVarsCount.put(v, m + 1);
                }
            }
    }
    return change;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) SelectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator) HashSet(java.util.HashSet) AbstractOperatorWithNestedPlans(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans)

Example 53 with AssignOperator

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

the class IntroduceAutogenerateIDRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    // match: commit OR distribute-result OR SINK - ... followed by:
    // [insert to internal dataset with autogenerated id] - assign - project
    // produce: insert - assign - assign* - project
    // **
    // OR [insert to internal dataset with autogenerated id] - assign - [datasource scan]
    // produce insert - assign - assign* - datasource scan
    AbstractLogicalOperator currentOp = (AbstractLogicalOperator) opRef.getValue();
    if (currentOp.getOperatorTag() == LogicalOperatorTag.DELEGATE_OPERATOR) {
        DelegateOperator dOp = (DelegateOperator) currentOp;
        if (!(dOp.getDelegate() instanceof CommitOperator)) {
            return false;
        } else if (!((CommitOperator) dOp.getDelegate()).isSink()) {
            return false;
        }
    } else if (currentOp.getOperatorTag() != LogicalOperatorTag.DISTRIBUTE_RESULT && currentOp.getOperatorTag() != LogicalOperatorTag.SINK) {
        return false;
    }
    ArrayDeque<AbstractLogicalOperator> opStack = new ArrayDeque<>();
    opStack.push(currentOp);
    while (currentOp.getInputs().size() == 1) {
        currentOp = (AbstractLogicalOperator) currentOp.getInputs().get(0).getValue();
        if (currentOp.getOperatorTag() == LogicalOperatorTag.INSERT_DELETE_UPSERT) {
            break;
        }
        opStack.push(currentOp);
    }
    if (currentOp.getOperatorTag() != LogicalOperatorTag.INSERT_DELETE_UPSERT) {
        return false;
    }
    InsertDeleteUpsertOperator insertOp = (InsertDeleteUpsertOperator) currentOp;
    if (insertOp.getOperation() != Kind.INSERT && insertOp.getOperation() != Kind.UPSERT) {
        return false;
    }
    DatasetDataSource dds = (DatasetDataSource) insertOp.getDataSource();
    boolean autogenerated = ((InternalDatasetDetails) dds.getDataset().getDatasetDetails()).isAutogenerated();
    if (!autogenerated) {
        return false;
    }
    if (((DataSource) insertOp.getDataSource()).getDatasourceType() != Type.INTERNAL_DATASET) {
        return false;
    }
    AbstractLogicalOperator parentOp = (AbstractLogicalOperator) currentOp.getInputs().get(0).getValue();
    if (parentOp.getOperatorTag() != LogicalOperatorTag.ASSIGN) {
        return false;
    }
    AssignOperator assignOp = (AssignOperator) parentOp;
    LogicalVariable inputRecord;
    //TODO: bug here. will not work for internal datasets with filters since the pattern becomes 
    //[project-assign-assign-insert]
    AbstractLogicalOperator grandparentOp = (AbstractLogicalOperator) parentOp.getInputs().get(0).getValue();
    if (grandparentOp.getOperatorTag() == LogicalOperatorTag.PROJECT) {
        ProjectOperator projectOp = (ProjectOperator) grandparentOp;
        inputRecord = projectOp.getVariables().get(0);
    } else if (grandparentOp.getOperatorTag() == LogicalOperatorTag.DATASOURCESCAN) {
        DataSourceScanOperator dssOp = (DataSourceScanOperator) grandparentOp;
        inputRecord = dssOp.getVariables().get(0);
    } else {
        return false;
    }
    List<String> pkFieldName = ((InternalDatasetDetails) dds.getDataset().getDatasetDetails()).getPrimaryKey().get(0);
    ILogicalExpression rec0 = new VariableReferenceExpression(inputRecord);
    ILogicalExpression rec1 = createPrimaryKeyRecordExpression(pkFieldName);
    ILogicalExpression mergedRec = createRecordMergeFunction(rec0, rec1);
    ILogicalExpression nonNullMergedRec = createNotNullFunction(mergedRec);
    LogicalVariable v = context.newVar();
    AssignOperator newAssign = new AssignOperator(v, new MutableObject<ILogicalExpression>(nonNullMergedRec));
    newAssign.getInputs().add(new MutableObject<ILogicalOperator>(grandparentOp));
    assignOp.getInputs().set(0, new MutableObject<ILogicalOperator>(newAssign));
    VariableUtilities.substituteVariables(assignOp, inputRecord, v, context);
    VariableUtilities.substituteVariables(insertOp, inputRecord, v, context);
    context.computeAndSetTypeEnvironmentForOperator(newAssign);
    context.computeAndSetTypeEnvironmentForOperator(assignOp);
    context.computeAndSetTypeEnvironmentForOperator(insertOp);
    ;
    for (AbstractLogicalOperator op : opStack) {
        VariableUtilities.substituteVariables(op, inputRecord, v, context);
        context.computeAndSetTypeEnvironmentForOperator(op);
    }
    return true;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) ProjectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.ProjectOperator) InternalDatasetDetails(org.apache.asterix.metadata.entities.InternalDatasetDetails) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) DatasetDataSource(org.apache.asterix.metadata.declared.DatasetDataSource) AString(org.apache.asterix.om.base.AString) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) ArrayDeque(java.util.ArrayDeque) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) InsertDeleteUpsertOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.InsertDeleteUpsertOperator) DataSourceScanOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.DataSourceScanOperator) DelegateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.DelegateOperator) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) CommitOperator(org.apache.asterix.algebra.operators.CommitOperator)

Example 54 with AssignOperator

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

the class LoadRecordFieldsRule method findFieldExpression.

// Finds a field expression.
private static ILogicalExpression findFieldExpression(AbstractLogicalOperator op, LogicalVariable recordVar, Object accessKey, IVariableTypeEnvironment typeEnvironment, FieldResolver resolver) throws AlgebricksException {
    for (Mutable<ILogicalOperator> child : op.getInputs()) {
        AbstractLogicalOperator opChild = (AbstractLogicalOperator) child.getValue();
        if (opChild.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
            AssignOperator op2 = (AssignOperator) opChild;
            int i = op2.getVariables().indexOf(recordVar);
            if (i >= 0) {
                AbstractLogicalExpression constr = (AbstractLogicalExpression) op2.getExpressions().get(i).getValue();
                return resolveFieldExpression(constr, accessKey, typeEnvironment, resolver);
            }
        } else if (opChild.getOperatorTag() == LogicalOperatorTag.NESTEDTUPLESOURCE) {
            NestedTupleSourceOperator nts = (NestedTupleSourceOperator) opChild;
            AbstractLogicalOperator opBelowNestedPlan = (AbstractLogicalOperator) nts.getDataSourceReference().getValue().getInputs().get(0).getValue();
            ILogicalExpression expr1 = findFieldExpression(opBelowNestedPlan, recordVar, accessKey, typeEnvironment, resolver);
            if (expr1 != null) {
                return expr1;
            }
        }
        ILogicalExpression expr2 = findFieldExpression(opChild, recordVar, accessKey, typeEnvironment, resolver);
        if (expr2 != null) {
            return expr2;
        }
    }
    return null;
}
Also used : NestedTupleSourceOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.NestedTupleSourceOperator) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) AbstractLogicalExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractLogicalExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)

Example 55 with AssignOperator

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

the class InlineUnnestFunctionRule method findUsedVarOrigin.

private ILogicalExpression findUsedVarOrigin(LogicalVariable usedVar, AbstractLogicalOperator parentOp, AbstractLogicalOperator currentOp) throws AlgebricksException {
    ILogicalExpression ret = null;
    if (currentOp.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
        List<LogicalVariable> producedVars = new ArrayList<LogicalVariable>();
        VariableUtilities.getProducedVariables(currentOp, producedVars);
        if (producedVars.contains(usedVar)) {
            AssignOperator assignOp = (AssignOperator) currentOp;
            int index = assignOp.getVariables().indexOf(usedVar);
            ILogicalExpression returnedExpr = assignOp.getExpressions().get(index).getValue();
            if (returnedExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
                AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) returnedExpr;
                if (BuiltinFunctions.isBuiltinUnnestingFunction(funcExpr.getFunctionIdentifier())) {
                    // we only inline for unnest functions
                    removeUnecessaryAssign(parentOp, currentOp, assignOp, index);
                    ret = returnedExpr;
                }
            } else if (returnedExpr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
                //recusively inline
                VariableReferenceExpression varExpr = (VariableReferenceExpression) returnedExpr;
                LogicalVariable var = varExpr.getVariableReference();
                ILogicalExpression finalExpr = findUsedVarOrigin(var, currentOp, (AbstractLogicalOperator) currentOp.getInputs().get(0).getValue());
                if (finalExpr != null) {
                    removeUnecessaryAssign(parentOp, currentOp, assignOp, index);
                    ret = finalExpr;
                }
            }
        }
    } else {
        for (Mutable<ILogicalOperator> child : currentOp.getInputs()) {
            ILogicalExpression expr = findUsedVarOrigin(usedVar, currentOp, (AbstractLogicalOperator) child.getValue());
            if (expr != null) {
                ret = expr;
            }
        }
    }
    return ret;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)

Aggregations

AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)95 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)79 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)75 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)58 Mutable (org.apache.commons.lang3.mutable.Mutable)56 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)52 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)42 ArrayList (java.util.ArrayList)41 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)41 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)32 Pair (org.apache.hyracks.algebricks.common.utils.Pair)30 MutableObject (org.apache.commons.lang3.mutable.MutableObject)24 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)24 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)19 QuantifiedPair (org.apache.asterix.lang.common.struct.QuantifiedPair)14 AsterixConstantValue (org.apache.asterix.om.constants.AsterixConstantValue)14 UnnestingFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression)14 List (java.util.List)12 AString (org.apache.asterix.om.base.AString)12 AggregateFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression)12