Search in sources :

Example 31 with ColumnName

use of io.confluent.ksql.name.ColumnName in project ksql by confluentinc.

the class AggregateParamsFactory method buildSchema.

private static LogicalSchema buildSchema(final LogicalSchema schema, final List<ColumnName> nonAggregateColumns, final List<KsqlAggregateFunction<?, ?, ?>> aggregateFunctions, final boolean useAggregate, final boolean addWindowBounds) {
    final LogicalSchema.Builder schemaBuilder = LogicalSchema.builder();
    schemaBuilder.keyColumns(schema.key());
    for (final ColumnName columnName : nonAggregateColumns) {
        final Column col = schema.findValueColumn(columnName).orElseThrow(IllegalArgumentException::new);
        schemaBuilder.valueColumn(col);
    }
    for (int i = 0; i < aggregateFunctions.size(); i++) {
        final KsqlAggregateFunction<?, ?, ?> aggregateFunction = aggregateFunctions.get(i);
        final ColumnName colName = ColumnNames.aggregateColumn(i);
        final SqlType fieldType = useAggregate ? aggregateFunction.getAggregateType() : aggregateFunction.returnType();
        schemaBuilder.valueColumn(colName, fieldType);
    }
    if (addWindowBounds) {
        // Add window bounds columns, as populated by WindowBoundsPopulator
        schemaBuilder.valueColumn(SystemColumns.WINDOWSTART_NAME, SystemColumns.WINDOWBOUND_TYPE);
        schemaBuilder.valueColumn(SystemColumns.WINDOWEND_NAME, SystemColumns.WINDOWBOUND_TYPE);
    }
    return schemaBuilder.build();
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) Column(io.confluent.ksql.schema.ksql.Column) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) SqlType(io.confluent.ksql.schema.ksql.types.SqlType)

Example 32 with ColumnName

use of io.confluent.ksql.name.ColumnName in project ksql by confluentinc.

the class GroupByParamsFactory method expressionSchema.

private static LogicalSchema expressionSchema(final LogicalSchema sourceSchema, final List<CompiledExpression> groupBys) {
    final ColumnAliasGenerator columnAliasGenerator = ColumnNames.columnAliasGenerator(Stream.of(sourceSchema));
    final LogicalSchema.Builder schemaBuilder = LogicalSchema.builder();
    for (final CompiledExpression groupBy : groupBys) {
        final Expression groupByExp = groupBy.getExpression();
        final ColumnName columnName = groupByExp instanceof ColumnReferenceExp ? ((ColumnReferenceExp) groupByExp).getColumnName() : columnAliasGenerator.uniqueAliasFor(groupByExp);
        schemaBuilder.keyColumn(columnName, groupBy.getExpressionType());
    }
    schemaBuilder.valueColumns(sourceSchema.value());
    return schemaBuilder.build();
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) ColumnReferenceExp(io.confluent.ksql.execution.expression.tree.ColumnReferenceExp) Expression(io.confluent.ksql.execution.expression.tree.Expression) CompiledExpression(io.confluent.ksql.execution.codegen.CompiledExpression) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) ColumnAliasGenerator(io.confluent.ksql.schema.ksql.ColumnAliasGenerator) CompiledExpression(io.confluent.ksql.execution.codegen.CompiledExpression)

Example 33 with ColumnName

use of io.confluent.ksql.name.ColumnName in project ksql by confluentinc.

the class ColumnNamesTest method shouldGenerateSimpleStructColumnName.

@Test
public void shouldGenerateSimpleStructColumnName() {
    // Given:
    final LogicalSchema schema = LogicalSchema.builder().build();
    final ColumnAliasGenerator generator = ColumnNames.columnAliasGenerator(Stream.of(schema));
    final DereferenceExpression exp = new DereferenceExpression(LOCATION, STRUCT_COLUMN, "Field_name");
    // When:
    final ColumnName result1 = generator.uniqueAliasFor(exp);
    final ColumnName result2 = generator.uniqueAliasFor(exp);
    // Then:
    assertThat(result1, is(ColumnName.of("Field_name")));
    assertThat(result2, is(ColumnName.of("Field_name_1")));
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) Test(org.junit.Test)

