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);
}
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;
}
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());
}
}
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);
}
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());
}
Aggregations