use of io.confluent.ksql.parser.KsqlParser.PreparedStatement in project ksql by confluentinc.
the class DefaultSchemaInjectorFunctionalTest method shouldInferSchema.
private void shouldInferSchema(final org.apache.avro.Schema avroSchema, final Schema expectedKqlSchema) {
// Given:
final Schema expectedSchema = new ConnectKsqlSchemaTranslator(SchemaTranslationPolicies.of(this.translationPolicy)).toKsqlSchema(expectedKqlSchema);
try {
if (testType == TestType.WITHOUT_SCHEMA_ID) {
when(srClient.getLatestSchemaMetadata(any())).thenReturn(new SchemaMetadata(1, 1, avroSchema.toString()));
}
when(srClient.getSchemaBySubjectAndId(any(), anyInt())).thenReturn(this.avroSchema);
when(this.avroSchema.schemaType()).thenReturn("AVRO");
when(this.avroSchema.rawSchema()).thenReturn(avroSchema);
constructCSASSource(expectedSchema);
} catch (final Exception e) {
throw new AssertionError(e);
}
final PreparedStatement<Statement> prepared = KsqlParserTestUtil.buildSingleAst(this.statement, metaStore, true);
// When:
final KsqlConfig ksqlConfig = new KsqlConfig(ImmutableMap.of());
final ConfiguredStatement<?> inferred = schemaInjector.inject(ConfiguredStatement.of(prepared, SessionConfig.of(ksqlConfig, ImmutableMap.of())));
// Then:
if (testType == TestType.CSAS_WITH_SCHEMA_ID) {
validateCsasInference((ConfiguredStatement<CreateStreamAsSelect>) inferred, this.avroSchema);
} else {
final Statement withSchema = KsqlParserTestUtil.buildSingleAst(inferred.getStatementText(), metaStore, true).getStatement();
final Schema actual = getSchemaForDdlStatement((CreateSource) withSchema);
assertThat(FORMATTER.format(actual), equalTo(FORMATTER.format(expectedSchema)));
assertThat(actual, equalTo(expectedSchema));
}
}
use of io.confluent.ksql.parser.KsqlParser.PreparedStatement in project ksql by confluentinc.
the class WSQueryEndpoint method executeStreamQuery.
public void executeStreamQuery(final ServerWebSocket webSocket, final MultiMap requestParams, final KsqlSecurityContext ksqlSecurityContext, final Context context) {
try {
final long startTimeNanos = Time.SYSTEM.nanoseconds();
activenessRegistrar.updateLastRequestTime();
validateVersion(requestParams);
final KsqlRequest request = parseRequest(requestParams);
try {
CommandStoreUtil.waitForCommandSequenceNumber(commandQueue, request, commandQueueCatchupTimeout);
} catch (final InterruptedException e) {
log.debug("Interrupted while waiting for command queue " + "to reach specified command sequence number", e);
SessionUtil.closeSilently(webSocket, INTERNAL_SERVER_ERROR.code(), e.getMessage());
return;
} catch (final TimeoutException e) {
log.debug("Timeout while processing request", e);
SessionUtil.closeSilently(webSocket, TRY_AGAIN_LATER.code(), e.getMessage());
return;
}
final PreparedStatement<?> preparedStatement = parseStatement(request);
final Statement statement = preparedStatement.getStatement();
authorizationValidator.ifPresent(validator -> validator.checkAuthorization(ksqlSecurityContext, ksqlEngine.getMetaStore(), statement));
final RequestContext requestContext = new RequestContext(webSocket, request, ksqlSecurityContext);
if (statement instanceof Query) {
handleQuery(requestContext, (Query) statement, startTimeNanos, context);
} else if (statement instanceof PrintTopic) {
handlePrintTopic(requestContext, (PrintTopic) statement);
} else {
throw new IllegalArgumentException("Unexpected statement type " + statement);
}
} catch (final TopicAuthorizationException e) {
log.debug("Error processing request", e);
SessionUtil.closeSilently(webSocket, INVALID_MESSAGE_TYPE.code(), errorHandler.kafkaAuthorizationErrorMessage(e));
} catch (final Exception e) {
log.debug("Error processing request", e);
SessionUtil.closeSilently(webSocket, INVALID_MESSAGE_TYPE.code(), e.getMessage());
}
}
use of io.confluent.ksql.parser.KsqlParser.PreparedStatement in project ksql by confluentinc.
the class KsqlContextTest method shouldUnsetProperty.
@SuppressWarnings("unchecked")
@Test
public void shouldUnsetProperty() {
// Given:
when(ksqlEngine.parse(any())).thenReturn(ImmutableList.of(PARSED_STMT_0, PARSED_STMT_0));
final Map<String, Object> properties = ImmutableMap.of(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
final PreparedStatement<UnsetProperty> unset = PreparedStatement.of("UNSET SOMETHING", new UnsetProperty(Optional.empty(), ConsumerConfig.AUTO_OFFSET_RESET_CONFIG));
when(ksqlEngine.prepare(any())).thenReturn((PreparedStatement) unset).thenReturn(PREPARED_STMT_0);
// When:
ksqlContext.sql("SQL;", properties);
// Then:
verify(ksqlEngine).execute(serviceContext, ConfiguredStatement.of(PREPARED_STMT_0, SessionConfig.of(SOME_CONFIG, ImmutableMap.of())));
}
use of io.confluent.ksql.parser.KsqlParser.PreparedStatement in project ksql by confluentinc.
the class KsqlContextTest method shouldSetPropertyOnlyOnCommandsFollowingTheSetStatement.
@SuppressWarnings("unchecked")
@Test
public void shouldSetPropertyOnlyOnCommandsFollowingTheSetStatement() {
// Given:
when(ksqlEngine.parse(any())).thenReturn(ImmutableList.of(PARSED_STMT_0, PARSED_STMT_0));
final PreparedStatement<SetProperty> set = PreparedStatement.of("SET SOMETHING", new SetProperty(Optional.empty(), ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"));
when(ksqlEngine.prepare(any())).thenReturn((PreparedStatement) PREPARED_STMT_0).thenReturn(set);
// When:
ksqlContext.sql("SQL;", ImmutableMap.of());
// Then:
verify(ksqlEngine).execute(serviceContext, ConfiguredStatement.of(PREPARED_STMT_0, SessionConfig.of(SOME_CONFIG, ImmutableMap.of())));
}
use of io.confluent.ksql.parser.KsqlParser.PreparedStatement in project ksql by confluentinc.
the class KsqlContextTest method shouldSetProperty.
@SuppressWarnings("unchecked")
@Test
public void shouldSetProperty() {
// Given:
when(ksqlEngine.parse(any())).thenReturn(ImmutableList.of(PARSED_STMT_0, PARSED_STMT_0));
final PreparedStatement<SetProperty> set = PreparedStatement.of("SET SOMETHING", new SetProperty(Optional.empty(), ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"));
when(ksqlEngine.prepare(any())).thenReturn((PreparedStatement) set).thenReturn(PREPARED_STMT_0);
// When:
ksqlContext.sql("SQL;", ImmutableMap.of());
// Then:
verify(ksqlEngine).execute(serviceContext, ConfiguredStatement.of(PREPARED_STMT_0, SessionConfig.of(SOME_CONFIG, ImmutableMap.of(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"))));
}
Aggregations