use of org.apache.hyracks.algebricks.core.algebra.expressions.StatefulFunctionCallExpression in project asterixdb by apache.
the class RemoveRedundantListifyRule method applies.
private boolean applies(Mutable<ILogicalOperator> opRef, Set<LogicalVariable> varUsedAbove, IOptimizationContext context) throws AlgebricksException {
AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
if (op1.getOperatorTag() != LogicalOperatorTag.UNNEST) {
return false;
}
UnnestOperator unnest1 = (UnnestOperator) op1;
ILogicalExpression expr = unnest1.getExpressionRef().getValue();
if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
return false;
}
if (((AbstractFunctionCallExpression) expr).getFunctionIdentifier() != BuiltinFunctions.SCAN_COLLECTION) {
return false;
}
AbstractFunctionCallExpression functionCall = (AbstractFunctionCallExpression) expr;
ILogicalExpression functionCallArgExpr = functionCall.getArguments().get(0).getValue();
if (functionCallArgExpr.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
return false;
}
LogicalVariable unnestedVar = ((VariableReferenceExpression) functionCallArgExpr).getVariableReference();
if (varUsedAbove.contains(unnestedVar)) {
return false;
}
Mutable<ILogicalOperator> aggregateParentRef = opRef;
AbstractLogicalOperator r = op1;
boolean metAggregate = false;
while (r.getInputs().size() == 1) {
aggregateParentRef = r.getInputs().get(0);
r = (AbstractLogicalOperator) aggregateParentRef.getValue();
if (r.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
AssignOperator assign = (AssignOperator) r;
List<LogicalVariable> variables = assign.getVariables();
// The assign operator doesn't produce any variable that is used by the unnest.
if (variables.contains(unnestedVar)) {
return false;
}
} else {
if (r.getOperatorTag() == LogicalOperatorTag.AGGREGATE) {
metAggregate = true;
}
break;
}
}
if (!metAggregate) {
return false;
}
AggregateOperator agg = (AggregateOperator) r;
if (agg.getVariables().size() > 1) {
return false;
}
LogicalVariable aggVar = agg.getVariables().get(0);
ILogicalExpression aggFun = agg.getExpressions().get(0).getValue();
if (!aggVar.equals(unnestedVar) || ((AbstractLogicalExpression) aggFun).getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
return false;
}
AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) aggFun;
if (!BuiltinFunctions.LISTIFY.equals(f.getFunctionIdentifier())) {
return false;
}
if (f.getArguments().size() != 1) {
return false;
}
ILogicalExpression arg0 = f.getArguments().get(0).getValue();
if (((AbstractLogicalExpression) arg0).getExpressionTag() != LogicalExpressionTag.VARIABLE) {
return false;
}
LogicalVariable paramVar = ((VariableReferenceExpression) arg0).getVariableReference();
List<LogicalVariable> assgnVars = new ArrayList<>(1);
assgnVars.add(unnest1.getVariable());
List<Mutable<ILogicalExpression>> assgnExprs = new ArrayList<>(1);
assgnExprs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(paramVar)));
AssignOperator assign = new AssignOperator(assgnVars, assgnExprs);
assign.getInputs().add(agg.getInputs().get(0));
context.computeAndSetTypeEnvironmentForOperator(assign);
LogicalVariable posVar = unnest1.getPositionalVariable();
if (posVar == null) {
// Removes the aggregate operator.
aggregateParentRef.setValue(assign);
} else {
List<LogicalVariable> raggVars = new ArrayList<>(1);
raggVars.add(posVar);
List<Mutable<ILogicalExpression>> rAggExprs = new ArrayList<>(1);
StatefulFunctionCallExpression tidFun = new StatefulFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.TID), UnpartitionedPropertyComputer.INSTANCE);
rAggExprs.add(new MutableObject<ILogicalExpression>(tidFun));
RunningAggregateOperator rAgg = new RunningAggregateOperator(raggVars, rAggExprs);
rAgg.getInputs().add(new MutableObject<ILogicalOperator>(assign));
aggregateParentRef.setValue(rAgg);
context.computeAndSetTypeEnvironmentForOperator(rAgg);
}
// Removes the unnest operator.
opRef.setValue(unnest1.getInputs().get(0).getValue());
return true;
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.StatefulFunctionCallExpression in project asterixdb by apache.
the class LogicalExpressionDeepCopyWithNewVariablesVisitor method visitStatefulFunctionCallExpression.
@Override
public ILogicalExpression visitStatefulFunctionCallExpression(StatefulFunctionCallExpression expr, Void arg) throws AlgebricksException {
StatefulFunctionCallExpression exprCopy = new StatefulFunctionCallExpression(expr.getFunctionInfo(), expr.getPropertiesComputer(), deepCopyExpressionReferenceList(expr.getArguments()));
deepCopyAnnotations(expr, exprCopy);
deepCopyOpaqueParameters(expr, exprCopy);
return exprCopy;
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.StatefulFunctionCallExpression in project asterixdb by apache.
the class PullPositionalVariableFromUnnestRule method rewritePost.
@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
if (op.getOperatorTag() != LogicalOperatorTag.UNNEST) {
return false;
}
UnnestOperator unnest = (UnnestOperator) op;
LogicalVariable p = unnest.getPositionalVariable();
if (p == null) {
return false;
}
ArrayList<LogicalVariable> rOpVars = new ArrayList<LogicalVariable>();
rOpVars.add(p);
ArrayList<Mutable<ILogicalExpression>> rOpExprList = new ArrayList<Mutable<ILogicalExpression>>();
StatefulFunctionCallExpression fce = new StatefulFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.TID), UnpartitionedPropertyComputer.INSTANCE);
rOpExprList.add(new MutableObject<ILogicalExpression>(fce));
RunningAggregateOperator rOp = new RunningAggregateOperator(rOpVars, rOpExprList);
rOp.setExecutionMode(unnest.getExecutionMode());
RunningAggregatePOperator rPop = new RunningAggregatePOperator();
rOp.setPhysicalOperator(rPop);
rOp.getInputs().add(new MutableObject<ILogicalOperator>(unnest));
opRef.setValue(rOp);
unnest.setPositionalVariable(null);
context.computeAndSetTypeEnvironmentForOperator(rOp);
context.computeAndSetTypeEnvironmentForOperator(unnest);
return true;
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.StatefulFunctionCallExpression 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));
}
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.StatefulFunctionCallExpression in project asterixdb by apache.
the class RunningAggregatePOperator method contributeRuntimeOperator.
@Override
public void contributeRuntimeOperator(IHyracksJobBuilder builder, JobGenContext context, ILogicalOperator op, IOperatorSchema opSchema, IOperatorSchema[] inputSchemas, IOperatorSchema outerPlanSchema) throws AlgebricksException {
RunningAggregateOperator ragg = (RunningAggregateOperator) op;
List<LogicalVariable> variables = ragg.getVariables();
List<Mutable<ILogicalExpression>> expressions = ragg.getExpressions();
int[] outColumns = new int[variables.size()];
for (int i = 0; i < outColumns.length; i++) {
outColumns[i] = opSchema.findVariable(variables.get(i));
}
IRunningAggregateEvaluatorFactory[] runningAggFuns = new IRunningAggregateEvaluatorFactory[expressions.size()];
IExpressionRuntimeProvider expressionRuntimeProvider = context.getExpressionRuntimeProvider();
for (int i = 0; i < runningAggFuns.length; i++) {
StatefulFunctionCallExpression expr = (StatefulFunctionCallExpression) expressions.get(i).getValue();
runningAggFuns[i] = expressionRuntimeProvider.createRunningAggregateFunctionFactory(expr, context.getTypeEnvironment(op.getInputs().get(0).getValue()), inputSchemas, context);
}
// TODO push projections into the operator
int[] projectionList = JobGenHelper.projectAllVariables(opSchema);
RunningAggregateRuntimeFactory runtime = new RunningAggregateRuntimeFactory(outColumns, runningAggFuns, projectionList);
// contribute one Asterix framewriter
RecordDescriptor recDesc = JobGenHelper.mkRecordDescriptor(context.getTypeEnvironment(op), opSchema, context);
builder.contributeMicroOperator(ragg, runtime, recDesc);
// and contribute one edge from its child
ILogicalOperator src = ragg.getInputs().get(0).getValue();
builder.contributeGraphEdge(src, 0, ragg, 0);
}
Aggregations