Search in sources :

Example 6 with CreateSourceAsProperties

use of io.confluent.ksql.parser.properties.with.CreateSourceAsProperties 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 7 with CreateSourceAsProperties

use of io.confluent.ksql.parser.properties.with.CreateSourceAsProperties 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 8 with CreateSourceAsProperties

use of io.confluent.ksql.parser.properties.with.CreateSourceAsProperties 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 9 with CreateSourceAsProperties

use of io.confluent.ksql.parser.properties.with.CreateSourceAsProperties in project ksql by confluentinc.

the class DefaultSchemaInjectorTest method givenFormatsAndProps.

private void givenFormatsAndProps(final String keyFormat, final String valueFormat, final Map<String, Literal> additionalProps) {
    final HashMap<String, Literal> props = new HashMap<>(BASE_PROPS);
    if (keyFormat != null) {
        props.put("KEY_FORMAT", new StringLiteral(keyFormat));
    }
    if (valueFormat != null) {
        props.put("VALUE_FORMAT", new StringLiteral(valueFormat));
    }
    props.putAll(additionalProps);
    final CreateSourceProperties csProps = CreateSourceProperties.from(props);
    final CreateSourceAsProperties casProps = CreateSourceAsProperties.from(props);
    when(cs.getProperties()).thenReturn(csProps);
    when(ct.getProperties()).thenReturn(csProps);
    when(csas.getProperties()).thenReturn(casProps);
    when(ctas.getProperties()).thenReturn(casProps);
/*
    when(csas.getSink()).thenReturn(
        Sink.of(SourceName.of("csas"), true, false, casProps));
    when(ctas.getSink()).thenReturn(
        Sink.of(SourceName.of("ctas"), true, false, casProps));
     */
}
Also used : StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) HashMap(java.util.HashMap) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Literal(io.confluent.ksql.execution.expression.tree.Literal) BooleanLiteral(io.confluent.ksql.execution.expression.tree.BooleanLiteral) CreateSourceAsProperties(io.confluent.ksql.parser.properties.with.CreateSourceAsProperties) Matchers.containsString(org.hamcrest.Matchers.containsString) CreateSourceProperties(io.confluent.ksql.parser.properties.with.CreateSourceProperties)

Aggregations

CreateSourceAsProperties (io.confluent.ksql.parser.properties.with.CreateSourceAsProperties)9 CreateAsSelect (io.confluent.ksql.parser.tree.CreateAsSelect)6 SchemaAndId (io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId)3 Column (io.confluent.ksql.schema.ksql.Column)2 SimpleColumn (io.confluent.ksql.schema.ksql.SimpleColumn)2 FormatInfo (io.confluent.ksql.serde.FormatInfo)2 KsqlException (io.confluent.ksql.util.KsqlException)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 CreateSourceCommand (io.confluent.ksql.execution.ddl.commands.CreateSourceCommand)1 KsqlTopic (io.confluent.ksql.execution.ddl.commands.KsqlTopic)1 BooleanLiteral (io.confluent.ksql.execution.expression.tree.BooleanLiteral)1 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)1 Literal (io.confluent.ksql.execution.expression.tree.Literal)1 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)1 DataSource (io.confluent.ksql.metastore.model.DataSource)1 CreateSourceProperties (io.confluent.ksql.parser.properties.with.CreateSourceProperties)1 CreateStreamAsSelect (io.confluent.ksql.parser.tree.CreateStreamAsSelect)1 Format (io.confluent.ksql.serde.Format)1