use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator in project asterixdb by apache.
the class LangExpressionToPlanTranslator method visitAndOrOperator.
protected Pair<ILogicalOperator, LogicalVariable> visitAndOrOperator(OperatorExpr op, Mutable<ILogicalOperator> tupSource) throws CompilationException {
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);
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 TranslationException("Unexpected operator " + ops.get(i) + " in an OperatorExpr starting with " + opLogical);
}
f.getArguments().add(new MutableObject<>(p.first));
}
LogicalVariable assignedVar = context.newVar();
AssignOperator a = new AssignOperator(assignedVar, new MutableObject<>(f));
a.getInputs().add(topOp);
return new Pair<>(a, assignedVar);
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator in project asterixdb by apache.
the class LangExpressionToPlanTranslator method langExprToAlgExpression.
protected Pair<ILogicalExpression, Mutable<ILogicalOperator>> langExprToAlgExpression(Expression expr, Mutable<ILogicalOperator> topOpRef) throws CompilationException {
switch(expr.getKind()) {
case VARIABLE_EXPRESSION:
VariableReferenceExpression ve = new VariableReferenceExpression(context.getVar(((VariableExpr) expr).getVar().getId()));
return new Pair<>(ve, topOpRef);
case LITERAL_EXPRESSION:
LiteralExpr val = (LiteralExpr) expr;
return new Pair<>(new ConstantExpression(new AsterixConstantValue(ConstantHelper.objectFromLiteral(val.getValue()))), topOpRef);
default:
if (expressionNeedsNoNesting(expr)) {
Pair<ILogicalOperator, LogicalVariable> p = expr.accept(this, topOpRef);
ILogicalExpression exp = ((AssignOperator) p.first).getExpressions().get(0).getValue();
return new Pair<>(exp, p.first.getInputs().get(0));
} else {
Mutable<ILogicalOperator> srcRef = new MutableObject<>();
Pair<ILogicalOperator, LogicalVariable> p = expr.accept(this, srcRef);
if (p.first.getOperatorTag() == LogicalOperatorTag.SUBPLAN) {
if (topOpRef.getValue() != null) {
srcRef.setValue(topOpRef.getValue());
} else {
// Re-binds the bottom operator reference to {@code topOpRef}.
rebindBottomOpRef(p.first, srcRef, topOpRef);
}
Mutable<ILogicalOperator> top2 = new MutableObject<>(p.first);
return new Pair<>(new VariableReferenceExpression(p.second), top2);
} else {
SubplanOperator s = new SubplanOperator();
s.getInputs().add(topOpRef);
srcRef.setValue(new NestedTupleSourceOperator(new MutableObject<>(s)));
Mutable<ILogicalOperator> planRoot = new MutableObject<>(p.first);
s.setRootOp(planRoot);
return new Pair<>(new VariableReferenceExpression(p.second), new MutableObject<>(s));
}
}
}
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator in project asterixdb by apache.
the class AqlExpressionToPlanTranslator method produceFlworPlan.
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);
}
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator in project asterixdb by apache.
the class SqlppExpressionToPlanTranslator method visit.
@Override
public Pair<ILogicalOperator, LogicalVariable> visit(CaseExpression caseExpression, Mutable<ILogicalOperator> tupSource) throws CompilationException {
//Creates a series of subplan operators, one for each branch.
Mutable<ILogicalOperator> currentOpRef = tupSource;
ILogicalOperator currentOperator = null;
List<Expression> whenExprList = caseExpression.getWhenExprs();
List<Expression> thenExprList = caseExpression.getThenExprs();
List<ILogicalExpression> branchCondVarReferences = new ArrayList<>();
List<ILogicalExpression> allVarReferences = new ArrayList<>();
for (int index = 0; index < whenExprList.size(); ++index) {
Pair<ILogicalOperator, LogicalVariable> whenExprResult = whenExprList.get(index).accept(this, currentOpRef);
currentOperator = whenExprResult.first;
// Variable whenConditionVar is corresponds to the current "WHEN" condition.
LogicalVariable whenConditionVar = whenExprResult.second;
Mutable<ILogicalExpression> branchEntraceConditionExprRef = new MutableObject<>(new VariableReferenceExpression(whenConditionVar));
// even though multiple "WHEN" conditions can be satisfied.
if (!branchCondVarReferences.isEmpty()) {
// The additional filter generated here makes sure the the tuple has not
// entered other matched "WHEN...THEN" case.
List<Mutable<ILogicalExpression>> andArgs = new ArrayList<>();
andArgs.add(generateNoMatchedPrecedingWhenBranchesFilter(branchCondVarReferences));
andArgs.add(branchEntraceConditionExprRef);
// A "THEN" branch can be entered only when the tuple has not enter any other preceding
// branches and the current "WHEN" condition is TRUE.
branchEntraceConditionExprRef = new MutableObject<>(new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.AND), andArgs));
}
// Translates the corresponding "THEN" expression.
Pair<ILogicalOperator, LogicalVariable> opAndVarForThen = constructSubplanOperatorForBranch(currentOperator, branchEntraceConditionExprRef, thenExprList.get(index));
branchCondVarReferences.add(new VariableReferenceExpression(whenConditionVar));
allVarReferences.add(new VariableReferenceExpression(whenConditionVar));
allVarReferences.add(new VariableReferenceExpression(opAndVarForThen.second));
currentOperator = opAndVarForThen.first;
currentOpRef = new MutableObject<>(currentOperator);
}
// Creates a subplan for the "ELSE" branch.
Mutable<ILogicalExpression> elseCondExprRef = generateNoMatchedPrecedingWhenBranchesFilter(branchCondVarReferences);
Pair<ILogicalOperator, LogicalVariable> opAndVarForElse = constructSubplanOperatorForBranch(currentOperator, elseCondExprRef, caseExpression.getElseExpr());
// Uses switch-case function to select the results of two branches.
LogicalVariable selectVar = context.newVar();
List<Mutable<ILogicalExpression>> arguments = new ArrayList<>();
arguments.add(new MutableObject<>(new ConstantExpression(new AsterixConstantValue(ABoolean.TRUE))));
for (ILogicalExpression argVar : allVarReferences) {
arguments.add(new MutableObject<>(argVar));
}
arguments.add(new MutableObject<>(new VariableReferenceExpression(opAndVarForElse.second)));
AbstractFunctionCallExpression swithCaseExpr = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.SWITCH_CASE), arguments);
AssignOperator assignOp = new AssignOperator(selectVar, new MutableObject<>(swithCaseExpr));
assignOp.getInputs().add(new MutableObject<>(opAndVarForElse.first));
// Unnests the selected (a "THEN" or "ELSE" branch) result.
LogicalVariable unnestVar = context.newVar();
UnnestOperator unnestOp = new UnnestOperator(unnestVar, new MutableObject<>(new UnnestingFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.SCAN_COLLECTION), Collections.singletonList(new MutableObject<>(new VariableReferenceExpression(selectVar))))));
unnestOp.getInputs().add(new MutableObject<>(assignOp));
// Produces the final assign operator.
LogicalVariable resultVar = context.newVar();
AssignOperator finalAssignOp = new AssignOperator(resultVar, new MutableObject<>(new VariableReferenceExpression(unnestVar)));
finalAssignOp.getInputs().add(new MutableObject<>(unnestOp));
return new Pair<>(finalAssignOp, resultVar);
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator in project asterixdb by apache.
the class EquivalenceClassUtils method findOrCreatePrimaryKeyOpAndVariables.
/**
* Find the header variables that can imply all subplan-local live variables at <code>operator</code>.
*
* @param operator
* the operator of interest.
* @param usedForCorrelationJoin
* whether the generated primary key will be used for a join that recovers the correlation.
* @param context
* the optimization context.
* @return Pair<ILogicalOperator, Set<LogicalVariable>>, an operator (which is either the original parameter
* <code>operator</code> or a newly created operator) and
* a set of primary key variables at the operator.
* @throws AlgebricksException
*/
public static Pair<ILogicalOperator, Set<LogicalVariable>> findOrCreatePrimaryKeyOpAndVariables(ILogicalOperator operator, boolean usedForCorrelationJoin, IOptimizationContext context) throws AlgebricksException {
computePrimaryKeys(operator, context);
Set<LogicalVariable> liveVars = new HashSet<>();
VariableUtilities.getSubplanLocalLiveVariables(operator, liveVars);
Set<LogicalVariable> primaryKeyVars = new HashSet<>();
Set<LogicalVariable> noKeyVars = new HashSet<>();
for (LogicalVariable liveVar : liveVars) {
List<LogicalVariable> keyVars = context.findPrimaryKey(liveVar);
if (keyVars != null) {
keyVars.retainAll(liveVars);
}
if ((keyVars == null || keyVars.isEmpty())) {
noKeyVars.add(liveVar);
} else {
primaryKeyVars.addAll(keyVars);
}
}
primaryKeyVars.retainAll(liveVars);
if (primaryKeyVars.containsAll(noKeyVars)) {
return new Pair<ILogicalOperator, Set<LogicalVariable>>(operator, primaryKeyVars);
} else {
LogicalVariable assignVar = context.newVar();
ILogicalOperator assignOp = new AssignOperator(assignVar, new MutableObject<ILogicalExpression>(new StatefulFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.CREATE_QUERY_UID), null)));
OperatorPropertiesUtil.markMovable(assignOp, !usedForCorrelationJoin);
assignOp.getInputs().add(new MutableObject<ILogicalOperator>(operator));
context.addPrimaryKey(new FunctionalDependency(Collections.singletonList(assignVar), new ArrayList<LogicalVariable>(liveVars)));
context.computeAndSetTypeEnvironmentForOperator(assignOp);
return new Pair<ILogicalOperator, Set<LogicalVariable>>(assignOp, Collections.singleton(assignVar));
}
}
Aggregations