use of io.confluent.ksql.schema.ksql.types.SqlLambdaResolved in project ksql by confluentinc.
the class UdfLoaderTest method shouldLoadLambdaReduceUdfs.
@Test
public void shouldLoadLambdaReduceUdfs() {
// Given:
final SqlLambdaResolved lambda = SqlLambdaResolved.of(ImmutableList.of(SqlTypes.INTEGER, SqlTypes.INTEGER, SqlTypes.INTEGER), SqlTypes.INTEGER);
// When:
final KsqlScalarFunction fun = FUNC_REG.getUdfFactory(FunctionName.of("reduce")).getFunction(ImmutableList.of(SqlArgument.of(SqlMap.of(SqlTypes.INTEGER, SqlTypes.INTEGER)), SqlArgument.of(SqlTypes.INTEGER), SqlArgument.of(lambda)));
// Then:
assertThat(fun.name().text(), equalToIgnoringCase("reduce"));
}
use of io.confluent.ksql.schema.ksql.types.SqlLambdaResolved in project ksql by confluentinc.
the class UdfLoaderTest method shouldLoadLambdaTransformUdfs.
@Test
public void shouldLoadLambdaTransformUdfs() {
// Given:
final SqlLambdaResolved lambda = SqlLambdaResolved.of(ImmutableList.of(SqlTypes.INTEGER), SqlTypes.INTEGER);
// When:
final KsqlScalarFunction fun = FUNC_REG.getUdfFactory(FunctionName.of("transform")).getFunction(ImmutableList.of(SqlArgument.of(SqlArray.of(SqlTypes.INTEGER)), SqlArgument.of(lambda)));
// Then:
assertThat(fun.name().text(), equalToIgnoringCase("transform"));
}
use of io.confluent.ksql.schema.ksql.types.SqlLambdaResolved 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.SqlLambdaResolved 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);
}
Aggregations