Search in sources :

Example 1 with FunctionSignature

use of org.apache.asterix.common.functions.FunctionSignature in project asterixdb by apache.

the class SqlppQueryRewriter method inlineDeclaredUdfs.

protected void inlineDeclaredUdfs() throws CompilationException {
    List<FunctionSignature> funIds = new ArrayList<FunctionSignature>();
    for (FunctionDecl fdecl : declaredFunctions) {
        funIds.add(fdecl.getSignature());
    }
    List<FunctionDecl> usedStoredFunctionDecls = new ArrayList<>();
    for (Expression topLevelExpr : topExpr.getDirectlyEnclosedExpressions()) {
        usedStoredFunctionDecls.addAll(FunctionUtil.retrieveUsedStoredFunctions(metadataProvider, topLevelExpr, funIds, null, expr -> getFunctionCalls(expr), func -> functionRepository.getFunctionDecl(func), signature -> FunctionMapUtil.normalizeBuiltinFunctionSignature(signature, false)));
    }
    declaredFunctions.addAll(usedStoredFunctionDecls);
    if (!declaredFunctions.isEmpty()) {
        SqlppInlineUdfsVisitor visitor = new SqlppInlineUdfsVisitor(context, new SqlppFunctionBodyRewriterFactory(), /* the rewriter for function bodies expressions*/
        declaredFunctions, metadataProvider);
        while (topExpr.accept(visitor, declaredFunctions)) {
        // loop until no more changes
        }
    }
    declaredFunctions.removeAll(usedStoredFunctionDecls);
}
Also used : UnnestClause(org.apache.asterix.lang.sqlpp.clause.UnnestClause) OperatorExpressionVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.OperatorExpressionVisitor) JoinClause(org.apache.asterix.lang.sqlpp.clause.JoinClause) FromTerm(org.apache.asterix.lang.sqlpp.clause.FromTerm) VariableCheckAndRewriteVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.VariableCheckAndRewriteVisitor) SqlppGlobalAggregationSugarVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SqlppGlobalAggregationSugarVisitor) InlineColumnAliasVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.InlineColumnAliasVisitor) SelectElement(org.apache.asterix.lang.sqlpp.clause.SelectElement) SelectRegular(org.apache.asterix.lang.sqlpp.clause.SelectRegular) FunctionParser(org.apache.asterix.lang.sqlpp.parser.FunctionParser) NestClause(org.apache.asterix.lang.sqlpp.clause.NestClause) ArrayList(java.util.ArrayList) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) HavingClause(org.apache.asterix.lang.sqlpp.clause.HavingClause) IReturningStatement(org.apache.asterix.lang.common.base.IReturningStatement) ISqlppVisitor(org.apache.asterix.lang.sqlpp.visitor.base.ISqlppVisitor) FunctionUtil(org.apache.asterix.lang.common.util.FunctionUtil) LetClause(org.apache.asterix.lang.common.clause.LetClause) SubstituteGroupbyExpressionWithVariableVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SubstituteGroupbyExpressionWithVariableVisitor) SqlppListInputFunctionRewriteVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SqlppListInputFunctionRewriteVisitor) SetOperationRight(org.apache.asterix.lang.sqlpp.struct.SetOperationRight) SelectBlock(org.apache.asterix.lang.sqlpp.clause.SelectBlock) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) SelectSetOperation(org.apache.asterix.lang.sqlpp.clause.SelectSetOperation) FunctionDecl(org.apache.asterix.lang.common.statement.FunctionDecl) CompilationException(org.apache.asterix.common.exceptions.CompilationException) SelectClause(org.apache.asterix.lang.sqlpp.clause.SelectClause) SqlppInlineUdfsVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SqlppInlineUdfsVisitor) SqlppGroupByVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SqlppGroupByVisitor) Expression(org.apache.asterix.lang.common.base.Expression) Set(java.util.Set) LangRewritingContext(org.apache.asterix.lang.common.rewrites.LangRewritingContext) Projection(org.apache.asterix.lang.sqlpp.clause.Projection) AbstractBinaryCorrelateClause(org.apache.asterix.lang.sqlpp.clause.AbstractBinaryCorrelateClause) SqlppParserFactory(org.apache.asterix.lang.sqlpp.parser.SqlppParserFactory) IQueryRewriter(org.apache.asterix.lang.common.base.IQueryRewriter) SqlppBuiltinFunctionRewriteVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SqlppBuiltinFunctionRewriteVisitor) GatherFunctionCallsVisitor(org.apache.asterix.lang.common.visitor.GatherFunctionCallsVisitor) List(java.util.List) MetadataProvider(org.apache.asterix.metadata.declared.MetadataProvider) GenerateColumnNameVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.GenerateColumnNameVisitor) FunctionMapUtil(org.apache.asterix.lang.sqlpp.util.FunctionMapUtil) InlineWithExpressionVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.InlineWithExpressionVisitor) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) FromClause(org.apache.asterix.lang.sqlpp.clause.FromClause) IndependentSubquery(org.apache.asterix.lang.sqlpp.expression.IndependentSubquery) SetOperationVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SetOperationVisitor) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) ArrayList(java.util.ArrayList) SqlppInlineUdfsVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SqlppInlineUdfsVisitor) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) FunctionDecl(org.apache.asterix.lang.common.statement.FunctionDecl)

Example 2 with FunctionSignature

use of org.apache.asterix.common.functions.FunctionSignature in project asterixdb by apache.

the class FunctionMapUtil method normalizedListInputFunctions.

