use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator in project asterixdb by apache.
the class PushAssignBelowUnionAllRule method rewritePost.
@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
if (!op.hasInputs()) {
return false;
}
boolean modified = false;
for (int i = 0; i < op.getInputs().size(); i++) {
AbstractLogicalOperator childOp = (AbstractLogicalOperator) op.getInputs().get(i).getValue();
if (childOp.getOperatorTag() != LogicalOperatorTag.ASSIGN) {
continue;
}
AssignOperator assignOp = (AssignOperator) childOp;
for (Mutable<ILogicalExpression> expr : assignOp.getExpressions()) {
if (!expr.getValue().isFunctional()) {
return false;
}
}
AbstractLogicalOperator childOfChildOp = (AbstractLogicalOperator) assignOp.getInputs().get(0).getValue();
if (childOfChildOp.getOperatorTag() != LogicalOperatorTag.UNIONALL) {
continue;
}
UnionAllOperator unionOp = (UnionAllOperator) childOfChildOp;
Set<LogicalVariable> assignUsedVars = new HashSet<LogicalVariable>();
VariableUtilities.getUsedVariables(assignOp, assignUsedVars);
List<LogicalVariable> assignVars = assignOp.getVariables();
AssignOperator[] newAssignOps = new AssignOperator[2];
for (int j = 0; j < unionOp.getInputs().size(); j++) {
newAssignOps[j] = createAssignBelowUnionAllBranch(unionOp, j, assignOp, assignUsedVars, context);
}
// Add original assign variables to the union variable mappings.
for (int j = 0; j < assignVars.size(); j++) {
LogicalVariable first = newAssignOps[0].getVariables().get(j);
LogicalVariable second = newAssignOps[1].getVariables().get(j);
Triple<LogicalVariable, LogicalVariable, LogicalVariable> varMapping = new Triple<LogicalVariable, LogicalVariable, LogicalVariable>(first, second, assignVars.get(j));
unionOp.getVariableMappings().add(varMapping);
}
context.computeAndSetTypeEnvironmentForOperator(unionOp);
// Remove original assign operator.
op.getInputs().set(i, assignOp.getInputs().get(0));
context.computeAndSetTypeEnvironmentForOperator(op);
modified = true;
}
return modified;
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator in project asterixdb by apache.
the class PushAssignBelowUnionAllRule method cloneAssignOperator.
/**
* Clones the given assign operator changing the returned variables to be new ones.
* Also, leaves the inputs of the clone clear.
*/
private AssignOperator cloneAssignOperator(AssignOperator assignOp, IOptimizationContext context) {
List<LogicalVariable> vars = new ArrayList<LogicalVariable>();
List<Mutable<ILogicalExpression>> exprs = new ArrayList<Mutable<ILogicalExpression>>();
int numVars = assignOp.getVariables().size();
for (int i = 0; i < numVars; i++) {
vars.add(context.newVar());
exprs.add(new MutableObject<ILogicalExpression>(assignOp.getExpressions().get(i).getValue().cloneExpression()));
}
AssignOperator assignCloneOp = new AssignOperator(vars, exprs);
assignCloneOp.setExecutionMode(assignOp.getExecutionMode());
return assignCloneOp;
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator in project asterixdb by apache.
the class PushFunctionsBelowJoin method pushDownFunctions.
private boolean pushDownFunctions(AbstractBinaryJoinOperator joinOp, int inputIndex, List<Mutable<ILogicalExpression>> funcExprs, IOptimizationContext context) throws AlgebricksException {
ILogicalOperator joinInputOp = joinOp.getInputs().get(inputIndex).getValue();
liveVars.clear();
VariableUtilities.getLiveVariables(joinInputOp, liveVars);
Iterator<Mutable<ILogicalExpression>> funcIter = funcExprs.iterator();
List<LogicalVariable> assignVars = null;
List<Mutable<ILogicalExpression>> assignExprs = null;
while (funcIter.hasNext()) {
Mutable<ILogicalExpression> funcExprRef = funcIter.next();
ILogicalExpression funcExpr = funcExprRef.getValue();
usedVars.clear();
funcExpr.getUsedVariables(usedVars);
// Check if we can push the function down this branch.
if (liveVars.containsAll(usedVars)) {
if (assignVars == null) {
assignVars = new ArrayList<LogicalVariable>();
assignExprs = new ArrayList<Mutable<ILogicalExpression>>();
}
// Replace the original expression with a variable reference expression.
LogicalVariable replacementVar = context.newVar();
assignVars.add(replacementVar);
assignExprs.add(new MutableObject<ILogicalExpression>(funcExpr));
funcExprRef.setValue(new VariableReferenceExpression(replacementVar));
funcIter.remove();
}
}
// Create new assign operator below the join if any functions can be pushed.
if (assignVars != null) {
AssignOperator newAssign = new AssignOperator(assignVars, assignExprs);
newAssign.getInputs().add(new MutableObject<ILogicalOperator>(joinInputOp));
newAssign.setExecutionMode(joinOp.getExecutionMode());
joinOp.getInputs().get(inputIndex).setValue(newAssign);
context.computeAndSetTypeEnvironmentForOperator(newAssign);
return true;
}
return false;
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator in project asterixdb by apache.
the class FactorRedundantGroupAndDecorVarsRule method factorRedundantRhsVars.
private boolean factorRedundantRhsVars(List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> veList, Mutable<ILogicalOperator> opRef, Map<LogicalVariable, LogicalVariable> varRhsToLhs, IOptimizationContext context) throws AlgebricksException {
varRhsToLhs.clear();
ListIterator<Pair<LogicalVariable, Mutable<ILogicalExpression>>> iter = veList.listIterator();
boolean changed = false;
while (iter.hasNext()) {
Pair<LogicalVariable, Mutable<ILogicalExpression>> p = iter.next();
if (p.second.getValue().getExpressionTag() != LogicalExpressionTag.VARIABLE) {
continue;
}
LogicalVariable v = GroupByOperator.getDecorVariable(p);
LogicalVariable lhs = varRhsToLhs.get(v);
if (lhs != null) {
if (p.first != null) {
AssignOperator assign = new AssignOperator(p.first, new MutableObject<ILogicalExpression>(new VariableReferenceExpression(lhs)));
ILogicalOperator op = opRef.getValue();
assign.getInputs().add(new MutableObject<ILogicalOperator>(op));
opRef.setValue(assign);
context.computeAndSetTypeEnvironmentForOperator(assign);
}
iter.remove();
changed = true;
} else {
varRhsToLhs.put(v, p.first);
}
}
return changed;
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator in project asterixdb by apache.
the class InlineAssignIntoAggregateRule method inlined.
private boolean inlined(Mutable<ILogicalOperator> r) throws AlgebricksException {
AbstractLogicalOperator op1 = (AbstractLogicalOperator) r.getValue();
if (op1.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
return false;
}
AbstractLogicalOperator op2 = (AbstractLogicalOperator) op1.getInputs().get(0).getValue();
if (op2.getOperatorTag() != LogicalOperatorTag.ASSIGN) {
return false;
}
AggregateOperator agg = (AggregateOperator) op1;
AssignOperator assign = (AssignOperator) op2;
VarExprSubstitution ves = new VarExprSubstitution(assign.getVariables(), assign.getExpressions());
for (Mutable<ILogicalExpression> exprRef : agg.getExpressions()) {
ILogicalExpression expr = exprRef.getValue();
Pair<Boolean, ILogicalExpression> p = expr.accept(ves, null);
if (p.first == true) {
exprRef.setValue(p.second);
}
// AbstractLogicalExpression ale = (AbstractLogicalExpression) expr;
// ale.accept(ves, null);
}
List<Mutable<ILogicalOperator>> op1InpList = op1.getInputs();
op1InpList.clear();
op1InpList.add(op2.getInputs().get(0));
return true;
}
Aggregations