Search in sources :

Example 76 with AbstractFunctionCallExpression

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

the class MetaKeyExpressionReferenceTransform method transform.

@Override
public boolean transform(Mutable<ILogicalExpression> exprRef) throws AlgebricksException {
    ILogicalExpression expr = exprRef.getValue();
    if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        return false;
    }
    AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr;
    if (!funcExpr.getFunctionIdentifier().equals(BuiltinFunctions.META_KEY)) {
        return false;
    }
    // Function is meta key access
    for (int i = 0; i < metaKeyAccessExpressions.size(); i++) {
        if (metaKeyAccessExpressions.get(i).equals(funcExpr)) {
            exprRef.setValue(new VariableReferenceExpression(keyVars.get(i)));
            return true;
        }
    }
    return false;
}
Also used : ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)

Example 77 with AbstractFunctionCallExpression

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

the class PushAggFuncIntoStandaloneAggregateRule method containsAggregate.

/**
     * Recursively check whether the list of expressions contains an aggregate function.
     *
     * @param exprRefs
     * @return true if the list contains an aggregate function and false otherwise.
     */
private boolean containsAggregate(List<Mutable<ILogicalExpression>> exprRefs) {
    for (Mutable<ILogicalExpression> exprRef : exprRefs) {
        ILogicalExpression expr = exprRef.getValue();
        if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
            continue;
        }
        AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr;
        FunctionIdentifier funcIdent = BuiltinFunctions.getAggregateFunction(funcExpr.getFunctionIdentifier());
        if (funcIdent == null) {
            // Recursively look in func args.
            if (containsAggregate(funcExpr.getArguments())) {
                return true;
            }
        } else {
            // This is an aggregation function.
            return true;
        }
    }
    return false;
}
Also used : FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)

Example 78 with AbstractFunctionCallExpression

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

the class PushAggFuncIntoStandaloneAggregateRule method fingAggFuncExprRef.

private boolean fingAggFuncExprRef(List<Mutable<ILogicalExpression>> exprRefs, LogicalVariable aggVar, List<Mutable<ILogicalExpression>> srcAssignExprRefs) {
    for (Mutable<ILogicalExpression> exprRef : exprRefs) {
        ILogicalExpression expr = exprRef.getValue();
        if (expr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
            if (((VariableReferenceExpression) expr).getVariableReference().equals(aggVar)) {
                return false;
            }
        }
        if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
            continue;
        }
        AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr;
        FunctionIdentifier funcIdent = BuiltinFunctions.getAggregateFunction(funcExpr.getFunctionIdentifier());
        if (funcIdent == null) {
            // Recursively look in func args.
            if (fingAggFuncExprRef(funcExpr.getArguments(), aggVar, srcAssignExprRefs) == false) {
                return false;
            }
        } else {
            // Check if this is the expr that uses aggVar.
            Collection<LogicalVariable> usedVars = new HashSet<LogicalVariable>();
            funcExpr.getUsedVariables(usedVars);
            if (usedVars.contains(aggVar)) {
                srcAssignExprRefs.add(exprRef);
            }
        }
    }
    return true;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) HashSet(java.util.HashSet)

Example 79 with AbstractFunctionCallExpression

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

the class InlineUnnestFunctionRule method inlineVariable.

/**
     * This method is to inline one variable
     *
     * @param usedVar
     *            A variable that is used by the scan-collection function in the unnest operator
     * @param unnestOp
     *            The unnest operator.
     * @throws AlgebricksException
     */
private void inlineVariable(LogicalVariable usedVar, UnnestOperator unnestOp) throws AlgebricksException {
    AbstractFunctionCallExpression expr = (AbstractFunctionCallExpression) unnestOp.getExpressionRef().getValue();
    List<Pair<AbstractFunctionCallExpression, Integer>> parentAndIndexList = new ArrayList<Pair<AbstractFunctionCallExpression, Integer>>();
    getParentFunctionExpression(usedVar, expr, parentAndIndexList);
    ILogicalExpression usedVarOrginExpr = findUsedVarOrigin(usedVar, unnestOp, (AbstractLogicalOperator) unnestOp.getInputs().get(0).getValue());
    if (usedVarOrginExpr != null) {
        for (Pair<AbstractFunctionCallExpression, Integer> parentAndIndex : parentAndIndexList) {
            //we only rewrite the top scan-collection function
            if (parentAndIndex.first.getFunctionIdentifier() == BuiltinFunctions.SCAN_COLLECTION && parentAndIndex.first == expr) {
                unnestOp.getExpressionRef().setValue(usedVarOrginExpr);
            }
        }
    }
}
Also used : ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ArrayList(java.util.ArrayList) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 80 with AbstractFunctionCallExpression

use of org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression 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

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