Search in sources :

Example 11 with SqlStruct

use of io.confluent.ksql.schema.ksql.types.SqlStruct in project ksql by confluentinc.

the class ExpressionFormatterTest method shouldFormatStruct.

@Test
public void shouldFormatStruct() {
    final SqlStruct struct = SqlStruct.builder().field("field1", SqlTypes.INTEGER).field("field2", SqlTypes.STRING).build();
    assertThat(ExpressionFormatter.formatExpression(new Type(struct)), equalTo("STRUCT<field1 INTEGER, field2 STRING>"));
}
Also used : Type(io.confluent.ksql.execution.expression.tree.Type) SqlStruct(io.confluent.ksql.schema.ksql.types.SqlStruct) Test(org.junit.Test)

Example 12 with SqlStruct

use of io.confluent.ksql.schema.ksql.types.SqlStruct in project ksql by confluentinc.

the class KsqlParserTest method shouldParseCustomTypesInCreateType.

@Test
public void shouldParseCustomTypesInCreateType() {
    // Given:
    final SqlStruct cookie = SqlStruct.builder().field("type", SqlTypes.STRING).build();
    metaStore.registerType("cookie", cookie);
    // When:
    final PreparedStatement<RegisterType> registerType = KsqlParserTestUtil.buildSingleAst("CREATE TYPE pantry AS ARRAY<COOKIE>;", metaStore);
    // Then:
    assertThat(registerType.getStatement().getType().getSqlType(), is(SqlArray.of(cookie)));
}
Also used : SqlStruct(io.confluent.ksql.schema.ksql.types.SqlStruct) RegisterType(io.confluent.ksql.parser.tree.RegisterType) Test(org.junit.Test)

Example 13 with SqlStruct

use of io.confluent.ksql.schema.ksql.types.SqlStruct in project ksql by confluentinc.

the class CastEvaluator method castStructToStruct.

@SuppressWarnings("OptionalGetWithoutIsPresent")
private static String castStructToStruct(final String innerCode, final SqlType from, final SqlType to, final KsqlConfig config) {
    final SqlStruct fromStruct = (SqlStruct) from;
    final SqlStruct toStruct = (SqlStruct) to;
    try {
        final String mappers = fromStruct.fields().stream().filter(fromField -> toStruct.field(fromField.name()).isPresent()).map(fromField -> castFieldToField(fromField, toStruct.field(fromField.name()).get(), config)).collect(Collectors.joining(System.lineSeparator(), "ImmutableMap.builder()\n\t\t", "\n\t\t.build()"));
        // Inefficient, but only way to pass type until SqlToJavaVisitor supports passing
        // additional parameters to the generated code. Hopefully, JVM optimises this away.
        final String schemaCode = "SchemaConverters.sqlToConnectConverter()" + ".toConnectSchema(" + SqlTypeCodeGen.generateCode(toStruct) + ")";
        return "CastEvaluator.castStruct(" + innerCode + ", " + mappers + "," + schemaCode + ")";
    } catch (final UnsupportedCastException e) {
        throw new UnsupportedCastException(from, to, e);
    }
}
Also used : Field(io.confluent.ksql.schema.ksql.types.SqlStruct.Field) ARRAY(io.confluent.ksql.schema.ksql.types.SqlBaseType.ARRAY) HashMap(java.util.HashMap) STRING(io.confluent.ksql.schema.ksql.types.SqlBaseType.STRING) SqlMap(io.confluent.ksql.schema.ksql.types.SqlMap) Function(java.util.function.Function) SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) BIGINT(io.confluent.ksql.schema.ksql.types.SqlBaseType.BIGINT) ArrayList(java.util.ArrayList) Schema(org.apache.kafka.connect.data.Schema) MAP(io.confluent.ksql.schema.ksql.types.SqlBaseType.MAP) INTEGER(io.confluent.ksql.schema.ksql.types.SqlBaseType.INTEGER) BOOLEAN(io.confluent.ksql.schema.ksql.types.SqlBaseType.BOOLEAN) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) STRUCT(io.confluent.ksql.schema.ksql.types.SqlBaseType.STRUCT) SchemaConverters(io.confluent.ksql.schema.ksql.SchemaConverters) DECIMAL(io.confluent.ksql.schema.ksql.types.SqlBaseType.DECIMAL) TIME(io.confluent.ksql.schema.ksql.types.SqlBaseType.TIME) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) SqlStruct(io.confluent.ksql.schema.ksql.types.SqlStruct) DATE(io.confluent.ksql.schema.ksql.types.SqlBaseType.DATE) ImmutableMap(com.google.common.collect.ImmutableMap) DOUBLE(io.confluent.ksql.schema.ksql.types.SqlBaseType.DOUBLE) KsqlConfig(io.confluent.ksql.util.KsqlConfig) SqlArray(io.confluent.ksql.schema.ksql.types.SqlArray) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) TIMESTAMP(io.confluent.ksql.schema.ksql.types.SqlBaseType.TIMESTAMP) SqlDecimal(io.confluent.ksql.schema.ksql.types.SqlDecimal) Struct(org.apache.kafka.connect.data.Struct) KsqlException(io.confluent.ksql.util.KsqlException) SqlStruct(io.confluent.ksql.schema.ksql.types.SqlStruct)

Example 14 with SqlStruct

use of io.confluent.ksql.schema.ksql.types.SqlStruct in project ksql by confluentinc.

the class KsqlParserTest method shouldParseCustomTypesInCreateSource.

@Test
public void shouldParseCustomTypesInCreateSource() {
    // Given:
    final SqlStruct cookie = SqlStruct.builder().field("type", SqlTypes.STRING).build();
    metaStore.registerType("cookie", cookie);
    // When:
    final PreparedStatement<CreateSource> createSource = KsqlParserTestUtil.buildSingleAst("CREATE STREAM foo (cookie COOKIE) WITH (KAFKA_TOPIC='foo', VALUE_FORMAT='AVRO');", metaStore);
    // Then:
    final TableElements elements = createSource.getStatement().getElements();
    assertThat(Iterables.size(elements), is(1));
    final TableElement element = elements.iterator().next();
    assertThat(element.getType().getSqlType(), is(cookie));
}
Also used : SqlStruct(io.confluent.ksql.schema.ksql.types.SqlStruct) TableElements(io.confluent.ksql.parser.tree.TableElements) CreateSource(io.confluent.ksql.parser.tree.CreateSource) TableElement(io.confluent.ksql.parser.tree.TableElement) Test(org.junit.Test)

Aggregations

SqlStruct (io.confluent.ksql.schema.ksql.types.SqlStruct)14 Test (org.junit.Test)11 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)5 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)4 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)4 CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)4 Expression (io.confluent.ksql.execution.expression.tree.Expression)4 Field (io.confluent.ksql.schema.ksql.types.SqlStruct.Field)4 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)3 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)3 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)3 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)3 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)3 NotExpression (io.confluent.ksql.execution.expression.tree.NotExpression)3 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)3 SimpleCaseExpression (io.confluent.ksql.execution.expression.tree.SimpleCaseExpression)3 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)3 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)2