Search in sources :

Example 1 with IStatementExecutor

use of org.apache.asterix.translator.IStatementExecutor in project asterixdb by apache.

the class FeedOperations method getConnectionJob.

private static JobSpecification getConnectionJob(SessionOutput sessionOutput, MetadataProvider metadataProvider, FeedConnection feedConnection, String[] locations, ILangCompilationProvider compilationProvider, IStorageComponentProvider storageComponentProvider, DefaultStatementExecutorFactory qtFactory, IHyracksClientConnection hcc) throws AlgebricksException, RemoteException, ACIDException {
    DataverseDecl dataverseDecl = new DataverseDecl(new Identifier(feedConnection.getDataverseName()));
    FeedConnectionRequest fcr = new FeedConnectionRequest(FeedRuntimeType.INTAKE, feedConnection.getAppliedFunctions(), feedConnection.getDatasetName(), feedConnection.getPolicyName(), feedConnection.getFeedId());
    SubscribeFeedStatement subscribeStmt = new SubscribeFeedStatement(locations, fcr);
    subscribeStmt.initialize(metadataProvider.getMetadataTxnContext());
    List<Statement> statements = new ArrayList<>();
    statements.add(dataverseDecl);
    statements.add(subscribeStmt);
    IStatementExecutor translator = qtFactory.create(metadataProvider.getApplicationContext(), statements, sessionOutput, compilationProvider, storageComponentProvider);
    // configure the metadata provider
    metadataProvider.getConfig().put(FunctionUtil.IMPORT_PRIVATE_FUNCTIONS, "" + Boolean.TRUE);
    metadataProvider.getConfig().put(FeedActivityDetails.FEED_POLICY_NAME, "" + subscribeStmt.getPolicy());
    metadataProvider.getConfig().put(FeedActivityDetails.COLLECT_LOCATIONS, StringUtils.join(subscribeStmt.getLocations(), ','));
    CompiledStatements.CompiledSubscribeFeedStatement csfs = new CompiledStatements.CompiledSubscribeFeedStatement(subscribeStmt.getSubscriptionRequest(), subscribeStmt.getVarCounter());
    return translator.rewriteCompileQuery(hcc, metadataProvider, subscribeStmt.getQuery(), csfs);
}
Also used : IStatementExecutor(org.apache.asterix.translator.IStatementExecutor) DataverseDecl(org.apache.asterix.lang.common.statement.DataverseDecl) Identifier(org.apache.asterix.lang.common.struct.Identifier) SubscribeFeedStatement(org.apache.asterix.lang.aql.statement.SubscribeFeedStatement) SubscribeFeedStatement(org.apache.asterix.lang.aql.statement.SubscribeFeedStatement) Statement(org.apache.asterix.lang.common.base.Statement) FeedConnectionRequest(org.apache.asterix.external.feed.management.FeedConnectionRequest) ArrayList(java.util.ArrayList) CompiledStatements(org.apache.asterix.translator.CompiledStatements)

Example 2 with IStatementExecutor

use of org.apache.asterix.translator.IStatementExecutor in project asterixdb by apache.

the class QueryTranslatorTest method test.

