Search in sources :

Example 21 with ColumnName

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

the class Selection method buildSchema.

private static LogicalSchema buildSchema(final LogicalSchema sourceSchema, final SelectValueMapper<?> mapper, final List<ColumnName> keyColumnNames) {
    final List<ColumnName> keyNames = keyColumnNames.isEmpty() ? getKeyColumnNames(sourceSchema) : keyColumnNames;
    final List<Column> keyCols = sourceSchema.key();
    if (keyNames.size() != keyCols.size()) {
        throw new IllegalArgumentException("key name count mismatch. " + "names: " + keyNames + ", " + "keys: " + keyCols);
    }
    final LogicalSchema.Builder schemaBuilder = LogicalSchema.builder();
    for (int i = 0; i != keyCols.size(); ++i) {
        schemaBuilder.keyColumn(keyNames.get(i), keyCols.get(i).type());
    }
    for (final SelectInfo select : mapper.getSelects()) {
        schemaBuilder.valueColumn(select.getFieldName(), select.getEvaluator().getExpressionType());
    }
    return schemaBuilder.build();
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) SelectInfo(io.confluent.ksql.execution.transform.select.SelectValueMapper.SelectInfo) Column(io.confluent.ksql.schema.ksql.Column) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema)

Example 22 with ColumnName

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

the class ColumnReferenceValidatorTest method shouldGetSourceForUnqualifiedColumnRef.

@Test
public void shouldGetSourceForUnqualifiedColumnRef() {
    // Given:
    final ColumnName column = ColumnName.of("qualified");
    final Expression expression = new QualifiedColumnReferenceExp(SourceName.of("fully"), column);
    when(sourceSchemas.sourcesWithField(any(), any())).thenReturn(sourceNames("something"));
    // When:
    analyzer.analyzeExpression(expression, CLAUSE_TYPE);
    // Then:
    verify(sourceSchemas).sourcesWithField(Optional.of(SourceName.of("fully")), column);
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) Expression(io.confluent.ksql.execution.expression.tree.Expression) QualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.QualifiedColumnReferenceExp) Test(org.junit.Test)

Example 23 with ColumnName

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

the class ColumnNamesTest method shouldIncrementSyntheticJoinColumnOnClashes.

@Test
public void shouldIncrementSyntheticJoinColumnOnClashes() {
    // Given:
    final LogicalSchema schema = LogicalSchema.builder().keyColumn(ColumnName.of("ROWKEY"), SqlTypes.STRING).keyColumn(ColumnName.of("ROWKEY_1"), SqlTypes.STRING).keyColumn(ColumnName.of("ROWKEY_2"), SqlTypes.STRING).valueColumn(ColumnName.of("someField"), SqlTypes.STRING).valueColumn(ColumnName.of("ROWKEY_3"), SqlTypes.STRING).build();
    // When:
    final ColumnName columnName = ColumnNames.generateSyntheticJoinKey(Stream.of(schema));
    // Then:
    assertThat(columnName, is(ColumnName.of("ROWKEY_4")));
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) Test(org.junit.Test)

Example 24 with ColumnName

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

the class ColumnNamesTest method shouldGenerateStructAliasesForSchemasWithAnyColumnNames.

@Test
public void shouldGenerateStructAliasesForSchemasWithAnyColumnNames() {
    // Given:
    final LogicalSchema schema = LogicalSchema.builder().keyColumn(ColumnName.of("12345"), SqlTypes.STRING).keyColumn(ColumnName.of("12startsWithNumbers"), SqlTypes.STRING).keyColumn(ColumnName.of("12startsAndEndsWithNumbers84"), SqlTypes.STRING).valueColumn(ColumnName.of("numbers12In45Middle"), SqlTypes.STRING).valueColumn(ColumnName.of("endsWithZero0"), SqlTypes.STRING).build();
    final ColumnAliasGenerator generator = ColumnNames.columnAliasGenerator(Stream.of(schema));
    final DereferenceExpression fieldWithNameClash = new DereferenceExpression(LOCATION, STRUCT_COLUMN, "12345");
    // When:
    final ColumnName result = generator.uniqueAliasFor(fieldWithNameClash);
    // Then:
    assertThat(result, is(ColumnName.of("12345_1")));
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) Test(org.junit.Test)

Example 25 with ColumnName

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

the class ColumnNamesTest method shouldEnsureGeneratedStructAliasesDoNotClash.

@Test
public void shouldEnsureGeneratedStructAliasesDoNotClash() {
    // Given:
    final LogicalSchema schema = LogicalSchema.builder().keyColumn(ColumnName.of("someField_2"), SqlTypes.STRING).keyColumn(ColumnName.of("other"), SqlTypes.STRING).keyColumn(ColumnName.of("someField_3"), SqlTypes.STRING).valueColumn(ColumnName.of("another"), SqlTypes.STRING).valueColumn(ColumnName.of("someField"), SqlTypes.STRING).build();
    final ColumnAliasGenerator generator = ColumnNames.columnAliasGenerator(Stream.of(schema));
    final DereferenceExpression fieldWithNameClash = new DereferenceExpression(LOCATION, STRUCT_COLUMN, "someField");
    // When:
    final ColumnName result1 = generator.uniqueAliasFor(fieldWithNameClash);
    final ColumnName result2 = generator.uniqueAliasFor(fieldWithNameClash);
    // Then:
    assertThat(result1, is(ColumnName.of("someField_1")));
    assertThat(result2, is(ColumnName.of("someField_4")));
}
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