in spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/SparkTypeConverter.java [112:192]
public DataType toSparkType(Type gravitinoType) {
if (gravitinoType instanceof Types.ByteType) {
if (((Types.ByteType) gravitinoType).signed()) {
return DataTypes.ByteType;
} else {
return DataTypes.ShortType;
}
} else if (gravitinoType instanceof Types.ShortType) {
if (((Types.ShortType) gravitinoType).signed()) {
return DataTypes.ShortType;
} else {
return DataTypes.IntegerType;
}
} else if (gravitinoType instanceof Types.IntegerType) {
if (((Types.IntegerType) gravitinoType).signed()) {
return DataTypes.IntegerType;
} else {
return DataTypes.LongType;
}
} else if (gravitinoType instanceof Types.LongType) {
if (((Types.LongType) gravitinoType).signed()) {
return DataTypes.LongType;
} else {
return DataTypes.createDecimalType(20, 0);
}
} else if (gravitinoType instanceof Types.FloatType) {
return DataTypes.FloatType;
} else if (gravitinoType instanceof Types.DoubleType) {
return DataTypes.DoubleType;
} else if (gravitinoType instanceof Types.DecimalType) {
Types.DecimalType decimalType = (Types.DecimalType) gravitinoType;
return DataTypes.createDecimalType(decimalType.precision(), decimalType.scale());
} else if (gravitinoType instanceof Types.StringType) {
return DataTypes.StringType;
} else if (gravitinoType instanceof Types.VarCharType) {
Types.VarCharType varCharType = (Types.VarCharType) gravitinoType;
return VarcharType.apply(varCharType.length());
} else if (gravitinoType instanceof Types.FixedCharType) {
Types.FixedCharType charType = (Types.FixedCharType) gravitinoType;
return CharType.apply((charType.length()));
} else if (gravitinoType instanceof Types.BinaryType) {
return DataTypes.BinaryType;
} else if (gravitinoType instanceof Types.BooleanType) {
return DataTypes.BooleanType;
} else if (gravitinoType instanceof Types.DateType) {
return DataTypes.DateType;
} else if (gravitinoType instanceof Types.TimestampType
&& ((Types.TimestampType) gravitinoType).hasTimeZone()) {
return DataTypes.TimestampType;
} else if (gravitinoType instanceof Types.ListType) {
Types.ListType listType = (Types.ListType) gravitinoType;
return DataTypes.createArrayType(
toSparkType(listType.elementType()), listType.elementNullable());
} else if (gravitinoType instanceof Types.MapType) {
Types.MapType mapType = (Types.MapType) gravitinoType;
return DataTypes.createMapType(
toSparkType(mapType.keyType()),
toSparkType(mapType.valueType()),
mapType.valueNullable());
} else if (gravitinoType instanceof Types.StructType) {
Types.StructType structType = (Types.StructType) gravitinoType;
List<StructField> fields =
Arrays.stream(structType.fields())
.map(
f ->
DataTypes.createStructField(
f.name(),
toSparkType(f.type()),
f.nullable(),
f.comment() == null
? new MetadataBuilder().build()
: new MetadataBuilder()
.putString(ConnectorConstants.COMMENT, f.comment())
.build()))
.collect(Collectors.toList());
return DataTypes.createStructType(fields);
} else if (gravitinoType instanceof Types.NullType) {
return DataTypes.NullType;
}
throw new UnsupportedOperationException("Not support " + gravitinoType.toString());
}