Search in sources :

Example 6 with QueryContainer

use of io.confluent.ksql.parser.tree.QueryContainer in project ksql by confluentinc.

the class ExplainExecutor method explainStatement.

private static QueryDescription explainStatement(final ConfiguredStatement<Explain> explain, final KsqlExecutionContext executionContext, final ServiceContext serviceContext) {
    final Statement statement = explain.getStatement().getStatement().orElseThrow(() -> new KsqlStatementException("must have either queryID or statement", explain.getStatementText()));
    if (!(statement instanceof Query || statement instanceof QueryContainer)) {
        throw new KsqlException("The provided statement does not run a ksql query");
    }
    final PreparedStatement<?> preparedStatement = PreparedStatement.of(explain.getStatementText().substring("EXPLAIN ".length()), statement);
    final QueryMetadata metadata;
    final KsqlExecutionContext sandbox = executionContext.createSandbox(serviceContext);
    if (preparedStatement.getStatement() instanceof Query) {
        metadata = sandbox.executeTransientQuery(serviceContext, ConfiguredStatement.of(preparedStatement, explain.getSessionConfig()).cast(), false);
    } else {
        metadata = sandbox.execute(serviceContext, ConfiguredStatement.of(preparedStatement, explain.getSessionConfig())).getQuery().orElseThrow(() -> new IllegalStateException("The provided statement did not run a ksql query"));
    }
    return QueryDescriptionFactory.forQueryMetadata(metadata, Collections.emptyMap());
}
Also used : QueryMetadata(io.confluent.ksql.util.QueryMetadata) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) Query(io.confluent.ksql.parser.tree.Query) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) Statement(io.confluent.ksql.parser.tree.Statement) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) KsqlException(io.confluent.ksql.util.KsqlException) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) QueryContainer(io.confluent.ksql.parser.tree.QueryContainer)

Example 7 with QueryContainer

use of io.confluent.ksql.parser.tree.QueryContainer in project ksql by confluentinc.

the class EngineExecutor method plan.

// Known to be non-empty
@SuppressWarnings("OptionalGetWithoutIsPresent")
KsqlPlan plan(final ConfiguredStatement<?> statement) {
    try {
        throwOnNonExecutableStatement(statement);
        if (statement.getStatement() instanceof ExecutableDdlStatement) {
            final boolean isSourceStream = statement.getStatement() instanceof CreateStream && ((CreateStream) statement.getStatement()).isSource();
            final boolean isSourceTable = statement.getStatement() instanceof CreateTable && ((CreateTable) statement.getStatement()).isSource();
            if ((isSourceStream || isSourceTable) && !isSourceTableMaterializationEnabled()) {
                throw new KsqlStatementException("Cannot execute command because source table " + "materialization is disabled.", statement.getStatementText());
            }
            if (isSourceTable) {
                return sourceTablePlan(statement);
            } else {
                final DdlCommand ddlCommand = engineContext.createDdlCommand(statement.getStatementText(), (ExecutableDdlStatement) statement.getStatement(), config);
                return KsqlPlan.ddlPlanCurrent(statement.getStatementText(), ddlCommand);
            }
        }
        final QueryContainer queryContainer = (QueryContainer) statement.getStatement();
        final ExecutorPlans plans = planQuery(statement, queryContainer.getQuery(), Optional.of(queryContainer.getSink()), queryContainer.getQueryId(), engineContext.getMetaStore());
        final KsqlStructuredDataOutputNode outputNode = (KsqlStructuredDataOutputNode) plans.logicalPlan.getNode().get();
        final Optional<DdlCommand> ddlCommand = maybeCreateSinkDdl(statement, outputNode);
        validateResultType(outputNode.getNodeOutputType(), statement);
        final QueryPlan queryPlan = new QueryPlan(getSourceNames(outputNode), outputNode.getSinkName(), plans.physicalPlan.getPhysicalPlan(), plans.physicalPlan.getQueryId(), getApplicationId(plans.physicalPlan.getQueryId(), getSourceNames(outputNode)));
        engineContext.createQueryValidator().validateQuery(config, plans.physicalPlan, engineContext.getQueryRegistry().getAllLiveQueries());
        return KsqlPlan.queryPlanCurrent(statement.getStatementText(), ddlCommand, queryPlan);
    } catch (final KsqlStatementException e) {
        throw e;
    } catch (final Exception e) {
        throw new KsqlStatementException(e.getMessage(), statement.getStatementText(), e);
    }
}
Also used : DdlCommand(io.confluent.ksql.execution.ddl.commands.DdlCommand) CreateTable(io.confluent.ksql.parser.tree.CreateTable) CreateStream(io.confluent.ksql.parser.tree.CreateStream) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) KsqlStructuredDataOutputNode(io.confluent.ksql.planner.plan.KsqlStructuredDataOutputNode) ExecutableDdlStatement(io.confluent.ksql.parser.tree.ExecutableDdlStatement) KsqlException(io.confluent.ksql.util.KsqlException) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) QueryContainer(io.confluent.ksql.parser.tree.QueryContainer)

