Search in sources :

Example 36 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 37 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 38 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)

Example 39 with KsqlStatementException

use of io.confluent.ksql.util.KsqlStatementException in project ksql by confluentinc.

the class ValidatedCommandFactory method createForTerminateQuery.

private static Command createForTerminateQuery(final ConfiguredStatement<? extends Statement> statement, final KsqlExecutionContext context) {
    final TerminateQuery terminateQuery = (TerminateQuery) statement.getStatement();
    final Optional<QueryId> queryId = terminateQuery.getQueryId();
    if (!queryId.isPresent()) {
        context.getPersistentQueries().forEach(PersistentQueryMetadata::close);
        return Command.of(statement);
    } else if (queryId.get().toString().toLowerCase().contains(KsqlConfig.KSQL_TRANSIENT_QUERY_NAME_PREFIX_DEFAULT)) {
        return Command.of(statement);
    }
    final PersistentQueryMetadata queryMetadata = context.getPersistentQuery(queryId.get()).orElseThrow(() -> new KsqlStatementException("Unknown queryId: " + queryId.get(), statement.getStatementText()));
    if (queryMetadata.getPersistentQueryType() == KsqlConstants.PersistentQueryType.CREATE_SOURCE) {
        throw new KsqlStatementException(String.format("Cannot terminate query '%s' because it is linked to a source table.", queryId.get()), statement.getStatementText());
    }
    queryMetadata.close();
    return Command.of(statement);
}
Also used : TerminateQuery(io.confluent.ksql.parser.tree.TerminateQuery) QueryId(io.confluent.ksql.query.QueryId) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata)

Example 40 with KsqlStatementException

use of io.confluent.ksql.util.KsqlStatementException in project ksql by confluentinc.

the class KsqlEngineTest method shouldThrowWhenExecutingDuplicateTable.

@Test
public void shouldThrowWhenExecutingDuplicateTable() {
    // Given:
    final List<ParsedStatement> parsed = ksqlEngine.parse("CREATE TABLE FOO AS SELECT * FROM TEST2; " + "CREATE TABLE FOO WITH (KAFKA_TOPIC='BAR') AS SELECT * FROM TEST2;");
    givenStatementAlreadyExecuted(parsed.get(0));
    final PreparedStatement<?> prepared = prepare(parsed.get(1));
    // When:
    final KsqlStatementException e = assertThrows(KsqlStatementException.class, () -> ksqlEngine.execute(serviceContext, ConfiguredStatement.of(prepared, SessionConfig.of(ksqlConfig, new HashMap<>()))));
    // Then:
    assertThat(e, rawMessage(is("Cannot add table 'FOO': A table with the same name already exists")));
    assertThat(e, statementText(is("CREATE TABLE FOO WITH (KAFKA_TOPIC='BAR') AS SELECT * FROM TEST2;")));
}
Also used : HashMap(java.util.HashMap) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) Test(org.junit.Test)

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