Search in sources :

Example 16 with Statement

use of org.apache.asterix.lang.common.base.Statement 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)

Example 17 with Statement

use of org.apache.asterix.lang.common.base.Statement in project asterixdb by apache.

the class RangeMapBuilder method parseHint.

public static IRangeMap parseHint(Object hint) throws CompilationException {
    ArrayBackedValueStorage abvs = new ArrayBackedValueStorage();
    DataOutput out = abvs.getDataOutput();
    ;
    abvs.reset();
    IParser parser = parserFactory.createParser((String) hint);
    List<Statement> hintStatements = parser.parse();
    if (hintStatements.size() != 1) {
        throw new CompilationException("Only one range statement is allowed for the range hint.");
    }
    // Translate the query into a Range Map
    if (hintStatements.get(0).getKind() != Statement.Kind.QUERY) {
        throw new CompilationException("Not a proper query for the range hint.");
    }
    Query q = (Query) hintStatements.get(0);
    if (q.getBody().getKind() != Kind.LIST_CONSTRUCTOR_EXPRESSION) {
        throw new CompilationException("The range hint must be a list.");
    }
    List<Expression> el = ((ListConstructor) q.getBody()).getExprList();
    int[] offsets = new int[el.size()];
    // Loop over list of literals
    for (int i = 0; i < el.size(); ++i) {
        Expression item = el.get(i);
        if (item.getKind() == Kind.LITERAL_EXPRESSION) {
            parseLiteralToBytes(item, out);
            offsets[i] = abvs.getLength();
        }
    // TODO Add support for composite fields.
    }
    return new RangeMap(1, abvs.getByteArray(), offsets);
}
Also used : DataOutput(java.io.DataOutput) CompilationException(org.apache.asterix.common.exceptions.CompilationException) Query(org.apache.asterix.lang.common.statement.Query) Statement(org.apache.asterix.lang.common.base.Statement) ListConstructor(org.apache.asterix.lang.common.expression.ListConstructor) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IRangeMap(org.apache.hyracks.dataflow.common.data.partition.range.IRangeMap) RangeMap(org.apache.hyracks.dataflow.common.data.partition.range.RangeMap) Expression(org.apache.asterix.lang.common.base.Expression) IParser(org.apache.asterix.lang.common.base.IParser)

Example 18 with Statement

use of org.apache.asterix.lang.common.base.Statement in project asterixdb by apache.

the class ParserTestExecutor method testSQLPPParser.

// Tests the SQL++ parser.
public void testSQLPPParser(File queryFile, File actualResultFile, File expectedFile) throws Exception {
    actualResultFile.getParentFile().mkdirs();
    PrintWriter writer = new PrintWriter(new FileOutputStream(actualResultFile));
    IParser parser = sqlppParserFactory.createParser(readTestFile(queryFile));
    GlobalConfig.ASTERIX_LOGGER.info(queryFile.toString());
    try {
        List<Statement> statements = parser.parse();
        List<FunctionDecl> functions = getDeclaredFunctions(statements);
        String dvName = getDefaultDataverse(statements);
        MetadataProvider metadataProvider = mock(MetadataProvider.class);
        @SuppressWarnings("unchecked") Map<String, String> config = mock(Map.class);
        when(metadataProvider.getDefaultDataverseName()).thenReturn(dvName);
        when(metadataProvider.getConfig()).thenReturn(config);
        when(config.get(FunctionUtil.IMPORT_PRIVATE_FUNCTIONS)).thenReturn("true");
        when(metadataProvider.findDataset(anyString(), anyString())).thenReturn(mock(Dataset.class));
        for (Statement st : statements) {
            if (st.getKind() == Statement.Kind.QUERY) {
                Query query = (Query) st;
                IQueryRewriter rewriter = sqlppRewriterFactory.createQueryRewriter();
                rewrite(rewriter, functions, query, metadataProvider, new LangRewritingContext(query.getVarCounter()));
                // Tests deep copy and deep equality.
                Query copiedQuery = (Query) SqlppRewriteUtil.deepCopy(query);
                Assert.assertEquals(query.hashCode(), copiedQuery.hashCode());
                Assert.assertEquals(query, copiedQuery);
            }
            SqlppAstPrintUtil.print(st, writer);
        }
        writer.close();
        // Compares the actual result and the expected result.
        runScriptAndCompareWithResult(queryFile, new PrintWriter(System.err), expectedFile, actualResultFile, ComparisonEnum.TEXT);
    } catch (Exception e) {
        GlobalConfig.ASTERIX_LOGGER.warning("Failed while testing file " + queryFile);
        throw e;
    } finally {
        writer.close();
    }
}
Also used : Query(org.apache.asterix.lang.common.statement.Query) Statement(org.apache.asterix.lang.common.base.Statement) Dataset(org.apache.asterix.metadata.entities.Dataset) Matchers.anyString(org.mockito.Matchers.anyString) IQueryRewriter(org.apache.asterix.lang.common.base.IQueryRewriter) ComparisonException(org.apache.asterix.test.common.ComparisonException) AsterixException(org.apache.asterix.common.exceptions.AsterixException) FunctionDecl(org.apache.asterix.lang.common.statement.FunctionDecl) MetadataProvider(org.apache.asterix.metadata.declared.MetadataProvider) FileOutputStream(java.io.FileOutputStream) PrintWriter(java.io.PrintWriter) IParser(org.apache.asterix.lang.common.base.IParser) LangRewritingContext(org.apache.asterix.lang.common.rewrites.LangRewritingContext)

Aggregations

Statement (org.apache.asterix.lang.common.base.Statement)18 IParser (org.apache.asterix.lang.common.base.IParser)12 IStatementExecutor (org.apache.asterix.translator.IStatementExecutor)7 PrintWriter (java.io.PrintWriter)4 SessionOutput (org.apache.asterix.translator.SessionOutput)4 ArrayList (java.util.ArrayList)3 AsterixException (org.apache.asterix.common.exceptions.AsterixException)3 CompilationException (org.apache.asterix.common.exceptions.CompilationException)3 TokenMgrError (org.apache.asterix.lang.aql.parser.TokenMgrError)3 Query (org.apache.asterix.lang.common.statement.Query)3 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 IClusterManagementWork (org.apache.asterix.common.api.IClusterManagementWork)2 ILangCompilationProvider (org.apache.asterix.compiler.provider.ILangCompilationProvider)2 FunctionDecl (org.apache.asterix.lang.common.statement.FunctionDecl)2 InsertStatement (org.apache.asterix.lang.common.statement.InsertStatement)2 VarIdentifier (org.apache.asterix.lang.common.struct.VarIdentifier)2 MetadataProvider (org.apache.asterix.metadata.declared.MetadataProvider)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1