Search in sources :

Example 11 with CreateSource

use of io.confluent.ksql.parser.tree.CreateSource in project ksql by confluentinc.

the class DefaultSchemaInjector method getKeySchema.

private Optional<SchemaAndId> getKeySchema(final ConfiguredStatement<CreateSource> statement) {
    final CreateSource csStmt = statement.getStatement();
    final CreateSourceProperties props = csStmt.getProperties();
    final FormatInfo keyFormat = SourcePropertiesUtil.getKeyFormat(props, csStmt.getName());
    if (!shouldInferSchema(props.getKeySchemaId(), statement, keyFormat, true)) {
        return Optional.empty();
    }
    return Optional.of(getSchema(Optional.of(props.getKafkaTopic()), props.getKeySchemaId(), keyFormat, // to have key schema inference always result in an unwrapped key
    SerdeFeaturesFactory.buildKeyFeatures(FormatFactory.of(keyFormat), true), statement.getStatementText(), true));
}
Also used : CreateSource(io.confluent.ksql.parser.tree.CreateSource) FormatInfo(io.confluent.ksql.serde.FormatInfo) CreateSourceProperties(io.confluent.ksql.parser.properties.with.CreateSourceProperties)

Example 12 with CreateSource

use of io.confluent.ksql.parser.tree.CreateSource 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 13 with CreateSource

use of io.confluent.ksql.parser.tree.CreateSource in project ksql by confluentinc.

the class DefaultSchemaInjector method shouldInferSchema.

@SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:NPathComplexity" })
private static boolean shouldInferSchema(final Optional<Integer> schemaId, final ConfiguredStatement<? extends Statement> statement, final FormatInfo formatInfo, final boolean isKey) {
    /*
     * Conditions for schema inference:
     *   1. key_schema_id or value_schema_id property exist or
     *   2. Table elements doesn't exist and format support schema inference for CS/CT
     *
     * Do validation when schemaId presents, so we need to infer schema. Conditions to meet:
     *  1. If schema id is provided, format must support schema inference.
     *  2. Table elements must be empty for CS/CT.
     */
    final String formatProp = isKey ? CommonCreateConfigs.KEY_FORMAT_PROPERTY : CommonCreateConfigs.VALUE_FORMAT_PROPERTY;
    final String schemaIdName = isKey ? CommonCreateConfigs.KEY_SCHEMA_ID : CommonCreateConfigs.VALUE_SCHEMA_ID;
    final String formatPropMsg = String.format("%s should support schema inference when %s is " + "provided. Current format is %s.", formatProp, schemaIdName, formatInfo.getFormat());
    final Format format;
    try {
        format = FormatFactory.of(formatInfo);
    } catch (KsqlException e) {
        if (e.getMessage().contains("does not support the following configs: [schemaId]")) {
            throw new KsqlException(formatPropMsg);
        }
        throw e;
    }
    final boolean cas = statement.getStatement() instanceof CreateAsSelect;
    final boolean hasTableElements;
    if (cas) {
        hasTableElements = false;
    } else {
        hasTableElements = isKey ? hasKeyElements((ConfiguredStatement<CreateSource>) statement) : hasValueElements((ConfiguredStatement<CreateSource>) statement);
    }
    if (schemaId.isPresent()) {
        if (!formatSupportsSchemaInference(format)) {
            throw new KsqlException(formatPropMsg);
        }
        if (hasTableElements) {
            final String msg = "Table elements and " + schemaIdName + " cannot both exist for create " + "statement.";
            throw new KsqlException(msg);
        }
        return true;
    }
    return !cas && !hasTableElements && formatSupportsSchemaInference(format);
}
Also used : Format(io.confluent.ksql.serde.Format) CreateSource(io.confluent.ksql.parser.tree.CreateSource) KsqlException(io.confluent.ksql.util.KsqlException) CreateAsSelect(io.confluent.ksql.parser.tree.CreateAsSelect)

Aggregations

CreateSource (io.confluent.ksql.parser.tree.CreateSource)13 CreateSourceProperties (io.confluent.ksql.parser.properties.with.CreateSourceProperties)7 FormatInfo (io.confluent.ksql.serde.FormatInfo)5 Format (io.confluent.ksql.serde.Format)4 CreateAsSelect (io.confluent.ksql.parser.tree.CreateAsSelect)3 TableElements (io.confluent.ksql.parser.tree.TableElements)3 SchemaAndId (io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaAndId)3 SerdeFeatures (io.confluent.ksql.serde.SerdeFeatures)3 KsqlException (io.confluent.ksql.util.KsqlException)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 CreateTable (io.confluent.ksql.parser.tree.CreateTable)2 TableElement (io.confluent.ksql.parser.tree.TableElement)2 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)2 ConfiguredStatement (io.confluent.ksql.statement.ConfiguredStatement)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Sets (com.google.common.collect.Sets)1 SetView (com.google.common.collect.Sets.SetView)1 KsqlExecutionContext (io.confluent.ksql.KsqlExecutionContext)1