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