use of io.confluent.ksql.schema.ksql.types.SqlMap 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.SqlMap in project ksql by confluentinc.
the class TermCompiler method visitSubscriptExpression.
@Override
public Term visitSubscriptExpression(final SubscriptExpression node, final Context context) {
final SqlType internalSchema = expressionTypeManager.getExpressionSqlType(node.getBase(), context.getLambdaSqlTypeMapping());
switch(internalSchema.baseType()) {
case ARRAY:
final SqlArray array = (SqlArray) internalSchema;
final Term listTerm = process(node.getBase(), context);
final Term indexTerm = process(node.getIndex(), context);
return new SubscriptTerm(listTerm, indexTerm, (o, index) -> ArrayAccess.arrayAccess(((List<?>) o), (Integer) index), array.getItemType());
case MAP:
final SqlMap mapSchema = (SqlMap) internalSchema;
final Term mapTerm = process(node.getBase(), context);
final Term keyTerm = process(node.getIndex(), context);
return new SubscriptTerm(mapTerm, keyTerm, (map, key) -> ((Map<?, ?>) map).get(key), mapSchema.getValueType());
default:
throw new UnsupportedOperationException();
}
}
use of io.confluent.ksql.schema.ksql.types.SqlMap in project ksql by confluentinc.
the class CastEvaluator method castMapToMap.
private static String castMapToMap(final String innerCode, final SqlType from, final SqlType to, final KsqlConfig config) {
final SqlMap fromMap = (SqlMap) from;
final SqlMap toMap = (SqlMap) to;
try {
return "CastEvaluator.castMap(" + innerCode + ", " + mapperFunction(fromMap.getKeyType(), toMap.getKeyType(), config) + ", " + mapperFunction(fromMap.getValueType(), toMap.getValueType(), config) + ")";
} catch (final UnsupportedCastException e) {
throw new UnsupportedCastException(from, to, e);
}
}
Aggregations