Search in sources :

Example 1 with StatefulFunctionCallExpression

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

the class RemoveRedundantListifyRule method applies.

private boolean applies(Mutable<ILogicalOperator> opRef, Set<LogicalVariable> varUsedAbove, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
    if (op1.getOperatorTag() != LogicalOperatorTag.UNNEST) {
        return false;
    }
    UnnestOperator unnest1 = (UnnestOperator) op1;
    ILogicalExpression expr = unnest1.getExpressionRef().getValue();
    if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        return false;
    }
    if (((AbstractFunctionCallExpression) expr).getFunctionIdentifier() != BuiltinFunctions.SCAN_COLLECTION) {
        return false;
    }
    AbstractFunctionCallExpression functionCall = (AbstractFunctionCallExpression) expr;
    ILogicalExpression functionCallArgExpr = functionCall.getArguments().get(0).getValue();
    if (functionCallArgExpr.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
        return false;
    }
    LogicalVariable unnestedVar = ((VariableReferenceExpression) functionCallArgExpr).getVariableReference();
    if (varUsedAbove.contains(unnestedVar)) {
        return false;
    }
    Mutable<ILogicalOperator> aggregateParentRef = opRef;
    AbstractLogicalOperator r = op1;
    boolean metAggregate = false;
    while (r.getInputs().size() == 1) {
        aggregateParentRef = r.getInputs().get(0);
        r = (AbstractLogicalOperator) aggregateParentRef.getValue();
        if (r.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
            AssignOperator assign = (AssignOperator) r;
            List<LogicalVariable> variables = assign.getVariables();
            // The assign operator doesn't produce any variable that is used by the unnest.
            if (variables.contains(unnestedVar)) {
                return false;
            }
        } else {
            if (r.getOperatorTag() == LogicalOperatorTag.AGGREGATE) {
                metAggregate = true;
            }
            break;
        }
    }
    if (!metAggregate) {
        return false;
    }
    AggregateOperator agg = (AggregateOperator) r;
    if (agg.getVariables().size() > 1) {
        return false;
    }
    LogicalVariable aggVar = agg.getVariables().get(0);
    ILogicalExpression aggFun = agg.getExpressions().get(0).getValue();
    if (!aggVar.equals(unnestedVar) || ((AbstractLogicalExpression) aggFun).getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
        return false;
    }
    AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) aggFun;
    if (!BuiltinFunctions.LISTIFY.equals(f.getFunctionIdentifier())) {
        return false;
    }
    if (f.getArguments().size() != 1) {
        return false;
    }
    ILogicalExpression arg0 = f.getArguments().get(0).getValue();
    if (((AbstractLogicalExpression) arg0).getExpressionTag() != LogicalExpressionTag.VARIABLE) {
        return false;
    }
    LogicalVariable paramVar = ((VariableReferenceExpression) arg0).getVariableReference();
    List<LogicalVariable> assgnVars = new ArrayList<>(1);
    assgnVars.add(unnest1.getVariable());
    List<Mutable<ILogicalExpression>> assgnExprs = new ArrayList<>(1);
    assgnExprs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(paramVar)));
    AssignOperator assign = new AssignOperator(assgnVars, assgnExprs);
    assign.getInputs().add(agg.getInputs().get(0));
    context.computeAndSetTypeEnvironmentForOperator(assign);
    LogicalVariable posVar = unnest1.getPositionalVariable();
    if (posVar == null) {
        // Removes the aggregate operator.
        aggregateParentRef.setValue(assign);
    } else {
        List<LogicalVariable> raggVars = new ArrayList<>(1);
        raggVars.add(posVar);
        List<Mutable<ILogicalExpression>> rAggExprs = new ArrayList<>(1);
        StatefulFunctionCallExpression tidFun = new StatefulFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.TID), UnpartitionedPropertyComputer.INSTANCE);
        rAggExprs.add(new MutableObject<ILogicalExpression>(tidFun));
        RunningAggregateOperator rAgg = new RunningAggregateOperator(raggVars, rAggExprs);
        rAgg.getInputs().add(new MutableObject<ILogicalOperator>(assign));
        aggregateParentRef.setValue(rAgg);
        context.computeAndSetTypeEnvironmentForOperator(rAgg);
    }
    // Removes the unnest operator.
    opRef.setValue(unnest1.getInputs().get(0).getValue());
    return true;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) AbstractLogicalExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractLogicalExpression) StatefulFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.StatefulFunctionCallExpression) 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) UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator) 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) AggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator) RunningAggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.RunningAggregateOperator) RunningAggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.RunningAggregateOperator)

