Search in sources :

Example 6 with SchemaAndId

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

the class DefaultSchemaInjector method buildElements.

private static TableElements buildElements(final ConfiguredStatement<CreateSource> preparedStatement, final Optional<SchemaAndId> keySchema, final Optional<SchemaAndId> valueSchema) {
    final List<TableElement> elements = new ArrayList<>();
    if (keySchema.isPresent()) {
        final ColumnConstraints constraints = getKeyConstraints(preparedStatement.getStatement());
        keySchema.get().columns.stream().map(col -> new TableElement(col.name(), new Type(col.type()), constraints)).forEach(elements::add);
    } else {
        getKeyColumns(preparedStatement).forEach(elements::add);
    }
    if (valueSchema.isPresent()) {
        valueSchema.get().columns.stream().map(col -> new TableElement(col.name(), new Type(col.type()))).forEach(elements::add);
    } else {
        getValueColumns(preparedStatement).forEach(elements::add);
    }
    return TableElements.of(elements);
}
Also used : IntStream(java.util.stream.IntStream) FormatFactory(io.confluent.ksql.serde.FormatFactory) ServiceContext(io.confluent.ksql.services.ServiceContext) SerdeFeaturesFactory(io.confluent.ksql.serde.SerdeFeaturesFactory) CommonCreateConfigs(io.confluent.ksql.properties.with.CommonCreateConfigs) TableElement(io.confluent.ksql.parser.tree.TableElement) ArrayList(java.util.ArrayList) CreateStream(io.confluent.ksql.parser.tree.CreateStream) Injector(io.confluent.ksql.statement.Injector) CreateTable(io.confluent.ksql.parser.tree.CreateTable) CreateSource(io.confluent.ksql.parser.tree.CreateSource) SchemaResult(io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaResult) ColumnConstraints(io.confluent.ksql.parser.tree.ColumnConstraints) SimpleColumn(io.confluent.ksql.schema.ksql.SimpleColumn) SerdeFeatures(io.confluent.ksql.serde.SerdeFeatures) CreateSourceAsProperties(io.confluent.ksql.parser.properties.with.CreateSourceAsProperties) SchemaAndId(io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId) CreateSourceCommand(io.confluent.ksql.execution.ddl.commands.CreateSourceCommand) ImmutableSet(com.google.common.collect.ImmutableSet) Type(io.confluent.ksql.execution.expression.tree.Type) ImmutableMap(com.google.common.collect.ImmutableMap) SerdeFeature(io.confluent.ksql.serde.SerdeFeature) SetView(com.google.common.collect.Sets.SetView) SandboxedServiceContext(io.confluent.ksql.services.SandboxedServiceContext) SqlFormatter(io.confluent.ksql.parser.SqlFormatter) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) SourcePropertiesUtil(io.confluent.ksql.parser.properties.with.SourcePropertiesUtil) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) ErrorMessageUtil(io.confluent.ksql.util.ErrorMessageUtil) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) Objects(java.util.Objects) CreateAsSelect(io.confluent.ksql.parser.tree.CreateAsSelect) CreateSourceProperties(io.confluent.ksql.parser.properties.with.CreateSourceProperties) List(java.util.List) Stream(java.util.stream.Stream) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) Format(io.confluent.ksql.serde.Format) TableElements(io.confluent.ksql.parser.tree.TableElements) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) Statement(io.confluent.ksql.parser.tree.Statement) Column(io.confluent.ksql.schema.ksql.Column) FormatInfo(io.confluent.ksql.serde.FormatInfo) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) Type(io.confluent.ksql.execution.expression.tree.Type) ColumnConstraints(io.confluent.ksql.parser.tree.ColumnConstraints) ArrayList(java.util.ArrayList) TableElement(io.confluent.ksql.parser.tree.TableElement)

Example 7 with SchemaAndId

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

the class DefaultSchemaInjector method forCreateStatement.

