Search in sources :

Example 1 with IQueryRewriter

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

the class APIFramework method reWriteQuery.

public Pair<IReturningStatement, Integer> reWriteQuery(List<FunctionDecl> declaredFunctions, MetadataProvider metadataProvider, IReturningStatement q, SessionOutput output) throws CompilationException {
    if (q == null) {
        return null;
    }
    SessionConfig conf = output.config();
    if (!conf.is(SessionConfig.FORMAT_ONLY_PHYSICAL_OPS) && conf.is(SessionConfig.OOB_EXPR_TREE)) {
        output.out().println();
        printPlanPrefix(output, "Expression tree");
        q.accept(astPrintVisitorFactory.createLangVisitor(output.out()), 0);
        printPlanPostfix(output);
    }
    IQueryRewriter rw = rewriterFactory.createQueryRewriter();
    rw.rewrite(declaredFunctions, q, metadataProvider, new LangRewritingContext(q.getVarCounter()));
    return new Pair<>(q, q.getVarCounter());
}
Also used : SessionConfig(org.apache.asterix.translator.SessionConfig) IQueryRewriter(org.apache.asterix.lang.common.base.IQueryRewriter) LangRewritingContext(org.apache.asterix.lang.common.rewrites.LangRewritingContext) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 2 with IQueryRewriter

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

Example 3 with IQueryRewriter

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

the class AbstractInlineUdfsVisitor method rewriteFunctionBody.

protected Expression rewriteFunctionBody(Expression expr) throws CompilationException {
    Query wrappedQuery = new Query(false);
    wrappedQuery.setBody(expr);
    wrappedQuery.setTopLevel(false);
    IQueryRewriter queryRewriter = rewriterFactory.createQueryRewriter();
    queryRewriter.rewrite(declaredFunctions, wrappedQuery, metadataProvider, context);
    return wrappedQuery.getBody();
}
Also used : Query(org.apache.asterix.lang.common.statement.Query) IQueryRewriter(org.apache.asterix.lang.common.base.IQueryRewriter)

Aggregations

IQueryRewriter (org.apache.asterix.lang.common.base.IQueryRewriter)3 LangRewritingContext (org.apache.asterix.lang.common.rewrites.LangRewritingContext)2 Query (org.apache.asterix.lang.common.statement.Query)2 FileOutputStream (java.io.FileOutputStream)1 PrintWriter (java.io.PrintWriter)1 AsterixException (org.apache.asterix.common.exceptions.AsterixException)1 IParser (org.apache.asterix.lang.common.base.IParser)1 Statement (org.apache.asterix.lang.common.base.Statement)1 FunctionDecl (org.apache.asterix.lang.common.statement.FunctionDecl)1 MetadataProvider (org.apache.asterix.metadata.declared.MetadataProvider)1 Dataset (org.apache.asterix.metadata.entities.Dataset)1 ComparisonException (org.apache.asterix.test.common.ComparisonException)1 SessionConfig (org.apache.asterix.translator.SessionConfig)1 Pair (org.apache.hyracks.algebricks.common.utils.Pair)1 Matchers.anyString (org.mockito.Matchers.anyString)1