Search in sources :

Example 1 with LangRewritingContext

use of org.apache.asterix.lang.common.rewrites.LangRewritingContext 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 LangRewritingContext

use of org.apache.asterix.lang.common.rewrites.LangRewritingContext in project asterixdb by apache.

the class AQLVariableSubstitutionUtil method substituteVariable.

public static ILangExpression substituteVariable(ILangExpression expression, Map<VariableExpr, Expression> varExprMap) throws CompilationException {
    AQLCloneAndSubstituteVariablesVisitor visitor = new AQLCloneAndSubstituteVariablesVisitor(new LangRewritingContext(0));
    VariableSubstitutionEnvironment env = new VariableSubstitutionEnvironment(varExprMap);
    return expression.accept(visitor, env).first;
}
Also used : VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) AQLCloneAndSubstituteVariablesVisitor(org.apache.asterix.lang.aql.visitor.AQLCloneAndSubstituteVariablesVisitor) LangRewritingContext(org.apache.asterix.lang.common.rewrites.LangRewritingContext)

Example 3 with LangRewritingContext

use of org.apache.asterix.lang.common.rewrites.LangRewritingContext 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

LangRewritingContext (org.apache.asterix.lang.common.rewrites.LangRewritingContext)3 IQueryRewriter (org.apache.asterix.lang.common.base.IQueryRewriter)2 FileOutputStream (java.io.FileOutputStream)1 PrintWriter (java.io.PrintWriter)1 AsterixException (org.apache.asterix.common.exceptions.AsterixException)1 AQLCloneAndSubstituteVariablesVisitor (org.apache.asterix.lang.aql.visitor.AQLCloneAndSubstituteVariablesVisitor)1 IParser (org.apache.asterix.lang.common.base.IParser)1 Statement (org.apache.asterix.lang.common.base.Statement)1 VariableSubstitutionEnvironment (org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment)1 FunctionDecl (org.apache.asterix.lang.common.statement.FunctionDecl)1 Query (org.apache.asterix.lang.common.statement.Query)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