/**
     * Rewrites a variable-arg, user-surface function call into an internal, list-arg function.
     *
     * @param callExpr
     *            The input call expression.
     * @return a new call expression that calls the corresponding AsterixDB internal function.
     */
public static CallExpr normalizedListInputFunctions(CallExpr callExpr) {
    FunctionSignature fs = callExpr.getFunctionSignature();
    String internalFuncName = LIST_INPUT_FUNCTION_MAP.get(fs.getName().toLowerCase());
    if (internalFuncName == null) {
        return callExpr;
    }
    callExpr.setFunctionSignature(new FunctionSignature(FunctionConstants.ASTERIX_NS, internalFuncName, 1));
    callExpr.setExprList(new ArrayList<>(Collections.singletonList(new ListConstructor(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR, callExpr.getExprList()))));
    return callExpr;
}
Also used : FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) ListConstructor(org.apache.asterix.lang.common.expression.ListConstructor)

Example 3 with FunctionSignature

use of org.apache.asterix.common.functions.FunctionSignature in project asterixdb by apache.

the class ExpressionTypeComputer method getTypeForFunction.

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.getNamespace(), fi.getName(), fi.getArity());
    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);
}
Also used : IResultTypeComputer(org.apache.asterix.om.typecomputer.base.IResultTypeComputer) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature)

Example 4 with FunctionSignature

use of org.apache.asterix.common.functions.FunctionSignature in project asterixdb by apache.

the class FunctionSignatures method get.

public FunctionSignature get(String dataverse, String name, int arity) {
    FunctionSignature fid = new FunctionSignature(dataverse, name, arity);
    FunctionExpressionMap possibleFD = functionMap.get(fid);
    if (possibleFD == null) {
        return null;
    } else {
        return possibleFD.get(arity);
    }
}
Also used : FunctionSignature(org.apache.asterix.common.functions.FunctionSignature)

Example 5 with FunctionSignature

use of org.apache.asterix.common.functions.FunctionSignature in project asterixdb by apache.

the class FeedConnectionTupleTranslator method createFeedConnFromRecord.

private FeedConnection createFeedConnFromRecord(ARecord feedConnRecord) {
    String dataverseName = ((AString) feedConnRecord.getValueByPos(MetadataRecordTypes.FEED_CONN_DATAVERSE_NAME_FIELD_INDEX)).getStringValue();
    String feedName = ((AString) feedConnRecord.getValueByPos(MetadataRecordTypes.FEED_CONN_FEED_NAME_FIELD_INDEX)).getStringValue();
    String datasetName = ((AString) feedConnRecord.getValueByPos(MetadataRecordTypes.FEED_CONN_DATASET_NAME_FIELD_INDEX)).getStringValue();
    String outputType = ((AString) feedConnRecord.getValueByPos(MetadataRecordTypes.FEED_CONN_OUTPUT_TYPE_INDEX)).getStringValue();
    String policyName = ((AString) feedConnRecord.getValueByPos(MetadataRecordTypes.FEED_CONN_POLICY_FIELD_INDEX)).getStringValue();
    ArrayList<FunctionSignature> appliedFunctions = null;
    Object o = feedConnRecord.getValueByPos(MetadataRecordTypes.FEED_CONN_APPLIED_FUNCTIONS_FIELD_INDEX);
    IACursor cursor;
    if (!(o instanceof ANull) && !(o instanceof AMissing)) {
        appliedFunctions = new ArrayList<>();
        FunctionSignature functionSignature;
        cursor = ((AUnorderedList) feedConnRecord.getValueByPos(MetadataRecordTypes.FEED_CONN_APPLIED_FUNCTIONS_FIELD_INDEX)).getCursor();
        while (cursor.next()) {
            //TODO: allow different arity
            functionSignature = new FunctionSignature(dataverseName, ((AString) cursor.get()).getStringValue(), 1);
            appliedFunctions.add(functionSignature);
        }
    }
    return new FeedConnection(dataverseName, feedName, datasetName, appliedFunctions, policyName, outputType);
}
Also used : ANull(org.apache.asterix.om.base.ANull) FeedConnection(org.apache.asterix.metadata.entities.FeedConnection) AMissing(org.apache.asterix.om.base.AMissing) AString(org.apache.asterix.om.base.AString) IACursor(org.apache.asterix.om.base.IACursor) AString(org.apache.asterix.om.base.AString) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature)

Aggregations

FunctionSignature (org.apache.asterix.common.functions.FunctionSignature)30 ArrayList (java.util.ArrayList)11 Expression (org.apache.asterix.lang.common.base.Expression)11 Function (org.apache.asterix.metadata.entities.Function)9 CompilationException (org.apache.asterix.common.exceptions.CompilationException)8 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)6 ACIDException (org.apache.asterix.common.exceptions.ACIDException)5 IOException (java.io.IOException)4 RemoteException (java.rmi.RemoteException)4 AsterixException (org.apache.asterix.common.exceptions.AsterixException)4 CallExpr (org.apache.asterix.lang.common.expression.CallExpr)4 LiteralExpr (org.apache.asterix.lang.common.expression.LiteralExpr)4 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)4 CaseExpression (org.apache.asterix.lang.sqlpp.expression.CaseExpression)4 MetadataException (org.apache.asterix.metadata.MetadataException)4 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)4 EntityId (org.apache.asterix.active.EntityId)3 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)3 MetadataTransactionContext (org.apache.asterix.metadata.MetadataTransactionContext)3 FeedConnection (org.apache.asterix.metadata.entities.FeedConnection)3