Search in sources :

Example 1 with TimestampColumn

use of io.confluent.ksql.execution.timestamp.TimestampColumn in project ksql by confluentinc.

the class LogicalPlanner method getTimestampColumn.

private Optional<TimestampColumn> getTimestampColumn(final LogicalSchema inputSchema, final ImmutableAnalysis analysis) {
    final Optional<ColumnName> timestampColumnName = analysis.getProperties().getTimestampColumnName();
    final Optional<TimestampColumn> timestampColumn = timestampColumnName.map(n -> new TimestampColumn(n, analysis.getProperties().getTimestampFormat()));
    TimestampExtractionPolicyFactory.validateTimestampColumn(ksqlConfig, inputSchema, timestampColumn);
    return timestampColumn;
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) TimestampColumn(io.confluent.ksql.execution.timestamp.TimestampColumn)

Example 2 with TimestampColumn

use of io.confluent.ksql.execution.timestamp.TimestampColumn 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 3 with TimestampColumn

use of io.confluent.ksql.execution.timestamp.TimestampColumn in project ksql by confluentinc.

the class CreateSourceFactory method createStreamCommand.

// This method is called by simple CREATE statements
public CreateStreamCommand createStreamCommand(final CreateStream statement, final KsqlConfig ksqlConfig) {
    final SourceName sourceName = statement.getName();
    final CreateSourceProperties props = statement.getProperties();
    final String topicName = ensureTopicExists(props, serviceContext);
    final LogicalSchema schema = buildSchema(statement.getElements(), ksqlConfig);
    final Optional<TimestampColumn> timestampColumn = buildTimestampColumn(ksqlConfig, props, schema);
    final DataSource dataSource = metaStore.getSource(sourceName);
    if (dataSource != null && !statement.isOrReplace() && !statement.isNotExists()) {
        final String sourceType = dataSource.getDataSourceType().getKsqlType();
        throw new KsqlException(String.format("Cannot add stream '%s': A %s with the same name already exists", sourceName.text(), sourceType.toLowerCase()));
    }
    throwIfCreateOrReplaceOnSourceStreamOrTable(statement, dataSource);
    return new CreateStreamCommand(sourceName, schema, timestampColumn, topicName, buildFormats(statement.getName(), schema, props, ksqlConfig), getWindowInfo(props), Optional.of(statement.isOrReplace()), Optional.of(statement.isSource()));
}
Also used : CreateStreamCommand(io.confluent.ksql.execution.ddl.commands.CreateStreamCommand) SourceName(io.confluent.ksql.name.SourceName) TimestampColumn(io.confluent.ksql.execution.timestamp.TimestampColumn) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) KsqlException(io.confluent.ksql.util.KsqlException) CreateSourceProperties(io.confluent.ksql.parser.properties.with.CreateSourceProperties) DataSource(io.confluent.ksql.metastore.model.DataSource)

Example 4 with TimestampColumn

use of io.confluent.ksql.execution.timestamp.TimestampColumn in project ksql by confluentinc.

the class CreateSourceFactory method createTableCommand.