Example 2 with StatefulFunctionCallExpression

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

the class LogicalExpressionDeepCopyWithNewVariablesVisitor method visitStatefulFunctionCallExpression.

@Override
public ILogicalExpression visitStatefulFunctionCallExpression(StatefulFunctionCallExpression expr, Void arg) throws AlgebricksException {
    StatefulFunctionCallExpression exprCopy = new StatefulFunctionCallExpression(expr.getFunctionInfo(), expr.getPropertiesComputer(), deepCopyExpressionReferenceList(expr.getArguments()));
    deepCopyAnnotations(expr, exprCopy);
    deepCopyOpaqueParameters(expr, exprCopy);
    return exprCopy;
}
Also used : StatefulFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.StatefulFunctionCallExpression)

Example 3 with StatefulFunctionCallExpression

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

the class PullPositionalVariableFromUnnestRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
    if (op.getOperatorTag() != LogicalOperatorTag.UNNEST) {
        return false;
    }
    UnnestOperator unnest = (UnnestOperator) op;
    LogicalVariable p = unnest.getPositionalVariable();
    if (p == null) {
        return false;
    }
    ArrayList<LogicalVariable> rOpVars = new ArrayList<LogicalVariable>();
    rOpVars.add(p);
    ArrayList<Mutable<ILogicalExpression>> rOpExprList = new ArrayList<Mutable<ILogicalExpression>>();
    StatefulFunctionCallExpression fce = new StatefulFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.TID), UnpartitionedPropertyComputer.INSTANCE);
    rOpExprList.add(new MutableObject<ILogicalExpression>(fce));
    RunningAggregateOperator rOp = new RunningAggregateOperator(rOpVars, rOpExprList);
    rOp.setExecutionMode(unnest.getExecutionMode());
    RunningAggregatePOperator rPop = new RunningAggregatePOperator();
    rOp.setPhysicalOperator(rPop);
    rOp.getInputs().add(new MutableObject<ILogicalOperator>(unnest));
    opRef.setValue(rOp);
    unnest.setPositionalVariable(null);
    context.computeAndSetTypeEnvironmentForOperator(rOp);
    context.computeAndSetTypeEnvironmentForOperator(unnest);
    return true;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) StatefulFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.StatefulFunctionCallExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) RunningAggregatePOperator(org.apache.hyracks.algebricks.core.algebra.operators.physical.RunningAggregatePOperator) UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) RunningAggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.RunningAggregateOperator)

Example 4 with StatefulFunctionCallExpression

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

the class EquivalenceClassUtils method findOrCreatePrimaryKeyOpAndVariables.

/**
     * Find the header variables that can imply all subplan-local live variables at <code>operator</code>.
     *
     * @param operator
     *            the operator of interest.
     * @param usedForCorrelationJoin
     *            whether the generated primary key will be used for a join that recovers the correlation.
     * @param context
     *            the optimization context.
     * @return Pair<ILogicalOperator, Set<LogicalVariable>>, an operator (which is either the original parameter
     *         <code>operator</code> or a newly created operator) and
     *         a set of primary key variables at the operator.
     * @throws AlgebricksException
     */
public static Pair<ILogicalOperator, Set<LogicalVariable>> findOrCreatePrimaryKeyOpAndVariables(ILogicalOperator operator, boolean usedForCorrelationJoin, IOptimizationContext context) throws AlgebricksException {
    computePrimaryKeys(operator, context);
    Set<LogicalVariable> liveVars = new HashSet<>();
    VariableUtilities.getSubplanLocalLiveVariables(operator, liveVars);
    Set<LogicalVariable> primaryKeyVars = new HashSet<>();
    Set<LogicalVariable> noKeyVars = new HashSet<>();
    for (LogicalVariable liveVar : liveVars) {
        List<LogicalVariable> keyVars = context.findPrimaryKey(liveVar);
        if (keyVars != null) {
            keyVars.retainAll(liveVars);
        }
        if ((keyVars == null || keyVars.isEmpty())) {
            noKeyVars.add(liveVar);
        } else {
            primaryKeyVars.addAll(keyVars);
        }
    }
    primaryKeyVars.retainAll(liveVars);
    if (primaryKeyVars.containsAll(noKeyVars)) {
        return new Pair<ILogicalOperator, Set<LogicalVariable>>(operator, primaryKeyVars);
    } else {
        LogicalVariable assignVar = context.newVar();
        ILogicalOperator assignOp = new AssignOperator(assignVar, new MutableObject<ILogicalExpression>(new StatefulFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.CREATE_QUERY_UID), null)));
        OperatorPropertiesUtil.markMovable(assignOp, !usedForCorrelationJoin);
        assignOp.getInputs().add(new MutableObject<ILogicalOperator>(operator));
        context.addPrimaryKey(new FunctionalDependency(Collections.singletonList(assignVar), new ArrayList<LogicalVariable>(liveVars)));
        context.computeAndSetTypeEnvironmentForOperator(assignOp);
        return new Pair<ILogicalOperator, Set<LogicalVariable>>(assignOp, Collections.singleton(assignVar));
    }
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) StatefulFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.StatefulFunctionCallExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) FunctionalDependency(org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency) HashSet(java.util.HashSet) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 5 with StatefulFunctionCallExpression

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

