@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); }
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 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; } }
@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); } }