in spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/SparkTypeConverter.java [52:110]
public Type toGravitinoType(DataType sparkType) {
if (sparkType instanceof ByteType) {
return Types.ByteType.get();
} else if (sparkType instanceof ShortType) {
return Types.ShortType.get();
} else if (sparkType instanceof IntegerType) {
return Types.IntegerType.get();
} else if (sparkType instanceof LongType) {
return Types.LongType.get();
} else if (sparkType instanceof FloatType) {
return Types.FloatType.get();
} else if (sparkType instanceof DoubleType) {
return Types.DoubleType.get();
} else if (sparkType instanceof DecimalType) {
DecimalType decimalType = (DecimalType) sparkType;
return Types.DecimalType.of(decimalType.precision(), decimalType.scale());
} else if (sparkType instanceof StringType) {
return Types.StringType.get();
} else if (sparkType instanceof VarcharType) {
VarcharType varcharType = (VarcharType) sparkType;
return Types.VarCharType.of(varcharType.length());
} else if (sparkType instanceof CharType) {
CharType charType = (CharType) sparkType;
return Types.FixedCharType.of(charType.length());
} else if (sparkType instanceof BinaryType) {
return Types.BinaryType.get();
} else if (sparkType instanceof BooleanType) {
return Types.BooleanType.get();
} else if (sparkType instanceof DateType) {
return Types.DateType.get();
} else if (sparkType instanceof TimestampType) {
return Types.TimestampType.withTimeZone();
} else if (sparkType instanceof ArrayType) {
ArrayType arrayType = (ArrayType) sparkType;
return Types.ListType.of(toGravitinoType(arrayType.elementType()), arrayType.containsNull());
} else if (sparkType instanceof MapType) {
MapType mapType = (MapType) sparkType;
return Types.MapType.of(
toGravitinoType(mapType.keyType()),
toGravitinoType(mapType.valueType()),
mapType.valueContainsNull());
} else if (sparkType instanceof StructType) {
StructType structType = (StructType) sparkType;
Types.StructType.Field[] fields =
Arrays.stream(structType.fields())
.map(
f ->
Types.StructType.Field.of(
f.name(),
toGravitinoType(f.dataType()),
f.nullable(),
f.getComment().isDefined() ? f.getComment().get() : null))
.toArray(Types.StructType.Field[]::new);
return Types.StructType.of(fields);
} else if (sparkType instanceof NullType) {
return Types.NullType.get();
}
throw new UnsupportedOperationException("Not support " + sparkType.toString());
}