the class RunningAggregatePOperator method contributeRuntimeOperator.

@Override
public void contributeRuntimeOperator(IHyracksJobBuilder builder, JobGenContext context, ILogicalOperator op, IOperatorSchema opSchema, IOperatorSchema[] inputSchemas, IOperatorSchema outerPlanSchema) throws AlgebricksException {
    RunningAggregateOperator ragg = (RunningAggregateOperator) op;
    List<LogicalVariable> variables = ragg.getVariables();
    List<Mutable<ILogicalExpression>> expressions = ragg.getExpressions();
    int[] outColumns = new int[variables.size()];
    for (int i = 0; i < outColumns.length; i++) {
        outColumns[i] = opSchema.findVariable(variables.get(i));
    }
    IRunningAggregateEvaluatorFactory[] runningAggFuns = new IRunningAggregateEvaluatorFactory[expressions.size()];
    IExpressionRuntimeProvider expressionRuntimeProvider = context.getExpressionRuntimeProvider();
    for (int i = 0; i < runningAggFuns.length; i++) {
        StatefulFunctionCallExpression expr = (StatefulFunctionCallExpression) expressions.get(i).getValue();
        runningAggFuns[i] = expressionRuntimeProvider.createRunningAggregateFunctionFactory(expr, context.getTypeEnvironment(op.getInputs().get(0).getValue()), inputSchemas, context);
    }
    // TODO push projections into the operator
    int[] projectionList = JobGenHelper.projectAllVariables(opSchema);
    RunningAggregateRuntimeFactory runtime = new RunningAggregateRuntimeFactory(outColumns, runningAggFuns, projectionList);
    // contribute one Asterix framewriter
    RecordDescriptor recDesc = JobGenHelper.mkRecordDescriptor(context.getTypeEnvironment(op), opSchema, context);
    builder.contributeMicroOperator(ragg, runtime, recDesc);
    // and contribute one edge from its child
    ILogicalOperator src = ragg.getInputs().get(0).getValue();
    builder.contributeGraphEdge(src, 0, ragg, 0);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) RunningAggregateRuntimeFactory(org.apache.hyracks.algebricks.runtime.operators.std.RunningAggregateRuntimeFactory) IRunningAggregateEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IRunningAggregateEvaluatorFactory) StatefulFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.StatefulFunctionCallExpression) RecordDescriptor(org.apache.hyracks.api.dataflow.value.RecordDescriptor) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) Mutable(org.apache.commons.lang3.mutable.Mutable) IExpressionRuntimeProvider(org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionRuntimeProvider) RunningAggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.RunningAggregateOperator)

Aggregations

StatefulFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.StatefulFunctionCallExpression)6 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)5 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)5 ArrayList (java.util.ArrayList)4 Mutable (org.apache.commons.lang3.mutable.Mutable)4 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)4 RunningAggregateOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.RunningAggregateOperator)4 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)3 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)3 UnnestOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator)3 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)2 AbstractLogicalExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractLogicalExpression)2 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)2 AggregateOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator)2 RunningAggregatePOperator (org.apache.hyracks.algebricks.core.algebra.operators.physical.RunningAggregatePOperator)2 HashSet (java.util.HashSet)1 Pair (org.apache.hyracks.algebricks.common.utils.Pair)1 IExpressionRuntimeProvider (org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionRuntimeProvider)1 GroupByOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator)1 FunctionalDependency (org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency)1