private static Pair<LogicalVariable, Mutable<ILogicalExpression>> getDecorPairByRhsVar(GroupByOperator gby, LogicalVariable var) { for (Pair<LogicalVariable, Mutable<ILogicalExpression>> ve : gby.getDecorList()) { if (ve.first == var) { return ve; } } return null; }
private boolean removeUnusedVarsFromGroupBy(GroupByOperator groupByOp, Set<LogicalVariable> toRemove) { if (toRemove == null || toRemove.isEmpty()) { return false; } Iterator<Pair<LogicalVariable, Mutable<ILogicalExpression>>> iter = groupByOp.getDecorList().iterator(); boolean modified = false; while (iter.hasNext()) { Pair<LogicalVariable, Mutable<ILogicalExpression>> varMapping = iter.next(); LogicalVariable decorVar = varMapping.first; // A decor var mapping can have a variable reference expression without a new variable definition, // which is for rebinding the referred variable. VariableReferenceExpression varExpr = (VariableReferenceExpression) varMapping.second.getValue(); LogicalVariable decorReferredVar = varExpr.getVariableReference(); boolean removeReBoundDecorVar = toRemove.contains(decorReferredVar); if ((decorVar != null && toRemove.contains(decorVar)) || removeReBoundDecorVar) { iter.remove(); modified = true; if (removeReBoundDecorVar) { // Do not need to remove that in the children pipeline. toRemove.remove(decorReferredVar); } } } return modified; }
@Override public String visitGroupByOperator(GroupByOperator op, Boolean showDetails) throws AlgebricksException { stringBuilder.setLength(0); stringBuilder.append("group by").append(op.isGroupAll() ? " (all)" : "").append(" ("); printVariableAndExprList(op.getGroupByList()); stringBuilder.append(") decor ("); printVariableAndExprList(op.getDecorList()); stringBuilder.append(")"); appendSchema(op, showDetails); appendAnnotations(op, showDetails); appendPhysicalOperatorInfo(op, showDetails); return stringBuilder.toString(); }
protected int[] getFdColumns(GroupByOperator gby, IOperatorSchema inputSchema) throws AlgebricksException { int numFds = gby.getDecorList().size(); int fdColumns[] = new int[numFds]; int j = 0; for (Pair<LogicalVariable, Mutable<ILogicalExpression>> p : gby.getDecorList()) { ILogicalExpression expr = p.second.getValue(); if (expr.getExpressionTag() != LogicalExpressionTag.VARIABLE) { throw new AlgebricksException("pre-sorted group-by expects variable references."); } VariableReferenceExpression v = (VariableReferenceExpression) expr; LogicalVariable decor = v.getVariableReference(); fdColumns[j++] = inputSchema.findVariable(decor); } return fdColumns; }
private LogicalVariable getNewDecorVar(GroupByOperator g, LogicalVariable v) { for (Pair<LogicalVariable, Mutable<ILogicalExpression>> p : g.getDecorList()) { ILogicalExpression e = p.second.getValue(); if (e.getExpressionTag() == LogicalExpressionTag.VARIABLE) { LogicalVariable v2 = ((VariableReferenceExpression) e).getVariableReference(); if (v2 == v) { return (p.first != null) ? p.first : v2; } } } return null; }
private void mapVariablesForGroupBy(ILogicalOperator left, ILogicalOperator right) { if (left.getOperatorTag() != right.getOperatorTag()) { return; } GroupByOperator leftOp = (GroupByOperator) left; GroupByOperator rightOp = (GroupByOperator) right; List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> leftPairs = leftOp.getGroupByList(); List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> rightPairs = rightOp.getGroupByList(); mapVarExprPairList(leftPairs, rightPairs); leftPairs = leftOp.getDecorList(); rightPairs = rightOp.getDecorList(); mapVarExprPairList(leftPairs, rightPairs); }
@Override public ILogicalOperator visitGroupByOperator(GroupByOperator op, Void arg) throws AlgebricksException { List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> groupByList = new ArrayList<>(); List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> decoList = new ArrayList<>(); ArrayList<ILogicalPlan> newSubplans = new ArrayList<>(); for (Pair<LogicalVariable, Mutable<ILogicalExpression>> pair : op.getGroupByList()) { groupByList.add(new Pair<>(pair.first, deepCopyExpressionRef(pair.second))); } for (Pair<LogicalVariable, Mutable<ILogicalExpression>> pair : op.getDecorList()) { decoList.add(new Pair<>(pair.first, deepCopyExpressionRef(pair.second))); } GroupByOperator gbyOp = new GroupByOperator(groupByList, decoList, newSubplans, op.isGroupAll()); for (ILogicalPlan plan : op.getNestedPlans()) { newSubplans.add(OperatorManipulationUtil.deepCopy(plan, gbyOp)); } return gbyOp; }
@Override public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException { AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue(); if (op.getOperatorTag() != LogicalOperatorTag.GROUP) { return false; } GroupByOperator gby = (GroupByOperator) op; Map<LogicalVariable, LogicalVariable> varRhsToLhs = new HashMap<LogicalVariable, LogicalVariable>(); boolean gvChanged = factorRedundantRhsVars(gby.getGroupByList(), opRef, varRhsToLhs, context); boolean dvChanged = factorRedundantRhsVars(gby.getDecorList(), opRef, varRhsToLhs, context); return gvChanged || dvChanged; }
@Override public Void visitGroupByOperator(GroupByOperator op, Void arg) throws AlgebricksException { for (ILogicalPlan p : op.getNestedPlans()) { for (Mutable<ILogicalOperator> r : p.getRoots()) { VariableUtilities.getLiveVariables(r.getValue(), producedVariables); } } for (Pair<LogicalVariable, Mutable<ILogicalExpression>> p : op.getGroupByList()) { if (p.first != null) { producedVariables.add(p.first); } } for (Pair<LogicalVariable, Mutable<ILogicalExpression>> p : op.getDecorList()) { if (p.first != null) { producedVariables.add(p.first); } } return null; }
private void push(Mutable<ILogicalOperator> opRefGby, Mutable<ILogicalOperator> opRefJoin, int branch, List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> decorToPush, List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> decorNotToPush, IOptimizationContext context) throws AlgebricksException { GroupByOperator gby = (GroupByOperator) opRefGby.getValue(); AbstractBinaryJoinOperator join = (AbstractBinaryJoinOperator) opRefJoin.getValue(); gby.getDecorList().clear(); gby.getDecorList().addAll(decorToPush); for (Pair<LogicalVariable, Mutable<ILogicalExpression>> p : decorNotToPush) { LogicalVariable v1 = p.first; if (v1 != null) { VariableReferenceExpression varRef = (VariableReferenceExpression) p.second.getValue(); LogicalVariable v2 = varRef.getVariableReference(); OperatorManipulationUtil.substituteVarRec(join, v2, v1, true, context); } } Mutable<ILogicalOperator> branchRef = join.getInputs().get(branch); ILogicalOperator opBranch = branchRef.getValue(); opRefJoin.setValue(opBranch); branchRef.setValue(gby); opRefGby.setValue(join); }
@Override public Void visitGroupByOperator(GroupByOperator op, Pair<LogicalVariable, LogicalVariable> pair) throws AlgebricksException { subst(pair.first, pair.second, op.getGroupByList()); subst(pair.first, pair.second, op.getDecorList()); substInNestedPlans(pair.first, pair.second, op); substVarTypes(op, pair); return null; }
@Override public Void visitGroupByOperator(GroupByOperator op, Void arg) throws AlgebricksException { for (Pair<LogicalVariable, Mutable<ILogicalExpression>> p : op.getGroupByList()) { sweepExpression(p.second.getValue()); } for (Pair<LogicalVariable, Mutable<ILogicalExpression>> p : op.getDecorList()) { sweepExpression(p.second.getValue()); } return null; }
@Override public Void visitGroupByOperator(GroupByOperator op, Void arg) throws AlgebricksException { for (ILogicalPlan p : op.getNestedPlans()) { for (Mutable<ILogicalOperator> r : p.getRoots()) { VariableUtilities.getLiveVariables(r.getValue(), schemaVariables); } } for (Pair<LogicalVariable, Mutable<ILogicalExpression>> p : op.getGroupByList()) { if (p.first != null) { schemaVariables.add(p.first); } } for (Pair<LogicalVariable, Mutable<ILogicalExpression>> p : op.getDecorList()) { if (p.first != null) { schemaVariables.add(p.first); } else { ILogicalExpression e = p.second.getValue(); if (e.getExpressionTag() == LogicalExpressionTag.VARIABLE) { schemaVariables.add(((VariableReferenceExpression) e).getVariableReference()); } } } return null; }
@Override public ILogicalOperator visitGroupByOperator(GroupByOperator op, ILogicalOperator arg) throws AlgebricksException { List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> groupByListCopy = deepCopyVariableExpressionReferencePairList(op.getGroupByList()); List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> decorListCopy = deepCopyVariableExpressionReferencePairList(op.getDecorList()); List<ILogicalPlan> nestedPlansCopy = new ArrayList<ILogicalPlan>(); GroupByOperator opCopy = new GroupByOperator(groupByListCopy, decorListCopy, nestedPlansCopy, op.isGroupAll()); deepCopyInputsAnnotationsAndExecutionMode(op, arg, opCopy); deepCopyPlanList(op.getNestedPlans(), nestedPlansCopy, opCopy); return opCopy; }
@Override public Void visitGroupByOperator(GroupByOperator op, Integer indent) throws AlgebricksException { addIndent(indent).append("group by" + (op.isGroupAll() ? " (all)" : "") + " ("); pprintVeList(op.getGroupByList(), indent); buffer.append(") decor ("); pprintVeList(op.getDecorList(), indent); buffer.append(") {"); printNestedPlans(op, indent); return null; }
@Override public Void visitGroupByOperator(GroupByOperator op, Void arg) throws AlgebricksException { visitNestedPlans(op); for (Pair<LogicalVariable, Mutable<ILogicalExpression>> g : op.getGroupByList()) { g.second.getValue().getUsedVariables(usedVariables); } for (Pair<LogicalVariable, Mutable<ILogicalExpression>> g : op.getDecorList()) { g.second.getValue().getUsedVariables(usedVariables); } return null; }
List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> decorList = groupOp.getDecorList(); if (!groupVars.isEmpty() || !decorList.isEmpty()) { return false;
@Override public Void visitGroupByOperator(GroupByOperator op, Integer indent) throws AlgebricksException { addIndent(indent).append("\"operator\": \"group-by\""); if (op.isGroupAll()) { buffer.append(",\n"); addIndent(indent).append("\"option\": \"all\""); } if (!op.getGroupByList().isEmpty()) { buffer.append(",\n"); addIndent(indent).append("\"group-by-list\": "); pprintVeList(op.getGroupByList(), indent); } if (!op.getDecorList().isEmpty()) { buffer.append(",\n"); addIndent(indent).append("\"decor-list\": "); pprintVeList(op.getDecorList(), indent); } if (!op.getNestedPlans().isEmpty()) { buffer.append(",\n"); addIndent(indent).append("\"subplan\": "); printNestedPlans(op, indent); } return null; }
@Override public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException { AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue(); if (op1.getOperatorTag() != LogicalOperatorTag.GROUP) { return false; } if (context.checkIfInDontApplySet(this, op1)) { return false; } context.addToDontApplySet(this, op1); GroupByOperator g = (GroupByOperator) op1; boolean r1 = extractComplexExpressions(g, g.getGroupByList(), context); boolean r2 = extractComplexExpressions(g, g.getDecorList(), context); boolean fired = r1 || r2; if (fired) { context.computeAndSetTypeEnvironmentForOperator(g); } return fired; }
for (Pair<LogicalVariable, Mutable<ILogicalExpression>> p : getDecorList()) { ILogicalExpression expr = p.second.getValue(); if (p.first != null) {