use of io.confluent.ksql.schema.ksql.types.SqlMap in project ksql by confluentinc.
the class SqlTypeWalkerTest method shouldVisitMap.
@Test
public void shouldVisitMap() {
// Given:
final SqlMap type = SqlTypes.map(SqlTypes.BIGINT, SqlTypes.INTEGER);
when(visitor.visitBigInt(any())).thenReturn("Expected-key");
when(visitor.visitInt(any())).thenReturn("Expected-value");
when(visitor.visitMap(any(), any(), any())).thenReturn("Expected");
// When:
final String result = SqlTypeWalker.visit(type, visitor);
// Then:
verify(visitor).visitInt(same(SqlTypes.INTEGER));
verify(visitor).visitMap(same(type), eq("Expected-key"), eq("Expected-value"));
assertThat(result, is("Expected"));
}
use of io.confluent.ksql.schema.ksql.types.SqlMap in project ksql by confluentinc.
the class ExpressionFormatterTest method shouldFormatMap.
@Test
public void shouldFormatMap() {
final SqlMap map = SqlTypes.map(SqlTypes.INTEGER, SqlTypes.BIGINT);
assertThat(ExpressionFormatter.formatExpression(new Type(map)), equalTo("MAP<INTEGER, BIGINT>"));
}
use of io.confluent.ksql.schema.ksql.types.SqlMap in project ksql by confluentinc.
the class SqlTypeWalker method visitMap.
private static <S, F> S visitMap(final SqlTypeWalker.Visitor<S, F> visitor, final SqlType type) {
final SqlMap map = (SqlMap) type;
final S key = visit(map.getKeyType(), visitor);
final S value = visit(map.getValueType(), visitor);
return visitor.visitMap(map, key, value);
}
use of io.confluent.ksql.schema.ksql.types.SqlMap in project ksql by confluentinc.
the class CastInterpreter method castToMapFunction.
public static CastFunction castToMapFunction(final SqlType from, final SqlType to, final KsqlConfig config) {
if (from.baseType() == SqlBaseType.MAP && to.baseType() == SqlBaseType.MAP) {
final SqlMap fromMap = (SqlMap) from;
final SqlMap toMap = (SqlMap) to;
final CastFunction keyCastFunction = castFunction(fromMap.getKeyType(), toMap.getKeyType(), config);
final CastFunction valueCastFunction = castFunction(fromMap.getValueType(), toMap.getValueType(), config);
return o -> CastEvaluator.castMap((Map<?, ?>) o, keyCastFunction::cast, valueCastFunction::cast);
}
throw new KsqlException("Unsupported cast to " + to + ": " + from);
}
use of io.confluent.ksql.schema.ksql.types.SqlMap in project ksql by confluentinc.
the class GenericsUtil method resolveGenerics.
/**
* Identifies a mapping from generic type to concrete type based on a {@code schema} and
* an {@code instance}, where the {@code instance} schema is expected to have no generic
* types and have the same nested structure as {@code schema}. Any Generic type mapping
* identified is added to the list passed in.
*
* @param mapping a list of GenericType to SqlType mappings
* @param schema the schema that may contain generics
* @param instance a schema with the same structure as {@code schema} but with no generics
*
* @return whether we were able to resolve generics in the instance and schema
*/
// CHECKSTYLE_RULES.OFF: NPathComplexity
// CHECKSTYLE_RULES.OFF: CyclomaticComplexity
private static boolean resolveGenerics(final List<Entry<GenericType, SqlType>> mapping, final ParamType schema, final SqlArgument instance) {
if (!isGeneric(schema) && !matches(schema, instance)) {
// cannot identify from type mismatch
return false;
} else if (!hasGenerics(schema)) {
// nothing left to identify
return true;
}
KsqlPreconditions.checkArgument(isGeneric(schema) || (matches(schema, instance)), "Cannot resolve generics if the schema and instance have differing types: " + schema + " vs. " + instance);
if (schema instanceof LambdaType) {
final LambdaType lambdaType = (LambdaType) schema;
final SqlLambda sqlLambda = instance.getSqlLambdaOrThrow();
if (lambdaType.inputTypes().size() == sqlLambda.getNumInputs()) {
if (sqlLambda instanceof SqlLambdaResolved) {
final SqlLambdaResolved sqlLambdaResolved = (SqlLambdaResolved) sqlLambda;
int i = 0;
for (final ParamType paramType : lambdaType.inputTypes()) {
if (!resolveGenerics(mapping, paramType, SqlArgument.of(sqlLambdaResolved.getInputType().get(i)))) {
return false;
}
i++;
}
return resolveGenerics(mapping, lambdaType.returnType(), SqlArgument.of(sqlLambdaResolved.getReturnType()));
} else {
return true;
}
} else {
return false;
}
}
final SqlType sqlType = instance.getSqlTypeOrThrow();
if (isGeneric(schema)) {
mapping.add(new HashMap.SimpleEntry<>((GenericType) schema, sqlType));
}
if (schema instanceof ArrayType) {
final SqlArray sqlArray = (SqlArray) sqlType;
return resolveGenerics(mapping, ((ArrayType) schema).element(), SqlArgument.of(sqlArray.getItemType()));
}
if (schema instanceof MapType) {
final SqlMap sqlMap = (SqlMap) sqlType;
final MapType mapType = (MapType) schema;
return resolveGenerics(mapping, mapType.key(), SqlArgument.of(sqlMap.getKeyType())) && resolveGenerics(mapping, mapType.value(), SqlArgument.of(sqlMap.getValueType()));
}
if (schema instanceof StructType) {
throw new KsqlException("Generic STRUCT is not yet supported");
}
return true;
}
Aggregations