public static IFunctionInfo getFunctionInfo(FunctionIdentifier fi) { return BuiltinFunctions.getAsterixFunctionInfo(fi); }
private FunctionIdentifier getAggregateFunctionForDistinct(FunctionSignature signature) { IFunctionInfo finfo = FunctionUtil.getFunctionInfo(signature); FunctionIdentifier aggFn = finfo != null ? BuiltinFunctions.getAggregateFunction(finfo.getFunctionIdentifier()) : null; return aggFn != null ? BuiltinFunctions.getAggregateFunctionForDistinct(aggFn) : null; }
@Override public IFunctionInfo lookupFunction(FunctionIdentifier fid) { return BuiltinFunctions.lookupFunction(fid); }
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; }
/** * Maps a user invoked function signature to a builtin internal function signature if possible. * * @param fs, * the signature of an user typed function. * @return the corresponding system internal function signature if it exists, otherwise * the input function synature. */ public static FunctionSignature normalizeBuiltinFunctionSignature(FunctionSignature fs) throws CompilationException { String name = fs.getName(); String lowerCaseName = name.toLowerCase(); String mappedName = FUNCTION_NAME_MAP.get(lowerCaseName); if (mappedName != null) { return new FunctionSignature(fs.getNamespace(), mappedName, fs.getArity()); } String understoreName = lowerCaseName.replace('_', '-'); FunctionSignature newFs = new FunctionSignature(fs.getNamespace(), understoreName, fs.getArity()); return BuiltinFunctions.isBuiltinCompilerFunction(newFs, true) ? newFs : fs; }
(AbstractFunctionCallExpression) srcAssignExprRef.getValue(); FunctionIdentifier aggFuncIdent = BuiltinFunctions.getAggregateFunction(assignFuncExpr.getFunctionIdentifier()); aggArgs.add(aggOpExpr.getArguments().get(0)); AggregateFunctionCallExpression aggFuncExpr = BuiltinFunctions.makeAggregateFunctionExpression(aggFuncIdent, aggArgs); aggFuncExpr.setSourceLocation(assignFuncExpr.getSourceLocation()); LogicalVariable newVar = context.newVar();
fs.getName()); boolean isWin = BuiltinFunctions.isWindowFunction(fi); boolean isWinAgg = isWin && BuiltinFunctions.windowFunctionHasProperty(fi, BuiltinFunctions.WindowFunctionProperty.HAS_LIST_ARG); boolean prohibitOrderClause = isWin && BuiltinFunctions.windowFunctionHasProperty(fi, BuiltinFunctions.WindowFunctionProperty.NO_ORDER_CLAUSE); boolean prohibitFrameClause = isWin && BuiltinFunctions.windowFunctionHasProperty(fi, BuiltinFunctions.WindowFunctionProperty.NO_FRAME_CLAUSE); frameRecordVarRef.setSourceLocation(sourceLoc); AggregateFunctionCallExpression listifyCall = BuiltinFunctions.makeAggregateFunctionExpression( BuiltinFunctions.LISTIFY, mkSingletonArrayList(new MutableObject<>(frameRecordVarRef))); listifyCall.setSourceLocation(sourceLoc); throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_STATE, sourceLoc, fcallExpr.getKind()); if (BuiltinFunctions.windowFunctionHasProperty(fi, BuiltinFunctions.WindowFunctionProperty.INJECT_ORDER_ARGS)) { for (Pair<OrderOperator.IOrder, Mutable<ILogicalExpression>> p : orderExprListOut) {
@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 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; } }
private IAType getTypeForFunction(AbstractFunctionCallExpression expr, IVariableTypeEnvironment env, IMetadataProvider<?, ?> mp) throws AlgebricksException { FunctionIdentifier fi = expr.getFunctionIdentifier(); // Note: built-in functions + udfs IResultTypeComputer rtc; FunctionSignature signature = new FunctionSignature(fi); if (BuiltinFunctions.isBuiltinCompilerFunction(signature, true)) { rtc = BuiltinFunctions.getResultTypeComputer(fi); } else { rtc = ((ExternalFunctionInfo) expr.getFunctionInfo()).getResultTypeComputer(); } if (rtc == null) { throw new AlgebricksException("Type computer missing for " + fi); } return rtc.computeType(expr, env, mp); }
/** * Whether a function signature is a SQL-92 core aggregate function. * * @param signature * , * the function signature. * @return true if the function signature is a SQL-92 core aggregate, * false otherwise. */ public static boolean isSql92AggregateFunction(FunctionSignature signature) { IFunctionInfo finfo = FunctionUtil.getFunctionInfo(new FunctionIdentifier(FunctionConstants.ASTERIX_NS, signature.getName().toLowerCase(), signature.getArity())); if (finfo == null) { return false; } return BuiltinFunctions.getAggregateFunction(finfo.getFunctionIdentifier()) != null; }
@Override public Expression visit(WindowExpression winExpr, ILangExpression arg) throws CompilationException { // skip variables inside list arguments of window functions (will be resolved by SqlppWindowExpressionVisitor) FunctionSignature fs = winExpr.getFunctionSignature(); FunctionIdentifier winfi = FunctionMapUtil.getInternalWindowFunction(fs); if (winfi != null) { if (BuiltinFunctions.windowFunctionHasProperty(winfi, BuiltinFunctions.WindowFunctionProperty.HAS_LIST_ARG)) { visitWindowExpressionExcludingExprList(winExpr, arg); List<Expression> exprList = winExpr.getExprList(); List<Expression> newExprList = new ArrayList<>(exprList.size()); Iterator<Expression> i = exprList.iterator(); newExprList.add(i.next()); // don't visit the list arg while (i.hasNext()) { newExprList.add(visit(i.next(), arg)); } winExpr.setExprList(newExprList); return winExpr; } } else if (FunctionMapUtil.isSql92AggregateFunction(fs)) { visitWindowExpressionExcludingExprList(winExpr, arg); return winExpr; } return super.visit(winExpr, arg); } }
private WindowOperator createHelperWindowOperator(List<Mutable<ILogicalExpression>> partExprList, List<Pair<OrderOperator.IOrder, Mutable<ILogicalExpression>>> orderExprList, LogicalVariable rowNumVar, LogicalVariable denseRankVar, ListSet<LogicalVariable> usedVars, SourceLocation sourceLoc) throws CompilationException { WindowOperator winOp = new WindowOperator(partExprList, orderExprList); winOp.setSourceLocation(sourceLoc); for (LogicalVariable usedVar : usedVars) { FunctionIdentifier fid; if (usedVar.equals(rowNumVar)) { fid = BuiltinFunctions.ROW_NUMBER_IMPL; } else if (usedVar.equals(denseRankVar)) { fid = BuiltinFunctions.DENSE_RANK_IMPL; } else { throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_STATE, sourceLoc, usedVar.toString()); } AbstractFunctionCallExpression valueExpr = BuiltinFunctions.makeWindowFunctionExpression(fid, new ArrayList<>()); if (BuiltinFunctions.windowFunctionHasProperty(valueExpr.getFunctionIdentifier(), BuiltinFunctions.WindowFunctionProperty.INJECT_ORDER_ARGS)) { for (Pair<OrderOperator.IOrder, Mutable<ILogicalExpression>> p : orderExprList) { valueExpr.getArguments().add(new MutableObject<>(p.second.getValue().cloneExpression())); } } valueExpr.setSourceLocation(winOp.getSourceLocation()); winOp.getVariables().add(usedVar); winOp.getExpressions().add(new MutableObject<>(valueExpr)); } return winOp; } }
public static void addFunction(FunctionIdentifier fi, IResultTypeComputer typeComputer, boolean isFunctional) { addFunctionWithDomain(fi, ATypeHierarchy.Domain.ANY, typeComputer, isFunctional); }
public static List<List<List<String>>> getFunctionDependencies(IQueryRewriter rewriter, Expression expression, MetadataProvider metadataProvider) throws CompilationException { Set<CallExpr> functionCalls = rewriter.getFunctionCalls(expression); //Get the List of used functions and used datasets List<List<String>> datasourceDependencies = new ArrayList<>(); List<List<String>> functionDependencies = new ArrayList<>(); for (CallExpr functionCall : functionCalls) { FunctionSignature signature = functionCall.getFunctionSignature(); FunctionIdentifier fid = new FunctionIdentifier(signature.getNamespace(), signature.getName(), signature.getArity()); if (fid.equals(BuiltinFunctions.DATASET)) { Pair<String, String> path = DatasetUtil.getDatasetInfo(metadataProvider, ((LiteralExpr) functionCall.getExprList().get(0)).getValue().getStringValue()); datasourceDependencies.add(Arrays.asList(path.first, path.second)); } else if (BuiltinFunctions.isBuiltinCompilerFunction(signature, false)) { continue; } else { functionDependencies.add(Arrays.asList(signature.getNamespace(), signature.getName(), Integer.toString(signature.getArity()))); } } List<List<List<String>>> dependencies = new ArrayList<>(); dependencies.add(datasourceDependencies); dependencies.add(functionDependencies); return dependencies; }
case FUNCTION_CALL: AbstractFunctionCallExpression fce = (AbstractFunctionCallExpression) expr; FunctionIdentifier fi = BuiltinFunctions.getAggregateFunction(fce.getFunctionIdentifier()); if (fi != null) { ILogicalExpression a1 = fce.getArguments().get(0).getValue(); LogicalVariable newVar = context.newVar(); AggregateFunctionCallExpression aggFun = BuiltinFunctions.makeAggregateFunctionExpression(fi, fce.getArguments()); aggFun.setSourceLocation(expr.getSourceLocation()); rewriteAggregateInNestedSubplan(argVar, nspOp, aggFun, newVar, context);
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; } } } }
FunctionIdentifier funcIdent = BuiltinFunctions.getAggregateFunction(funcExpr.getFunctionIdentifier()); if (funcIdent == null) {
@Override public Expression visit(WindowExpression winExpr, ILangExpression arg) throws CompilationException { if (!winExpr.hasWindowFieldList()) { SelectBlock selectBlock = stack.peek(); List<Pair<Expression, Identifier>> winFieldList = createWindowFieldList(selectBlock); winExpr.setWindowFieldList(winFieldList); } FunctionSignature signature = winExpr.getFunctionSignature(); FunctionIdentifier winfi = FunctionMapUtil.getInternalWindowFunction(signature); if (winfi != null) { winExpr.setFunctionSignature(new FunctionSignature(winfi)); rewriteSpecificWindowFunctions(winfi, winExpr); if (BuiltinFunctions.windowFunctionHasProperty(winfi, BuiltinFunctions.WindowFunctionProperty.HAS_LIST_ARG)) { wrapAggregationArguments(winExpr, 1); } } else if (FunctionMapUtil.isSql92AggregateFunction(signature)) { winExpr.setFunctionSignature(FunctionMapUtil.sql92ToCoreAggregateFunction(signature)); wrapAggregationArguments(winExpr, winExpr.getExprList().size()); } return super.visit(winExpr, arg); }
public static void addGlobalAgg(FunctionIdentifier fi, FunctionIdentifier globalfi) { aggregateToGlobalAggregate.put(getAsterixFunctionInfo(fi), getAsterixFunctionInfo(globalfi)); globalAggregateFunctions.add(getAsterixFunctionInfo(globalfi)); }