Search in sources :

Example 6 with SqlStruct

use of io.confluent.ksql.schema.ksql.types.SqlStruct 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 7 with SqlStruct

use of io.confluent.ksql.schema.ksql.types.SqlStruct 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 8 with SqlStruct

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

the class UdfLoaderTest method shouldLoadStructUdafs.

@SuppressWarnings("unchecked")
@Test
public void shouldLoadStructUdafs() {
    final Schema schema = SchemaBuilder.struct().field("A", Schema.OPTIONAL_INT32_SCHEMA).field("B", Schema.OPTIONAL_INT32_SCHEMA).optional().build();
    final SqlStruct sqlSchema = SqlTypes.struct().field("A", SqlTypes.INTEGER).field("B", SqlTypes.INTEGER).build();
    final KsqlAggregateFunction instance = FUNC_REG.getAggregateFunction(FunctionName.of("test_udaf"), sqlSchema, AggregateFunctionInitArguments.EMPTY_ARGS);
    assertThat(instance.getInitialValueSupplier().get(), equalTo(new Struct(schema).put("A", 0).put("B", 0)));
    assertThat(instance.aggregate(new Struct(schema).put("A", 0).put("B", 0), new Struct(schema).put("A", 1).put("B", 2)), equalTo(new Struct(schema).put("A", 1).put("B", 2)));
    assertThat(instance.getMerger().apply(null, new Struct(schema).put("A", 0).put("B", 0), new Struct(schema).put("A", 1).put("B", 2)), equalTo(new Struct(schema).put("A", 1).put("B", 2)));
}
Also used : SqlStruct(io.confluent.ksql.schema.ksql.types.SqlStruct) Schema(org.apache.kafka.connect.data.Schema) SqlStruct(io.confluent.ksql.schema.ksql.types.SqlStruct) Struct(org.apache.kafka.connect.data.Struct) Test(org.junit.Test)

Example 9 with SqlStruct

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

the class ExpressionTypeManagerTest method shouldEvaluateTypeForStructDereferenceInArray.

@Test
public void shouldEvaluateTypeForStructDereferenceInArray() {
    // Given:
    final SqlStruct inner = SqlTypes.struct().field("IN0", SqlTypes.INTEGER).build();
    final LogicalSchema schema = LogicalSchema.builder().keyColumn(SystemColumns.ROWKEY_NAME, SqlTypes.STRING).valueColumn(COL0, SqlTypes.array(inner)).build();
    expressionTypeManager = new ExpressionTypeManager(schema, functionRegistry);
    final Expression expression = new DereferenceExpression(Optional.empty(), new SubscriptExpression(TestExpressions.COL0, new IntegerLiteral(1)), "IN0");
    // When:
    final SqlType result = expressionTypeManager.getExpressionSqlType(expression);
    // Then:
    assertThat(result, is(SqlTypes.INTEGER));
}
Also used : SqlStruct(io.confluent.ksql.schema.ksql.types.SqlStruct) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) NotExpression(io.confluent.ksql.execution.expression.tree.NotExpression) SimpleCaseExpression(io.confluent.ksql.execution.expression.tree.SimpleCaseExpression) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 10 with SqlStruct

use of io.confluent.ksql.schema.ksql.types.SqlStruct 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

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