use of io.confluent.ksql.KsqlExecutionContext.ExecuteResult in project ksql by confluentinc.
the class EngineExecutor method execute.
ExecuteResult execute(final KsqlPlan plan, final boolean restoreInProgress) {
if (!plan.getQueryPlan().isPresent()) {
final String ddlResult = plan.getDdlCommand().map(ddl -> executeDdl(ddl, plan.getStatementText(), false, Collections.emptySet(), restoreInProgress)).orElseThrow(() -> new IllegalStateException("DdlResult should be present if there is no physical plan."));
return ExecuteResult.of(ddlResult);
}
final QueryPlan queryPlan = plan.getQueryPlan().get();
final KsqlConstants.PersistentQueryType persistentQueryType = plan.getPersistentQueryType().get();
// that attempt to write to a sink (i.e. INSERT or CREATE_AS).
if (persistentQueryType != KsqlConstants.PersistentQueryType.CREATE_SOURCE) {
final DataSource sinkSource = engineContext.getMetaStore().getSource(queryPlan.getSink().get());
if (sinkSource != null && sinkSource.isSource()) {
throw new KsqlException(String.format("Cannot insert into read-only %s: %s", sinkSource.getDataSourceType().getKsqlType().toLowerCase(), sinkSource.getName().text()));
}
}
final Optional<String> ddlResult = plan.getDdlCommand().map(ddl -> executeDdl(ddl, plan.getStatementText(), true, queryPlan.getSources(), restoreInProgress));
// Return if the source to create already exists.
if (ddlResult.isPresent() && ddlResult.get().contains("already exists")) {
return ExecuteResult.of(ddlResult.get());
}
// must be executed.
if (persistentQueryType == KsqlConstants.PersistentQueryType.CREATE_SOURCE && !isSourceTableMaterializationEnabled()) {
LOG.info(String.format("Source table query '%s' won't be materialized because '%s' is disabled.", plan.getStatementText(), KsqlConfig.KSQL_SOURCE_TABLE_MATERIALIZATION_ENABLED));
return ExecuteResult.of(ddlResult.get());
}
return ExecuteResult.of(executePersistentQuery(queryPlan, plan.getStatementText(), persistentQueryType));
}
use of io.confluent.ksql.KsqlExecutionContext.ExecuteResult 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.ExecuteResult in project ksql by confluentinc.
the class KsqlEngineTest method shouldRegisterPersistentQueriesOnlyInSandbox.
@Test
public void shouldRegisterPersistentQueriesOnlyInSandbox() {
// Given:
final PreparedStatement<?> prepared = prepare(parse("create table bar as select * from test2;").get(0));
// When:
final ExecuteResult result = sandbox.execute(sandboxServiceContext, ConfiguredStatement.of(prepared, SessionConfig.of(ksqlConfig, new HashMap<>())));
// Then:
assertThat(result.getQuery(), is(not(Optional.empty())));
assertThat(sandbox.getPersistentQuery(getQueryId(result.getQuery().get())), is(not(Optional.empty())));
assertThat(ksqlEngine.getPersistentQuery(getQueryId(result.getQuery().get())), is(Optional.empty()));
}
use of io.confluent.ksql.KsqlExecutionContext.ExecuteResult in project ksql by confluentinc.
the class KsqlEngineTest method shouldNotThrowWhenExecutingDuplicateTableWithIfNotExists.
@Test
public void shouldNotThrowWhenExecutingDuplicateTableWithIfNotExists() {
// Given:
final List<ParsedStatement> parsed = ksqlEngine.parse("CREATE TABLE FOO WITH (KAFKA_TOPIC='BAR') AS SELECT * FROM TEST2; " + "CREATE TABLE IF NOT EXISTS FOO WITH (KAFKA_TOPIC='BAR') AS SELECT * FROM TEST2;");
givenStatementAlreadyExecuted(parsed.get(0));
final PreparedStatement<?> prepared = prepare(parsed.get(1));
// When:
ExecuteResult executeResult = ksqlEngine.execute(serviceContext, ConfiguredStatement.of(prepared, SessionConfig.of(ksqlConfig, new HashMap<>())));
// Then:
assertThat(executeResult.getQuery(), is(Optional.empty()));
assertThat(executeResult.getCommandResult(), is(Optional.of("Cannot add table `FOO`: A table with the same name already exists.")));
}
use of io.confluent.ksql.KsqlExecutionContext.ExecuteResult in project ksql by confluentinc.
the class KsqlEngineTest method shouldExecuteDdlStatement.
@Test
public void shouldExecuteDdlStatement() {
// Given:
givenTopicsExist("foo");
final PreparedStatement<?> statement = prepare(parse("CREATE STREAM FOO (a int) WITH (kafka_topic='foo', value_format='json', key_format='kafka');").get(0));
// When:
final ExecuteResult result = sandbox.execute(sandboxServiceContext, ConfiguredStatement.of(statement, SessionConfig.of(ksqlConfig, new HashMap<>())));
// Then:
assertThat(result.getCommandResult(), is(Optional.of("Stream created")));
}
Aggregations