Search in sources :

Example 1 with Builder

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);
}
Also used : DataSource(io.confluent.ksql.metastore.model.DataSource) ColumnName(io.confluent.ksql.name.ColumnName) SourceName(io.confluent.ksql.name.SourceName) GrammaticalJoiner(io.confluent.ksql.util.GrammaticalJoiner) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) Name(io.confluent.ksql.name.Name) HashMap(java.util.HashMap) SelectItem(io.confluent.ksql.parser.tree.SelectItem) HashSet(java.util.HashSet) AsValue(io.confluent.ksql.function.udf.AsValue) Pair(io.confluent.ksql.util.Pair) ImmutableList(com.google.common.collect.ImmutableList) Analysis(io.confluent.ksql.analyzer.Analysis) Map(java.util.Map) MetaStore(io.confluent.ksql.metastore.MetaStore) Projection(io.confluent.ksql.planner.Projection) ColumnReferenceExp(io.confluent.ksql.execution.expression.tree.ColumnReferenceExp) RequiredColumns(io.confluent.ksql.planner.RequiredColumns) SystemColumns(io.confluent.ksql.schema.ksql.SystemColumns) Set(java.util.Set) KsqlConfig(io.confluent.ksql.util.KsqlConfig) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Collectors(java.util.stream.Collectors) SelectExpression(io.confluent.ksql.execution.plan.SelectExpression) Builder(io.confluent.ksql.schema.ksql.LogicalSchema.Builder) NodeLocation(io.confluent.ksql.parser.NodeLocation) List(java.util.List) Entry(java.util.Map.Entry) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Builder(io.confluent.ksql.schema.ksql.LogicalSchema.Builder) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) SelectExpression(io.confluent.ksql.execution.plan.SelectExpression) KsqlException(io.confluent.ksql.util.KsqlException) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) ColumnName(io.confluent.ksql.name.ColumnName) SourceName(io.confluent.ksql.name.SourceName) Name(io.confluent.ksql.name.Name) ColumnName(io.confluent.ksql.name.ColumnName) HashSet(java.util.HashSet)

Example 2 with Builder

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"));
}
Also used : Builder(io.confluent.ksql.schema.ksql.LogicalSchema.Builder) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Example 3 with Builder

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"));
}
Also used : Builder(io.confluent.ksql.schema.ksql.LogicalSchema.Builder) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Example 4 with Builder

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"));
}
Also used : Builder(io.confluent.ksql.schema.ksql.LogicalSchema.Builder) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Example 5 with Builder

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

Aggregations

Builder (io.confluent.ksql.schema.ksql.LogicalSchema.Builder)22 KsqlException (io.confluent.ksql.util.KsqlException)9 Test (org.junit.Test)9 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)7 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)6 SelectExpression (io.confluent.ksql.execution.plan.SelectExpression)5 ExpressionTypeManager (io.confluent.ksql.execution.util.ExpressionTypeManager)5 ColumnName (io.confluent.ksql.name.ColumnName)4 List (java.util.List)4 Optional (java.util.Optional)4 Collectors (java.util.stream.Collectors)4 ColumnReferenceExp (io.confluent.ksql.execution.expression.tree.ColumnReferenceExp)3 Expression (io.confluent.ksql.execution.expression.tree.Expression)3 UnqualifiedColumnReferenceExp (io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)3 ImmutableList (com.google.common.collect.ImmutableList)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 RewrittenAnalysis (io.confluent.ksql.analyzer.RewrittenAnalysis)2 CodeGenRunner (io.confluent.ksql.execution.codegen.CodeGenRunner)2 ExpressionEvaluator (io.confluent.ksql.execution.transform.ExpressionEvaluator)2 MetaStore (io.confluent.ksql.metastore.MetaStore)2