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