// This method is called by simple CREATE statements
public CreateTableCommand createTableCommand(final CreateTable statement, final KsqlConfig ksqlConfig) {
    final SourceName sourceName = statement.getName();
    final CreateSourceProperties props = statement.getProperties();
    final String topicName = ensureTopicExists(props, serviceContext);
    final LogicalSchema schema = buildSchema(statement.getElements(), ksqlConfig);
    final DataSource dataSource = metaStore.getSource(sourceName);
    if (dataSource != null && !statement.isOrReplace() && !statement.isNotExists()) {
        final String sourceType = dataSource.getDataSourceType().getKsqlType();
        throw new KsqlException(String.format("Cannot add table '%s': A %s with the same name already exists", sourceName.text(), sourceType.toLowerCase()));
    }
    if (schema.key().isEmpty()) {
        final boolean usingSchemaInference = props.getValueSchemaId().isPresent();
        final String additional = usingSchemaInference ? System.lineSeparator() + "Use a partial schema to define the primary key and still load the value columns from " + "the Schema Registry, for example:" + System.lineSeparator() + "\tCREATE TABLE " + sourceName.text() + " (ID INT PRIMARY KEY) WITH (...);" : "";
        throw new KsqlException("Tables require a PRIMARY KEY. Please define the PRIMARY KEY." + additional);
    }
    throwIfCreateOrReplaceOnSourceStreamOrTable(statement, dataSource);
    final Optional<TimestampColumn> timestampColumn = buildTimestampColumn(ksqlConfig, props, schema);
    return new CreateTableCommand(sourceName, schema, timestampColumn, topicName, buildFormats(statement.getName(), schema, props, ksqlConfig), getWindowInfo(props), Optional.of(statement.isOrReplace()), Optional.of(statement.isSource()));
}
Also used : SourceName(io.confluent.ksql.name.SourceName) TimestampColumn(io.confluent.ksql.execution.timestamp.TimestampColumn) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) KsqlException(io.confluent.ksql.util.KsqlException) CreateTableCommand(io.confluent.ksql.execution.ddl.commands.CreateTableCommand) CreateSourceProperties(io.confluent.ksql.parser.properties.with.CreateSourceProperties) DataSource(io.confluent.ksql.metastore.model.DataSource)

Example 5 with TimestampColumn

use of io.confluent.ksql.execution.timestamp.TimestampColumn in project ksql by confluentinc.

the class CreateSourceFactoryTest method shouldBuildTimestampColumnForStream.

@Test
public void shouldBuildTimestampColumnForStream() {
    // Given:
    givenProperty(CommonCreateConfigs.TIMESTAMP_NAME_PROPERTY, new StringLiteral(quote(ELEMENT2.getName().text())));
    final CreateStream statement = new CreateStream(SOME_NAME, STREAM_ELEMENTS, false, true, withProperties, false);
    // When:
    final CreateStreamCommand cmd = createSourceFactory.createStreamCommand(statement, ksqlConfig);
    // Then:
    assertThat(cmd.getTimestampColumn(), is(Optional.of(new TimestampColumn(ELEMENT2.getName(), Optional.empty()))));
}
Also used : CreateStreamCommand(io.confluent.ksql.execution.ddl.commands.CreateStreamCommand) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) CreateStream(io.confluent.ksql.parser.tree.CreateStream) TimestampColumn(io.confluent.ksql.execution.timestamp.TimestampColumn) Test(org.junit.Test)

Aggregations

TimestampColumn (io.confluent.ksql.execution.timestamp.TimestampColumn)20 Test (org.junit.Test)12 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)11 Matchers.containsString (org.hamcrest.Matchers.containsString)7 CreateStreamCommand (io.confluent.ksql.execution.ddl.commands.CreateStreamCommand)3 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)3 DataSource (io.confluent.ksql.metastore.model.DataSource)3 ColumnName (io.confluent.ksql.name.ColumnName)3 KsqlException (io.confluent.ksql.util.KsqlException)3 QueryContext (io.confluent.ksql.execution.context.QueryContext)2 CreateTableCommand (io.confluent.ksql.execution.ddl.commands.CreateTableCommand)2 TimestampExtractionPolicy (io.confluent.ksql.execution.streams.timestamp.TimestampExtractionPolicy)2 SourceName (io.confluent.ksql.name.SourceName)2 CreateSourceProperties (io.confluent.ksql.parser.properties.with.CreateSourceProperties)2 CreateStream (io.confluent.ksql.parser.tree.CreateStream)2 Column (io.confluent.ksql.schema.ksql.Column)2 Named (org.apache.kafka.streams.kstream.Named)2 GenericRow (io.confluent.ksql.GenericRow)1 Into (io.confluent.ksql.analyzer.Analysis.Into)1 KsqlTopic (io.confluent.ksql.execution.ddl.commands.KsqlTopic)1