use of io.confluent.ksql.schema.ksql.types.SqlType 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;
}
use of io.confluent.ksql.schema.ksql.types.SqlType in project ksql by confluentinc.
the class ParamTypes method areCompatible.
// CHECKSTYLE_RULES.OFF: CyclomaticComplexity
// CHECKSTYLE_RULES.OFF: NPathComplexity
public static boolean areCompatible(final SqlArgument argument, final ParamType declared, final boolean allowCast) {
// CHECKSTYLE_RULES.ON: CyclomaticComplexity
// CHECKSTYLE_RULES.ON: NPathComplexity
final Optional<SqlLambda> sqlLambdaOptional = argument.getSqlLambda();
if (sqlLambdaOptional.isPresent() && declared instanceof LambdaType) {
final LambdaType declaredLambda = (LambdaType) declared;
final SqlLambda sqlLambda = sqlLambdaOptional.get();
if (sqlLambda instanceof SqlLambdaResolved) {
final SqlLambdaResolved sqlLambdaResolved = (SqlLambdaResolved) sqlLambda;
if (sqlLambdaResolved.getInputType().size() != declaredLambda.inputTypes().size()) {
return false;
}
int i = 0;
for (final ParamType paramType : declaredLambda.inputTypes()) {
if (!areCompatible(SqlArgument.of(sqlLambdaResolved.getInputType().get(i)), paramType, allowCast)) {
return false;
}
i++;
}
return areCompatible(SqlArgument.of(sqlLambdaResolved.getReturnType()), declaredLambda.returnType(), allowCast);
} else {
return sqlLambda.getNumInputs() == declaredLambda.inputTypes().size();
}
}
if (argument.getSqlIntervalUnit().isPresent() && declared instanceof IntervalUnitType) {
return true;
} else if (argument.getSqlIntervalUnit().isPresent() || declared instanceof IntervalUnitType) {
return false;
}
final SqlType argumentSqlType = argument.getSqlTypeOrThrow();
if (argumentSqlType.baseType() == SqlBaseType.ARRAY && declared instanceof ArrayType) {
return areCompatible(SqlArgument.of(((SqlArray) argumentSqlType).getItemType()), ((ArrayType) declared).element(), allowCast);
}
if (argumentSqlType.baseType() == SqlBaseType.MAP && declared instanceof MapType) {
final SqlMap sqlType = (SqlMap) argumentSqlType;
final MapType mapType = (MapType) declared;
return areCompatible(SqlArgument.of(sqlType.getKeyType()), mapType.key(), allowCast) && areCompatible(SqlArgument.of(sqlType.getValueType()), mapType.value(), allowCast);
}
if (argumentSqlType.baseType() == SqlBaseType.STRUCT && declared instanceof StructType) {
return isStructCompatible(argumentSqlType, declared);
}
return isPrimitiveMatch(argumentSqlType, declared, allowCast);
}
use of io.confluent.ksql.schema.ksql.types.SqlType 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.SqlType in project ksql by confluentinc.
the class OperatorTest method getType.
private static SqlType getType(final SqlBaseType baseType) {
final SqlType type = TYPES.get(baseType);
assertThat("invalid test: need type for base type:" + baseType, type, is(notNullValue()));
return type;
}
use of io.confluent.ksql.schema.ksql.types.SqlType in project ksql by confluentinc.
the class FunctionArgumentsUtil method firstPassOverFunctionArguments.
private static List<SqlArgument> firstPassOverFunctionArguments(final List<Expression> arguments, final ExpressionTypeManager expressionTypeManager, final Map<String, SqlType> lambdaMapping) {
final List<SqlArgument> functionArgumentTypes = new ArrayList<>();
for (final Expression expression : arguments) {
if (expression instanceof LambdaFunctionCall) {
functionArgumentTypes.add(SqlArgument.of(SqlLambda.of(((LambdaFunctionCall) expression).getArguments().size())));
} else {
final SqlType resolvedArgType = expressionTypeManager.getExpressionSqlType(expression, new HashMap<>(lambdaMapping));
functionArgumentTypes.add(SqlArgument.of(resolvedArgType));
}
}
return functionArgumentTypes;
}
Aggregations