use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans in project asterixdb by apache.
the class OperatorPropertiesUtil method collectUsedAndProducedVariablesInPath.
/**
* @param op
* , the start operator.
* @param dest
* , the destination operator (a direct/indirect input operator).
* @param usedVars
* , the collection of used variables.
* @param producedVars
* , the collection of produced variables.
* @return if the current operator is on the path from the original start operator to the destination operator.
* @throws AlgebricksException
*/
private static boolean collectUsedAndProducedVariablesInPath(ILogicalOperator op, ILogicalOperator dest, Set<LogicalVariable> usedVars, Set<LogicalVariable> producedVars) throws AlgebricksException {
if (op == dest) {
return true;
}
if (((AbstractLogicalOperator) op).hasNestedPlans()) {
AbstractOperatorWithNestedPlans a = (AbstractOperatorWithNestedPlans) op;
for (ILogicalPlan p : a.getNestedPlans()) {
for (Mutable<ILogicalOperator> r : p.getRoots()) {
if (collectUsedAndProducedVariablesInPath(r.getValue(), dest, usedVars, producedVars)) {
VariableUtilities.getUsedVariables(r.getValue(), usedVars);
VariableUtilities.getProducedVariables(r.getValue(), producedVars);
return true;
}
}
}
}
for (Mutable<ILogicalOperator> childRef : op.getInputs()) {
if (collectUsedAndProducedVariablesInPath(childRef.getValue(), dest, usedVars, producedVars)) {
VariableUtilities.getUsedVariables(op, usedVars);
VariableUtilities.getProducedVariables(op, producedVars);
return true;
}
}
return false;
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans in project asterixdb by apache.
the class OperatorPropertiesUtil method computeSchemaAndPropertiesRecIfNull.
public static void computeSchemaAndPropertiesRecIfNull(AbstractLogicalOperator op, IOptimizationContext context) throws AlgebricksException {
if (op.getSchema() == null) {
for (Mutable<ILogicalOperator> i : op.getInputs()) {
computeSchemaAndPropertiesRecIfNull((AbstractLogicalOperator) i.getValue(), context);
}
if (op.hasNestedPlans()) {
AbstractOperatorWithNestedPlans a = (AbstractOperatorWithNestedPlans) op;
for (ILogicalPlan p : a.getNestedPlans()) {
for (Mutable<ILogicalOperator> r : p.getRoots()) {
computeSchemaAndPropertiesRecIfNull((AbstractLogicalOperator) r.getValue(), context);
}
}
}
op.recomputeSchema();
op.computeDeliveredPhysicalProperties(context);
}
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans in project asterixdb by apache.
the class OperatorPropertiesUtil method getFreeVariablesInSelfOrDesc.
/**
* Adds the free variables of the plan rooted at that operator to the
* collection provided.
*
* @param op
* @param vars
* - The collection to which the free variables will be added.
*/
public static void getFreeVariablesInSelfOrDesc(AbstractLogicalOperator op, Set<LogicalVariable> freeVars) throws AlgebricksException {
HashSet<LogicalVariable> produced = new HashSet<>();
VariableUtilities.getProducedVariables(op, produced);
for (LogicalVariable v : produced) {
freeVars.remove(v);
}
HashSet<LogicalVariable> used = new HashSet<>();
VariableUtilities.getUsedVariables(op, used);
for (LogicalVariable v : used) {
freeVars.add(v);
}
if (op.hasNestedPlans()) {
AbstractOperatorWithNestedPlans s = (AbstractOperatorWithNestedPlans) op;
getFreeVariablesInSubplans(s, freeVars);
}
for (Mutable<ILogicalOperator> i : op.getInputs()) {
getFreeVariablesInSelfOrDesc((AbstractLogicalOperator) i.getValue(), freeVars);
}
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans in project asterixdb by apache.
the class PushAggregateIntoNestedSubplanRule method extractAggFunctionsFromExpression.
/**
* @param exprRef
* @param nspWithAgg
* @param context
* @return a pair whose first member is a boolean which is true iff
* something was changed in the expression tree rooted at expr. The
* second member is the result of transforming expr.
* @throws AlgebricksException
*/
private Pair<Boolean, ILogicalExpression> extractAggFunctionsFromExpression(Mutable<ILogicalExpression> exprRef, Map<LogicalVariable, AbstractOperatorWithNestedPlans> nspWithAgg, Map<ILogicalExpression, ILogicalExpression> aggregateExprToVarExpr, IOptimizationContext context) throws AlgebricksException {
ILogicalExpression expr = exprRef.getValue();
switch(expr.getExpressionTag()) {
case FUNCTION_CALL:
AbstractFunctionCallExpression fce = (AbstractFunctionCallExpression) expr;
FunctionIdentifier fi = BuiltinFunctions.getAggregateFunction(fce.getFunctionIdentifier());
if (fi != null) {
ILogicalExpression a1 = fce.getArguments().get(0).getValue();
if (a1.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
LogicalVariable argVar = ((VariableReferenceExpression) a1).getVariableReference();
AbstractOperatorWithNestedPlans nspOp = nspWithAgg.get(argVar);
if (nspOp != null) {
if (!aggregateExprToVarExpr.containsKey(expr)) {
LogicalVariable newVar = context.newVar();
AggregateFunctionCallExpression aggFun = BuiltinFunctions.makeAggregateFunctionExpression(fi, fce.getArguments());
rewriteAggregateInNestedSubplan(argVar, nspOp, aggFun, newVar, context);
ILogicalExpression newVarExpr = new VariableReferenceExpression(newVar);
aggregateExprToVarExpr.put(expr, newVarExpr);
return new Pair<>(Boolean.TRUE, newVarExpr);
} else {
ILogicalExpression varExpr = aggregateExprToVarExpr.get(expr);
return new Pair<>(Boolean.TRUE, varExpr);
}
}
}
}
boolean change = false;
for (Mutable<ILogicalExpression> a : fce.getArguments()) {
Pair<Boolean, ILogicalExpression> aggArg = extractAggFunctionsFromExpression(a, nspWithAgg, aggregateExprToVarExpr, context);
if (aggArg.first.booleanValue()) {
a.setValue(aggArg.second);
change = true;
}
}
return new Pair<>(change, fce);
case VARIABLE:
case CONSTANT:
return new Pair<>(Boolean.FALSE, expr);
default:
throw new IllegalArgumentException();
}
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans in project asterixdb by apache.
the class PushAggregateIntoNestedSubplanRule method removeRedundantListifies.
private void removeRedundantListifies(Map<LogicalVariable, Integer> nspAggVars, Map<LogicalVariable, AbstractOperatorWithNestedPlans> nspWithAgg, Map<LogicalVariable, Integer> nspAggVarToPlanIndex) throws AlgebricksException {
List<Pair<AbstractOperatorWithNestedPlans, Integer>> removeList = new ArrayList<>();
for (Map.Entry<LogicalVariable, Integer> aggVarEntry : nspAggVars.entrySet()) {
LogicalVariable aggVar = aggVarEntry.getKey();
int occurs = aggVarEntry.getValue();
if (occurs == 0) {
AbstractOperatorWithNestedPlans nspOp = nspWithAgg.get(aggVar);
AggregateOperator aggOp = (AggregateOperator) nspOp.getNestedPlans().get(nspAggVarToPlanIndex.get(aggVar)).getRoots().get(0).getValue();
int pos = aggOp.getVariables().indexOf(aggVar);
if (pos >= 0) {
aggOp.getVariables().remove(pos);
aggOp.getExpressions().remove(pos);
List<LogicalVariable> producedVarsAtAgg = new ArrayList<>();
VariableUtilities.getProducedVariablesInDescendantsAndSelf(aggOp, producedVarsAtAgg);
if (producedVarsAtAgg.isEmpty()) {
removeList.add(new Pair<>(nspOp, nspAggVarToPlanIndex.get(aggVar)));
}
}
}
}
// Collects subplans that is to be removed.
Map<AbstractOperatorWithNestedPlans, List<ILogicalPlan>> nspToSubplanListMap = new HashMap<>();
for (Pair<AbstractOperatorWithNestedPlans, Integer> remove : removeList) {
AbstractOperatorWithNestedPlans groupByOperator = remove.first;
ILogicalPlan subplan = remove.first.getNestedPlans().get(remove.second);
if (nspToSubplanListMap.containsKey(groupByOperator)) {
List<ILogicalPlan> subplans = nspToSubplanListMap.get(groupByOperator);
subplans.add(subplan);
} else {
List<ILogicalPlan> subplans = new ArrayList<>();
subplans.add(subplan);
nspToSubplanListMap.put(groupByOperator, subplans);
}
}
// Removes subplans.
for (Map.Entry<AbstractOperatorWithNestedPlans, List<ILogicalPlan>> entry : nspToSubplanListMap.entrySet()) {
entry.getKey().getNestedPlans().removeAll(entry.getValue());
}
}
Aggregations