Search in sources :

Example 36 with AbstractFunctionCallExpression

use of org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression in project asterixdb by apache.

the class PushAggFuncIntoStandaloneAggregateRule method pushAggregateFunction.

private boolean pushAggregateFunction(AggregateOperator aggOp, AssignOperator assignOp, IOptimizationContext context) throws AlgebricksException {
    Mutable<ILogicalOperator> opRef3 = aggOp.getInputs().get(0);
    AbstractLogicalOperator op3 = (AbstractLogicalOperator) opRef3.getValue();
    // If there's a group by below the agg, then we want to have the agg pushed into the group by.
    if (op3.getOperatorTag() == LogicalOperatorTag.GROUP) {
        return false;
    }
    if (aggOp.getVariables().size() != 1) {
        return false;
    }
    ILogicalExpression aggExpr = aggOp.getExpressions().get(0).getValue();
    if (aggExpr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        return false;
    }
    AbstractFunctionCallExpression origAggFuncExpr = (AbstractFunctionCallExpression) aggExpr;
    if (origAggFuncExpr.getFunctionIdentifier() != BuiltinFunctions.LISTIFY) {
        return false;
    }
    LogicalVariable aggVar = aggOp.getVariables().get(0);
    List<LogicalVariable> used = new LinkedList<LogicalVariable>();
    VariableUtilities.getUsedVariables(assignOp, used);
    if (!used.contains(aggVar)) {
        return false;
    }
    List<Mutable<ILogicalExpression>> srcAssignExprRefs = new LinkedList<Mutable<ILogicalExpression>>();
    if (fingAggFuncExprRef(assignOp.getExpressions(), aggVar, srcAssignExprRefs) == false) {
        return false;
    }
    if (srcAssignExprRefs.isEmpty()) {
        return false;
    }
    AbstractFunctionCallExpression aggOpExpr = (AbstractFunctionCallExpression) aggOp.getExpressions().get(0).getValue();
    aggOp.getExpressions().clear();
    aggOp.getVariables().clear();
    for (Mutable<ILogicalExpression> srcAssignExprRef : srcAssignExprRefs) {
        AbstractFunctionCallExpression assignFuncExpr = (AbstractFunctionCallExpression) srcAssignExprRef.getValue();
        FunctionIdentifier aggFuncIdent = BuiltinFunctions.getAggregateFunction(assignFuncExpr.getFunctionIdentifier());
        // Push the agg func into the agg op.
        List<Mutable<ILogicalExpression>> aggArgs = new ArrayList<Mutable<ILogicalExpression>>();
        aggArgs.add(aggOpExpr.getArguments().get(0));
        AggregateFunctionCallExpression aggFuncExpr = BuiltinFunctions.makeAggregateFunctionExpression(aggFuncIdent, aggArgs);
        LogicalVariable newVar = context.newVar();
        aggOp.getVariables().add(newVar);
        aggOp.getExpressions().add(new MutableObject<ILogicalExpression>(aggFuncExpr));
        // The assign now just "renames" the variable to make sure the upstream plan still works.
        srcAssignExprRef.setValue(new VariableReferenceExpression(newVar));
    }
    context.computeAndSetTypeEnvironmentForOperator(aggOp);
    context.computeAndSetTypeEnvironmentForOperator(assignOp);
    return true;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AggregateFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Mutable(org.apache.commons.lang3.mutable.Mutable) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)

Example 37 with AbstractFunctionCallExpression

use of org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression in project asterixdb by apache.

the class PushAggregateIntoNestedSubplanRule method collectOneVarPerAggFromOpWithNestedPlans.

private List<LogicalVariable> collectOneVarPerAggFromOpWithNestedPlans(AbstractOperatorWithNestedPlans op) {
    List<ILogicalPlan> nPlans = op.getNestedPlans();
    if (nPlans == null || nPlans.isEmpty()) {
        return Collections.emptyList();
    }
    List<LogicalVariable> aggVars = new ArrayList<>();
    // test that the operator computes a "listify" aggregate
    for (int i = 0; i < nPlans.size(); i++) {
        AbstractLogicalOperator topOp = (AbstractLogicalOperator) nPlans.get(i).getRoots().get(0).getValue();
        if (topOp.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
            continue;
        }
        AggregateOperator agg = (AggregateOperator) topOp;
        if (agg.getVariables().size() != 1) {
            continue;
        }
        ILogicalExpression expr = agg.getExpressions().get(0).getValue();
        if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
            continue;
        }
        AbstractFunctionCallExpression fceAgg = (AbstractFunctionCallExpression) expr;
        if (fceAgg.getFunctionIdentifier() != BuiltinFunctions.LISTIFY) {
            continue;
        }
        aggVars.add(agg.getVariables().get(0));
    }
    return aggVars;
}
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) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ArrayList(java.util.ArrayList) AggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)

Example 38 with AbstractFunctionCallExpression

use of org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression in project asterixdb by apache.

the class IntroduceEnforcedListTypeRule method rewriteExpressions.

