Search in sources :

Example 16 with Function

use of org.apache.asterix.metadata.entities.Function in project asterixdb by apache.

the class MetadataNode method getDataverseFunctions.

@Override
public List<Function> getDataverseFunctions(JobId jobId, String dataverseName) throws MetadataException, RemoteException {
    try {
        ITupleReference searchKey = createTuple(dataverseName);
        FunctionTupleTranslator tupleReaderWriter = tupleTranslatorProvider.getFunctionTupleTranslator(false);
        IValueExtractor<Function> valueExtractor = new MetadataEntityValueExtractor<>(tupleReaderWriter);
        List<Function> results = new ArrayList<>();
        searchIndex(jobId, MetadataPrimaryIndexes.FUNCTION_DATASET, searchKey, valueExtractor, results);
        return results;
    } catch (HyracksDataException e) {
        throw new MetadataException(e);
    }
}
Also used : Function(org.apache.asterix.metadata.entities.Function) MetadataEntityValueExtractor(org.apache.asterix.metadata.valueextractors.MetadataEntityValueExtractor) ITupleReference(org.apache.hyracks.dataflow.common.data.accessors.ITupleReference) ArrayList(java.util.ArrayList) FunctionTupleTranslator(org.apache.asterix.metadata.entitytupletranslators.FunctionTupleTranslator) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 17 with Function

use of org.apache.asterix.metadata.entities.Function in project asterixdb by apache.

the class FunctionUtil method retrieveUsedStoredFunctions.

/**
     * Retrieve stored functions (from CREATE FUNCTION statements) that have been used in an expression.
     *
     * @param metadataProvider,
     *            the metadata provider
     * @param expression,
     *            the expression for analysis
     * @param declaredFunctions,
     *            a set of declared functions in the query, which can potentially override stored functions.
     * @param functionCollector,
     *            for collecting function calls in the <code>expression</code>
     * @param functionParser,
     *            for parsing stored functions in the string represetnation.
     * @param functionNormalizer,
     *            for normalizing function names.
     * @throws CompilationException
     */
public static List<FunctionDecl> retrieveUsedStoredFunctions(MetadataProvider metadataProvider, Expression expression, List<FunctionSignature> declaredFunctions, List<FunctionDecl> inputFunctionDecls, IFunctionCollector functionCollector, IFunctionParser functionParser, IFunctionNormalizer functionNormalizer) throws CompilationException {
    List<FunctionDecl> functionDecls = inputFunctionDecls == null ? new ArrayList<>() : new ArrayList<>(inputFunctionDecls);
    if (expression == null) {
        return functionDecls;
    }
    String value = metadataProvider.getConfig().get(FunctionUtil.IMPORT_PRIVATE_FUNCTIONS);
    boolean includePrivateFunctions = (value != null) ? Boolean.valueOf(value.toLowerCase()) : false;
    Set<FunctionSignature> functionCalls = functionCollector.getFunctionCalls(expression);
    for (FunctionSignature signature : functionCalls) {
        if (declaredFunctions != null && declaredFunctions.contains(signature)) {
            continue;
        }
        if (signature.getNamespace() == null) {
            signature.setNamespace(metadataProvider.getDefaultDataverseName());
        }
        String namespace = signature.getNamespace();
        // Checks the existence of the referred dataverse.
        if (metadataProvider.findDataverse(namespace) == null && !namespace.equals(FunctionConstants.ASTERIX_NS)) {
            throw new CompilationException("In function call \"" + namespace + "." + signature.getName() + "(...)\", the dataverse \"" + namespace + "\" cannot be found!");
        }
        Function function = lookupUserDefinedFunctionDecl(metadataProvider.getMetadataTxnContext(), signature);
        if (function == null) {
            FunctionSignature normalizedSignature = functionNormalizer == null ? signature : functionNormalizer.normalizeBuiltinFunctionSignature(signature);
            if (BuiltinFunctions.isBuiltinCompilerFunction(normalizedSignature, includePrivateFunctions)) {
                continue;
            }
            StringBuilder messageBuilder = new StringBuilder();
            if (!functionDecls.isEmpty()) {
                messageBuilder.append("function " + functionDecls.get(functionDecls.size() - 1).getSignature() + " depends upon function " + signature + " which is undefined");
            } else {
                messageBuilder.append("function " + signature + " is not defined");
            }
            throw new CompilationException(messageBuilder.toString());
        }
        if (function.getLanguage().equalsIgnoreCase(Function.LANGUAGE_AQL)) {
            FunctionDecl functionDecl = functionParser.getFunctionDecl(function);
            if (functionDecl != null) {
                if (functionDecls.contains(functionDecl)) {
                    throw new CompilationException("Recursive invocation " + functionDecls.get(functionDecls.size() - 1).getSignature() + " <==> " + functionDecl.getSignature());
                }
                functionDecls.add(functionDecl);
                functionDecls = retrieveUsedStoredFunctions(metadataProvider, functionDecl.getFuncBody(), declaredFunctions, functionDecls, functionCollector, functionParser, functionNormalizer);
            }
        }
    }
    return functionDecls;
}
Also used : CompilationException(org.apache.asterix.common.exceptions.CompilationException) Function(org.apache.asterix.metadata.entities.Function) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) FunctionDecl(org.apache.asterix.lang.common.statement.FunctionDecl)

Example 18 with Function

