@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; } }
aggArgs.add(aggOpExpr.getArguments().get(0)); AggregateFunctionCallExpression aggFuncExpr = BuiltinFunctions.makeAggregateFunctionExpression(aggFuncIdent, aggArgs); aggFuncExpr.setSourceLocation(assignFuncExpr.getSourceLocation()); LogicalVariable newVar = context.newVar();
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)); }
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); }
LogicalVariable newVar = context.newVar(); AggregateFunctionCallExpression aggFun = BuiltinFunctions.makeAggregateFunctionExpression(fi, fce.getArguments()); aggFun.setSourceLocation(expr.getSourceLocation()); rewriteAggregateInNestedSubplan(argVar, nspOp, aggFun, newVar, context);
s.getInputs().add(eo2.second); s.setSourceLocation(sourceLoc); fAgg = BuiltinFunctions.makeAggregateFunctionExpression(BuiltinFunctions.NON_EMPTY_STREAM, new ArrayList<>()); fAgg.setSourceLocation(sourceLoc); s.getInputs().add(eo2.second); s.setSourceLocation(sourceLoc); fAgg = BuiltinFunctions.makeAggregateFunctionExpression(BuiltinFunctions.EMPTY_STREAM, new ArrayList<>()); fAgg.setSourceLocation(sourceLoc);
private AbstractFunctionCallExpression lookupBuiltinFunction(String functionName, int arity, List<Mutable<ILogicalExpression>> args, SourceLocation sourceLoc) { AbstractFunctionCallExpression f; FunctionIdentifier fi = getBuiltinFunctionIdentifier(functionName, arity); if (fi == null) { return null; } if (BuiltinFunctions.isBuiltinAggregateFunction(fi)) { f = BuiltinFunctions.makeAggregateFunctionExpression(fi, args); } else if (BuiltinFunctions.isBuiltinUnnestingFunction(fi)) { UnnestingFunctionCallExpression ufce = new UnnestingFunctionCallExpression(FunctionUtil.getFunctionInfo(fi), args); ufce.setReturnsUniqueValues(BuiltinFunctions.returnsUniqueValues(fi)); f = ufce; } else if (BuiltinFunctions.isWindowFunction(fi)) { f = BuiltinFunctions.makeWindowFunctionExpression(fi, args); } else { f = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(fi), args); } f.setSourceLocation(sourceLoc); return f; }
frameRecordVarRef.setSourceLocation(sourceLoc); AggregateFunctionCallExpression listifyCall = BuiltinFunctions.makeAggregateFunctionExpression( BuiltinFunctions.LISTIFY, mkSingletonArrayList(new MutableObject<>(frameRecordVarRef))); listifyCall.setSourceLocation(sourceLoc);
AggregateFunctionCallExpression fListify = BuiltinFunctions.makeAggregateFunctionExpression( BuiltinFunctions.LISTIFY, mkSingletonArrayList(new MutableObject<>(varToListifyRef))); fListify.setSourceLocation(currentTopOp.getSourceLocation());