use of org.apache.asterix.lang.common.base.IParser in project asterixdb by apache.
the class FunctionParser method getFunctionDecl.
public FunctionDecl getFunctionDecl(Function function) throws CompilationException {
String functionBody = function.getFunctionBody();
List<String> params = function.getParams();
List<VarIdentifier> varIdentifiers = new ArrayList<VarIdentifier>();
StringBuilder builder = new StringBuilder();
builder.append(" use dataverse " + function.getDataverseName() + ";");
builder.append(" declare function " + function.getName().split("@")[0]);
builder.append("(");
boolean first = true;
for (String param : params) {
VarIdentifier varId = new VarIdentifier(param);
varIdentifiers.add(varId);
if (first) {
first = false;
} else {
builder.append(",");
}
builder.append(param);
}
builder.append("){\n").append(functionBody).append("\n}");
IParser parser = parserFactory.createParser(new CharSequenceReader(builder));
List<Statement> statements = parser.parse();
FunctionDecl decl = (FunctionDecl) statements.get(1);
return decl;
}
use of org.apache.asterix.lang.common.base.IParser in project asterixdb by apache.
the class AsterixJavaClient method compile.
public void compile(boolean optimize, boolean printRewrittenExpressions, boolean printLogicalPlan, boolean printOptimizedPlan, boolean printPhysicalOpsOnly, boolean generateBinaryRuntime, boolean printJob) throws Exception {
queryJobSpec = null;
dmlJobs = null;
if (queryText == null) {
return;
}
int ch;
StringBuilder builder = new StringBuilder();
while ((ch = queryText.read()) != -1) {
builder.append((char) ch);
}
IParser parser = parserFactory.createParser(builder.toString());
List<Statement> statements = parser.parse();
MetadataManager.INSTANCE.init();
SessionConfig conf = new SessionConfig(OutputFormat.ADM, optimize, true, generateBinaryRuntime);
conf.setOOBData(false, printRewrittenExpressions, printLogicalPlan, printOptimizedPlan, printJob);
if (printPhysicalOpsOnly) {
conf.set(SessionConfig.FORMAT_ONLY_PHYSICAL_OPS, true);
}
SessionOutput output = new SessionOutput(conf, writer);
IStatementExecutor translator = statementExecutorFactory.create(appCtx, statements, output, compilationProvider, storageComponentProvider);
translator.compileAndExecute(hcc, null, QueryTranslator.ResultDelivery.IMMEDIATE, null, new IStatementExecutor.Stats());
writer.flush();
}
use of org.apache.asterix.lang.common.base.IParser in project asterixdb by apache.
the class QueryServiceServlet method executeStatement.
protected void executeStatement(String statementsText, SessionOutput sessionOutput, ResultDelivery delivery, IStatementExecutor.Stats stats, RequestParameters param, String handleUrl, long[] outExecStartEnd) throws Exception {
IClusterManagementWork.ClusterState clusterState = ClusterStateManager.INSTANCE.getState();
if (clusterState != IClusterManagementWork.ClusterState.ACTIVE) {
// using a plain IllegalStateException here to get into the right catch clause for a 500
throw new IllegalStateException("Cannot execute request, cluster is " + clusterState);
}
IParser parser = compilationProvider.getParserFactory().createParser(statementsText);
List<Statement> statements = parser.parse();
MetadataManager.INSTANCE.init();
IStatementExecutor translator = statementExecutorFactory.create((ICcApplicationContext) appCtx, statements, sessionOutput, compilationProvider, componentProvider);
outExecStartEnd[0] = System.nanoTime();
translator.compileAndExecute(getHyracksClientConnection(), getHyracksDataset(), delivery, null, stats, param.clientContextID, queryCtx);
outExecStartEnd[1] = System.nanoTime();
}
use of org.apache.asterix.lang.common.base.IParser in project asterixdb by apache.
the class RestApiServlet method doHandle.
private void doHandle(IServletResponse response, String query, SessionOutput sessionOutput, ResultDelivery resultDelivery) throws JsonProcessingException {
try {
response.setStatus(HttpResponseStatus.OK);
IHyracksClientConnection hcc = (IHyracksClientConnection) ctx.get(HYRACKS_CONNECTION_ATTR);
IHyracksDataset hds = (IHyracksDataset) ctx.get(HYRACKS_DATASET_ATTR);
if (hds == null) {
synchronized (ctx) {
hds = (IHyracksDataset) ctx.get(HYRACKS_DATASET_ATTR);
if (hds == null) {
hds = new HyracksDataset(hcc, appCtx.getCompilerProperties().getFrameSize(), ResultReader.NUM_READERS);
ctx.put(HYRACKS_DATASET_ATTR, hds);
}
}
}
IParser parser = parserFactory.createParser(query);
List<Statement> aqlStatements = parser.parse();
validate(aqlStatements);
MetadataManager.INSTANCE.init();
IStatementExecutor translator = statementExecutorFactory.create(appCtx, aqlStatements, sessionOutput, compilationProvider, componentProvider);
translator.compileAndExecute(hcc, hds, resultDelivery, null, new IStatementExecutor.Stats());
} catch (AsterixException | TokenMgrError | org.apache.asterix.aqlplus.parser.TokenMgrError pe) {
response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
GlobalConfig.ASTERIX_LOGGER.log(Level.SEVERE, pe.getMessage(), pe);
String errorMessage = ResultUtil.buildParseExceptionMessage(pe, query);
ObjectNode errorResp = ResultUtil.getErrorResponse(2, errorMessage, "", ResultUtil.extractFullStackTrace(pe));
sessionOutput.out().write(new ObjectMapper().writeValueAsString(errorResp));
} catch (Exception e) {
GlobalConfig.ASTERIX_LOGGER.log(Level.SEVERE, e.getMessage(), e);
response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
ResultUtil.apiErrorHandler(sessionOutput.out(), e);
}
}
use of org.apache.asterix.lang.common.base.IParser in project asterixdb by apache.
the class FunctionParser method getFunctionDecl.
public FunctionDecl getFunctionDecl(Function function) throws CompilationException {
String functionBody = function.getFunctionBody();
List<String> params = function.getParams();
StringBuilder builder = new StringBuilder();
builder.append(" use " + function.getDataverseName() + ";");
builder.append(" declare function " + function.getName().split("@")[0]);
builder.append("(");
for (String param : params) {
VarIdentifier varId = SqlppVariableUtil.toUserDefinedVariableName(param);
builder.append(varId);
builder.append(",");
}
if (params.size() > 0) {
builder.delete(builder.length() - 1, builder.length());
}
builder.append(")");
builder.append("{");
builder.append("\n");
builder.append(functionBody);
builder.append("\n");
builder.append("}");
IParser parser = parserFactory.createParser(new StringReader(new String(builder)));
List<Statement> statements = parser.parse();
FunctionDecl decl = (FunctionDecl) statements.get(1);
return decl;
}
Aggregations