[Improve](map) support map cast with map literal and implicate nested scala cast (#26126)

This commit is contained in:
amory
2023-11-02 22:56:42 +08:00
committed by GitHub
parent 3f47eb5e89
commit 93a934e775
9 changed files with 291 additions and 9 deletions

View File

@ -192,8 +192,10 @@ public class MapType extends Type {
}
public static boolean canCastTo(MapType type, MapType targetType) {
return Type.canCastTo(type.getKeyType(), targetType.getKeyType())
&& Type.canCastTo(type.getValueType(), targetType.getValueType());
return (targetType.getKeyType().isStringType() && type.getKeyType().isStringType()
|| Type.canCastTo(type.getKeyType(), targetType.getKeyType()))
&& (Type.canCastTo(type.getValueType(), targetType.getValueType())
|| targetType.getValueType().isStringType() && type.getValueType().isStringType());
}
@Override

View File

@ -2169,6 +2169,17 @@ public abstract class Type {
return false;
}
return matchExactType(((ArrayType) type2).getItemType(), ((ArrayType) type1).getItemType());
} else if (type2.isMapType()) {
// For types array, we also need to check contains null for case like
// cast(array<not_null(int)> as array<int>)
if (!((MapType) type2).getIsKeyContainsNull() == ((MapType) type1).getIsKeyContainsNull()) {
return false;
}
if (!((MapType) type2).getIsValueContainsNull() == ((MapType) type1).getIsValueContainsNull()) {
return false;
}
return matchExactType(((MapType) type2).getKeyType(), ((MapType) type1).getKeyType())
&& matchExactType(((MapType) type2).getValueType(), ((MapType) type1).getValueType());
} else {
return true;
}