Search in sources :

Example 6 with KsqlExecutionContext

use of io.confluent.ksql.KsqlExecutionContext 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 7 with KsqlExecutionContext

use of io.confluent.ksql.KsqlExecutionContext in project ksql by confluentinc.

the class KsqlContext method sql.

public List<QueryMetadata> sql(final String sql, final Map<String, ?> overriddenProperties) {
    final List<ParsedStatement> statements = ksqlEngine.parse(sql);
    final KsqlExecutionContext sandbox = ksqlEngine.createSandbox(ksqlEngine.getServiceContext());
    final Map<String, Object> validationOverrides = new HashMap<>(overriddenProperties);
    for (final ParsedStatement stmt : statements) {
        execute(sandbox, stmt, ksqlConfig, validationOverrides, injectorFactory.apply(sandbox, sandbox.getServiceContext()));
    }
    final List<QueryMetadata> queries = new ArrayList<>();
    final Injector injector = injectorFactory.apply(ksqlEngine, serviceContext);
    final Map<String, Object> executionOverrides = new HashMap<>(overriddenProperties);
    for (final ParsedStatement parsed : statements) {
        execute(ksqlEngine, parsed, ksqlConfig, executionOverrides, injector).getQuery().ifPresent(queries::add);
    }
    for (final QueryMetadata queryMetadata : queries) {
        if (queryMetadata instanceof PersistentQueryMetadata) {
            queryMetadata.start();
        } else {
            LOG.warn("Ignoring statemenst: {}", sql);
            LOG.warn("Only CREATE statements can run in KSQL embedded mode.");
        }
    }
    return queries;
}
Also used : PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) QueryMetadata(io.confluent.ksql.util.QueryMetadata) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) Injector(io.confluent.ksql.statement.Injector) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata)

Example 8 with KsqlExecutionContext

use of io.confluent.ksql.KsqlExecutionContext in project ksql by confluentinc.

the class KsqlEngineTestUtil method execute.

@SuppressWarnings({ "rawtypes", "unchecked" })
private static ExecuteResult execute(final ServiceContext serviceContext, final KsqlExecutionContext executionContext, final ParsedStatement stmt, final KsqlConfig ksqlConfig, final Map<String, Object> overriddenProperties, final Optional<DefaultSchemaInjector> schemaInjector) {
    final PreparedStatement<?> prepared = executionContext.prepare(stmt);
    final ConfiguredStatement<?> configured = ConfiguredStatement.of(prepared, SessionConfig.of(ksqlConfig, overriddenProperties));
    final ConfiguredStatement<?> withFormats = new DefaultFormatInjector().inject(configured);
    final ConfiguredStatement<?> withSchema = schemaInjector.map(injector -> injector.inject(withFormats)).orElse((ConfiguredStatement) withFormats);
    final ConfiguredStatement<?> reformatted = new SqlFormatInjector(executionContext).inject(withSchema);
    try {
        return executionContext.execute(serviceContext, reformatted);
    } catch (final KsqlStatementException e) {
        // can easily check that the failed statement is the input statement
        throw new KsqlStatementException(e.getRawMessage(), stmt.getStatementText(), e.getCause());
    }
}
Also used : Query(io.confluent.ksql.parser.tree.Query) SequentialQueryIdGenerator(io.confluent.ksql.query.id.SequentialQueryIdGenerator) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) ServiceContext(io.confluent.ksql.services.ServiceContext) SchemaRegistryTopicSchemaSupplier(io.confluent.ksql.schema.ksql.inference.SchemaRegistryTopicSchemaSupplier) ProcessingLogContext(io.confluent.ksql.logging.processing.ProcessingLogContext) MutableMetaStore(io.confluent.ksql.metastore.MutableMetaStore) MetricCollectors(io.confluent.ksql.metrics.MetricCollectors) Function(java.util.function.Function) SessionConfig(io.confluent.ksql.config.SessionConfig) TransientQueryMetadata(io.confluent.ksql.util.TransientQueryMetadata) ExecuteResult(io.confluent.ksql.KsqlExecutionContext.ExecuteResult) Map(java.util.Map) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) DefaultFormatInjector(io.confluent.ksql.format.DefaultFormatInjector) QueryIdGenerator(io.confluent.ksql.query.id.QueryIdGenerator) QueryMetadata(io.confluent.ksql.util.QueryMetadata) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) KsqlConfig(io.confluent.ksql.util.KsqlConfig) DefaultSchemaInjector(io.confluent.ksql.schema.ksql.inference.DefaultSchemaInjector) Collectors(java.util.stream.Collectors) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) List(java.util.List) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) KsqlEngineMetrics(io.confluent.ksql.internal.KsqlEngineMetrics) Optional(java.util.Optional) Collections(java.util.Collections) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) DefaultFormatInjector(io.confluent.ksql.format.DefaultFormatInjector) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException)

