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();
}
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();
}
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")));
}
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")));
}
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")));
}
Aggregations