@Override public ILogicalOperator visitAssignOperator(AssignOperator op, Void arg) throws AlgebricksException { ArrayList<LogicalVariable> newList = new ArrayList<>(); ArrayList<Mutable<ILogicalExpression>> newExpressions = new ArrayList<>(); newList.addAll(op.getVariables()); deepCopyExpressionRefs(newExpressions, op.getExpressions()); return new AssignOperator(newList, newExpressions); }
private AssignOperator getAssignOperator(LogicalVariable unnestVariable, LogicalVariable outputVariable, IFunctionInfo inputFunction) { List<Mutable<ILogicalExpression>> nodeArgs = new ArrayList<Mutable<ILogicalExpression>>(); nodeArgs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(unnestVariable))); ScalarFunctionCallExpression unctionExpression = new ScalarFunctionCallExpression(inputFunction, nodeArgs); Mutable<ILogicalExpression> nodeTreeIdExpression = new MutableObject<ILogicalExpression>(unctionExpression); return new AssignOperator(outputVariable, nodeTreeIdExpression); }
/** * 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.setSourceLocation(assignOp.getSourceLocation()); assignCloneOp.setExecutionMode(assignOp.getExecutionMode()); return assignCloneOp; } }
/** * 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 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 ILogicalOperator visitAssignOperator(AssignOperator op, ILogicalOperator arg) throws AlgebricksException { AssignOperator opCopy = new AssignOperator(deepCopyVariableList(op.getVariables()), exprDeepCopyVisitor.deepCopyExpressionReferenceList(op.getExpressions())); deepCopyInputsAnnotationsAndExecutionMode(op, arg, opCopy); return opCopy; }
@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; }
@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); }
private Pair<ILogicalOperator, LogicalVariable> createRecordConstructorAssignOp(Set<LogicalVariable> inputLiveVars, SourceLocation sourceLoc) { // Creates a nested record. List<Mutable<ILogicalExpression>> recordConstructorArgs = new ArrayList<>(); for (LogicalVariable inputLiveVar : inputLiveVars) { if (!correlatedKeyVars.contains(inputLiveVar)) { recordConstructorArgs.add(new MutableObject<>(new ConstantExpression( new AsterixConstantValue(new AString(Integer.toString(inputLiveVar.getId())))))); VariableReferenceExpression inputLiveVarRef = new VariableReferenceExpression(inputLiveVar); inputLiveVarRef.setSourceLocation(sourceLoc); recordConstructorArgs.add(new MutableObject<>(inputLiveVarRef)); } } LogicalVariable recordVar = context.newVar(); ScalarFunctionCallExpression openRecConstr = new ScalarFunctionCallExpression( FunctionUtil.getFunctionInfo(BuiltinFunctions.OPEN_RECORD_CONSTRUCTOR), recordConstructorArgs); openRecConstr.setSourceLocation(sourceLoc); Mutable<ILogicalExpression> recordExprRef = new MutableObject<ILogicalExpression>(openRecConstr); AssignOperator assignOp = new AssignOperator(recordVar, recordExprRef); assignOp.setSourceLocation(sourceLoc); return new Pair<>(assignOp, recordVar); }
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); }
private Pair<ILogicalOperator, LogicalVariable> produceFlworPlan(boolean noForClause, boolean isTop, Mutable<ILogicalOperator> resOpRef, LogicalVariable resVar) { if (isTop) { ProjectOperator pr = new ProjectOperator(resVar); pr.getInputs().add(resOpRef); return new Pair<>(pr, resVar); } else if (noForClause) { ILogicalOperator resOp = resOpRef.getValue(); if (resOp.getOperatorTag() == LogicalOperatorTag.ASSIGN) { return new Pair<>(resOp, resVar); } LogicalVariable newResVar = context.newVar(); ILogicalOperator assign = new AssignOperator(newResVar, new MutableObject<>(new VariableReferenceExpression(resVar))); assign.getInputs().add(resOpRef); return new Pair<>(assign, newResVar); } else { return aggListifyForSubquery(resVar, resOpRef, false); } }
private AssignOperator processExists(ILogicalExpression inputExpr, LogicalVariable v1, boolean not, SourceLocation sourceLoc) { AbstractFunctionCallExpression count = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.SCALAR_COUNT)); count.getArguments().add(new MutableObject<>(inputExpr)); count.setSourceLocation(sourceLoc); AbstractFunctionCallExpression comparison = new ScalarFunctionCallExpression( FunctionUtil.getFunctionInfo(not ? BuiltinFunctions.EQ : BuiltinFunctions.NEQ)); ConstantExpression eZero = new ConstantExpression(new AsterixConstantValue(new AInt64(0L))); eZero.setSourceLocation(sourceLoc); comparison.getArguments().add(new MutableObject<>(count)); comparison.getArguments().add(new MutableObject<>(eZero)); comparison.setSourceLocation(sourceLoc); AssignOperator a = new AssignOperator(v1, new MutableObject<>(comparison)); a.setSourceLocation(sourceLoc); return a; }
@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(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); }