Search in sources :

Example 1 with IParser

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;
}
Also used : CharSequenceReader(org.apache.commons.io.input.CharSequenceReader) VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) Statement(org.apache.asterix.lang.common.base.Statement) ArrayList(java.util.ArrayList) IParser(org.apache.asterix.lang.common.base.IParser) FunctionDecl(org.apache.asterix.lang.common.statement.FunctionDecl)

Example 2 with IParser

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();
}
Also used : IStatementExecutor(org.apache.asterix.translator.IStatementExecutor) SessionOutput(org.apache.asterix.translator.SessionOutput) Statement(org.apache.asterix.lang.common.base.Statement) SessionConfig(org.apache.asterix.translator.SessionConfig) IParser(org.apache.asterix.lang.common.base.IParser)

Example 3 with IParser

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();
}
Also used : IStatementExecutor(org.apache.asterix.translator.IStatementExecutor) Statement(org.apache.asterix.lang.common.base.Statement) IClusterManagementWork(org.apache.asterix.common.api.IClusterManagementWork) IParser(org.apache.asterix.lang.common.base.IParser)

Example 4 with IParser

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);
    }
}
Also used : IHyracksClientConnection(org.apache.hyracks.api.client.IHyracksClientConnection) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Statement(org.apache.asterix.lang.common.base.Statement) TokenMgrError(org.apache.asterix.lang.aql.parser.TokenMgrError) AsterixException(org.apache.asterix.common.exceptions.AsterixException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IStatementExecutor(org.apache.asterix.translator.IStatementExecutor) AsterixException(org.apache.asterix.common.exceptions.AsterixException) HyracksDataset(org.apache.hyracks.client.dataset.HyracksDataset) IHyracksDataset(org.apache.hyracks.api.dataset.IHyracksDataset) IHyracksDataset(org.apache.hyracks.api.dataset.IHyracksDataset) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IParser(org.apache.asterix.lang.common.base.IParser)

Example 5 with IParser

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;
}
Also used : VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) Statement(org.apache.asterix.lang.common.base.Statement) StringReader(java.io.StringReader) IParser(org.apache.asterix.lang.common.base.IParser) FunctionDecl(org.apache.asterix.lang.common.statement.FunctionDecl)

Aggregations

IParser (org.apache.asterix.lang.common.base.IParser)14 Statement (org.apache.asterix.lang.common.base.Statement)12 IStatementExecutor (org.apache.asterix.translator.IStatementExecutor)5 PrintWriter (java.io.PrintWriter)4 AsterixException (org.apache.asterix.common.exceptions.AsterixException)4 CompilationException (org.apache.asterix.common.exceptions.CompilationException)3 TokenMgrError (org.apache.asterix.lang.aql.parser.TokenMgrError)3 FunctionDecl (org.apache.asterix.lang.common.statement.FunctionDecl)3 Query (org.apache.asterix.lang.common.statement.Query)3 SessionOutput (org.apache.asterix.translator.SessionOutput)3 BufferedReader (java.io.BufferedReader)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 StringReader (java.io.StringReader)2 StringWriter (java.io.StringWriter)2 IClusterManagementWork (org.apache.asterix.common.api.IClusterManagementWork)2 ILangCompilationProvider (org.apache.asterix.compiler.provider.ILangCompilationProvider)2 IParserFactory (org.apache.asterix.lang.common.base.IParserFactory)2