use of io.confluent.ksql.schema.ksql.types.SqlStruct.Field in project ksql by confluentinc.
the class ParamTypes method isStructCompatible.
private static boolean isStructCompatible(final SqlType actual, final ParamType declared) {
final SqlStruct actualStruct = (SqlStruct) actual;
// consider a struct that is empty to match any other struct
if (actualStruct.fields().isEmpty() || ((StructType) declared).getSchema().isEmpty()) {
return true;
}
for (final Entry<String, ParamType> entry : ((StructType) declared).getSchema().entrySet()) {
final String k = entry.getKey();
final Optional<Field> field = actualStruct.field(k);
// intentionally do not allow implicit casting within structs
if (!field.isPresent() || !areCompatible(SqlArgument.of(field.get().type()), entry.getValue(), false)) {
return false;
}
}
return actualStruct.fields().size() == ((StructType) declared).getSchema().size();
}
use of io.confluent.ksql.schema.ksql.types.SqlStruct.Field in project ksql by confluentinc.
the class SqlTypeWalker method visitStruct.
private static <S, F> S visitStruct(final SqlTypeWalker.Visitor<S, F> visitor, final SqlType type) {
final SqlStruct struct = (SqlStruct) type;
final List<F> fields = struct.fields().stream().map(field -> visitField(visitor, field)).collect(Collectors.toList());
return visitor.visitStruct(struct, fields);
}
use of io.confluent.ksql.schema.ksql.types.SqlStruct.Field in project ksql by confluentinc.
the class SqlTypeWalkerTest method shouldStartWalkingFromField.
@Test
public void shouldStartWalkingFromField() {
// Given:
final SqlPrimitiveType type = SqlTypes.BOOLEAN;
final Field field = mock(Field.class);
when(field.type()).thenReturn(type);
when(visitor.visitBoolean(any())).thenReturn("Expected");
when(visitor.visitField(any(), any())).thenReturn(22);
// When:
final Integer result = SqlTypeWalker.visit(field, visitor);
// Then:
verify(visitor).visitBoolean(same(type));
verify(visitor).visitField(same(field), eq("Expected"));
assertThat(result, is(22));
}
use of io.confluent.ksql.schema.ksql.types.SqlStruct.Field in project ksql by confluentinc.
the class SqlTypeWalkerTest method shouldVisitStruct.
@Test
public void shouldVisitStruct() {
// Given:
final SqlStruct type = SqlTypes.struct().field("0", SqlTypes.DOUBLE).field("1", SqlTypes.INTEGER).build();
when(visitor.visitDouble(any())).thenReturn("0");
when(visitor.visitInt(any())).thenReturn("1");
when(visitor.visitField(any(), any())).thenAnswer(inv -> {
final int fieldName = Integer.parseInt(inv.<Field>getArgument(0).name());
final int expectedArg = Integer.parseInt(inv.getArgument(1));
assertThat(fieldName, is(expectedArg));
return fieldName;
});
when(visitor.visitStruct(any(), any())).thenReturn("Expected");
// When:
final String result = SqlTypeWalker.visit(type, visitor);
// Then:
verify(visitor).visitDouble(same(SqlTypes.DOUBLE));
verify(visitor).visitInt(same(SqlTypes.INTEGER));
verify(visitor).visitStruct(same(type), eq(ImmutableList.of(0, 1)));
assertThat(result, is("Expected"));
}
Aggregations