Example 8 with QueryContainer

use of io.confluent.ksql.parser.tree.QueryContainer in project ksql by confluentinc.

the class AstBuilderTest method shouldDefaultToEmitChangesForCsas.

@Test
public void shouldDefaultToEmitChangesForCsas() {
    // Given:
    final SingleStatementContext stmt = givenQuery("CREATE STREAM X AS SELECT * FROM TEST1;");
    // When:
    final Query result = ((QueryContainer) builder.buildStatement(stmt)).getQuery();
    // Then:
    assertThat("Should be push", result.isPullQuery(), is(false));
    assertThat(result.getRefinement().get().getOutputRefinement(), is(OutputRefinement.CHANGES));
}
Also used : Query(io.confluent.ksql.parser.tree.Query) SingleStatementContext(io.confluent.ksql.parser.SqlBaseParser.SingleStatementContext) QueryContainer(io.confluent.ksql.parser.tree.QueryContainer) Test(org.junit.Test)

Example 9 with QueryContainer

use of io.confluent.ksql.parser.tree.QueryContainer in project ksql by confluentinc.

the class AstBuilderTest method shouldSupportExplicitEmitChangesForInsertInto.

@Test
public void shouldSupportExplicitEmitChangesForInsertInto() {
    // Given:
    final SingleStatementContext stmt = givenQuery("INSERT INTO TEST1 SELECT * FROM TEST2 EMIT CHANGES;");
    // When:
    final Query result = ((QueryContainer) builder.buildStatement(stmt)).getQuery();
    // Then:
    assertThat("Should be push", result.isPullQuery(), is(false));
    assertThat(result.getRefinement().get().getOutputRefinement(), is(OutputRefinement.CHANGES));
}
Also used : Query(io.confluent.ksql.parser.tree.Query) SingleStatementContext(io.confluent.ksql.parser.SqlBaseParser.SingleStatementContext) QueryContainer(io.confluent.ksql.parser.tree.QueryContainer) Test(org.junit.Test)

Example 10 with QueryContainer

use of io.confluent.ksql.parser.tree.QueryContainer in project ksql by confluentinc.

the class AstBuilderTest method shouldSupportExplicitEmitChangesForCtas.

@Test
public void shouldSupportExplicitEmitChangesForCtas() {
    // Given:
    final SingleStatementContext stmt = givenQuery("CREATE TABLE X AS SELECT COUNT(1) FROM TEST1 GROUP BY ROWKEY EMIT CHANGES;");
    // When:
    final Query result = ((QueryContainer) builder.buildStatement(stmt)).getQuery();
    // Then:
    assertThat("Should be push", result.isPullQuery(), is(false));
    assertThat(result.getRefinement().get().getOutputRefinement(), is(OutputRefinement.CHANGES));
}
Also used : Query(io.confluent.ksql.parser.tree.Query) SingleStatementContext(io.confluent.ksql.parser.SqlBaseParser.SingleStatementContext) QueryContainer(io.confluent.ksql.parser.tree.QueryContainer) Test(org.junit.Test)

Aggregations

QueryContainer (io.confluent.ksql.parser.tree.QueryContainer)11 Query (io.confluent.ksql.parser.tree.Query)10 SingleStatementContext (io.confluent.ksql.parser.SqlBaseParser.SingleStatementContext)9 Test (org.junit.Test)9 KsqlException (io.confluent.ksql.util.KsqlException)2 KsqlStatementException (io.confluent.ksql.util.KsqlStatementException)2 KsqlExecutionContext (io.confluent.ksql.KsqlExecutionContext)1 DdlCommand (io.confluent.ksql.execution.ddl.commands.DdlCommand)1 PreparedStatement (io.confluent.ksql.parser.KsqlParser.PreparedStatement)1 CreateStream (io.confluent.ksql.parser.tree.CreateStream)1 CreateTable (io.confluent.ksql.parser.tree.CreateTable)1 ExecutableDdlStatement (io.confluent.ksql.parser.tree.ExecutableDdlStatement)1 Statement (io.confluent.ksql.parser.tree.Statement)1 KsqlStructuredDataOutputNode (io.confluent.ksql.planner.plan.KsqlStructuredDataOutputNode)1 ConfiguredStatement (io.confluent.ksql.statement.ConfiguredStatement)1 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)1 QueryMetadata (io.confluent.ksql.util.QueryMetadata)1