Example 9 with KsqlExecutionContext

use of io.confluent.ksql.KsqlExecutionContext in project ksql by confluentinc.

the class KsqlEngineTest method shouldCreateSandboxWithSandboxedQueryMetadata.

@Test
public void shouldCreateSandboxWithSandboxedQueryMetadata() {
    // Given:
    final QueryMetadata transientQ = KsqlEngineTestUtil.executeQuery(serviceContext, ksqlEngine, "select * from test1 EMIT CHANGES;", ksqlConfig, Collections.emptyMap());
    final QueryMetadata persistentQ = KsqlEngineTestUtil.execute(serviceContext, ksqlEngine, "create stream banana as select * from test1 EMIT CHANGES;", ksqlConfig, Collections.emptyMap()).get(0);
    // When:
    final KsqlExecutionContext sandbox = ksqlEngine.createSandbox(serviceContext);
    // Then:
    assertThat(sandbox.getAllLiveQueries().size(), is(2));
    assertSandboxHasQuery(sandbox, transientQ);
    assertSandboxHasQuery(sandbox, persistentQ);
}
Also used : SandboxedTransientQueryMetadata(io.confluent.ksql.util.SandboxedTransientQueryMetadata) TransientQueryMetadata(io.confluent.ksql.util.TransientQueryMetadata) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) QueryMetadata(io.confluent.ksql.util.QueryMetadata) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) Test(org.junit.Test)

Example 10 with KsqlExecutionContext

use of io.confluent.ksql.KsqlExecutionContext in project ksql by confluentinc.

the class KsqlEngineTest method shouldNotCleanUpInternalTopicsOnSandboxQueryClose.

@Test
public void shouldNotCleanUpInternalTopicsOnSandboxQueryClose() {
    // Given:
    KsqlEngineTestUtil.execute(serviceContext, ksqlEngine, "create stream s1 with (value_format = 'avro') as select * from test1;", ksqlConfig, Collections.emptyMap());
    final KsqlExecutionContext sandbox = ksqlEngine.createSandbox(serviceContext);
    // When:
    sandbox.getPersistentQueries().forEach(PersistentQueryMetadata::close);
    // Then:
    awaitCleanupComplete();
    verify(topicClient, never()).deleteInternalTopics(any());
}
Also used : KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) Test(org.junit.Test)

Aggregations

KsqlExecutionContext (io.confluent.ksql.KsqlExecutionContext)13 ConfiguredStatement (io.confluent.ksql.statement.ConfiguredStatement)7 ServiceContext (io.confluent.ksql.services.ServiceContext)6 SessionProperties (io.confluent.ksql.rest.SessionProperties)5 List (java.util.List)5 Optional (java.util.Optional)5 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)4 QueryMetadata (io.confluent.ksql.util.QueryMetadata)4 Collectors (java.util.stream.Collectors)4 ParsedStatement (io.confluent.ksql.parser.KsqlParser.ParsedStatement)3 KsqlWarning (io.confluent.ksql.rest.entity.KsqlWarning)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 ImmutableList (com.google.common.collect.ImmutableList)2 PreparedStatement (io.confluent.ksql.parser.KsqlParser.PreparedStatement)2 Query (io.confluent.ksql.parser.tree.Query)2 ConnectResponse (io.confluent.ksql.services.ConnectClient.ConnectResponse)2 Injector (io.confluent.ksql.statement.Injector)2 KsqlConfig (io.confluent.ksql.util.KsqlConfig)2 KsqlStatementException (io.confluent.ksql.util.KsqlStatementException)2