use of org.apache.asterix.metadata.entities.Function in project asterixdb by apache.

the class SubscribeFeedStatement method initialize.

public void initialize(MetadataTransactionContext mdTxnCtx) throws MetadataException {
    this.query = new Query(false);
    EntityId sourceFeedId = connectionRequest.getReceivingFeedId();
    Feed subscriberFeed = MetadataManager.INSTANCE.getFeed(mdTxnCtx, connectionRequest.getReceivingFeedId().getDataverse(), connectionRequest.getReceivingFeedId().getEntityName());
    if (subscriberFeed == null) {
        throw new IllegalStateException(" Subscriber feed " + subscriberFeed + " not found.");
    }
    String feedOutputType = getOutputType(mdTxnCtx);
    StringBuilder builder = new StringBuilder();
    builder.append("use dataverse " + sourceFeedId.getDataverse() + ";\n");
    builder.append("set" + " " + FunctionUtil.IMPORT_PRIVATE_FUNCTIONS + " " + "'" + Boolean.TRUE + "'" + ";\n");
    builder.append("set" + " " + FeedActivityDetails.FEED_POLICY_NAME + " " + "'" + connectionRequest.getPolicy() + "'" + ";\n");
    builder.append("insert into dataset " + connectionRequest.getTargetDataset() + " ");
    builder.append(" (" + " for $x in feed-collect ('" + sourceFeedId.getDataverse() + "'" + "," + "'" + sourceFeedId.getEntityName() + "'" + "," + "'" + connectionRequest.getReceivingFeedId().getEntityName() + "'" + "," + "'" + connectionRequest.getSubscriptionLocation().name() + "'" + "," + "'" + connectionRequest.getTargetDataset() + "'" + "," + "'" + feedOutputType + "'" + ")");
    List<FunctionSignature> functionsToApply = connectionRequest.getFunctionsToApply();
    if ((functionsToApply != null) && functionsToApply.isEmpty()) {
        builder.append(" return $x");
    } else {
        Function function;
        String rValueName = "x";
        String lValueName = "y";
        int variableIndex = 0;
        for (FunctionSignature appliedFunction : functionsToApply) {
            function = MetadataManager.INSTANCE.getFunction(mdTxnCtx, appliedFunction);
            variableIndex++;
            switch(function.getLanguage().toUpperCase()) {
                case Function.LANGUAGE_AQL:
                    builder.append(" let " + "$" + lValueName + variableIndex + ":=" + function.getName() + "(" + "$" + rValueName + ")");
                    rValueName = lValueName + variableIndex;
                    break;
                case Function.LANGUAGE_JAVA:
                    builder.append(" let " + "$" + lValueName + variableIndex + ":=" + function.getName() + "(" + "$" + rValueName + ")");
                    rValueName = lValueName + variableIndex;
                    break;
            }
            builder.append("\n");
        }
        builder.append("return $" + lValueName + variableIndex);
    }
    builder.append(")");
    builder.append(";");
    if (LOGGER.isLoggable(Level.INFO)) {
        LOGGER.info("Connect feed statement translated to\n" + builder.toString());
    }
    IParser parser = parserFactory.createParser(new StringReader(builder.toString()));
    List<Statement> statements;
    try {
        statements = parser.parse();
        query = ((InsertStatement) statements.get(INSERT_STATEMENT_POS)).getQuery();
    } catch (CompilationException pe) {
        throw new MetadataException(pe);
    }
}
Also used : CompilationException(org.apache.asterix.common.exceptions.CompilationException) Query(org.apache.asterix.lang.common.statement.Query) InsertStatement(org.apache.asterix.lang.common.statement.InsertStatement) Statement(org.apache.asterix.lang.common.base.Statement) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) MetadataException(org.apache.asterix.metadata.MetadataException) EntityId(org.apache.asterix.active.EntityId) Function(org.apache.asterix.metadata.entities.Function) StringReader(java.io.StringReader) Feed(org.apache.asterix.metadata.entities.Feed) IParser(org.apache.asterix.lang.common.base.IParser)

Aggregations

Function (org.apache.asterix.metadata.entities.Function)18 FunctionSignature (org.apache.asterix.common.functions.FunctionSignature)9 ACIDException (org.apache.asterix.common.exceptions.ACIDException)8 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)8 RemoteException (java.rmi.RemoteException)7 IOException (java.io.IOException)6 AsterixException (org.apache.asterix.common.exceptions.AsterixException)6 CompilationException (org.apache.asterix.common.exceptions.CompilationException)6 MetadataException (org.apache.asterix.metadata.MetadataException)6 MetadataTransactionContext (org.apache.asterix.metadata.MetadataTransactionContext)6 ArrayList (java.util.ArrayList)4 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)4 ITupleReference (org.apache.hyracks.dataflow.common.data.accessors.ITupleReference)4 EntityId (org.apache.asterix.active.EntityId)3 DatasourceAdapter (org.apache.asterix.metadata.entities.DatasourceAdapter)3 Dataverse (org.apache.asterix.metadata.entities.Dataverse)3 Feed (org.apache.asterix.metadata.entities.Feed)3 FeedConnection (org.apache.asterix.metadata.entities.FeedConnection)3 ActiveJobNotificationHandler (org.apache.asterix.active.ActiveJobNotificationHandler)2 ActiveLifecycleListener (org.apache.asterix.active.ActiveLifecycleListener)2