Search in sources :

Example 6 with Builder

use of io.confluent.ksql.schema.ksql.LogicalSchema.Builder in project ksql by confluentinc.

the class JoinParamsFactory method createSchema.

public static LogicalSchema createSchema(final ColumnName keyColName, final LogicalSchema leftSchema, final LogicalSchema rightSchema) {
    final SqlType keyType = throwOnKeyMismatch(leftSchema, rightSchema);
    final Builder builder = LogicalSchema.builder().keyColumn(keyColName, keyType).valueColumns(leftSchema.value()).valueColumns(rightSchema.value());
    if (neitherContain(keyColName, leftSchema, rightSchema)) {
        // Append key to value so its accessible during processing:
        builder.valueColumn(keyColName, keyType);
    }
    return builder.build();
}
Also used : Builder(io.confluent.ksql.schema.ksql.LogicalSchema.Builder) SqlType(io.confluent.ksql.schema.ksql.types.SqlType)

Example 7 with Builder

use of io.confluent.ksql.schema.ksql.LogicalSchema.Builder in project ksql by confluentinc.

the class QueryProjectNode method buildOutputSchema.

/**
 * Builds the output schema of the project node.
 * The output schema comprises of exactly the columns that appear in the SELECT clause of the
 * query.
 * @param metaStore the metastore
 * @return the project node's output schema
 */
private LogicalSchema buildOutputSchema(final MetaStore metaStore) {
    final LogicalSchema outputSchema;
    final LogicalSchema parentSchema = getSource().getSchema();
    final boolean isWindowed = analysis.getFrom().getDataSource().getKsqlTopic().getKeyFormat().isWindowed();
    if (isSelectStar()) {
        outputSchema = buildPullQuerySelectStarSchema(parentSchema.withoutPseudoAndKeyColsInValue(ksqlConfig), isWindowed);
    } else {
        final List<SelectExpression> projects = projection.selectItems().stream().map(SingleColumn.class::cast).map(si -> SelectExpression.of(si.getAlias().orElseThrow(IllegalStateException::new), si.getExpression())).collect(Collectors.toList());
        outputSchema = selectOutputSchema(metaStore, projects, isWindowed);
    }
    if (isScalablePush) {
        // 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();
        outputSchema.columns().forEach(builder::valueColumn);
        return builder.build();
    }
    return outputSchema;
}
Also used : CodeGenRunner(io.confluent.ksql.execution.codegen.CodeGenRunner) SelectItem(io.confluent.ksql.parser.tree.SelectItem) RewrittenAnalysis(io.confluent.ksql.analyzer.RewrittenAnalysis) SingleColumn(io.confluent.ksql.parser.tree.SingleColumn) ImmutableList(com.google.common.collect.ImmutableList) ExpressionTypeManager(io.confluent.ksql.execution.util.ExpressionTypeManager) MetaStore(io.confluent.ksql.metastore.MetaStore) Projection(io.confluent.ksql.planner.Projection) AllColumns(io.confluent.ksql.parser.tree.AllColumns) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) InterpretedExpressionFactory(io.confluent.ksql.execution.interpreter.InterpretedExpressionFactory) SystemColumns(io.confluent.ksql.schema.ksql.SystemColumns) ExpressionEvaluator(io.confluent.ksql.execution.transform.ExpressionEvaluator) Expression(io.confluent.ksql.execution.expression.tree.Expression) 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) Objects(java.util.Objects) List(java.util.List) QueryPlannerOptions(io.confluent.ksql.planner.QueryPlannerOptions) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) SqlTypes(io.confluent.ksql.schema.ksql.types.SqlTypes) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) Builder(io.confluent.ksql.schema.ksql.LogicalSchema.Builder) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) SelectExpression(io.confluent.ksql.execution.plan.SelectExpression) SingleColumn(io.confluent.ksql.parser.tree.SingleColumn)

Example 8 with Builder

use of io.confluent.ksql.schema.ksql.LogicalSchema.Builder in project ksql by confluentinc.

the class RowGenerator method buildLogicalSchema.

private static LogicalSchema buildLogicalSchema(final Generator generator, final AvroData avroData, final String keyFieldName) {
    final org.apache.kafka.connect.data.Schema connectSchema = avroData.toConnectSchema(generator.schema());
    final Field keyField = connectSchema.field(keyFieldName);
    if (keyField == null) {
        throw new IllegalArgumentException("key field does not exist in schema: " + keyFieldName);
    }
    final Builder schemaBuilder = LogicalSchema.builder();
    final ConnectToSqlTypeConverter converter = SchemaConverters.connectToSqlConverter();
    schemaBuilder.keyColumn(KEY_COL_NAME, converter.toSqlType(keyField.schema()));
    connectSchema.fields().forEach(f -> schemaBuilder.valueColumn(ColumnName.of(f.name()), converter.toSqlType(f.schema())));
    return schemaBuilder.build();
}
Also used : Field(org.apache.kafka.connect.data.Field) ConnectToSqlTypeConverter(io.confluent.ksql.schema.ksql.SchemaConverters.ConnectToSqlTypeConverter) Builder(io.confluent.ksql.schema.ksql.LogicalSchema.Builder)

Example 9 with Builder

use of io.confluent.ksql.schema.ksql.LogicalSchema.Builder in project ksql by confluentinc.

the class LogicalSchemaTest method shouldThrowOnMultipleHeadersColumns.

@Test
public void shouldThrowOnMultipleHeadersColumns() {
    // Given:
    final Builder builder = LogicalSchema.builder().headerColumn(H0, Optional.empty());
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> builder.headerColumn(F0, Optional.empty()));
    // Then:
    assertThat(e.getMessage(), containsString("Schema already contains a HEADERS column."));
}
Also used : Builder(io.confluent.ksql.schema.ksql.LogicalSchema.Builder) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Example 10 with Builder

use of io.confluent.ksql.schema.ksql.LogicalSchema.Builder in project ksql by confluentinc.

the class LogicalSchemaTest method shouldDuplicateViaAsBuilder.

@Test
public void shouldDuplicateViaAsBuilder() {
    // Given:
    final Builder builder = SOME_SCHEMA.asBuilder();
    // When:
    final LogicalSchema clone = builder.build();
    // Then:
    assertThat(clone, is(SOME_SCHEMA));
}
Also used : Builder(io.confluent.ksql.schema.ksql.LogicalSchema.Builder) Test(org.junit.Test)

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