Search in sources :

Example 1 with CreateSourceCommand

use of io.confluent.ksql.execution.ddl.commands.CreateSourceCommand 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 2 with CreateSourceCommand

use of io.confluent.ksql.execution.ddl.commands.CreateSourceCommand 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)

Aggregations

CreateSourceCommand (io.confluent.ksql.execution.ddl.commands.CreateSourceCommand)2 SchemaAndId (io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId)2 SandboxedServiceContext (io.confluent.ksql.services.SandboxedServiceContext)2 ServiceContext (io.confluent.ksql.services.ServiceContext)2 KsqlException (io.confluent.ksql.util.KsqlException)2 KsqlStatementException (io.confluent.ksql.util.KsqlStatementException)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 CreateSourceAsProperties (io.confluent.ksql.parser.properties.with.CreateSourceAsProperties)1 CreateAsSelect (io.confluent.ksql.parser.tree.CreateAsSelect)1 KsqlSchemaRegistryNotConfiguredException (io.confluent.ksql.util.KsqlSchemaRegistryNotConfiguredException)1