Search in sources :

Example 56 with LogicalVariable

use of org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable in project asterixdb by apache.

the class LoadRecordFieldsRule method findAndEliminateRedundantFieldAccess.

/**
     * Rewrite
     * assign $x := field-access($y, "field")
     * assign $y := record-constructor { "field": Expr, ... }
     * into
     * assign $x := Expr
     * assign $y := record-constructor { "field": Expr, ... }
     */
private static boolean findAndEliminateRedundantFieldAccess(AssignOperator assign, IOptimizationContext context) throws AlgebricksException {
    ILogicalExpression expr = getFirstExpr(assign);
    AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expr;
    ILogicalExpression arg0 = f.getArguments().get(0).getValue();
    if (arg0.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
        return false;
    }
    VariableReferenceExpression vre = (VariableReferenceExpression) arg0;
    LogicalVariable recordVar = vre.getVariableReference();
    ILogicalExpression arg1 = f.getArguments().get(1).getValue();
    if (arg1.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
        return false;
    }
    IVariableTypeEnvironment typeEnvironment = context.getOutputTypeEnvironment(assign);
    ConstantExpression ce = (ConstantExpression) arg1;
    ILogicalExpression fldExpr;
    if (f.getFunctionIdentifier().equals(BuiltinFunctions.FIELD_ACCESS_BY_NAME)) {
        String fldName = ((AString) ((AsterixConstantValue) ce.getValue()).getObject()).getStringValue();
        fldExpr = findFieldExpression(assign, recordVar, fldName, typeEnvironment, (name, expression, env) -> findFieldByNameFromRecordConstructor(name, expression));
    } else if (f.getFunctionIdentifier().equals(BuiltinFunctions.FIELD_ACCESS_BY_INDEX)) {
        Integer fldIdx = ((AInt32) ((AsterixConstantValue) ce.getValue()).getObject()).getIntegerValue();
        fldExpr = findFieldExpression(assign, recordVar, fldIdx, typeEnvironment, LoadRecordFieldsRule::findFieldByIndexFromRecordConstructor);
    } else if (f.getFunctionIdentifier().equals(BuiltinFunctions.FIELD_ACCESS_NESTED)) {
        return false;
    } else {
        throw new IllegalStateException();
    }
    if (fldExpr == null) {
        return false;
    }
    // check the liveness of the new expression
    List<LogicalVariable> usedVariables = new ArrayList<>();
    fldExpr.getUsedVariables(usedVariables);
    List<LogicalVariable> liveInputVars = new ArrayList<>();
    VariableUtilities.getLiveVariables(assign, liveInputVars);
    usedVariables.removeAll(liveInputVars);
    if (usedVariables.isEmpty()) {
        assign.getExpressions().get(0).setValue(fldExpr);
        return true;
    } else {
        return false;
    }
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) OperatorPropertiesUtil(org.apache.hyracks.algebricks.core.algebra.util.OperatorPropertiesUtil) IOptimizationContext(org.apache.hyracks.algebricks.core.algebra.base.IOptimizationContext) NestedTupleSourceOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.NestedTupleSourceOperator) AbstractOperatorWithNestedPlans(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans) ConstantExpressionUtil(org.apache.asterix.om.utils.ConstantExpressionUtil) LogicalExpressionTag(org.apache.hyracks.algebricks.core.algebra.base.LogicalExpressionTag) AbstractLogicalExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractLogicalExpression) VariableUtilities(org.apache.hyracks.algebricks.core.algebra.operators.logical.visitors.VariableUtilities) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ILogicalExpressionReferenceTransform(org.apache.hyracks.algebricks.core.algebra.visitors.ILogicalExpressionReferenceTransform) OperatorAnnotation(org.apache.asterix.algebra.base.OperatorAnnotation) ARecordType(org.apache.asterix.om.types.ARecordType) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) AlgebricksBuiltinFunctions(org.apache.hyracks.algebricks.core.algebra.functions.AlgebricksBuiltinFunctions) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) IAlgebraicRewriteRule(org.apache.hyracks.algebricks.core.rewriter.base.IAlgebraicRewriteRule) FunctionalDependency(org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency) LinkedList(java.util.LinkedList) BuiltinFunctions(org.apache.asterix.om.functions.BuiltinFunctions) MutableObject(org.apache.commons.lang3.mutable.MutableObject) LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AString(org.apache.asterix.om.base.AString) Iterator(java.util.Iterator) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) LogicalOperatorTag(org.apache.hyracks.algebricks.core.algebra.base.LogicalOperatorTag) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AInt32(org.apache.asterix.om.base.AInt32) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) List(java.util.List) AnalysisUtil(org.apache.asterix.optimizer.base.AnalysisUtil) IVariableTypeEnvironment(org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) SelectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator) Mutable(org.apache.commons.lang3.mutable.Mutable) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ArrayList(java.util.ArrayList) AString(org.apache.asterix.om.base.AString) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) AString(org.apache.asterix.om.base.AString) IVariableTypeEnvironment(org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment)

