@Override public Long visitAssignOperator(AssignOperator op, Void arg) throws AlgebricksException { return op.getInputs().get(0).getValue().accept(this, arg); }
private void removeUnecessaryAssign(AbstractLogicalOperator parentOp, AbstractLogicalOperator currentOp, AssignOperator assignOp, int index) { assignOp.getVariables().remove(index); assignOp.getExpressions().remove(index); if (assignOp.getVariables().size() == 0) { int opIndex = parentOp.getInputs().indexOf(new MutableObject<ILogicalOperator>(currentOp)); parentOp.getInputs().get(opIndex).setValue(assignOp.getInputs().get(0).getValue()); } } }
/** * Pushes one field-access assignment above toPushThroughChildRef * * @param toPush * @param toPushThroughChildRef */ private static void pushAccessAboveOpRef(AssignOperator toPush, Mutable<ILogicalOperator> toPushThroughChildRef, IOptimizationContext context) throws AlgebricksException { List<Mutable<ILogicalOperator>> tpInpList = toPush.getInputs(); tpInpList.clear(); tpInpList.add(new MutableObject<>(toPushThroughChildRef.getValue())); toPushThroughChildRef.setValue(toPush); findAndEliminateRedundantFieldAccess(toPush, context); }
/** * Inject variables to indicate non-matches for the right branch of a left-outer join. * * @param joinOp * the leftouter join operator. */ private void injectNullCheckVars(AbstractBinaryJoinOperator joinOp) { LogicalVariable assignVar = context.newVar(); AssignOperator assignOp = new AssignOperator(assignVar, new MutableObject<ILogicalExpression>(ConstantExpression.TRUE)); assignOp.setSourceLocation(joinOp.getSourceLocation()); assignOp.getInputs().add(joinOp.getInputs().get(1)); joinOp.getInputs().set(1, new MutableObject<ILogicalOperator>(assignOp)); nullCheckVars.add(assignVar); }
private LogicalVariable createAssignment(ILogicalExpression expr, TranslationContext tCtx) { if (expr.getExpressionTag() == LogicalExpressionTag.VARIABLE) { return ((VariableReferenceExpression) expr).getVariableReference(); } LogicalVariable result = newLogicalVariable(); AssignOperator aOp = new AssignOperator(result, mutable(expr)); aOp.getInputs().add(mutable(tCtx.op)); tCtx.op = aOp; return result; }
private AssignOperator createAssignBelowUnionAllBranch(UnionAllOperator unionOp, int inputIndex, AssignOperator originalAssignOp, Set<LogicalVariable> assignUsedVars, IOptimizationContext context) throws AlgebricksException { AssignOperator newAssignOp = cloneAssignOperator(originalAssignOp, context); newAssignOp.getInputs() .add(new MutableObject<ILogicalOperator>(unionOp.getInputs().get(inputIndex).getValue())); unionOp.getInputs().get(inputIndex).setValue(newAssignOp); int numVarMappings = unionOp.getVariableMappings().size(); for (int i = 0; i < numVarMappings; i++) { Triple<LogicalVariable, LogicalVariable, LogicalVariable> varMapping = unionOp.getVariableMappings().get(i); if (assignUsedVars.contains(varMapping.third)) { LogicalVariable replacementVar; if (inputIndex == 0) { replacementVar = varMapping.first; } else { replacementVar = varMapping.second; } VariableUtilities.substituteVariables(newAssignOp, varMapping.third, replacementVar, context); } } context.computeAndSetTypeEnvironmentForOperator(newAssignOp); return newAssignOp; }
private void removeGroupByAndOuterUnnest(Mutable<ILogicalOperator> opRef, IOptimizationContext context, LeftOuterUnnestOperator outerUnnest, GroupByOperator gbyOperator, LeftOuterJoinOperator lojOperator, LogicalVariable listifyVar) throws AlgebricksException { List<LogicalVariable> lhs = new ArrayList<>(); List<Mutable<ILogicalExpression>> rhs = new ArrayList<>(); lhs.add(outerUnnest.getVariable()); VariableReferenceExpression listifyVarRef = new VariableReferenceExpression(listifyVar); listifyVarRef.setSourceLocation(gbyOperator.getSourceLocation()); rhs.add(new MutableObject<ILogicalExpression>(listifyVarRef)); List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> gbyList = gbyOperator.getGroupByList(); for (Pair<LogicalVariable, Mutable<ILogicalExpression>> gbyPair : gbyList) { lhs.add(gbyPair.first); rhs.add(gbyPair.second); } AssignOperator assignOp = new AssignOperator(lhs, rhs); assignOp.setSourceLocation(outerUnnest.getSourceLocation()); assignOp.getInputs().add(new MutableObject<ILogicalOperator>(lojOperator)); context.computeAndSetTypeEnvironmentForOperator(assignOp); opRef.setValue(assignOp); }
@Override public Pair<ILogicalOperator, LogicalVariable> visitMetaVariableExpr(MetaVariableExpr me, Mutable<ILogicalOperator> tupSource) throws CompilationException { LogicalVariable var = context.newVar(); AssignOperator a = new AssignOperator(var, new MutableObject<ILogicalExpression>(metaScopeExp.getVariableReferenceExpression(me.getVar()))); a.getInputs().add(tupSource); return new Pair<ILogicalOperator, LogicalVariable>(a, var); }
private boolean extractFirstArg(AbstractFunctionCallExpression fce, ILogicalOperator op, IOptimizationContext context) throws AlgebricksException { ILogicalExpression firstArg = fce.getArguments().get(0).getValue(); if (firstArg.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) { return false; } SourceLocation sourceLoc = firstArg.getSourceLocation(); LogicalVariable var1 = context.newVar(); AssignOperator assignOp = new AssignOperator(new ArrayList<>(Collections.singletonList(var1)), new ArrayList<>(Collections.singletonList(new MutableObject<>(firstArg)))); assignOp.setSourceLocation(sourceLoc); VariableReferenceExpression var1Ref = new VariableReferenceExpression(var1); var1Ref.setSourceLocation(sourceLoc); fce.getArguments().get(0).setValue(var1Ref); assignOp.getInputs().add(new MutableObject<>(op.getInputs().get(0).getValue())); op.getInputs().get(0).setValue(assignOp); context.computeAndSetTypeEnvironmentForOperator(assignOp); return true; }
protected Pair<ILogicalOperator, LogicalVariable> visitAndOrOperator(OperatorExpr op, Mutable<ILogicalOperator> tupSource) throws CompilationException { SourceLocation sourceLoc = op.getSourceLocation(); List<OperatorType> ops = op.getOpList(); int nOps = ops.size(); List<Expression> exprs = op.getExprList(); Mutable<ILogicalOperator> topOp = tupSource; OperatorType opLogical = ops.get(0); AbstractFunctionCallExpression f = createFunctionCallExpressionForBuiltinOperator(opLogical, sourceLoc); for (int i = 0; i <= nOps; i++) { Pair<ILogicalExpression, Mutable<ILogicalOperator>> p = langExprToAlgExpression(exprs.get(i), topOp); topOp = p.second; // now look at the operator if (i < nOps && ops.get(i) != opLogical) { throw new CompilationException(ErrorCode.COMPILATION_UNEXPECTED_OPERATOR, sourceLoc, ops.get(i), opLogical); } f.getArguments().add(new MutableObject<>(p.first)); } LogicalVariable assignedVar = context.newVar(); AssignOperator a = new AssignOperator(assignedVar, new MutableObject<>(f)); a.getInputs().add(topOp); a.setSourceLocation(sourceLoc); return new Pair<>(a, assignedVar); }
@Override public Pair<ILogicalOperator, LogicalVariable> visit(VariableExpr v, Mutable<ILogicalOperator> tupSource) throws CompilationException { // Should we ever get to this method? ILogicalExpression oldVRef = translateVariableRef(v); LogicalVariable var = context.newVar(); AssignOperator a = new AssignOperator(var, new MutableObject<>(oldVRef)); a.getInputs().add(tupSource); a.setSourceLocation(v.getSourceLocation()); return new Pair<>(a, var); }
private Pair<List<Mutable<ILogicalExpression>>, ILogicalOperator> translateWindowBoundaryExpr( Expression boundaryExpr, List<Pair<OrderOperator.IOrder, Mutable<ILogicalExpression>>> valueExprs, Mutable<ILogicalOperator> tupSource, OperatorType boundaryOperator) throws CompilationException { SourceLocation sourceLoc = boundaryExpr.getSourceLocation(); if (valueExprs.size() != 1) { throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_STATE, sourceLoc, valueExprs.size()); } ILogicalExpression valueExpr = valueExprs.get(0).second.getValue(); AbstractFunctionCallExpression resultExpr = createFunctionCallExpressionForBuiltinOperator(boundaryOperator, sourceLoc); resultExpr.getArguments().add(new MutableObject<>(valueExpr.cloneExpression())); Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(boundaryExpr, tupSource); resultExpr.getArguments().add(new MutableObject<>(eo.first)); LogicalVariable resultVar = context.newVar(); AssignOperator assignOp = new AssignOperator(resultVar, new MutableObject<>(resultExpr)); assignOp.setSourceLocation(sourceLoc); assignOp.getInputs().add(eo.second); VariableReferenceExpression resultVarRefExpr = new VariableReferenceExpression(resultVar); resultVarRefExpr.setSourceLocation(sourceLoc); return new Pair<>(mkSingletonArrayList(new MutableObject<>(resultVarRefExpr)), assignOp); }
protected static LogicalVariable extractExprIntoAssignOpRef(ILogicalExpression gExpr, Mutable<ILogicalOperator> opRef2, IOptimizationContext context) throws AlgebricksException { LogicalVariable v = context.newVar(); AssignOperator a = new AssignOperator(v, new MutableObject<>(gExpr)); a.setSourceLocation(gExpr.getSourceLocation()); a.getInputs().add(new MutableObject<>(opRef2.getValue())); opRef2.setValue(a); if (gExpr.getExpressionTag() == LogicalExpressionTag.CONSTANT) { context.addNotToBeInlinedVar(v); } context.computeAndSetTypeEnvironmentForOperator(a); return v; }
@Override public Pair<ILogicalOperator, LogicalVariable> visit(LetClause lc, Mutable<ILogicalOperator> tupSource) throws CompilationException { LogicalVariable v; AssignOperator returnedOp; Expression bindingExpr = lc.getBindingExpr(); SourceLocation sourceLoc = bindingExpr.getSourceLocation(); if (bindingExpr.getKind() == Kind.VARIABLE_EXPRESSION) { VariableExpr bindingVarExpr = (VariableExpr) bindingExpr; ILogicalExpression prevVarRef = translateVariableRef(bindingVarExpr); v = context.newVarFromExpression(lc.getVarExpr()); returnedOp = new AssignOperator(v, new MutableObject<>(prevVarRef)); returnedOp.getInputs().add(tupSource); returnedOp.setSourceLocation(sourceLoc); } else { v = context.newVarFromExpression(lc.getVarExpr()); Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(bindingExpr, tupSource); returnedOp = new AssignOperator(v, new MutableObject<>(eo.first)); returnedOp.getInputs().add(eo.second); returnedOp.setSourceLocation(sourceLoc); } return new Pair<>(returnedOp, v); }
@Override public Pair<ILogicalOperator, LogicalVariable> visit(LiteralExpr l, Mutable<ILogicalOperator> tupSource) { SourceLocation sourceLoc = l.getSourceLocation(); LogicalVariable var = context.newVar(); AsterixConstantValue cValue = new AsterixConstantValue(ConstantHelper.objectFromLiteral(l.getValue())); ConstantExpression cExpr = new ConstantExpression(cValue); cExpr.setSourceLocation(sourceLoc); AssignOperator a = new AssignOperator(var, new MutableObject<>(cExpr)); a.setSourceLocation(sourceLoc); if (tupSource != null) { a.getInputs().add(tupSource); } return new Pair<>(a, var); }
@Override public Pair<ILogicalOperator, LogicalVariable> visit(ListConstructor lc, Mutable<ILogicalOperator> tupSource) throws CompilationException { SourceLocation sourceLoc = lc.getSourceLocation(); FunctionIdentifier fid = (lc.getType() == ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR) ? BuiltinFunctions.ORDERED_LIST_CONSTRUCTOR : BuiltinFunctions.UNORDERED_LIST_CONSTRUCTOR; AbstractFunctionCallExpression f = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(fid)); f.setSourceLocation(sourceLoc); LogicalVariable v1 = context.newVar(); AssignOperator a = new AssignOperator(v1, new MutableObject<>(f)); a.setSourceLocation(sourceLoc); Mutable<ILogicalOperator> topOp = tupSource; for (Expression expr : lc.getExprList()) { Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(expr, topOp); f.getArguments().add(new MutableObject<>(eo.first)); topOp = eo.second; } a.getInputs().add(topOp); return new Pair<>(a, v1); }
@Override public Pair<ILogicalOperator, LogicalVariable> visit(RecordConstructor rc, Mutable<ILogicalOperator> tupSource) throws CompilationException { AbstractFunctionCallExpression f = new ScalarFunctionCallExpression( FunctionUtil.getFunctionInfo(BuiltinFunctions.OPEN_RECORD_CONSTRUCTOR)); f.setSourceLocation(rc.getSourceLocation()); LogicalVariable v1 = context.newVar(); AssignOperator a = new AssignOperator(v1, new MutableObject<>(f)); a.setSourceLocation(rc.getSourceLocation()); Mutable<ILogicalOperator> topOp = tupSource; for (FieldBinding fb : rc.getFbList()) { Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo1 = langExprToAlgExpression(fb.getLeftExpr(), topOp); f.getArguments().add(new MutableObject<>(eo1.first)); topOp = eo1.second; Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo2 = langExprToAlgExpression(fb.getRightExpr(), topOp); f.getArguments().add(new MutableObject<>(eo2.first)); topOp = eo2.second; } a.getInputs().add(topOp); return new Pair<>(a, v1); }
@Override public Pair<ILogicalOperator, LogicalVariable> visit(Query q, Mutable<ILogicalOperator> tupSource) throws CompilationException { Expression queryBody = q.getBody(); SourceLocation sourceLoc = queryBody.getSourceLocation(); if (queryBody.getKind() == Kind.SELECT_EXPRESSION) { SelectExpression selectExpr = (SelectExpression) queryBody; if (q.isTopLevel()) { selectExpr.setSubquery(false); } return queryBody.accept(this, tupSource); } else { LogicalVariable var = context.newVar(); Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(queryBody, tupSource); AssignOperator assignOp = new AssignOperator(var, new MutableObject<>(eo.first)); assignOp.getInputs().add(eo.second); assignOp.setSourceLocation(sourceLoc); ProjectOperator projectOp = new ProjectOperator(var); projectOp.getInputs().add(new MutableObject<>(assignOp)); projectOp.setSourceLocation(sourceLoc); return new Pair<>(projectOp, var); } }
@Override public Pair<ILogicalOperator, LogicalVariable> visit(FieldAccessor fa, Mutable<ILogicalOperator> tupSource) throws CompilationException { SourceLocation sourceLoc = fa.getSourceLocation(); Pair<ILogicalExpression, Mutable<ILogicalOperator>> p = langExprToAlgExpression(fa.getExpr(), tupSource); LogicalVariable v = context.newVarFromExpression(fa); AbstractFunctionCallExpression fldAccess = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.FIELD_ACCESS_BY_NAME)); fldAccess.setSourceLocation(sourceLoc); fldAccess.getArguments().add(new MutableObject<>(p.first)); ConstantExpression faExpr = new ConstantExpression(new AsterixConstantValue(new AString(fa.getIdent().getValue()))); faExpr.setSourceLocation(sourceLoc); fldAccess.getArguments().add(new MutableObject<>(faExpr)); AssignOperator a = new AssignOperator(v, new MutableObject<>(fldAccess)); a.getInputs().add(p.second); a.setSourceLocation(sourceLoc); return new Pair<>(a, v); }
@Test public void noBlockingPlan() throws AlgebricksException { EmptyTupleSourceOperator ets = new EmptyTupleSourceOperator(); ets.setExecutionMode(UNPARTITIONED); AssignOperator assignOperator = new AssignOperator(Collections.emptyList(), null); assignOperator.setExecutionMode(UNPARTITIONED); assignOperator.getInputs().add(new MutableObject<>(ets)); ExchangeOperator exchange = new ExchangeOperator(); exchange.setExecutionMode(UNPARTITIONED); exchange.setPhysicalOperator(new OneToOneExchangePOperator()); exchange.getInputs().add(new MutableObject<>(assignOperator)); DistributeResultOperator resultOperator = new DistributeResultOperator(null, null); resultOperator.setExecutionMode(UNPARTITIONED); resultOperator.getInputs().add(new MutableObject<>(exchange)); ALogicalPlanImpl plan = new ALogicalPlanImpl(Collections.singletonList(new MutableObject(resultOperator))); List<PlanStage> stages = ResourceUtils.getStages(plan); // ensure a single stage plan final int expectedStages = 1; Assert.assertEquals(expectedStages, stages.size()); validateStages(stages, resultOperator, exchange, ets, assignOperator); // frame size for every operator final long expectedMemory = stages.get(0).getOperators().size() * FRAME_SIZE; assertRequiredMemory(stages, expectedMemory); }