use of io.confluent.ksql.util.KsqlStatementException in project ksql by confluentinc.
the class KsqlResourceTest method shouldFailWhenTopicInferenceFailsDuringExecute.
@Test
public void shouldFailWhenTopicInferenceFailsDuringExecute() {
// Given:
givenSource(DataSourceType.KSTREAM, "ORDERS1", "ORDERS1", SOME_SCHEMA);
when(topicInjector.inject(any())).thenThrow(new KsqlStatementException("boom", "some-sql"));
// When:
final KsqlRestException e = assertThrows(KsqlRestException.class, () -> makeRequest("CREATE STREAM orders2 AS SELECT * FROM orders1;"));
// Then:
assertThat(e, exceptionStatusCode(is(BAD_REQUEST.code())));
assertThat(e, exceptionErrorMessage(errorCode(is(Errors.ERROR_CODE_BAD_STATEMENT))));
assertThat(e, exceptionStatementErrorMessage(errorMessage(is("boom"))));
}
use of io.confluent.ksql.util.KsqlStatementException in project ksql by confluentinc.
the class DefaultSchemaInjector method forCreateAsStatement.
private Optional<ConfiguredStatement<CreateAsSelect>> forCreateAsStatement(final ConfiguredStatement<CreateAsSelect> statement) {
final CreateAsSelect csStmt = statement.getStatement();
final CreateSourceAsProperties properties = csStmt.getProperties();
// Don't need to inject schema if no key schema id and value schema id
if (!properties.getKeySchemaId().isPresent() && !properties.getValueSchemaId().isPresent()) {
return Optional.empty();
}
final CreateSourceCommand createSourceCommand;
try {
final ServiceContext sandboxServiceContext = SandboxedServiceContext.create(serviceContext);
createSourceCommand = (CreateSourceCommand) executionContext.createSandbox(sandboxServiceContext).plan(sandboxServiceContext, statement).getDdlCommand().get();
} catch (final Exception e) {
throw new KsqlStatementException("Could not determine output schema for query due to error: " + e.getMessage(), statement.getStatementText(), e);
}
final Optional<SchemaAndId> keySchema = getCreateAsKeySchema(statement, createSourceCommand);
final Optional<SchemaAndId> valueSchema = getCreateAsValueSchema(statement, createSourceCommand);
final CreateAsSelect withSchema = addSchemaFieldsCas(statement, keySchema, valueSchema);
final PreparedStatement<CreateAsSelect> prepared = buildPreparedStatement(withSchema);
final ImmutableMap.Builder<String, Object> overrideBuilder = ImmutableMap.builder();
// Only store raw schema if schema id is provided by user
if (properties.getKeySchemaId().isPresent()) {
keySchema.map(schemaAndId -> overrideBuilder.put(CommonCreateConfigs.KEY_SCHEMA_ID, schemaAndId));
}
if (properties.getValueSchemaId().isPresent()) {
valueSchema.map(schemaAndId -> overrideBuilder.put(CommonCreateConfigs.VALUE_SCHEMA_ID, schemaAndId));
}
final ConfiguredStatement<CreateAsSelect> configured = ConfiguredStatement.of(prepared, statement.getSessionConfig().copyWith(overrideBuilder.build()));
return Optional.of(configured);
}
use of io.confluent.ksql.util.KsqlStatementException in project ksql by confluentinc.
the class SchemaRegisterInjector method registerRawSchema.
private void registerRawSchema(final SchemaAndId schemaAndId, final String topic, final String statementText, final String subject, final Boolean isKey) {
final int id;
try {
id = SchemaRegistryUtil.registerSchema(serviceContext.getSchemaRegistryClient(), schemaAndId.rawSchema, topic, subject, isKey);
} catch (KsqlException e) {
throw new KsqlStatementException("Could not register schema for topic: " + e.getMessage(), statementText, e);
}
final boolean isSandbox = serviceContext instanceof SandboxedServiceContext;
// will return fixed id when register is called.
if (!isSandbox && id != schemaAndId.id) {
final String schemaIdPropStr = isKey ? CommonCreateConfigs.KEY_SCHEMA_ID : CommonCreateConfigs.VALUE_SCHEMA_ID;
throw new KsqlStatementException("Schema id registered is " + id + " which is different from provided " + schemaIdPropStr + " " + schemaAndId.id + "." + System.lineSeparator() + "Topic: " + topic + System.lineSeparator() + "Subject: " + subject + System.lineSeparator() + "Schema: " + schemaAndId.rawSchema, statementText);
}
}
use of io.confluent.ksql.util.KsqlStatementException in project ksql by confluentinc.
the class SchemaRegisterInjector method registerForCreateAs.
private void registerForCreateAs(final ConfiguredStatement<? extends CreateAsSelect> cas) {
final CreateSourceCommand createSourceCommand;
try {
final ServiceContext sandboxServiceContext = SandboxedServiceContext.create(serviceContext);
createSourceCommand = (CreateSourceCommand) executionContext.createSandbox(sandboxServiceContext).plan(sandboxServiceContext, cas).getDdlCommand().get();
} catch (final Exception e) {
throw new KsqlStatementException("Could not determine output schema for query due to error: " + e.getMessage(), cas.getStatementText(), e);
}
final SchemaAndId rawKeySchema = (SchemaAndId) cas.getSessionConfig().getOverrides().get(CommonCreateConfigs.KEY_SCHEMA_ID);
final SchemaAndId rawValueSchema = (SchemaAndId) cas.getSessionConfig().getOverrides().get(CommonCreateConfigs.VALUE_SCHEMA_ID);
registerSchemas(createSourceCommand.getSchema(), Pair.of(rawKeySchema, rawValueSchema), createSourceCommand.getTopicName(), createSourceCommand.getFormats().getKeyFormat(), createSourceCommand.getFormats().getKeyFeatures(), createSourceCommand.getFormats().getValueFormat(), createSourceCommand.getFormats().getValueFeatures(), cas.getSessionConfig().getConfig(false), cas.getStatementText(), true);
}
use of io.confluent.ksql.util.KsqlStatementException in project ksql by confluentinc.
the class InsertValuesExecutor method buildRecord.
private ProducerRecord<byte[], byte[]> buildRecord(final ConfiguredStatement<InsertValues> statement, final MetaStore metaStore, final DataSource dataSource, final ServiceContext serviceContext) {
throwIfDisabled(statement.getSessionConfig().getConfig(false));
final InsertValues insertValues = statement.getStatement();
final KsqlConfig config = statement.getSessionConfig().getConfig(true);
try {
final KsqlGenericRecord row = new GenericRecordFactory(config, metaStore, clock).build(insertValues.getColumns(), insertValues.getValues(), dataSource.getSchema(), dataSource.getDataSourceType());
final byte[] key = serializeKey(row.key, dataSource, config, serviceContext);
final byte[] value = serializeValue(row.value, dataSource, config, serviceContext);
final String topicName = dataSource.getKafkaTopicName();
return new ProducerRecord<>(topicName, null, row.ts, key, value);
} catch (final Exception e) {
throw new KsqlStatementException(createInsertFailedExceptionMessage(insertValues) + " " + e.getMessage(), statement.getStatementText(), e);
}
}
Aggregations