|
| 1 | +package org.apache.fluss.lake.paimon; |
| 2 | + |
| 3 | +import org.apache.fluss.types.DataType; |
| 4 | +import org.apache.fluss.types.DataTypes; |
| 5 | + |
| 6 | +import org.apache.paimon.types.ArrayType; |
| 7 | +import org.apache.paimon.types.BigIntType; |
| 8 | +import org.apache.paimon.types.BinaryType; |
| 9 | +import org.apache.paimon.types.BooleanType; |
| 10 | +import org.apache.paimon.types.CharType; |
| 11 | +import org.apache.paimon.types.DataField; |
| 12 | +import org.apache.paimon.types.DataTypeVisitor; |
| 13 | +import org.apache.paimon.types.DateType; |
| 14 | +import org.apache.paimon.types.DecimalType; |
| 15 | +import org.apache.paimon.types.DoubleType; |
| 16 | +import org.apache.paimon.types.FloatType; |
| 17 | +import org.apache.paimon.types.IntType; |
| 18 | +import org.apache.paimon.types.LocalZonedTimestampType; |
| 19 | +import org.apache.paimon.types.MapType; |
| 20 | +import org.apache.paimon.types.MultisetType; |
| 21 | +import org.apache.paimon.types.RowType; |
| 22 | +import org.apache.paimon.types.SmallIntType; |
| 23 | +import org.apache.paimon.types.TimeType; |
| 24 | +import org.apache.paimon.types.TimestampType; |
| 25 | +import org.apache.paimon.types.TinyIntType; |
| 26 | +import org.apache.paimon.types.VarBinaryType; |
| 27 | +import org.apache.paimon.types.VarCharType; |
| 28 | +import org.apache.paimon.types.VariantType; |
| 29 | + |
| 30 | +/** Convert from Paimon's data type to Fluss's data type. */ |
| 31 | +public class PaimonDataTypeToFlussDataType implements DataTypeVisitor<DataType> { |
| 32 | + |
| 33 | + public static final PaimonDataTypeToFlussDataType INSTANCE = |
| 34 | + new PaimonDataTypeToFlussDataType(); |
| 35 | + |
| 36 | + @Override |
| 37 | + public DataType visit(CharType charType) { |
| 38 | + return withNullability(DataTypes.CHAR(charType.getLength()), charType.isNullable()); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public DataType visit(VarCharType varCharType) { |
| 43 | + return withNullability(DataTypes.STRING(), varCharType.isNullable()); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public DataType visit(BooleanType booleanType) { |
| 48 | + return withNullability(DataTypes.BOOLEAN(), booleanType.isNullable()); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public DataType visit(BinaryType binaryType) { |
| 53 | + return withNullability(DataTypes.BINARY(binaryType.getLength()), binaryType.isNullable()); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public DataType visit(VarBinaryType varBinaryType) { |
| 58 | + return withNullability(DataTypes.BYTES(), varBinaryType.isNullable()); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public DataType visit(DecimalType decimalType) { |
| 63 | + return withNullability( |
| 64 | + DataTypes.DECIMAL(decimalType.getPrecision(), decimalType.getScale()), |
| 65 | + decimalType.isNullable()); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public DataType visit(TinyIntType tinyIntType) { |
| 70 | + return withNullability(DataTypes.TINYINT(), tinyIntType.isNullable()); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public DataType visit(SmallIntType smallIntType) { |
| 75 | + return withNullability(DataTypes.SMALLINT(), smallIntType.isNullable()); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public DataType visit(IntType intType) { |
| 80 | + return withNullability(DataTypes.INT(), intType.isNullable()); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public DataType visit(BigIntType bigIntType) { |
| 85 | + return withNullability(DataTypes.BIGINT(), bigIntType.isNullable()); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public DataType visit(FloatType floatType) { |
| 90 | + return withNullability(DataTypes.FLOAT(), floatType.isNullable()); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public DataType visit(DoubleType doubleType) { |
| 95 | + return withNullability(DataTypes.DOUBLE(), doubleType.isNullable()); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public DataType visit(DateType dateType) { |
| 100 | + return withNullability(DataTypes.DATE(), dateType.isNullable()); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public DataType visit(TimeType timeType) { |
| 105 | + return withNullability(DataTypes.TIME(), timeType.isNullable()); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public DataType visit(TimestampType timestampType) { |
| 110 | + return withNullability( |
| 111 | + DataTypes.TIMESTAMP(timestampType.getPrecision()), timestampType.isNullable()); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public DataType visit(LocalZonedTimestampType localZonedTimestampType) { |
| 116 | + return withNullability( |
| 117 | + DataTypes.TIMESTAMP_LTZ(localZonedTimestampType.getPrecision()), |
| 118 | + localZonedTimestampType.isNullable()); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public DataType visit(VariantType variantType) { |
| 123 | + throw new UnsupportedOperationException("VariantType is not supported for Fluss."); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public DataType visit(ArrayType arrayType) { |
| 128 | + return withNullability( |
| 129 | + DataTypes.ARRAY(arrayType.getElementType().accept(this)), arrayType.isNullable()); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public DataType visit(MultisetType multisetType) { |
| 134 | + throw new UnsupportedOperationException("MultisetType is not supported for Fluss."); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public DataType visit(MapType mapType) { |
| 139 | + return withNullability( |
| 140 | + DataTypes.MAP( |
| 141 | + mapType.getKeyType().accept(this), mapType.getValueType().accept(this)), |
| 142 | + mapType.isNullable()); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public DataType visit(RowType rowType) { |
| 147 | + org.apache.fluss.types.RowType.Builder rowTypeBuilder = |
| 148 | + org.apache.fluss.types.RowType.builder(); |
| 149 | + for (DataField field : rowType.getFields()) { |
| 150 | + rowTypeBuilder.field(field.name(), field.type().accept(this), field.description()); |
| 151 | + } |
| 152 | + return withNullability(rowTypeBuilder.build(), rowType.isNullable()); |
| 153 | + } |
| 154 | + |
| 155 | + private DataType withNullability(DataType fluss, boolean nullable) { |
| 156 | + if (fluss.isNullable() != nullable) { |
| 157 | + return nullable ? fluss.nullable() : fluss.notNull(); |
| 158 | + } |
| 159 | + return fluss; |
| 160 | + } |
| 161 | +} |
0 commit comments