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);
}
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);
}
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());
}
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));
}
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"));
}
Aggregations