Search in sources :

Example 11 with SchemaAndId

use of io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId in project ksql by confluentinc.

the class SchemaRegisterInjector method registerForCreateSource.

private void registerForCreateSource(final ConfiguredStatement<? extends CreateSource> cs) {
    // since this injector is chained after the TopicCreateInjector,
    // we can assume that the kafka topic is always present in the
    // statement properties
    final CreateSource statement = cs.getStatement();
    final LogicalSchema schema = statement.getElements().toLogicalSchema();
    final FormatInfo keyFormatInfo = SourcePropertiesUtil.getKeyFormat(statement.getProperties(), statement.getName());
    final Format keyFormat = tryGetFormat(keyFormatInfo, true, cs.getStatementText());
    final SerdeFeatures keyFeatures = SerdeFeaturesFactory.buildKeyFeatures(schema, keyFormat);
    final FormatInfo valueFormatInfo = SourcePropertiesUtil.getValueFormat(statement.getProperties());
    final Format valueFormat = tryGetFormat(valueFormatInfo, false, cs.getStatementText());
    final SerdeFeatures valFeatures = SerdeFeaturesFactory.buildValueFeatures(schema, valueFormat, statement.getProperties().getValueSerdeFeatures(), cs.getSessionConfig().getConfig(false));
    final SchemaAndId rawKeySchema = (SchemaAndId) cs.getSessionConfig().getOverrides().get(CommonCreateConfigs.KEY_SCHEMA_ID);
    final SchemaAndId rawValueSchema = (SchemaAndId) cs.getSessionConfig().getOverrides().get(CommonCreateConfigs.VALUE_SCHEMA_ID);
    registerSchemas(schema, Pair.of(rawKeySchema, rawValueSchema), statement.getProperties().getKafkaTopic(), keyFormatInfo, keyFeatures, valueFormatInfo, valFeatures, cs.getSessionConfig().getConfig(false), cs.getStatementText(), false);
}
Also used : Format(io.confluent.ksql.serde.Format) SchemaAndId(io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId) CreateSource(io.confluent.ksql.parser.tree.CreateSource) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) FormatInfo(io.confluent.ksql.serde.FormatInfo) SerdeFeatures(io.confluent.ksql.serde.SerdeFeatures)

Example 12 with SchemaAndId

use of io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId 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 13 with SchemaAndId

use of io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId in project ksql by confluentinc.

the class DefaultSchemaInjectorTest method shouldGetValueSchemaWithSchemaId.

@Test
public void shouldGetValueSchemaWithSchemaId() {
    // Given:
    givenValueButNotKeyInferenceSupported(ImmutableMap.of("VALUE_SCHEMA_ID", new IntegerLiteral(42)));
    // When:
    final ConfiguredStatement<CreateStream> result = injector.inject(csStatement);
    // Then:
    assertThat(result.getStatementText(), is("CREATE STREAM `cs` (" + "`intField` INTEGER, " + "`bigIntField` BIGINT, " + "`doubleField` DOUBLE, " + "`stringField` STRING, " + "`booleanField` BOOLEAN, " + "`arrayField` ARRAY<INTEGER>, " + "`mapField` MAP<STRING, BIGINT>, " + "`structField` STRUCT<`s0` BIGINT>, " + "`decimalField` DECIMAL(4, 2)) " + "WITH (KAFKA_TOPIC='some-topic', KEY_FORMAT='kafka', " + "VALUE_FORMAT='json_sr', VALUE_SCHEMA_FULL_NAME='myrecord', VALUE_SCHEMA_ID=42);"));
    assertThat(result.getSessionConfig().getOverrides(), hasKey(ConnectFormat.VALUE_SCHEMA_ID));
    SchemaAndId schemaAndId = (SchemaAndId) result.getSessionConfig().getOverrides().get(ConnectFormat.VALUE_SCHEMA_ID);
    assertThat(schemaAndId.id, is(42));
    assertThat(schemaAndId.rawSchema, sameInstance(VALUE_AVRO_SCHEMA));
    verify(schemaSupplier).getValueSchema(any(), eq(Optional.of(42)), any(), any());
}
Also used : SchemaAndId(io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId) CreateStream(io.confluent.ksql.parser.tree.CreateStream) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 14 with SchemaAndId

