@Override public ILogicalExpression visitAggregateFunctionCallExpression(AggregateFunctionCallExpression expr, Void arg) throws AlgebricksException { AggregateFunctionCallExpression exprCopy = new AggregateFunctionCallExpression(expr.getFunctionInfo(), expr.isTwoStep(), deepCopyExpressionReferenceList(expr.getArguments())); exprCopy.setStepOneAggregate(expr.getStepOneAggregate()); exprCopy.setStepTwoAggregate(expr.getStepTwoAggregate()); deepCopyAnnotations(expr, exprCopy); deepCopyOpaqueParameters(expr, exprCopy); copySourceLocation(expr, exprCopy); return exprCopy; }
@Override public AggregateFunctionCallExpression cloneExpression() { cloneAnnotations(); List<Mutable<ILogicalExpression>> clonedArgs = cloneArguments(); AggregateFunctionCallExpression fun = new AggregateFunctionCallExpression(finfo, twoStep, clonedArgs); fun.setStepTwoAggregate(stepTwoAggregate); fun.setStepOneAggregate(stepOneAggregate); fun.setSourceLocation(sourceLoc); // opaqueParameters are not really cloned fun.setOpaqueParameters(getOpaqueParameters()); return fun; }
@Override public ILogicalExpression createMergeAggregation(LogicalVariable originalProducedVar, ILogicalExpression expr, IOptimizationContext env) throws AlgebricksException { AggregateFunctionCallExpression agg = (AggregateFunctionCallExpression) expr; FunctionIdentifier fid = agg.getFunctionIdentifier(); VariableReferenceExpression tempVarExpr = new VariableReferenceExpression(originalProducedVar); tempVarExpr.setSourceLocation(agg.getSourceLocation()); List<Mutable<ILogicalExpression>> arguments = new ArrayList<Mutable<ILogicalExpression>>(); Mutable<ILogicalExpression> mutableExpression = new MutableObject<ILogicalExpression>(tempVarExpr); arguments.add(mutableExpression); /** * For global aggregate, the merge function is ALWAYS the same as the original aggregate function. */ FunctionIdentifier mergeFid = BuiltinFunctions.isGlobalAggregateFunction(fid) ? fid : BuiltinFunctions.getIntermediateAggregateFunction(fid); if (mergeFid == null) { /** * In this case, no merge function (unimplemented) for the local-side aggregate function */ return null; } AggregateFunctionCallExpression mergeExpr = BuiltinFunctions.makeAggregateFunctionExpression(mergeFid, arguments); mergeExpr.setSourceLocation(agg.getSourceLocation()); return mergeExpr; } }
public static AggregateFunctionCallExpression makeAggregateFunctionExpression(FunctionIdentifier fi, List<Mutable<ILogicalExpression>> args) { IFunctionInfo finfo = getAsterixFunctionInfo(fi); IFunctionInfo fiLocal = aggregateToLocalAggregate.get(finfo); IFunctionInfo fiGlobal = aggregateToGlobalAggregate.get(finfo); if (fiLocal != null && fiGlobal != null) { AggregateFunctionCallExpression fun = new AggregateFunctionCallExpression(finfo, true, args); fun.setStepTwoAggregate(fiGlobal); fun.setStepOneAggregate(fiLocal); return fun; } else { return new AggregateFunctionCallExpression(finfo, false, args); } }
if (!aggFun.isTwoStep()) { return new Pair<>(false, null); Mutable<ILogicalExpression> expRef = initExprs.get(i); AggregateFunctionCallExpression aggFun = (AggregateFunctionCallExpression) expRef.getValue(); SourceLocation aggFunSourceLoc = aggFun.getSourceLocation(); IFunctionInfo fi1 = aggFun.getStepOneAggregate(); List<Mutable<ILogicalExpression>> newArgs = new ArrayList<>(aggFun.getArguments().size()); for (Mutable<ILogicalExpression> er : aggFun.getArguments()) { newArgs.add(new MutableObject<>(er.getValue().cloneExpression())); IFunctionInfo fi2 = aggFun.getStepTwoAggregate(); inf.simAggs = new ArrayList<>(); toReplaceSet.add(inf); AggregateFunctionCallExpression aggLocal = new AggregateFunctionCallExpression(fi1, false, newArgs); aggLocal.setSourceLocation(aggFunSourceLoc); pushedExprs.add(new MutableObject<>(aggLocal)); AggregateExprInfo aei = new AggregateExprInfo();
@Override public String visitAggregateFunctionCallExpression(AggregateFunctionCallExpression expr, Integer indent) throws AlgebricksException { if (expr.isTwoStep()) { IFunctionInfo step1Agg = expr.getStepOneAggregate(); String step1 = (step1Agg != null ? step1Agg.getFunctionIdentifier().toString() : "null"); IFunctionInfo step2Agg = expr.getStepTwoAggregate(); String step2 = (step2Agg != null ? step2Agg.getFunctionIdentifier().toString() : "null"); StringBuilder sb = new StringBuilder(); sb.append("function-call: [" + step1 + "|" + step2 + "], Args:"); appendArguments(sb, expr.getArguments(), indent + 2); return sb.toString(); } else if (expr.getFunctionInfo() == null) { StringBuilder sb = new StringBuilder(); sb.append("function-call: [null], Args:"); appendArguments(sb, expr.getArguments(), indent + 2); return sb.toString(); } else { return appendFunction(new StringBuilder(), expr, indent).toString(); } }
recordVarRef.setSourceLocation(sourceLoc); aggArgList.add(new MutableObject<>(recordVarRef)); AggregateFunctionCallExpression aggExpr = new AggregateFunctionCallExpression( FunctionUtil.getFunctionInfo(BuiltinFunctions.LISTIFY), false, aggArgList); aggExpr.setSourceLocation(sourceLoc); aggExprList.add(new MutableObject<>(aggExpr)); AggregateOperator aggOp = new AggregateOperator(aggVarList, aggExprList);
private void rewriteAggregateInNestedSubplan(LogicalVariable oldAggVar, AbstractOperatorWithNestedPlans nspOp, AggregateFunctionCallExpression aggFun, LogicalVariable newAggVar, IOptimizationContext context) throws AlgebricksException { for (int j = 0; j < nspOp.getNestedPlans().size(); j++) { AggregateOperator aggOp = (AggregateOperator) nspOp.getNestedPlans().get(j).getRoots().get(0).getValue(); int n = aggOp.getVariables().size(); for (int i = 0; i < n; i++) { LogicalVariable v = aggOp.getVariables().get(i); if (v.equals(oldAggVar)) { AbstractFunctionCallExpression oldAggExpr = (AbstractFunctionCallExpression) aggOp.getExpressions().get(i).getValue(); AggregateFunctionCallExpression newAggFun = BuiltinFunctions .makeAggregateFunctionExpression(aggFun.getFunctionIdentifier(), new ArrayList<>()); newAggFun.setSourceLocation(oldAggExpr.getSourceLocation()); for (Mutable<ILogicalExpression> arg : oldAggExpr.getArguments()) { ILogicalExpression cloned = arg.getValue().cloneExpression(); newAggFun.getArguments().add(new MutableObject<>(cloned)); } aggOp.getVariables().add(newAggVar); aggOp.getExpressions().add(new MutableObject<>(newAggFun)); context.computeAndSetTypeEnvironmentForOperator(aggOp); break; } } } }
@Override public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException { // Check if aggregate function. AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue(); if (op.getOperatorTag() != LogicalOperatorTag.AGGREGATE) { return false; } AggregateOperator aggregate = (AggregateOperator) op; Mutable<ILogicalExpression> mutableLogicalExpression = aggregate.getExpressions().get(0); ILogicalExpression logicalExpression = mutableLogicalExpression.getValue(); if (logicalExpression.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) { return false; } AbstractFunctionCallExpression functionCall = (AbstractFunctionCallExpression) logicalExpression; if (AGGREGATE_MAP.containsKey(functionCall.getFunctionIdentifier())) { AggregateFunctionCallExpression aggregateFunctionCall = (AggregateFunctionCallExpression) functionCall; if (aggregateFunctionCall.isTwoStep()) { return false; } aggregateFunctionCall.setTwoStep(true); aggregateFunctionCall.setStepOneAggregate(AGGREGATE_MAP.get(functionCall.getFunctionIdentifier()).first); aggregateFunctionCall.setStepTwoAggregate(AGGREGATE_MAP.get(functionCall.getFunctionIdentifier()).second); return true; } return false; }
private static ILogicalExpression afce(Function fn, boolean isTwoStep, ILogicalExpression... argExprs) { List<Mutable<ILogicalExpression>> args = new ArrayList<Mutable<ILogicalExpression>>(); for (ILogicalExpression e : argExprs) { args.add(mutable(e)); } return new AggregateFunctionCallExpression(fn, isTwoStep, args); }
@Override public Object getType(ILogicalExpression expr, IVariableTypeEnvironment env, IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException { AggregateFunctionCallExpression agg = (AggregateFunctionCallExpression) expr; FunctionIdentifier partialFid = agg.getFunctionIdentifier(); if (partialFid.equals(BuiltinFunctions.SERIAL_GLOBAL_AVG)) { partialFid = BuiltinFunctions.SERIAL_LOCAL_AVG; } AggregateFunctionCallExpression partialAgg = BuiltinFunctions.makeAggregateFunctionExpression(partialFid, agg.getArguments()); return getTypeForFunction(partialAgg, env, metadataProvider); }
AggregateFunctionCallExpression aggFuncExpr = BuiltinFunctions.makeAggregateFunctionExpression(aggFuncIdent, aggArgs); aggFuncExpr.setSourceLocation(assignFuncExpr.getSourceLocation()); LogicalVariable newVar = context.newVar(); aggOp.getVariables().add(newVar);
protected Pair<ILogicalOperator, LogicalVariable> aggListifyForSubquery(LogicalVariable var, Mutable<ILogicalOperator> opRef, boolean bProject) { SourceLocation sourceLoc = opRef.getValue().getSourceLocation(); AggregateFunctionCallExpression funAgg = BuiltinFunctions.makeAggregateFunctionExpression(BuiltinFunctions.LISTIFY, new ArrayList<>()); funAgg.getArguments().add(new MutableObject<>(new VariableReferenceExpression(var))); funAgg.setSourceLocation(sourceLoc); LogicalVariable varListified = context.newSubplanOutputVar(); AggregateOperator agg = new AggregateOperator(mkSingletonArrayList(varListified), mkSingletonArrayList(new MutableObject<>(funAgg))); agg.getInputs().add(opRef); agg.setSourceLocation(sourceLoc); ILogicalOperator res; if (bProject) { ProjectOperator pr = new ProjectOperator(varListified); pr.getInputs().add(new MutableObject<>(agg)); pr.setSourceLocation(sourceLoc); res = pr; } else { res = agg; } return new Pair<>(res, varListified); }
@Override public IAggregateEvaluatorFactory createAggregateFunctionFactory(AggregateFunctionCallExpression expr, IVariableTypeEnvironment env, IOperatorSchema[] inputSchemas, JobGenContext context) throws AlgebricksException { Function fn = (Function) expr.getFunctionInfo(); IScalarEvaluatorFactory[] argFactories = createArgumentEvaluatorFactories(env, inputSchemas, context, expr.getArguments()); try { return fn.createAggregateEvaluatorFactory(argFactories); } catch (SystemException e) { throw new AlgebricksException(e); } }
AggregateFunctionCallExpression fListify = BuiltinFunctions.makeAggregateFunctionExpression( BuiltinFunctions.LISTIFY, mkSingletonArrayList(new MutableObject<>(varToListifyRef))); fListify.setSourceLocation(currentTopOp.getSourceLocation()); mkSingletonArrayList(new MutableObject<>(fListify))); aggOp.getInputs().add(new MutableObject<>(currentTopOp)); aggOp.setSourceLocation(fListify.getSourceLocation());
Mutable<ILogicalExpression> aggArgExprRef = new MutableObject<>(unnestVarRef); AggregateFunctionCallExpression aggExpr = new AggregateFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.LISTIFY), false, new ArrayList<>(Collections.singletonList(aggArgExprRef))); aggExpr.setSourceLocation(sourceLoc); AggregateOperator aggregateOperator = new AggregateOperator(new ArrayList<>(Collections.singletonList(aggVar)), new ArrayList<>(Collections.singletonList(new MutableObject<>(aggExpr))));
public static AggregateFunctionCallExpression makeSerializableAggregateFunctionExpression(FunctionIdentifier fi, List<Mutable<ILogicalExpression>> args) { IFunctionInfo finfo = getAsterixFunctionInfo(fi); IFunctionInfo serializableFinfo = aggregateToSerializableAggregate.get(finfo); if (serializableFinfo == null) { throw new IllegalStateException( "no serializable implementation for aggregate function " + serializableFinfo); } IFunctionInfo fiLocal = aggregateToLocalAggregate.get(serializableFinfo); IFunctionInfo fiGlobal = aggregateToGlobalAggregate.get(serializableFinfo); if (fiLocal != null && fiGlobal != null) { AggregateFunctionCallExpression fun = new AggregateFunctionCallExpression(serializableFinfo, true, args); fun.setStepTwoAggregate(fiGlobal); fun.setStepOneAggregate(fiLocal); return fun; } else { return new AggregateFunctionCallExpression(serializableFinfo, false, args); } }
private AggregateOperator getAggregateOperator(IFunctionInfo aggregateFunction, Mutable<ILogicalExpression> aggregateArgs, LogicalVariable aggregateVariable) { List<LogicalVariable> aggregateVariables = new ArrayList<LogicalVariable>(); aggregateVariables.add(aggregateVariable); List<Mutable<ILogicalExpression>> aggregateSequenceArgs = new ArrayList<Mutable<ILogicalExpression>>(); aggregateSequenceArgs.add(aggregateArgs); List<Mutable<ILogicalExpression>> exprs = new ArrayList<Mutable<ILogicalExpression>>(); ILogicalExpression aggregateExp = new AggregateFunctionCallExpression(aggregateFunction, true, aggregateSequenceArgs); Mutable<ILogicalExpression> aggregateExpRef = new MutableObject<ILogicalExpression>(aggregateExp); exprs.add(aggregateExpRef); return new AggregateOperator(aggregateVariables, exprs); }
@Override public ISerializedAggregateEvaluatorFactory createSerializableAggregateFunctionFactory( AggregateFunctionCallExpression expr, IVariableTypeEnvironment env, IOperatorSchema[] inputSchemas, JobGenContext context) throws AlgebricksException { IScalarEvaluatorFactory[] args = codegenArguments(expr, env, inputSchemas, context); IFunctionDescriptor fd = resolveFunction(expr, env, context); switch (fd.getFunctionDescriptorTag()) { case AGGREGATE: { if (BuiltinFunctions.isAggregateFunctionSerializable(fd.getIdentifier())) { AggregateFunctionCallExpression serialAggExpr = BuiltinFunctions .makeSerializableAggregateFunctionExpression(fd.getIdentifier(), expr.getArguments()); IFunctionDescriptor afdd = resolveFunction(serialAggExpr, env, context); return afdd.createSerializableAggregateEvaluatorFactory(args); } else { throw new AlgebricksException( "Trying to create a serializable aggregate from a non-serializable aggregate function descriptor. (fi=" + expr.getFunctionIdentifier() + ")"); } } case SERIALAGGREGATE: { return fd.createSerializableAggregateEvaluatorFactory(args); } default: throw new IllegalStateException( "Invalid function descriptor " + fd.getFunctionDescriptorTag() + " expected " + FunctionDescriptorTag.SERIALAGGREGATE + " or " + FunctionDescriptorTag.AGGREGATE); } }
protected ILogicalPlan createNestedPlanWithAggregate(LogicalVariable aggOutputVar, FunctionIdentifier aggFunc, ILogicalExpression aggFnInput, Mutable<ILogicalOperator> aggOpInput) { SourceLocation sourceLoc = aggFnInput.getSourceLocation(); AggregateFunctionCallExpression aggFnCall = BuiltinFunctions.makeAggregateFunctionExpression(aggFunc, mkSingletonArrayList(new MutableObject<>(aggFnInput))); aggFnCall.setSourceLocation(sourceLoc); AggregateOperator aggOp = new AggregateOperator(mkSingletonArrayList(aggOutputVar), mkSingletonArrayList(new MutableObject<>(aggFnCall))); aggOp.getInputs().add(aggOpInput); aggOp.setSourceLocation(sourceLoc); return new ALogicalPlanImpl(new MutableObject<>(aggOp)); }