Search in sources :

Example 1 with ConfiguredKsqlPlan

use of io.confluent.ksql.planner.plan.ConfiguredKsqlPlan in project ksql by confluentinc.

the class ValidatedCommandFactory method createForPlannedQuery.

private static Command createForPlannedQuery(final ConfiguredStatement<? extends Statement> statement, final ServiceContext serviceContext, final KsqlExecutionContext context) {
    final KsqlPlan plan = context.plan(serviceContext, statement);
    ConfiguredKsqlPlan configuredPlan = ConfiguredKsqlPlan.of(plan, statement.getSessionConfig());
    final KsqlExecutionContext.ExecuteResult result = context.execute(serviceContext, configuredPlan);
    if (result.getQuery().isPresent() && result.getQuery().get() instanceof PersistentQueryMetadataImpl && configuredPlan.getConfig().getConfig(false).getBoolean(KsqlConfig.KSQL_SHARED_RUNTIME_ENABLED)) {
        configuredPlan = ConfiguredKsqlPlan.of(plan, statement.getSessionConfig().copyWith(ImmutableMap.of(KsqlConfig.KSQL_SHARED_RUNTIME_ENABLED, false)));
    }
    return Command.of(configuredPlan);
}
Also used : ConfiguredKsqlPlan(io.confluent.ksql.planner.plan.ConfiguredKsqlPlan) PersistentQueryMetadataImpl(io.confluent.ksql.util.PersistentQueryMetadataImpl) KsqlPlan(io.confluent.ksql.engine.KsqlPlan) ConfiguredKsqlPlan(io.confluent.ksql.planner.plan.ConfiguredKsqlPlan) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext)

Example 2 with ConfiguredKsqlPlan

use of io.confluent.ksql.planner.plan.ConfiguredKsqlPlan in project ksql by confluentinc.

the class TestExecutorUtil method execute.

/**
 * @param srClient if supplied, then schemas can be inferred from the schema registry.
 * @return a list of persistent queries that should be run by the test executor, if a query was
 *         replaced via a CREATE OR REPLACE statement it will only appear once in the output list
 */
@SuppressWarnings("OptionalGetWithoutIsPresent")
private static List<PersistentQueryAndSources> execute(final KsqlEngine engine, final TestCase testCase, final KsqlConfig ksqlConfig, final ServiceContext serviceContext, final Optional<SchemaRegistryClient> srClient, final StubKafkaService stubKafkaService, final TestExecutionListener listener) {
    final Map<QueryId, PersistentQueryAndSources> queries = new LinkedHashMap<>();
    int idx = 0;
    final Iterator<PlannedStatement> plans = planTestCase(engine, testCase, ksqlConfig, serviceContext, srClient, stubKafkaService);
    try {
        while (plans.hasNext()) {
            ++idx;
            final PlannedStatement planned = plans.next();
            if (planned.insertValues.isPresent()) {
                final ConfiguredStatement<InsertValues> insertValues = planned.insertValues.get();
                final SessionProperties sessionProperties = new SessionProperties(insertValues.getSessionConfig().getOverrides(), new KsqlHostInfo("host", 50), buildUrl(), false);
                StubInsertValuesExecutor.of(stubKafkaService).execute(insertValues, sessionProperties, engine, engine.getServiceContext());
                continue;
            }
            final ConfiguredKsqlPlan plan = planned.plan.orElseThrow(IllegalStateException::new);
            listener.acceptPlan(plan);
            final ExecuteResultAndSources result = executePlan(engine, plan);
            if (!result.getSources().isPresent()) {
                continue;
            }
            final PersistentQueryMetadata query = (PersistentQueryMetadata) result.getExecuteResult().getQuery().get();
            listener.acceptQuery(query);
            queries.put(query.getQueryId(), new PersistentQueryAndSources(query, result.getSources().get()));
        }
        return ImmutableList.copyOf(queries.values());
    } catch (final KsqlStatementException e) {
        if (testCase.expectedException().isPresent() && plans.hasNext()) {
            throw new AssertionError("Only the last statement in a negative test should fail. " + "Yet in this case statement " + idx + " failed.", e);
        }
        throw e;
    }
}
Also used : ConfiguredKsqlPlan(io.confluent.ksql.planner.plan.ConfiguredKsqlPlan) KsqlHostInfo(io.confluent.ksql.util.KsqlHostInfo) QueryId(io.confluent.ksql.query.QueryId) LinkedHashMap(java.util.LinkedHashMap) SessionProperties(io.confluent.ksql.rest.SessionProperties) InsertValues(io.confluent.ksql.parser.tree.InsertValues) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata)

Example 3 with ConfiguredKsqlPlan