private boolean rewriteExpressions(List<Mutable<ILogicalExpression>> expressions, IVariableTypeEnvironment env) throws AlgebricksException {
    boolean changed = false;
    for (Mutable<ILogicalExpression> exprRef : expressions) {
        ILogicalExpression expr = exprRef.getValue();
        if (expr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
            AbstractFunctionCallExpression argFuncExpr = (AbstractFunctionCallExpression) expr;
            IAType exprType = (IAType) env.getType(argFuncExpr);
            if (StaticTypeCastUtil.rewriteListExpr(argFuncExpr, exprType, exprType, env)) {
                TypeCastUtils.resetRequiredAndInputTypes(argFuncExpr);
                changed = true;
            }
        }
    }
    return changed;
}
Also used : ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) IAType(org.apache.asterix.om.types.IAType)

Example 39 with AbstractFunctionCallExpression

use of org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression in project asterixdb by apache.

the class IntroduceMaterializationForInsertWithSelfScanRule method checkIfInsertAndScanDatasetsSame.

private boolean checkIfInsertAndScanDatasetsSame(AbstractLogicalOperator op, String insertDatasetName) {
    boolean sameDataset = false;
    for (int i = 0; i < op.getInputs().size(); ++i) {
        AbstractLogicalOperator descendantOp = (AbstractLogicalOperator) op.getInputs().get(i).getValue();
        if (descendantOp.getOperatorTag() == LogicalOperatorTag.UNNEST_MAP) {
            UnnestMapOperator unnestMapOp = (UnnestMapOperator) descendantOp;
            ILogicalExpression unnestExpr = unnestMapOp.getExpressionRef().getValue();
            if (unnestExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
                AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) unnestExpr;
                FunctionIdentifier fid = f.getFunctionIdentifier();
                if (!fid.equals(BuiltinFunctions.INDEX_SEARCH)) {
                    throw new IllegalStateException();
                }
                AccessMethodJobGenParams jobGenParams = new AccessMethodJobGenParams();
                jobGenParams.readFromFuncArgs(f.getArguments());
                boolean isPrimaryIndex = jobGenParams.isPrimaryIndex();
                String indexName = jobGenParams.getIndexName();
                if (isPrimaryIndex && indexName.compareTo(insertDatasetName) == 0) {
                    return true;
                }
            }
        } else if (descendantOp.getOperatorTag() == LogicalOperatorTag.DATASOURCESCAN) {
            DataSourceScanOperator dataSourceScanOp = (DataSourceScanOperator) descendantOp;
            DataSource ds = (DataSource) dataSourceScanOp.getDataSource();
            if ((ds.getDatasourceType() == Type.INTERNAL_DATASET || ds.getDatasourceType() == Type.EXTERNAL_DATASET) && ((DatasetDataSource) ds).getDataset().getDatasetName().compareTo(insertDatasetName) == 0) {
                return true;
            }
        }
        sameDataset = checkIfInsertAndScanDatasetsSame(descendantOp, insertDatasetName);
        if (sameDataset) {
            break;
        }
    }
    return sameDataset;
}
Also used : FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) AccessMethodJobGenParams(org.apache.asterix.optimizer.rules.am.AccessMethodJobGenParams) DataSourceScanOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.DataSourceScanOperator) UnnestMapOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestMapOperator) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) DataSource(org.apache.asterix.metadata.declared.DataSource) DatasetDataSource(org.apache.asterix.metadata.declared.DatasetDataSource)

Example 40 with AbstractFunctionCallExpression

use of org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression in project asterixdb by apache.

