Search in sources :

Example 1 with Statement

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

use of org.apache.asterix.lang.common.base.Statement 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 3 with Statement

use of org.apache.asterix.lang.common.base.Statement in project asterixdb by apache.

the class AQLFormatPrintUtil method toSQLPPString.

public static String toSQLPPString(List<Statement> exprs) throws CompilationException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    PrintWriter output = new PrintWriter(bos);
    AQLToSQLPPPrintVisitor visitor = new AQLToSQLPPPrintVisitor(output);
    for (Statement expr : exprs) {
        expr.accept(visitor, 0);
    }
    output.close();
    return bos.toString();
}
Also used : Statement(org.apache.asterix.lang.common.base.Statement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AQLToSQLPPPrintVisitor(org.apache.asterix.lang.aql.visitor.AQLToSQLPPPrintVisitor) PrintWriter(java.io.PrintWriter)

Example 4 with Statement

use of org.apache.asterix.lang.common.base.Statement 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 5 with Statement

use of org.apache.asterix.lang.common.base.Statement 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)

Aggregations

Statement (org.apache.asterix.lang.common.base.Statement)18 IParser (org.apache.asterix.lang.common.base.IParser)12 IStatementExecutor (org.apache.asterix.translator.IStatementExecutor)7 PrintWriter (java.io.PrintWriter)4 SessionOutput (org.apache.asterix.translator.SessionOutput)4 ArrayList (java.util.ArrayList)3 AsterixException (org.apache.asterix.common.exceptions.AsterixException)3 CompilationException (org.apache.asterix.common.exceptions.CompilationException)3 TokenMgrError (org.apache.asterix.lang.aql.parser.TokenMgrError)3 Query (org.apache.asterix.lang.common.statement.Query)3 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 IClusterManagementWork (org.apache.asterix.common.api.IClusterManagementWork)2 ILangCompilationProvider (org.apache.asterix.compiler.provider.ILangCompilationProvider)2 FunctionDecl (org.apache.asterix.lang.common.statement.FunctionDecl)2 InsertStatement (org.apache.asterix.lang.common.statement.InsertStatement)2 VarIdentifier (org.apache.asterix.lang.common.struct.VarIdentifier)2 MetadataProvider (org.apache.asterix.metadata.declared.MetadataProvider)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1