Example 57 with LogicalVariable

use of org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable in project asterixdb by apache.

the class PushGroupByThroughProduct method canPushThrough.

private PushTestResult canPushThrough(GroupByOperator gby, ILogicalOperator branch, List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> toPush, List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> notToPush) throws AlgebricksException {
    Collection<LogicalVariable> fromBranch = new HashSet<LogicalVariable>();
    VariableUtilities.getLiveVariables(branch, fromBranch);
    Collection<LogicalVariable> usedInGbyExprList = new ArrayList<LogicalVariable>();
    for (Pair<LogicalVariable, Mutable<ILogicalExpression>> p : gby.getGroupByList()) {
        p.second.getValue().getUsedVariables(usedInGbyExprList);
    }
    if (!fromBranch.containsAll(usedInGbyExprList)) {
        return PushTestResult.FALSE;
    }
    Set<LogicalVariable> free = new HashSet<LogicalVariable>();
    for (ILogicalPlan p : gby.getNestedPlans()) {
        for (Mutable<ILogicalOperator> r : p.getRoots()) {
            OperatorPropertiesUtil.getFreeVariablesInSelfOrDesc((AbstractLogicalOperator) r.getValue(), free);
        }
    }
    if (!fromBranch.containsAll(free)) {
        return PushTestResult.FALSE;
    }
    Set<LogicalVariable> decorVarRhs = new HashSet<LogicalVariable>();
    decorVarRhs.clear();
    for (Pair<LogicalVariable, Mutable<ILogicalExpression>> p : gby.getDecorList()) {
        ILogicalExpression expr = p.second.getValue();
        if (expr.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
            return PushTestResult.FALSE;
        }
        VariableReferenceExpression varRef = (VariableReferenceExpression) expr;
        LogicalVariable v = varRef.getVariableReference();
        if (decorVarRhs.contains(v)) {
            return PushTestResult.REPEATED_DECORS;
        }
        decorVarRhs.add(v);
        if (fromBranch.contains(v)) {
            toPush.add(p);
        } else {
            notToPush.add(p);
        }
    }
    return PushTestResult.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) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) HashSet(java.util.HashSet)

Example 58 with LogicalVariable

use of org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable 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 59 with LogicalVariable

use of org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable 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 60 with LogicalVariable

use of org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable in project asterixdb by apache.

the class AddEquivalenceClassForRecordConstructorRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    if (op.getOperatorTag() != LogicalOperatorTag.ASSIGN) {
        return false;
    }
    // Computes FDs and equivalence classes for the operator.
    PhysicalOptimizationsUtil.computeFDsAndEquivalenceClasses(op, context);
    AssignOperator assignOp = (AssignOperator) op;
    List<LogicalVariable> vars = assignOp.getVariables();
    List<Mutable<ILogicalExpression>> exprRefs = assignOp.getExpressions();
    return addEquivalenceClassesForRecordConstructor(vars, exprRefs, assignOp, context);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) Mutable(org.apache.commons.lang3.mutable.Mutable) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)

Aggregations

LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)376 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)196 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)182 ArrayList (java.util.ArrayList)171 Mutable (org.apache.commons.lang3.mutable.Mutable)136 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)127 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)92 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)79 Pair (org.apache.hyracks.algebricks.common.utils.Pair)75 HashSet (java.util.HashSet)60 MutableObject (org.apache.commons.lang3.mutable.MutableObject)60 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)60 ILogicalPlan (org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)54 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)46 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)36 GroupByOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator)33 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)32 AggregateOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator)28 FunctionalDependency (org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency)28 IAType (org.apache.asterix.om.types.IAType)27