use of io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId in project ksql by confluentinc.

the class DefaultSchemaInjectorFunctionalTest method validateCsasInference.

private static void validateCsasInference(final ConfiguredStatement<CreateStreamAsSelect> statement, final AvroSchema expectedSchema) {
    assertThat(statement.getSessionConfig().getOverrides(), hasKey(CommonCreateConfigs.VALUE_SCHEMA_ID));
    final SchemaAndId schemaAndId = (SchemaAndId) statement.getSessionConfig().getOverrides().get(CommonCreateConfigs.VALUE_SCHEMA_ID);
    assertThat(schemaAndId.rawSchema, is(expectedSchema));
}
Also used : SchemaAndId(io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId)

Example 15 with SchemaAndId

use of io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId in project ksql by confluentinc.

the class SchemaRegisterInjectorTest method shouldThrowInconsistentSchemaIdExceptionWithOverrideSchema.

@Test
public void shouldThrowInconsistentSchemaIdExceptionWithOverrideSchema() throws IOException, RestClientException {
    // Given:
    when(schemaRegistryClient.register(anyString(), any(ParsedSchema.class))).thenReturn(2);
    final SchemaAndId schemaAndId = SchemaAndId.schemaAndId(SCHEMA.value(), AVRO_SCHEMA, 1);
    givenStatement("CREATE STREAM source (id int key, f1 varchar) " + "WITH (" + "kafka_topic='expectedName', " + "key_format='PROTOBUF', " + "value_format='AVRO', " + "value_schema_id=1, " + "partitions=1" + ");", Pair.of(null, schemaAndId));
    // When:
    final Exception e = assertThrows(KsqlStatementException.class, () -> injector.inject(statement));
    // Then:
    assertThat(e.getMessage(), containsString("Schema id registered is 2 which is " + "different from provided VALUE_SCHEMA_ID 1." + System.lineSeparator() + "Topic: expectedName" + System.lineSeparator() + "Subject: expectedName-value"));
}
Also used : SchemaAndId(io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) KsqlSchemaRegistryNotConfiguredException(io.confluent.ksql.util.KsqlSchemaRegistryNotConfiguredException) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) KsqlException(io.confluent.ksql.util.KsqlException) IOException(java.io.IOException) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) KsqlSchemaAuthorizationException(io.confluent.ksql.exception.KsqlSchemaAuthorizationException) Test(org.junit.Test)

Aggregations

SchemaAndId (io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId)18 Test (org.junit.Test)10 KsqlException (io.confluent.ksql.util.KsqlException)8 KsqlStatementException (io.confluent.ksql.util.KsqlStatementException)8 KsqlSchemaRegistryNotConfiguredException (io.confluent.ksql.util.KsqlSchemaRegistryNotConfiguredException)6 ParsedSchema (io.confluent.kafka.schemaregistry.ParsedSchema)5 RestClientException (io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)5 KsqlSchemaAuthorizationException (io.confluent.ksql.exception.KsqlSchemaAuthorizationException)5 IOException (java.io.IOException)5 CreateSourceAsProperties (io.confluent.ksql.parser.properties.with.CreateSourceAsProperties)4 CreateAsSelect (io.confluent.ksql.parser.tree.CreateAsSelect)4 FormatInfo (io.confluent.ksql.serde.FormatInfo)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 CreateSourceCommand (io.confluent.ksql.execution.ddl.commands.CreateSourceCommand)3 CreateSource (io.confluent.ksql.parser.tree.CreateSource)3 Column (io.confluent.ksql.schema.ksql.Column)3 SimpleColumn (io.confluent.ksql.schema.ksql.SimpleColumn)3 SerdeFeatures (io.confluent.ksql.serde.SerdeFeatures)3 CreateStream (io.confluent.ksql.parser.tree.CreateStream)2 Format (io.confluent.ksql.serde.Format)2