private Optional<ConfiguredStatement<CreateSource>> forCreateStatement(final ConfiguredStatement<CreateSource> statement) {
    final Optional<SchemaAndId> keySchema = getKeySchema(statement);
    final Optional<SchemaAndId> valueSchema = getValueSchema(statement);
    if (!keySchema.isPresent() && !valueSchema.isPresent()) {
        return Optional.empty();
    }
    final CreateSource withSchema = addSchemaFields(statement, keySchema, valueSchema);
    final PreparedStatement<CreateSource> prepared = buildPreparedStatement(withSchema);
    final ImmutableMap.Builder<String, Object> overrideBuilder = ImmutableMap.builder();
    // Only store raw schema if schema id is provided by user
    if (withSchema.getProperties().getKeySchemaId().isPresent()) {
        keySchema.map(schemaAndId -> overrideBuilder.put(CommonCreateConfigs.KEY_SCHEMA_ID, schemaAndId));
    }
    if (withSchema.getProperties().getValueSchemaId().isPresent()) {
        valueSchema.map(schemaAndId -> overrideBuilder.put(CommonCreateConfigs.VALUE_SCHEMA_ID, schemaAndId));
    }
    final ConfiguredStatement<CreateSource> configured = ConfiguredStatement.of(prepared, statement.getSessionConfig().copyWith(overrideBuilder.build()));
    return Optional.of(configured);
}
Also used : SchemaAndId(io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId) CreateSource(io.confluent.ksql.parser.tree.CreateSource) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 8 with SchemaAndId

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

the class DefaultSchemaInjector method getCreateAsKeySchema.

private Optional<SchemaAndId> getCreateAsKeySchema(final ConfiguredStatement<CreateAsSelect> statement, final CreateSourceCommand createSourceCommand) {
    final CreateAsSelect csStmt = statement.getStatement();
    final CreateSourceAsProperties props = csStmt.getProperties();
    final FormatInfo keyFormat = createSourceCommand.getFormats().getKeyFormat();
    if (!shouldInferSchema(props.getKeySchemaId(), statement, keyFormat, true)) {
        return Optional.empty();
    }
    // until we support user-configuration of single key wrapping/unwrapping, we choose
    // to have key schema inference always result in an unwrapped key
    final SerdeFeatures serdeFeatures = SerdeFeaturesFactory.buildKeyFeatures(FormatFactory.of(keyFormat), true);
    if (!shouldInferSchema(props.getKeySchemaId(), statement, keyFormat, true)) {
        return Optional.empty();
    }
    final SchemaAndId schemaAndId = getSchema(props.getKafkaTopic(), props.getKeySchemaId(), keyFormat, serdeFeatures, statement.getStatementText(), true);
    final List<Column> tableColumns = createSourceCommand.getSchema().key();
    checkColumnsCompatibility(props.getKeySchemaId(), tableColumns, schemaAndId.columns, true);
    return Optional.of(schemaAndId);
}
Also used : SchemaAndId(io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId) SimpleColumn(io.confluent.ksql.schema.ksql.SimpleColumn) Column(io.confluent.ksql.schema.ksql.Column) CreateSourceAsProperties(io.confluent.ksql.parser.properties.with.CreateSourceAsProperties) CreateAsSelect(io.confluent.ksql.parser.tree.CreateAsSelect) FormatInfo(io.confluent.ksql.serde.FormatInfo) SerdeFeatures(io.confluent.ksql.serde.SerdeFeatures)

Example 9 with SchemaAndId

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

the class DefaultSchemaInjector method getCreateAsValueSchema.

private Optional<SchemaAndId> getCreateAsValueSchema(final ConfiguredStatement<CreateAsSelect> statement, final CreateSourceCommand createSourceCommand) {
    final CreateAsSelect csStmt = statement.getStatement();
    final CreateSourceAsProperties props = csStmt.getProperties();
    final FormatInfo valueFormat = createSourceCommand.getFormats().getValueFormat();
    if (!shouldInferSchema(props.getValueSchemaId(), statement, valueFormat, false)) {
        return Optional.empty();
    }
    final SchemaAndId schemaAndId = getSchema(props.getKafkaTopic(), props.getValueSchemaId(), valueFormat, createSourceCommand.getFormats().getValueFeatures(), statement.getStatementText(), false);
    final List<Column> tableColumns = createSourceCommand.getSchema().value();
    checkColumnsCompatibility(props.getValueSchemaId(), tableColumns, schemaAndId.columns, false);
    return Optional.of(schemaAndId);
}
Also used : SchemaAndId(io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId) SimpleColumn(io.confluent.ksql.schema.ksql.SimpleColumn) Column(io.confluent.ksql.schema.ksql.Column) CreateSourceAsProperties(io.confluent.ksql.parser.properties.with.CreateSourceAsProperties) CreateAsSelect(io.confluent.ksql.parser.tree.CreateAsSelect) FormatInfo(io.confluent.ksql.serde.FormatInfo)

Example 10 with SchemaAndId

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

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