Search in sources :

Example 26 with FunctionSignature

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

the class FormatPrintVisitor method visit.

@Override
public Void visit(FunctionDropStatement del, Integer step) throws CompilationException {
    out.print(skip(step) + "drop function ");
    FunctionSignature funcSignature = del.getFunctionSignature();
    out.print(funcSignature.toString());
    out.println(SEMICOLON);
    return null;
}
Also used : FunctionSignature(org.apache.asterix.common.functions.FunctionSignature)

Example 27 with FunctionSignature

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

the class ClauseComparator method visit.

@Override
public Void visit(CallExpr callExpr, Integer step) throws CompilationException {
    FunctionSignature signature = callExpr.getFunctionSignature();
    if (signature.getNamespace() != null && signature.getNamespace().equals("Metadata") && signature.getName().equals("dataset") && signature.getArity() == 1) {
        LiteralExpr expr = (LiteralExpr) callExpr.getExprList().get(0);
        out.print(normalize(expr.getValue().getStringValue()));
    } else {
        printHints(callExpr.getHints(), step);
        out.print(generateFullName(callExpr.getFunctionSignature().getNamespace(), callExpr.getFunctionSignature().getName()) + "(");
        printDelimitedExpressions(callExpr.getExprList(), COMMA, step);
        out.print(")");
    }
    return null;
}
Also used : LiteralExpr(org.apache.asterix.lang.common.expression.LiteralExpr) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature)

Example 28 with FunctionSignature

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

the class AqlDeleteRewriteVisitor method visit.

@Override
public Void visit(DeleteStatement deleteStmt, Void visitArg) {
    List<Expression> arguments = new ArrayList<>();
    Identifier dataverseName = deleteStmt.getDataverseName();
    Identifier datasetName = deleteStmt.getDatasetName();
    String arg = dataverseName == null ? datasetName.getValue() : dataverseName.getValue() + "." + datasetName.getValue();
    LiteralExpr argumentLiteral = new LiteralExpr(new StringLiteral(arg));
    arguments.add(argumentLiteral);
    CallExpr callExpression = new CallExpr(new FunctionSignature(FunctionConstants.ASTERIX_NS, "dataset", 1), arguments);
    List<Clause> clauseList = new ArrayList<>();
    VariableExpr var = deleteStmt.getVariableExpr();
    Clause forClause = new ForClause(var, callExpression);
    clauseList.add(forClause);
    Clause whereClause = null;
    Expression condition = deleteStmt.getCondition();
    if (condition != null) {
        whereClause = new WhereClause(condition);
        clauseList.add(whereClause);
    }
    VariableExpr returnExpr = new VariableExpr(var.getVar());
    returnExpr.setIsNewVar(false);
    FLWOGRExpression flowgr = new FLWOGRExpression(clauseList, returnExpr);
    Query query = new Query(false);
    query.setBody(flowgr);
    deleteStmt.setQuery(query);
    return null;
}
Also used : Query(org.apache.asterix.lang.common.statement.Query) ArrayList(java.util.ArrayList) WhereClause(org.apache.asterix.lang.common.clause.WhereClause) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) ForClause(org.apache.asterix.lang.aql.clause.ForClause) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) Identifier(org.apache.asterix.lang.common.struct.Identifier) StringLiteral(org.apache.asterix.lang.common.literal.StringLiteral) Expression(org.apache.asterix.lang.common.base.Expression) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) LiteralExpr(org.apache.asterix.lang.common.expression.LiteralExpr) CallExpr(org.apache.asterix.lang.common.expression.CallExpr) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) ForClause(org.apache.asterix.lang.aql.clause.ForClause) Clause(org.apache.asterix.lang.common.base.Clause) WhereClause(org.apache.asterix.lang.common.clause.WhereClause)

Example 29 with FunctionSignature

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

the class AqlQueryRewriter method inlineDeclaredUdfs.

private void inlineDeclaredUdfs() throws CompilationException {
    if (topStatement == null) {
        return;
    }
    List<FunctionSignature> funIds = new ArrayList<FunctionSignature>();
    for (FunctionDecl fdecl : declaredFunctions) {
        funIds.add(fdecl.getSignature());
    }
    List<FunctionDecl> storedFunctionDecls = new ArrayList<>();
    for (Expression topLevelExpr : topStatement.getDirectlyEnclosedExpressions()) {
        storedFunctionDecls.addAll(FunctionUtil.retrieveUsedStoredFunctions(metadataProvider, topLevelExpr, funIds, null, expr -> getFunctionCalls(expr), func -> functionParser.getFunctionDecl(func), signature -> CommonFunctionMapUtil.normalizeBuiltinFunctionSignature(signature)));
        declaredFunctions.addAll(storedFunctionDecls);
    }
    if (!declaredFunctions.isEmpty()) {
        AQLInlineUdfsVisitor visitor = new AQLInlineUdfsVisitor(context, new AQLRewriterFactory(), declaredFunctions, metadataProvider);
        while (topStatement.accept(visitor, declaredFunctions)) {
        // loop until no more changes
        }
    }
    declaredFunctions.removeAll(storedFunctionDecls);
}
Also used : IAQLVisitor(org.apache.asterix.lang.aql.visitor.base.IAQLVisitor) ForClause(org.apache.asterix.lang.aql.clause.ForClause) DistinctClause(org.apache.asterix.lang.aql.clause.DistinctClause) ArrayList(java.util.ArrayList) GroupbyClause(org.apache.asterix.lang.common.clause.GroupbyClause) IReturningStatement(org.apache.asterix.lang.common.base.IReturningStatement) FunctionUtil(org.apache.asterix.lang.common.util.FunctionUtil) LetClause(org.apache.asterix.lang.common.clause.LetClause) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) Kind(org.apache.asterix.lang.common.base.Expression.Kind) FunctionDecl(org.apache.asterix.lang.common.statement.FunctionDecl) CompilationException(org.apache.asterix.common.exceptions.CompilationException) AQLParserFactory(org.apache.asterix.lang.aql.parser.AQLParserFactory) Clause(org.apache.asterix.lang.common.base.Clause) Expression(org.apache.asterix.lang.common.base.Expression) Set(java.util.Set) LangRewritingContext(org.apache.asterix.lang.common.rewrites.LangRewritingContext) VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) CommonFunctionMapUtil(org.apache.asterix.lang.common.util.CommonFunctionMapUtil) IQueryRewriter(org.apache.asterix.lang.common.base.IQueryRewriter) GatherFunctionCallsVisitor(org.apache.asterix.lang.common.visitor.GatherFunctionCallsVisitor) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) List(java.util.List) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) MetadataProvider(org.apache.asterix.metadata.declared.MetadataProvider) AqlBuiltinFunctionRewriteVisitor(org.apache.asterix.lang.aql.rewrites.visitor.AqlBuiltinFunctionRewriteVisitor) AQLInlineUdfsVisitor(org.apache.asterix.lang.aql.visitor.AQLInlineUdfsVisitor) UnionExpr(org.apache.asterix.lang.aql.expression.UnionExpr) FunctionParser(org.apache.asterix.lang.aql.parser.FunctionParser) AQLInlineUdfsVisitor(org.apache.asterix.lang.aql.visitor.AQLInlineUdfsVisitor) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) Expression(org.apache.asterix.lang.common.base.Expression) ArrayList(java.util.ArrayList) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) FunctionDecl(org.apache.asterix.lang.common.statement.FunctionDecl)

Example 30 with FunctionSignature

use of org.apache.asterix.common.functions.FunctionSignature 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

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