Example 34 with ColumnName

use of io.confluent.ksql.name.ColumnName in project ksql by confluentinc.

the class ColumnNamesTest method shouldEnsureGeneratedAliasesAreCaseSensitive.

@Test
public void shouldEnsureGeneratedAliasesAreCaseSensitive() {
    // Given:
    final LogicalSchema schema = LogicalSchema.builder().keyColumn(ColumnName.of("KSQL_COL_0"), SqlTypes.STRING).keyColumn(ColumnName.of("KSQL_COL_1"), SqlTypes.STRING).keyColumn(ColumnName.of("ksql_COL_2"), SqlTypes.STRING).valueColumn(ColumnName.of("someField"), SqlTypes.STRING).valueColumn(ColumnName.of("SOMEFIELD_1"), SqlTypes.STRING).build();
    final ColumnAliasGenerator generator = ColumnNames.columnAliasGenerator(Stream.of(schema));
    // When:
    final ColumnName result1 = generator.uniqueAliasFor(new DereferenceExpression(LOCATION, STRUCT_COLUMN, "KSQL_COL"));
    final ColumnName result2 = generator.uniqueAliasFor(new DereferenceExpression(LOCATION, STRUCT_COLUMN, "someField"));
    // Then:
    assertThat(result1, is(ColumnName.of("KSQL_COL_2")));
    assertThat(result2, is(ColumnName.of("someField_1")));
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) Test(org.junit.Test)

Example 35 with ColumnName

use of io.confluent.ksql.name.ColumnName in project ksql by confluentinc.

the class ColumnNamesTest method shouldEnsureGeneratedStructAliasesDoNotClashForNumericFieldNames.

@Test
public void shouldEnsureGeneratedStructAliasesDoNotClashForNumericFieldNames() {
    // Given:
    final LogicalSchema schema = LogicalSchema.builder().keyColumn(ColumnName.of("1"), SqlTypes.STRING).keyColumn(ColumnName.of("2"), SqlTypes.STRING).keyColumn(ColumnName.of("3_1"), SqlTypes.STRING).valueColumn(ColumnName.of("4"), SqlTypes.STRING).valueColumn(ColumnName.of("5"), SqlTypes.STRING).build();
    final ColumnAliasGenerator generator = ColumnNames.columnAliasGenerator(Stream.of(schema));
    final DereferenceExpression fieldWithNameClash = new DereferenceExpression(LOCATION, STRUCT_COLUMN, "3");
    // When:
    final ColumnName result1 = generator.uniqueAliasFor(fieldWithNameClash);
    final ColumnName result2 = generator.uniqueAliasFor(fieldWithNameClash);
    // Then:
    assertThat(result1, is(ColumnName.of("3")));
    assertThat(result2, is(ColumnName.of("3_2")));
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) Test(org.junit.Test)

Aggregations

ColumnName (io.confluent.ksql.name.ColumnName)63 Test (org.junit.Test)32 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)31 Expression (io.confluent.ksql.execution.expression.tree.Expression)23 KsqlException (io.confluent.ksql.util.KsqlException)13 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)11 Column (io.confluent.ksql.schema.ksql.Column)11 UnqualifiedColumnReferenceExp (io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)10 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)9 ColumnReferenceExp (io.confluent.ksql.execution.expression.tree.ColumnReferenceExp)8 Optional (java.util.Optional)8 Collectors (java.util.stream.Collectors)8 FunctionCall (io.confluent.ksql.execution.expression.tree.FunctionCall)7 List (java.util.List)7 TimestampColumn (io.confluent.ksql.execution.timestamp.TimestampColumn)6 Builder (io.confluent.ksql.schema.ksql.LogicalSchema.Builder)6 Set (java.util.Set)6 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)5 QualifiedColumnReferenceExp (io.confluent.ksql.execution.expression.tree.QualifiedColumnReferenceExp)5 SelectExpression (io.confluent.ksql.execution.plan.SelectExpression)5