Search in sources :

Example 56 with VariableReferenceExpression

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

the class InvertedIndexAccessMethod method analyzeGetItemFuncExpr.

public boolean analyzeGetItemFuncExpr(AbstractFunctionCallExpression funcExpr, List<AbstractLogicalOperator> assignsAndUnnests, AccessMethodAnalysisContext analysisCtx) throws AlgebricksException {
    if (funcExpr.getFunctionIdentifier() != BuiltinFunctions.GET_ITEM) {
        return false;
    }
    ILogicalExpression arg1 = funcExpr.getArguments().get(0).getValue();
    ILogicalExpression arg2 = funcExpr.getArguments().get(1).getValue();
    // The second arg is the item index to be accessed. It must be a constant.
    if (arg2.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
        return false;
    }
    // If it is a variable we must track its origin in the assigns to get the original function expr.
    if (arg1.getExpressionTag() != LogicalExpressionTag.VARIABLE && arg1.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        return false;
    }
    AbstractFunctionCallExpression matchedFuncExpr = null;
    // The get-item arg is function call, directly check if it's optimizable.
    if (arg1.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
        matchedFuncExpr = (AbstractFunctionCallExpression) arg1;
    }
    // The get-item arg is a variable. Search the assigns and unnests for its origination function.
    int matchedAssignOrUnnestIndex = -1;
    if (arg1.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
        VariableReferenceExpression varRefExpr = (VariableReferenceExpression) arg1;
        // Try to find variable ref expr in all assigns.
        for (int i = 0; i < assignsAndUnnests.size(); i++) {
            AbstractLogicalOperator op = assignsAndUnnests.get(i);
            if (op.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
                AssignOperator assign = (AssignOperator) op;
                List<LogicalVariable> assignVars = assign.getVariables();
                List<Mutable<ILogicalExpression>> assignExprs = assign.getExpressions();
                for (int j = 0; j < assignVars.size(); j++) {
                    LogicalVariable var = assignVars.get(j);
                    if (var != varRefExpr.getVariableReference()) {
                        continue;
                    }
                    // We've matched the variable in the first assign. Now analyze the originating function.
                    ILogicalExpression matchedExpr = assignExprs.get(j).getValue();
                    if (matchedExpr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
                        return false;
                    }
                    matchedFuncExpr = (AbstractFunctionCallExpression) matchedExpr;
                    break;
                }
            } else {
                UnnestOperator unnest = (UnnestOperator) op;
                LogicalVariable var = unnest.getVariable();
                if (var == varRefExpr.getVariableReference()) {
                    ILogicalExpression matchedExpr = unnest.getExpressionRef().getValue();
                    if (matchedExpr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
                        return false;
                    }
                    AbstractFunctionCallExpression unnestFuncExpr = (AbstractFunctionCallExpression) matchedExpr;
                    if (unnestFuncExpr.getFunctionIdentifier() != BuiltinFunctions.SCAN_COLLECTION) {
                        return false;
                    }
                    matchedFuncExpr = (AbstractFunctionCallExpression) unnestFuncExpr.getArguments().get(0).getValue();
                }
            }
            // We've already found a match.
            if (matchedFuncExpr != null) {
                matchedAssignOrUnnestIndex = i;
                break;
            }
        }
    }
    // Check that the matched function is optimizable by this access method.
    if (!secondLevelFuncIdents.contains(matchedFuncExpr.getFunctionIdentifier())) {
        return false;
    }
    boolean selectMatchFound = analyzeSelectSimilarityCheckFuncExprArgs(matchedFuncExpr, assignsAndUnnests, matchedAssignOrUnnestIndex, analysisCtx);
    boolean joinMatchFound = analyzeJoinSimilarityCheckFuncExprArgs(matchedFuncExpr, assignsAndUnnests, matchedAssignOrUnnestIndex, analysisCtx);
    if (selectMatchFound || joinMatchFound) {
        return true;
    }
    return false;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) Mutable(org.apache.commons.lang3.mutable.Mutable) UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator) 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) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)

Example 57 with VariableReferenceExpression

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

the class InvertedIndexAccessMethod method createPrimaryKeysEqJoinCondition.

private Mutable<ILogicalExpression> createPrimaryKeysEqJoinCondition(List<LogicalVariable> originalSubTreePKs, List<LogicalVariable> surrogateSubTreePKs) {
    List<Mutable<ILogicalExpression>> eqExprs = new ArrayList<Mutable<ILogicalExpression>>();
    int numPKVars = originalSubTreePKs.size();
    for (int i = 0; i < numPKVars; i++) {
        List<Mutable<ILogicalExpression>> args = new ArrayList<Mutable<ILogicalExpression>>();
        args.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(surrogateSubTreePKs.get(i))));
        args.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(originalSubTreePKs.get(i))));
        ILogicalExpression eqFunc = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(AlgebricksBuiltinFunctions.EQ), args);
        eqExprs.add(new MutableObject<ILogicalExpression>(eqFunc));
    }
    if (eqExprs.size() == 1) {
        return eqExprs.get(0);
    } else {
        ILogicalExpression andFunc = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(AlgebricksBuiltinFunctions.AND), eqExprs);
        return new MutableObject<ILogicalExpression>(andFunc);
    }
}
Also used : 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) ArrayList(java.util.ArrayList) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Example 58 with VariableReferenceExpression

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

the class AssignOperator method computeOutputTypeEnvironment.

