use of io.confluent.ksql.parser.KsqlParser.ParsedStatement in project ksql by confluentinc.
the class RequestHandlerTest method shouldDefaultToDistributor.
@Test
public void shouldDefaultToDistributor() {
// Given
givenRequestHandler(ImmutableMap.of());
// When
final List<ParsedStatement> statements = KSQL_PARSER.parse(SOME_STREAM_SQL);
final KsqlEntityList entities = handler.execute(securityContext, statements, sessionProperties);
// Then
assertThat(entities, contains(entity));
verify(distributor, times(2)).execute(argThat(is(configured(preparedStatement(instanceOf(CreateStream.class)), ImmutableMap.of(), ksqlConfig))), eq(ksqlEngine), eq(securityContext));
}
use of io.confluent.ksql.parser.KsqlParser.ParsedStatement in project ksql by confluentinc.
the class RequestHandlerTest method shouldWaitForDistributedStatements.
@Test
public void shouldWaitForDistributedStatements() {
// Given
final KsqlEntity entity1 = mock(KsqlEntity.class);
final KsqlEntity entity2 = mock(KsqlEntity.class);
final KsqlEntity entity3 = mock(KsqlEntity.class);
final StatementExecutor<CreateStream> customExecutor = givenReturningExecutor(CreateStream.class, entity1, entity2, entity3);
givenRequestHandler(ImmutableMap.of(CreateStream.class, customExecutor));
final List<ParsedStatement> statements = KSQL_PARSER.parse("CREATE STREAM x WITH (value_format='json', kafka_topic='x');" + "CREATE STREAM y WITH (value_format='json', kafka_topic='y');" + "CREATE STREAM z WITH (value_format='json', kafka_topic='z');");
// When
handler.execute(securityContext, statements, sessionProperties);
// Then
verify(sync).waitFor(argThat(hasItems(entity1, entity2)), any());
// since the entities passed into sync#waitFor are always the same object, mockito
// cannot verify the original two arguments
verify(sync, times(3)).waitFor(any(), any());
}
use of io.confluent.ksql.parser.KsqlParser.ParsedStatement in project ksql by confluentinc.
the class RequestHandlerTest method shouldThrowOnCreateStreamIfFeatureFlagIsDisabled.
@Test
public void shouldThrowOnCreateStreamIfFeatureFlagIsDisabled() {
// Given
final StatementExecutor<CreateStream> customExecutor = givenReturningExecutor(CreateStream.class, mock(KsqlEntity.class));
when(ksqlConfig.getBoolean(KsqlConfig.KSQL_HEADERS_COLUMNS_ENABLED)).thenReturn(false);
givenRequestHandler(ImmutableMap.of(CreateStream.class, customExecutor));
// When
final List<ParsedStatement> statements = KSQL_PARSER.parse("CREATE STREAM x (c1 ARRAY<STRUCT<`KEY` STRING, `VALUE` BYTES>> HEADERS) " + "WITH (value_format='json', kafka_topic='x');");
final Exception e = assertThrows(KsqlException.class, () -> handler.execute(securityContext, statements, sessionProperties));
// Then
assertThat(e.getMessage(), containsString("Cannot create Stream because schema with headers columns is disabled."));
}
use of io.confluent.ksql.parser.KsqlParser.ParsedStatement 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.parser.KsqlParser.ParsedStatement 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());
}
}
Aggregations