@Test
public void test() throws Exception {
    List<Statement> statements = new ArrayList<>();
    SessionOutput mockSessionOutput = mock(SessionOutput.class);
    RunStatement mockRunStatement = mock(RunStatement.class);
    // Mocks AppContextInfo.
    CcApplicationContext mockAsterixAppContextInfo = mock(CcApplicationContext.class);
    ExternalProperties mockAsterixExternalProperties = mock(ExternalProperties.class);
    when(mockAsterixAppContextInfo.getExternalProperties()).thenReturn(mockAsterixExternalProperties);
    when(mockAsterixExternalProperties.getAPIServerPort()).thenReturn(19002);
    // Mocks AsterixClusterProperties.
    Cluster mockCluster = mock(Cluster.class);
    MasterNode mockMasterNode = mock(MasterNode.class);
    ClusterProperties mockClusterProperties = mock(ClusterProperties.class);
    setFinalStaticField(ClusterProperties.class.getDeclaredField("INSTANCE"), mockClusterProperties);
    when(mockClusterProperties.getCluster()).thenReturn(mockCluster);
    when(mockCluster.getMasterNode()).thenReturn(mockMasterNode);
    when(mockMasterNode.getClientIp()).thenReturn("127.0.0.1");
    IStatementExecutor aqlTranslator = new DefaultStatementExecutorFactory().create(mockAsterixAppContextInfo, statements, mockSessionOutput, new AqlCompilationProvider(), new StorageComponentProvider());
    List<String> parameters = new ArrayList<>();
    parameters.add("examples/pregelix-example-jar-with-dependencies.jar");
    parameters.add("org.apache.pregelix.example.PageRankVertex");
    parameters.add("-ip 10.0.2.15 -port 3199");
    when(mockRunStatement.getParameters()).thenReturn(parameters);
    // Test a customer command without "-cust-prop".
    List<String> cmds = (List<String>) PA.invokeMethod(aqlTranslator, "constructPregelixCommand(org.apache.asterix.lang.common.statement.RunStatement," + "String,String,String,String)", mockRunStatement, "fromDataverse", "fromDataset", "toDataverse", "toDataset");
    List<String> expectedCmds = Arrays.asList(new String[] { "bin/pregelix", "examples/pregelix-example-jar-with-dependencies.jar", "org.apache.pregelix.example.PageRankVertex", "-ip", "10.0.2.15", "-port", "3199", "-cust-prop", "pregelix.asterixdb.url=http://127.0.0.1:19002,pregelix.asterixdb.source=true,pregelix.asterixdb.sink=true,pregelix.asterixdb.input.dataverse=fromDataverse,pregelix.asterixdb.input.dataset=fromDataset,pregelix.asterixdb.output.dataverse=toDataverse,pregelix.asterixdb.output.dataset=toDataset,pregelix.asterixdb.output.cleanup=false,pregelix.asterixdb.input.converterclass=org.apache.pregelix.example.converter.VLongIdInputVertexConverter,pregelix.asterixdb.output.converterclass=org.apache.pregelix.example.converter.VLongIdOutputVertexConverter" });
    Assert.assertEquals(cmds, expectedCmds);
    parameters.remove(parameters.size() - 1);
    parameters.add("-ip 10.0.2.15 -port 3199 -cust-prop " + "pregelix.asterixdb.input.converterclass=org.apache.pregelix.example.converter.TestInputVertexConverter," + "pregelix.asterixdb.output.converterclass=org.apache.pregelix.example.converter.TestOutputVertexConverter");
    // Test a customer command with "-cust-prop".
    cmds = (List<String>) PA.invokeMethod(aqlTranslator, "constructPregelixCommand(org.apache.asterix.lang.common.statement.RunStatement," + "String,String,String,String)", mockRunStatement, "fromDataverse", "fromDataset", "toDataverse", "toDataset");
    expectedCmds = Arrays.asList(new String[] { "bin/pregelix", "examples/pregelix-example-jar-with-dependencies.jar", "org.apache.pregelix.example.PageRankVertex", "-ip", "10.0.2.15", "-port", "3199", "-cust-prop", "pregelix.asterixdb.url=http://127.0.0.1:19002,pregelix.asterixdb.source=true,pregelix.asterixdb.sink=true,pregelix.asterixdb.input.dataverse=fromDataverse,pregelix.asterixdb.input.dataset=fromDataset,pregelix.asterixdb.output.dataverse=toDataverse,pregelix.asterixdb.output.dataset=toDataset,pregelix.asterixdb.output.cleanup=false,pregelix.asterixdb.input.converterclass=org.apache.pregelix.example.converter.TestInputVertexConverter,pregelix.asterixdb.output.converterclass=org.apache.pregelix.example.converter.TestOutputVertexConverter" });
    Assert.assertEquals(cmds, expectedCmds);
}
Also used : RunStatement(org.apache.asterix.lang.common.statement.RunStatement) MasterNode(org.apache.asterix.event.schema.cluster.MasterNode) AqlCompilationProvider(org.apache.asterix.compiler.provider.AqlCompilationProvider) RunStatement(org.apache.asterix.lang.common.statement.RunStatement) Statement(org.apache.asterix.lang.common.base.Statement) ArrayList(java.util.ArrayList) ExternalProperties(org.apache.asterix.common.config.ExternalProperties) Cluster(org.apache.asterix.event.schema.cluster.Cluster) StorageComponentProvider(org.apache.asterix.file.StorageComponentProvider) IStatementExecutor(org.apache.asterix.translator.IStatementExecutor) CcApplicationContext(org.apache.asterix.runtime.utils.CcApplicationContext) SessionOutput(org.apache.asterix.translator.SessionOutput) DefaultStatementExecutorFactory(org.apache.asterix.app.translator.DefaultStatementExecutorFactory) ClusterProperties(org.apache.asterix.common.config.ClusterProperties) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 3 with IStatementExecutor

use of org.apache.asterix.translator.IStatementExecutor 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 4 with IStatementExecutor

use of org.apache.asterix.translator.IStatementExecutor 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 5 with IStatementExecutor

use of org.apache.asterix.translator.IStatementExecutor 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)

Aggregations

Statement (org.apache.asterix.lang.common.base.Statement)7 IStatementExecutor (org.apache.asterix.translator.IStatementExecutor)7 IParser (org.apache.asterix.lang.common.base.IParser)5 SessionOutput (org.apache.asterix.translator.SessionOutput)4 TokenMgrError (org.apache.asterix.lang.aql.parser.TokenMgrError)3 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 ArrayList (java.util.ArrayList)2 IClusterManagementWork (org.apache.asterix.common.api.IClusterManagementWork)2 AsterixException (org.apache.asterix.common.exceptions.AsterixException)2 ILangCompilationProvider (org.apache.asterix.compiler.provider.ILangCompilationProvider)2 SessionConfig (org.apache.asterix.translator.SessionConfig)2 IHyracksClientConnection (org.apache.hyracks.api.client.IHyracksClientConnection)2 IHyracksDataset (org.apache.hyracks.api.dataset.IHyracksDataset)2 HyracksDataset (org.apache.hyracks.client.dataset.HyracksDataset)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 StringWriter (java.io.StringWriter)1 List (java.util.List)1