@Override
public IVariableTypeEnvironment computeOutputTypeEnvironment(ITypingContext ctx) throws AlgebricksException {
    PropagatingTypeEnvironment env = createPropagatingAllInputsTypeEnvironment(ctx);
    int n = variables.size();
    for (int i = 0; i < n; i++) {
        env.setVarType(variables.get(i), ctx.getExpressionTypeComputer().getType(expressions.get(i).getValue(), ctx.getMetadataProvider(), env));
        if (expressions.get(i).getValue().getExpressionTag() == LogicalExpressionTag.VARIABLE) {
            LogicalVariable var = ((VariableReferenceExpression) expressions.get(i).getValue()).getVariableReference();
            for (List<LogicalVariable> list : env.getCorrelatedMissableVariableLists()) {
                if (list.contains(var)) {
                    list.add(variables.get(i));
                }
            }
        }
    }
    return env;
}
Also used : PropagatingTypeEnvironment(org.apache.hyracks.algebricks.core.algebra.typing.PropagatingTypeEnvironment) LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)

Example 59 with VariableReferenceExpression

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

the class FDsAndEquivClassesVisitor method visitDistinctOperator.

@Override
public Void visitDistinctOperator(DistinctOperator op, IOptimizationContext ctx) throws AlgebricksException {
    ILogicalOperator op0 = op.getInputs().get(0).getValue();
    List<FunctionalDependency> functionalDependencies = new ArrayList<FunctionalDependency>();
    ctx.putFDList(op, functionalDependencies);
    for (FunctionalDependency inherited : getOrComputeFDs(op0, ctx)) {
        boolean isCoveredByDistinctByVars = true;
        for (LogicalVariable v : inherited.getHead()) {
            if (!op.isDistinctByVar(v)) {
                isCoveredByDistinctByVars = false;
            }
        }
        if (isCoveredByDistinctByVars) {
            List<LogicalVariable> newTail = new ArrayList<LogicalVariable>();
            for (LogicalVariable v2 : inherited.getTail()) {
                if (op.isDistinctByVar(v2)) {
                    newTail.add(v2);
                }
            }
            if (!newTail.isEmpty()) {
                List<LogicalVariable> newHead = new ArrayList<LogicalVariable>(inherited.getHead());
                FunctionalDependency newFd = new FunctionalDependency(newHead, newTail);
                functionalDependencies.add(newFd);
            }
        }
    }
    Set<LogicalVariable> gbySet = new HashSet<LogicalVariable>();
    List<Mutable<ILogicalExpression>> expressions = op.getExpressions();
    for (Mutable<ILogicalExpression> pRef : expressions) {
        ILogicalExpression p = pRef.getValue();
        if (p.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
            VariableReferenceExpression v = (VariableReferenceExpression) p;
            gbySet.add(v.getVariableReference());
        }
    }
    LocalGroupingProperty lgp = new LocalGroupingProperty(gbySet);
    Map<LogicalVariable, EquivalenceClass> equivalenceClasses = getOrComputeEqClasses(op0, ctx);
    ctx.putEquivalenceClassMap(op, equivalenceClasses);
    ILocalStructuralProperty normalizedLgp = lgp.normalize(equivalenceClasses, functionalDependencies);
    Set<LogicalVariable> normSet = new ListSet<>();
    normalizedLgp.getColumns(normSet);
    List<Mutable<ILogicalExpression>> newDistinctByList = new ArrayList<Mutable<ILogicalExpression>>();
    for (Mutable<ILogicalExpression> p2Ref : expressions) {
        ILogicalExpression p2 = p2Ref.getValue();
        if (p2.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
            VariableReferenceExpression var2 = (VariableReferenceExpression) p2;
            LogicalVariable v2 = var2.getVariableReference();
            if (normSet.contains(v2)) {
                newDistinctByList.add(p2Ref);
            }
        } else {
            newDistinctByList.add(p2Ref);
        }
    }
    expressions.clear();
    expressions.addAll(newDistinctByList);
    return null;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) LocalGroupingProperty(org.apache.hyracks.algebricks.core.algebra.properties.LocalGroupingProperty) ArrayList(java.util.ArrayList) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) ListSet(org.apache.hyracks.algebricks.common.utils.ListSet) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) FunctionalDependency(org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency) EquivalenceClass(org.apache.hyracks.algebricks.core.algebra.base.EquivalenceClass) ILocalStructuralProperty(org.apache.hyracks.algebricks.core.algebra.properties.ILocalStructuralProperty) HashSet(java.util.HashSet)

Example 60 with VariableReferenceExpression

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

the class DistinctOperator method getDistinctByVarList.

public List<LogicalVariable> getDistinctByVarList() {
    List<LogicalVariable> varList = new ArrayList<LogicalVariable>(expressions.size());
    for (Mutable<ILogicalExpression> eRef : expressions) {
        ILogicalExpression e = eRef.getValue();
        if (e.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
            VariableReferenceExpression v = (VariableReferenceExpression) e;
            varList.add(v.getVariableReference());
        }
    }
    return varList;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) ArrayList(java.util.ArrayList)

Aggregations

VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)128 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)117 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)111 Mutable (org.apache.commons.lang3.mutable.Mutable)87 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)74 ArrayList (java.util.ArrayList)67 MutableObject (org.apache.commons.lang3.mutable.MutableObject)48 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)46 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)36 Pair (org.apache.hyracks.algebricks.common.utils.Pair)34 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)34 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)33 ILogicalPlan (org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)24 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)22 GroupByOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator)19 List (java.util.List)18 AsterixConstantValue (org.apache.asterix.om.constants.AsterixConstantValue)18 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)18 HashSet (java.util.HashSet)17 AggregateFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression)17