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