use of io.confluent.ksql.planner.plan.ConfiguredKsqlPlan in project ksql by confluentinc.

the class TestExecutorUtilTest method shouldPlanTestCase.

@Test
public void shouldPlanTestCase() {
    // Given:
    final TestCase testCase = loadTestCase(PROJECT_AND_FILTER);
    // When:
    final Iterator<PlannedStatement> plans = TestExecutorUtil.planTestCase(ksqlEngine, testCase, ksqlConfig, serviceContext, Optional.of(serviceContext.getSchemaRegistryClient()), stubKafkaService);
    // Then:
    final List<ConfiguredKsqlPlan> asList = new LinkedList<>();
    while (plans.hasNext()) {
        final PlannedStatement planned = plans.next();
        final ConfiguredKsqlPlan plan = planned.plan.orElseThrow(() -> new AssertionError("Should be plan"));
        ksqlEngine.execute(ksqlEngine.getServiceContext(), plan);
        asList.add(plan);
    }
    assertThat(asList.size(), is(2));
    assertThat(asList.get(0).getPlan().getStatementText(), startsWith("CREATE STREAM TEST"));
    assertThat(asList.get(1).getPlan().getStatementText(), startsWith("CREATE STREAM S1 AS SELECT"));
}
Also used : ConfiguredKsqlPlan(io.confluent.ksql.planner.plan.ConfiguredKsqlPlan) PlannedStatement(io.confluent.ksql.test.tools.TestExecutorUtil.PlannedStatement) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 4 with ConfiguredKsqlPlan

use of io.confluent.ksql.planner.plan.ConfiguredKsqlPlan in project ksql by confluentinc.

the class InteractiveStatementExecutor method executePlan.

private void executePlan(final Command command, final CommandId commandId, final Optional<CommandStatusFuture> commandStatusFuture, final KsqlPlan plan, final Mode mode, final long offset, final boolean restoreInProgress) {
    final KsqlConfig mergedConfig = buildMergedConfig(command);
    final ConfiguredKsqlPlan configured = ConfiguredKsqlPlan.of(plan, SessionConfig.of(mergedConfig, command.getOverwriteProperties()));
    putStatus(commandId, commandStatusFuture, new CommandStatus(CommandStatus.Status.EXECUTING, "Executing statement"));
    final ExecuteResult result = ksqlEngine.execute(serviceContext, configured, restoreInProgress);
    queryIdGenerator.setNextId(offset + 1);
    if (result.getQuery().isPresent()) {
        if (mode == Mode.EXECUTE) {
            result.getQuery().get().start();
        }
    }
    final String successMessage = getSuccessMessage(result);
    final Optional<QueryId> queryId = result.getQuery().map(QueryMetadata::getQueryId);
    final CommandStatus successStatus = new CommandStatus(CommandStatus.Status.SUCCESS, successMessage, queryId);
    putFinalStatus(commandId, commandStatusFuture, successStatus);
}
Also used : ConfiguredKsqlPlan(io.confluent.ksql.planner.plan.ConfiguredKsqlPlan) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) QueryMetadata(io.confluent.ksql.util.QueryMetadata) QueryId(io.confluent.ksql.query.QueryId) KsqlConfig(io.confluent.ksql.util.KsqlConfig) CommandStatus(io.confluent.ksql.rest.entity.CommandStatus) ExecuteResult(io.confluent.ksql.KsqlExecutionContext.ExecuteResult)

Aggregations

ConfiguredKsqlPlan (io.confluent.ksql.planner.plan.ConfiguredKsqlPlan)4 QueryId (io.confluent.ksql.query.QueryId)2 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)2 KsqlExecutionContext (io.confluent.ksql.KsqlExecutionContext)1 ExecuteResult (io.confluent.ksql.KsqlExecutionContext.ExecuteResult)1 KsqlPlan (io.confluent.ksql.engine.KsqlPlan)1 InsertValues (io.confluent.ksql.parser.tree.InsertValues)1 SessionProperties (io.confluent.ksql.rest.SessionProperties)1 CommandStatus (io.confluent.ksql.rest.entity.CommandStatus)1 PlannedStatement (io.confluent.ksql.test.tools.TestExecutorUtil.PlannedStatement)1 KsqlConfig (io.confluent.ksql.util.KsqlConfig)1 KsqlHostInfo (io.confluent.ksql.util.KsqlHostInfo)1 KsqlStatementException (io.confluent.ksql.util.KsqlStatementException)1 PersistentQueryMetadataImpl (io.confluent.ksql.util.PersistentQueryMetadataImpl)1 QueryMetadata (io.confluent.ksql.util.QueryMetadata)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 Test (org.junit.Test)1