Search in sources :

Example 1 with Field

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();
}
Also used : Field(io.confluent.ksql.schema.ksql.types.SqlStruct.Field) SqlStruct(io.confluent.ksql.schema.ksql.types.SqlStruct)

Example 2 with Field

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);
}
Also used : SqlStruct(io.confluent.ksql.schema.ksql.types.SqlStruct) Field(io.confluent.ksql.schema.ksql.types.SqlStruct.Field) ImmutableMap(com.google.common.collect.ImmutableMap) SqlPrimitiveType(io.confluent.ksql.schema.ksql.types.SqlPrimitiveType) BiFunction(java.util.function.BiFunction) SqlArray(io.confluent.ksql.schema.ksql.types.SqlArray) SqlMap(io.confluent.ksql.schema.ksql.types.SqlMap) Collectors(java.util.stream.Collectors) SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) List(java.util.List) SqlDecimal(io.confluent.ksql.schema.ksql.types.SqlDecimal) Map(java.util.Map) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) SqlStruct(io.confluent.ksql.schema.ksql.types.SqlStruct)

Example 3 with Field

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));
}
Also used : Field(io.confluent.ksql.schema.ksql.types.SqlStruct.Field) SqlPrimitiveType(io.confluent.ksql.schema.ksql.types.SqlPrimitiveType) Test(org.junit.Test)

Example 4 with Field

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"));
}
Also used : Field(io.confluent.ksql.schema.ksql.types.SqlStruct.Field) SqlStruct(io.confluent.ksql.schema.ksql.types.SqlStruct) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

Field (io.confluent.ksql.schema.ksql.types.SqlStruct.Field)4 SqlStruct (io.confluent.ksql.schema.ksql.types.SqlStruct)3 SqlPrimitiveType (io.confluent.ksql.schema.ksql.types.SqlPrimitiveType)2 Test (org.junit.Test)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 SqlArray (io.confluent.ksql.schema.ksql.types.SqlArray)1 SqlBaseType (io.confluent.ksql.schema.ksql.types.SqlBaseType)1 SqlDecimal (io.confluent.ksql.schema.ksql.types.SqlDecimal)1 SqlMap (io.confluent.ksql.schema.ksql.types.SqlMap)1 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)1 List (java.util.List)1 Map (java.util.Map)1 BiFunction (java.util.function.BiFunction)1 Collectors (java.util.stream.Collectors)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1