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());
}
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();
}
}
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();
}
Aggregations