use of io.confluent.ksql.schema.ksql.LogicalSchema.Builder in project ksql by confluentinc.
the class FinalProjectNode method build.
private Pair<LogicalSchema, List<SelectExpression>> build(final MetaStore metaStore, final KsqlConfig ksqlConfig) {
final LogicalSchema parentSchema = getSource().getSchema();
final Optional<LogicalSchema> targetSchema = getTargetSchema(metaStore);
final List<SelectExpression> selectExpressions = SelectionUtil.buildSelectExpressions(getSource(), projection.selectItems(), targetSchema);
final LogicalSchema schema = SelectionUtil.buildProjectionSchema(parentSchema, selectExpressions, metaStore);
if (into.isPresent()) {
// Persistent queries have key columns as value columns - final projection can exclude them:
final Map<ColumnName, Set<ColumnName>> seenKeyColumns = new HashMap<>();
selectExpressions.removeIf(se -> {
if (se.getExpression() instanceof UnqualifiedColumnReferenceExp) {
final ColumnName columnName = ((UnqualifiedColumnReferenceExp) se.getExpression()).getColumnName();
// Window bounds columns are currently removed if not aliased:
if (SystemColumns.isWindowBound(columnName) && se.getAlias().equals(columnName)) {
return true;
}
if (parentSchema.isKeyColumn(columnName)) {
seenKeyColumns.computeIfAbsent(columnName, k -> new HashSet<>()).add(se.getAlias());
return true;
}
}
return false;
});
for (final Entry<ColumnName, Set<ColumnName>> seenKey : seenKeyColumns.entrySet()) {
if (seenKey.getValue().size() > 1) {
final String keys = GrammaticalJoiner.and().join(seenKey.getValue().stream().map(Name::text).sorted());
throw new KsqlException("The projection contains a key column (" + seenKey.getKey() + ") more than once, aliased as: " + keys + "." + System.lineSeparator() + "Each key column must only be in the projection once. " + "If you intended to copy the key into the value, then consider using the " + AsValue.NAME + " function to indicate which key reference should be copied.");
}
}
}
final LogicalSchema nodeSchema;
if (into.isPresent()) {
nodeSchema = schema.withoutPseudoAndKeyColsInValue(ksqlConfig);
} else {
// Transient queries return key columns in the value, so the projection includes them, and
// the schema needs to include them too:
final Builder builder = LogicalSchema.builder();
builder.keyColumns(parentSchema.key());
schema.columns().forEach(builder::valueColumn);
nodeSchema = builder.build();
}
return Pair.of(nodeSchema, selectExpressions);
}
use of io.confluent.ksql.schema.ksql.LogicalSchema.Builder in project ksql by confluentinc.
the class LogicalSchemaTest method shouldThrowOnDuplicateValueColumnName.
@Test
public void shouldThrowOnDuplicateValueColumnName() {
// Given:
final Builder builder = LogicalSchema.builder().valueColumn(VALUE, BIGINT);
// When:
final Exception e = assertThrows(KsqlException.class, () -> builder.valueColumn(VALUE, BIGINT));
// Then:
assertThat(e.getMessage(), containsString("Duplicate value columns found in schema: `value` BIGINT"));
}
use of io.confluent.ksql.schema.ksql.LogicalSchema.Builder in project ksql by confluentinc.
the class LogicalSchemaTest method shouldThrowOnDuplicateHeaderColumnName.
@Test
public void shouldThrowOnDuplicateHeaderColumnName() {
// Given:
final Builder builder = LogicalSchema.builder().headerColumn(H0, Optional.of("key0"));
// When:
final Exception e = assertThrows(KsqlException.class, () -> builder.headerColumn(H0, Optional.of("key1")));
// Then:
assertThat(e.getMessage(), containsString("Duplicate headers columns found in schema: `h0` BYTES"));
}
use of io.confluent.ksql.schema.ksql.LogicalSchema.Builder in project ksql by confluentinc.
the class LogicalSchemaTest method shouldThrowOnDuplicateKeyColumnName.
@Test
public void shouldThrowOnDuplicateKeyColumnName() {
// Given:
final Builder builder = LogicalSchema.builder().keyColumn(KEY, BIGINT);
// When:
final Exception e = assertThrows(KsqlException.class, () -> builder.keyColumn(KEY, BIGINT));
// Then:
assertThat(e.getMessage(), containsString("Duplicate key columns found in schema: `key` BIGINT"));
}
use of io.confluent.ksql.schema.ksql.LogicalSchema.Builder in project ksql by confluentinc.
the class QueryStreamWriter method buildHeader.
private StreamedRow buildHeader() {
final QueryId queryId = queryMetadata.getQueryId();
// Push queries only return value columns, but query metadata schema includes key and meta:
final LogicalSchema storedSchema = queryMetadata.getLogicalSchema();
final Builder projectionSchema = LogicalSchema.builder();
storedSchema.value().forEach(projectionSchema::valueColumn);
// No session consistency offered for push or stream pull queries
return StreamedRow.header(queryId, projectionSchema.build());
}
Aggregations