the class NestGroupByRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
    if (op1.getOperatorTag() != LogicalOperatorTag.SUBPLAN) {
        return false;
    }
    SubplanOperator subplan = (SubplanOperator) op1;
    if (subplan.getNestedPlans().size() != 1) {
        return false;
    }
    ILogicalPlan p = subplan.getNestedPlans().get(0);
    if (p.getRoots().size() != 1) {
        return false;
    }
    Set<LogicalVariable> free = new HashSet<LogicalVariable>();
    OperatorPropertiesUtil.getFreeVariablesInSubplans(subplan, free);
    if (free.size() != 1) {
        return false;
    }
    LogicalVariable fVar = null;
    for (LogicalVariable v : free) {
        fVar = v;
        break;
    }
    AbstractLogicalOperator op2 = (AbstractLogicalOperator) op1.getInputs().get(0).getValue();
    if (op2.getOperatorTag() != LogicalOperatorTag.GROUP) {
        return false;
    }
    GroupByOperator gby = (GroupByOperator) op2;
    if (gby.getNestedPlans().size() != 1) {
        return false;
    }
    ILogicalPlan p2 = gby.getNestedPlans().get(0);
    if (p2.getRoots().size() != 1) {
        return false;
    }
    Mutable<ILogicalOperator> r2 = p2.getRoots().get(0);
    AbstractLogicalOperator opr2 = (AbstractLogicalOperator) r2.getValue();
    if (opr2.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
        return false;
    }
    AggregateOperator aggOuter = (AggregateOperator) opr2;
    int posInAggList = aggOuter.getVariables().indexOf(fVar);
    if (posInAggList < 0) {
        return false;
    }
    AbstractLogicalOperator outerAggSon = (AbstractLogicalOperator) aggOuter.getInputs().get(0).getValue();
    if (outerAggSon.getOperatorTag() != LogicalOperatorTag.NESTEDTUPLESOURCE) {
        return false;
    }
    ILogicalExpression eAgg = aggOuter.getExpressions().get(posInAggList).getValue();
    if (eAgg.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        return false;
    }
    AbstractFunctionCallExpression listifyCall = (AbstractFunctionCallExpression) eAgg;
    if (listifyCall.getFunctionIdentifier() != BuiltinFunctions.LISTIFY) {
        return false;
    }
    ILogicalExpression argListify = listifyCall.getArguments().get(0).getValue();
    if (argListify.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
        return false;
    }
    Mutable<ILogicalOperator> r = p.getRoots().get(0);
    AbstractLogicalOperator opInS = (AbstractLogicalOperator) r.getValue();
    if (opInS.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
        return false;
    }
    AggregateOperator aggInner = (AggregateOperator) opInS;
    do {
        opInS = (AbstractLogicalOperator) opInS.getInputs().get(0).getValue();
    } while (opInS.getOperatorTag() == LogicalOperatorTag.ASSIGN);
    if (opInS.getOperatorTag() != LogicalOperatorTag.GROUP) {
        return false;
    }
    AbstractLogicalOperator unnestParent = opInS;
    AbstractLogicalOperator opUnder = (AbstractLogicalOperator) opInS.getInputs().get(0).getValue();
    // skip Assigns
    while (opUnder.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
        unnestParent = opUnder;
        opUnder = (AbstractLogicalOperator) opUnder.getInputs().get(0).getValue();
    }
    if (opUnder.getOperatorTag() != LogicalOperatorTag.UNNEST) {
        return false;
    }
    UnnestOperator unnest = (UnnestOperator) opUnder;
    AbstractLogicalOperator unnestSon = (AbstractLogicalOperator) unnest.getInputs().get(0).getValue();
    if (unnestSon.getOperatorTag() != LogicalOperatorTag.NESTEDTUPLESOURCE) {
        return false;
    }
    NestedTupleSourceOperator innerNts = (NestedTupleSourceOperator) unnestSon;
    ILogicalExpression eUnnest = unnest.getExpressionRef().getValue();
    if (eUnnest.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        return false;
    }
    AbstractFunctionCallExpression uf = (AbstractFunctionCallExpression) eUnnest;
    if (uf.getFunctionIdentifier() != BuiltinFunctions.SCAN_COLLECTION) {
        return false;
    }
    ILogicalExpression scanArg = uf.getArguments().get(0).getValue();
    if (scanArg.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
        return false;
    }
    if (((VariableReferenceExpression) scanArg).getVariableReference() != fVar) {
        return false;
    }
    LogicalVariable uVar = unnest.getVariable();
    GroupByOperator innerGby = (GroupByOperator) opInS;
    Set<LogicalVariable> freeInInnerGby = new HashSet<LogicalVariable>();
    OperatorPropertiesUtil.getFreeVariablesInSubplans(innerGby, freeInInnerGby);
    for (LogicalVariable v : freeInInnerGby) {
        if (v != uVar) {
            return false;
        }
    }
    unnestParent.getInputs().get(0).setValue(innerNts);
    LogicalVariable listifiedVar = ((VariableReferenceExpression) argListify).getVariableReference();
    substInSubplan(aggInner, uVar, listifiedVar, context);
    gby.getNestedPlans().add(p);
    innerNts.getDataSourceReference().setValue(gby);
    opRef.setValue(gby);
    OperatorPropertiesUtil.typePlan(p, context);
    OperatorPropertiesUtil.typePlan(p2, context);
    context.computeAndSetTypeEnvironmentForOperator(gby);
    return true;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) NestedTupleSourceOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.NestedTupleSourceOperator) GroupByOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) SubplanOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SubplanOperator) UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) AggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) HashSet(java.util.HashSet)

Aggregations

AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)138 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)111 Mutable (org.apache.commons.lang3.mutable.Mutable)59 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)54 ArrayList (java.util.ArrayList)50 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)38 IAType (org.apache.asterix.om.types.IAType)37 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)37 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)35 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)35 FunctionIdentifier (org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier)34 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)33 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)25 ARecordType (org.apache.asterix.om.types.ARecordType)20 AsterixConstantValue (org.apache.asterix.om.constants.AsterixConstantValue)19 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)19 Pair (org.apache.hyracks.algebricks.common.utils.Pair)19 MutableObject (org.apache.commons.lang3.mutable.MutableObject)18 AString (org.apache.asterix.om.base.AString)17 MetadataProvider (org.apache.asterix.metadata.declared.MetadataProvider)13