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