use of io.confluent.ksql.schema.ksql.Column in project ksql by confluentinc.
the class TimestampExtractionPolicyFactory method create.
public static TimestampExtractionPolicy create(final KsqlConfig ksqlConfig, final LogicalSchema schema, final Optional<TimestampColumn> timestampColumn) {
if (!timestampColumn.isPresent()) {
return new MetadataTimestampExtractionPolicy(getDefaultTimestampExtractor(ksqlConfig));
}
final ColumnName col = timestampColumn.get().getColumn();
final Optional<String> timestampFormat = timestampColumn.get().getFormat();
final Column column = schema.findColumn(col).orElseThrow(() -> new KsqlException("The TIMESTAMP column set in the WITH clause does not exist in the schema: '" + col.toString(FormatOptions.noEscape()) + "'"));
final SqlBaseType tsColumnType = column.type().baseType();
if (tsColumnType == SqlBaseType.STRING) {
final String format = timestampFormat.orElseThrow(() -> new KsqlException("A String timestamp field has been specified without" + " also specifying the " + CommonCreateConfigs.TIMESTAMP_FORMAT_PROPERTY.toLowerCase()));
return new StringTimestampExtractionPolicy(col, format);
}
if (timestampFormat.isPresent()) {
throw new KsqlException("'" + CommonCreateConfigs.TIMESTAMP_FORMAT_PROPERTY + "' set in the WITH clause can only be used " + "when the timestamp column is of type STRING.");
}
if (tsColumnType == SqlBaseType.BIGINT) {
return new LongColumnTimestampExtractionPolicy(col);
}
if (tsColumnType == SqlBaseType.TIMESTAMP) {
return new TimestampColumnTimestampExtractionPolicy(col);
}
throw new KsqlException("Timestamp column, " + col + ", should be LONG(INT64), TIMESTAMP," + " or a String with a " + CommonCreateConfigs.TIMESTAMP_FORMAT_PROPERTY.toLowerCase() + " specified.");
}
use of io.confluent.ksql.schema.ksql.Column in project ksql by confluentinc.
the class AlterSourceFactory method create.
public AlterSourceCommand create(final AlterSource statement) {
final DataSource dataSource = metaStore.getSource(statement.getName());
final String dataSourceType = statement.getDataSourceType().getKsqlType();
if (dataSource != null && dataSource.isSource()) {
throw new KsqlException(String.format("Cannot alter %s '%s': ALTER operations are not supported on source %s.", dataSourceType.toLowerCase(), statement.getName().text(), dataSourceType.toLowerCase() + "s"));
}
final List<Column> newColumns = statement.getAlterOptions().stream().map(alterOption -> Column.of(ColumnName.of(alterOption.getColumnName()), alterOption.getType().getSqlType(), Namespace.VALUE, 0)).collect(Collectors.toList());
return new AlterSourceCommand(statement.getName(), dataSourceType, newColumns);
}
use of io.confluent.ksql.schema.ksql.Column in project ksql by confluentinc.
the class GenericRecordFactory method buildKey.
private static GenericKey buildKey(final LogicalSchema schema, final Map<ColumnName, Object> values) {
final Builder builder = GenericKey.builder(schema);
schema.key().stream().map(Column::name).map(values::get).forEach(builder::append);
return builder.build();
}
use of io.confluent.ksql.schema.ksql.Column in project ksql by confluentinc.
the class Selection method buildSchema.
private static LogicalSchema buildSchema(final LogicalSchema sourceSchema, final SelectValueMapper<?> mapper, final List<ColumnName> keyColumnNames) {
final List<ColumnName> keyNames = keyColumnNames.isEmpty() ? getKeyColumnNames(sourceSchema) : keyColumnNames;
final List<Column> keyCols = sourceSchema.key();
if (keyNames.size() != keyCols.size()) {
throw new IllegalArgumentException("key name count mismatch. " + "names: " + keyNames + ", " + "keys: " + keyCols);
}
final LogicalSchema.Builder schemaBuilder = LogicalSchema.builder();
for (int i = 0; i != keyCols.size(); ++i) {
schemaBuilder.keyColumn(keyNames.get(i), keyCols.get(i).type());
}
for (final SelectInfo select : mapper.getSelects()) {
schemaBuilder.valueColumn(select.getFieldName(), select.getEvaluator().getExpressionType());
}
return schemaBuilder.build();
}
use of io.confluent.ksql.schema.ksql.Column in project ksql by confluentinc.
the class KeyValueExtractor method extractKey.
public static GenericKey extractKey(final JsonObject values, final LogicalSchema logicalSchema, final SqlValueCoercer sqlValueCoercer) {
final List<Column> keyColumns = logicalSchema.key();
final GenericKey.Builder builder = GenericKey.builder(logicalSchema);
for (final Column keyColumn : keyColumns) {
final Object value = values.getValue(keyColumn.name().text());
if (value == null) {
throw new KsqlApiException("Key field must be specified: " + keyColumn.name().text(), Errors.ERROR_CODE_BAD_REQUEST);
}
final Object coercedValue = coerceObject(value, keyColumn.type(), sqlValueCoercer);
builder.append(coercedValue);
}
return builder.build();
}
Aggregations