Search in sources :

Example 46 with KsqlStatementException

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"))));
}
Also used : KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) Test(org.junit.Test)

Example 47 with KsqlStatementException

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);
}
Also used : ServiceContext(io.confluent.ksql.services.ServiceContext) SandboxedServiceContext(io.confluent.ksql.services.SandboxedServiceContext) CreateAsSelect(io.confluent.ksql.parser.tree.CreateAsSelect) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) KsqlException(io.confluent.ksql.util.KsqlException) ImmutableMap(com.google.common.collect.ImmutableMap) SchemaAndId(io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId) CreateSourceAsProperties(io.confluent.ksql.parser.properties.with.CreateSourceAsProperties) CreateSourceCommand(io.confluent.ksql.execution.ddl.commands.CreateSourceCommand) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException)

Example 48 with KsqlStatementException

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);
    }
}
Also used : KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) SandboxedServiceContext(io.confluent.ksql.services.SandboxedServiceContext) KsqlException(io.confluent.ksql.util.KsqlException)

Example 49 with KsqlStatementException

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);
}
Also used : SchemaAndId(io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId) ServiceContext(io.confluent.ksql.services.ServiceContext) SandboxedServiceContext(io.confluent.ksql.services.SandboxedServiceContext) CreateSourceCommand(io.confluent.ksql.execution.ddl.commands.CreateSourceCommand) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) KsqlSchemaRegistryNotConfiguredException(io.confluent.ksql.util.KsqlSchemaRegistryNotConfiguredException) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) KsqlException(io.confluent.ksql.util.KsqlException)

Example 50 with KsqlStatementException

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);
    }
}
Also used : GenericRecordFactory(io.confluent.ksql.engine.generic.GenericRecordFactory) InsertValues(io.confluent.ksql.parser.tree.InsertValues) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) KsqlConfig(io.confluent.ksql.util.KsqlConfig) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) KsqlTopicAuthorizationException(io.confluent.ksql.exception.KsqlTopicAuthorizationException) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) KsqlException(io.confluent.ksql.util.KsqlException) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) ExecutionException(java.util.concurrent.ExecutionException) KsqlSchemaAuthorizationException(io.confluent.ksql.exception.KsqlSchemaAuthorizationException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) KsqlGenericRecord(io.confluent.ksql.engine.generic.KsqlGenericRecord)

Aggregations

KsqlStatementException (io.confluent.ksql.util.KsqlStatementException)54 Test (org.junit.Test)28 KsqlException (io.confluent.ksql.util.KsqlException)14 Query (io.confluent.ksql.parser.tree.Query)11 ParsedStatement (io.confluent.ksql.parser.KsqlParser.ParsedStatement)10 ConfiguredStatement (io.confluent.ksql.statement.ConfiguredStatement)7 KsqlConfig (io.confluent.ksql.util.KsqlConfig)6 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)6 SessionConfig (io.confluent.ksql.config.SessionConfig)5 Statement (io.confluent.ksql.parser.tree.Statement)5 ServiceContext (io.confluent.ksql.services.ServiceContext)5 HashMap (java.util.HashMap)5 DataSource (io.confluent.ksql.metastore.model.DataSource)4 PreparedStatement (io.confluent.ksql.parser.KsqlParser.PreparedStatement)4 QueryId (io.confluent.ksql.query.QueryId)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 ExecuteResult (io.confluent.ksql.KsqlExecutionContext.ExecuteResult)3 ImmutableAnalysis (io.confluent.ksql.analyzer.ImmutableAnalysis)3 DdlCommand (io.confluent.ksql.execution.ddl.commands.DdlCommand)3 CreateAsSelect (io.confluent.ksql.parser.tree.CreateAsSelect)3