Search in sources :

Example 6 with Column

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.");
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) TimestampColumn(io.confluent.ksql.execution.timestamp.TimestampColumn) Column(io.confluent.ksql.schema.ksql.Column) SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) KsqlException(io.confluent.ksql.util.KsqlException)

Example 7 with Column

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);
}
Also used : DataSource(io.confluent.ksql.metastore.model.DataSource) AlterSource(io.confluent.ksql.parser.tree.AlterSource) List(java.util.List) ColumnName(io.confluent.ksql.name.ColumnName) Objects.requireNonNull(java.util.Objects.requireNonNull) MetaStore(io.confluent.ksql.metastore.MetaStore) AlterSourceCommand(io.confluent.ksql.execution.ddl.commands.AlterSourceCommand) KsqlException(io.confluent.ksql.util.KsqlException) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Column(io.confluent.ksql.schema.ksql.Column) Collectors(java.util.stream.Collectors) Namespace(io.confluent.ksql.schema.ksql.Column.Namespace) AlterSourceCommand(io.confluent.ksql.execution.ddl.commands.AlterSourceCommand) Column(io.confluent.ksql.schema.ksql.Column) KsqlException(io.confluent.ksql.util.KsqlException) DataSource(io.confluent.ksql.metastore.model.DataSource)

Example 8 with Column

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();
}
Also used : Column(io.confluent.ksql.schema.ksql.Column) Builder(io.confluent.ksql.GenericKey.Builder)

Example 9 with Column

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();
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) SelectInfo(io.confluent.ksql.execution.transform.select.SelectValueMapper.SelectInfo) Column(io.confluent.ksql.schema.ksql.Column) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema)

Example 10 with Column

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();
}
Also used : Column(io.confluent.ksql.schema.ksql.Column) JsonObject(io.vertx.core.json.JsonObject) GenericKey(io.confluent.ksql.GenericKey) KsqlApiException(io.confluent.ksql.api.server.KsqlApiException)

Aggregations

Column (io.confluent.ksql.schema.ksql.Column)29 ColumnName (io.confluent.ksql.name.ColumnName)14 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)14 Collectors (java.util.stream.Collectors)9 Expression (io.confluent.ksql.execution.expression.tree.Expression)8 UnqualifiedColumnReferenceExp (io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)8 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)8 KsqlException (io.confluent.ksql.util.KsqlException)8 List (java.util.List)8 Optional (java.util.Optional)8 ColumnReferenceExp (io.confluent.ksql.execution.expression.tree.ColumnReferenceExp)7 TimestampColumn (io.confluent.ksql.execution.timestamp.TimestampColumn)7 ExpressionTypeManager (io.confluent.ksql.execution.util.ExpressionTypeManager)7 Set (java.util.Set)7 SelectExpression (io.confluent.ksql.execution.plan.SelectExpression)6 KsqlTopic (io.confluent.ksql.execution.ddl.commands.KsqlTopic)5 DataSource (io.confluent.ksql.metastore.model.DataSource)5 SourceName (io.confluent.ksql.name.SourceName)5 Builder (io.confluent.ksql.schema.ksql.LogicalSchema.Builder)5 FormatInfo (io.confluent.ksql.serde.FormatInfo)5