use of org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression in project asterixdb by apache.
the class AbstractUnnestPOperator method contributeRuntimeOperator.
@Override
public void contributeRuntimeOperator(IHyracksJobBuilder builder, JobGenContext context, ILogicalOperator op, IOperatorSchema opSchema, IOperatorSchema[] inputSchemas, IOperatorSchema outerPlanSchema) throws AlgebricksException {
AbstractUnnestNonMapOperator unnest = (AbstractUnnestNonMapOperator) op;
int outCol = opSchema.findVariable(unnest.getVariable());
ILogicalExpression unnestExpr = unnest.getExpressionRef().getValue();
IExpressionRuntimeProvider expressionRuntimeProvider = context.getExpressionRuntimeProvider();
boolean exit = false;
if (unnestExpr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
exit = true;
} else {
AbstractFunctionCallExpression fce = (AbstractFunctionCallExpression) unnestExpr;
if (fce.getKind() != FunctionKind.UNNEST) {
exit = true;
}
}
if (exit) {
throw new AlgebricksException("Unnest expression " + unnestExpr + " is not an unnesting function call.");
}
UnnestingFunctionCallExpression agg = (UnnestingFunctionCallExpression) unnestExpr;
IUnnestingEvaluatorFactory unnestingFactory = expressionRuntimeProvider.createUnnestingFunctionFactory(agg, context.getTypeEnvironment(op.getInputs().get(0).getValue()), inputSchemas, context);
int[] projectionList = JobGenHelper.projectAllVariables(opSchema);
UnnestRuntimeFactory unnestRuntime = new UnnestRuntimeFactory(outCol, unnestingFactory, projectionList, unnest.getPositionWriter(), leftOuter, context.getMissingWriterFactory());
RecordDescriptor recDesc = JobGenHelper.mkRecordDescriptor(context.getTypeEnvironment(op), opSchema, context);
builder.contributeMicroOperator(unnest, unnestRuntime, recDesc);
ILogicalOperator src = unnest.getInputs().get(0).getValue();
builder.contributeGraphEdge(src, 0, unnest, 0);
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression in project asterixdb by apache.
the class FeedScanCollectionToUnnest method rewritePost.
@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
if (context.checkIfInDontApplySet(this, op)) {
return false;
}
if (op.getOperatorTag() != LogicalOperatorTag.UNNEST) {
context.addToDontApplySet(this, op);
return false;
}
UnnestOperator unnest = (UnnestOperator) op;
ILogicalExpression unnestExpr = unnest.getExpressionRef().getValue();
if (needsScanCollection(unnestExpr, op)) {
ILogicalExpression newExpr = new UnnestingFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.SCAN_COLLECTION), new MutableObject<ILogicalExpression>(unnestExpr));
unnest.getExpressionRef().setValue(newExpr);
context.addToDontApplySet(this, op);
return true;
}
context.addToDontApplySet(this, op);
return false;
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression in project asterixdb by apache.
the class LogicalExpressionDeepCopyWithNewVariablesVisitor method visitUnnestingFunctionCallExpression.
@Override
public ILogicalExpression visitUnnestingFunctionCallExpression(UnnestingFunctionCallExpression expr, Void arg) throws AlgebricksException {
UnnestingFunctionCallExpression exprCopy = new UnnestingFunctionCallExpression(expr.getFunctionInfo(), deepCopyExpressionReferenceList(expr.getArguments()));
deepCopyAnnotations(expr, exprCopy);
deepCopyOpaqueParameters(expr, exprCopy);
return exprCopy;
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression in project asterixdb by apache.
the class LangExpressionToPlanTranslator method visit.
@Override
public Pair<ILogicalOperator, LogicalVariable> visit(IfExpr ifexpr, Mutable<ILogicalOperator> tupSource) throws CompilationException {
// In the most general case, IfThenElse is translated in the following
// way.
//
// We assign the result of the condition to one variable varCond.
// We create one subplan which contains the plan for the "then" branch,
// on top of which there is a selection whose condition is varCond.
// Similarly, we create one subplan for the "else" branch, in which the
// selection is not(varCond).
// Finally, we select the desired result.
Pair<ILogicalOperator, LogicalVariable> pCond = ifexpr.getCondExpr().accept(this, tupSource);
LogicalVariable varCond = pCond.second;
// Creates a subplan for the "then" branch.
Pair<ILogicalOperator, LogicalVariable> opAndVarForThen = constructSubplanOperatorForBranch(pCond.first, new MutableObject<>(new VariableReferenceExpression(varCond)), ifexpr.getThenExpr());
// Creates a subplan for the "else" branch.
AbstractFunctionCallExpression notVarCond = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(AlgebricksBuiltinFunctions.NOT), Collections.singletonList(generateAndNotIsUnknownWrap(new VariableReferenceExpression(varCond))));
Pair<ILogicalOperator, LogicalVariable> opAndVarForElse = constructSubplanOperatorForBranch(opAndVarForThen.first, new MutableObject<>(notVarCond), ifexpr.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 VariableReferenceExpression(varCond)));
arguments.add(new MutableObject<>(ConstantExpression.TRUE));
arguments.add(new MutableObject<>(new VariableReferenceExpression(opAndVarForThen.second)));
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 ("if" or "else") 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 result.
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.expressions.UnnestingFunctionCallExpression in project asterixdb by apache.
the class LangExpressionToPlanTranslator method translateUnionAllFromInputExprs.
// Generates the plan for "UNION ALL" or union expression from its input expressions.
protected Pair<ILogicalOperator, LogicalVariable> translateUnionAllFromInputExprs(List<ILangExpression> inputExprs, Mutable<ILogicalOperator> tupSource) throws CompilationException {
List<Mutable<ILogicalOperator>> inputOpRefsToUnion = new ArrayList<>();
List<LogicalVariable> vars = new ArrayList<>();
for (ILangExpression expr : inputExprs) {
// Visits the expression of one branch.
Pair<ILogicalOperator, LogicalVariable> opAndVar = expr.accept(this, tupSource);
// Creates an unnest operator.
LogicalVariable unnestVar = context.newVar();
List<Mutable<ILogicalExpression>> args = new ArrayList<>();
args.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(opAndVar.second)));
UnnestOperator unnestOp = new UnnestOperator(unnestVar, new MutableObject<ILogicalExpression>(new UnnestingFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.SCAN_COLLECTION), args)));
unnestOp.getInputs().add(new MutableObject<>(opAndVar.first));
inputOpRefsToUnion.add(new MutableObject<ILogicalOperator>(unnestOp));
vars.add(unnestVar);
}
// Creates a tree of binary union-all operators.
UnionAllOperator topUnionAllOp = null;
LogicalVariable topUnionVar = null;
Iterator<Mutable<ILogicalOperator>> inputOpRefIterator = inputOpRefsToUnion.iterator();
Mutable<ILogicalOperator> leftInputBranch = inputOpRefIterator.next();
Iterator<LogicalVariable> inputVarIterator = vars.iterator();
LogicalVariable leftInputVar = inputVarIterator.next();
while (inputOpRefIterator.hasNext()) {
// Generates the variable triple <leftVar, rightVar, outputVar> .
topUnionVar = context.newVar();
Triple<LogicalVariable, LogicalVariable, LogicalVariable> varTriple = new Triple<>(leftInputVar, inputVarIterator.next(), topUnionVar);
List<Triple<LogicalVariable, LogicalVariable, LogicalVariable>> varTriples = new ArrayList<>();
varTriples.add(varTriple);
// Creates a binary union-all operator.
topUnionAllOp = new UnionAllOperator(varTriples);
topUnionAllOp.getInputs().add(leftInputBranch);
topUnionAllOp.getInputs().add(inputOpRefIterator.next());
// Re-assigns leftInputBranch and leftInputVar.
leftInputBranch = new MutableObject<>(topUnionAllOp);
leftInputVar = topUnionVar;
}
return new Pair<>(topUnionAllOp, topUnionVar);
}
Aggregations