From 8d9c89784162c00543178dc0caa6c9e13636adb1 Mon Sep 17 00:00:00 2001 From: wgcn <1026688210@qq.com> Date: Fri, 7 Jun 2024 14:04:54 +0800 Subject: [PATCH] fix --- .../org/apache/paimon/data/BinaryWriter.java | 76 +++++-------------- .../serializer/InternalRowSerializer.java | 2 +- 2 files changed, 19 insertions(+), 59 deletions(-) diff --git a/paimon-common/src/main/java/org/apache/paimon/data/BinaryWriter.java b/paimon-common/src/main/java/org/apache/paimon/data/BinaryWriter.java index caee792a55267..8c890be52381a 100644 --- a/paimon-common/src/main/java/org/apache/paimon/data/BinaryWriter.java +++ b/paimon-common/src/main/java/org/apache/paimon/data/BinaryWriter.java @@ -24,6 +24,7 @@ import org.apache.paimon.data.serializer.InternalSerializers; import org.apache.paimon.data.serializer.Serializer; import org.apache.paimon.types.DataType; +import org.apache.paimon.types.DataTypeRoot; import org.apache.paimon.types.DecimalType; import org.apache.paimon.types.LocalZonedTimestampType; import org.apache.paimon.types.TimestampType; @@ -150,6 +151,11 @@ static void write( * @param elementType the element type */ static ValueSetter createValueSetter(DataType elementType) { + return createValueSetter(elementType, null); + } + + static ValueSetter createValueSetter(DataType elementType, Serializer serializer) { + Serializer finalSerializer = createSerializerIfNeed(elementType, serializer); // ordered by type root definition switch (elementType.getTypeRoot()) { case CHAR: @@ -184,81 +190,35 @@ static ValueSetter createValueSetter(DataType elementType) { return (writer, pos, value) -> writer.writeTimestamp(pos, (Timestamp) value, timestampPrecision); case ARRAY: - final Serializer arraySerializer = - InternalSerializers.create(elementType); return (writer, pos, value) -> writer.writeArray( pos, (InternalArray) value, - (InternalArraySerializer) arraySerializer); + (InternalArraySerializer) finalSerializer); case MULTISET: case MAP: - final Serializer mapSerializer = - InternalSerializers.create(elementType); return (writer, pos, value) -> writer.writeMap( - pos, (InternalMap) value, (InternalMapSerializer) mapSerializer); + pos, (InternalMap) value, (InternalMapSerializer) finalSerializer); case ROW: - final Serializer rowSerializer = - InternalSerializers.create(elementType); return (writer, pos, value) -> writer.writeRow( - pos, (InternalRow) value, (InternalRowSerializer) rowSerializer); + pos, (InternalRow) value, (InternalRowSerializer) finalSerializer); default: throw new IllegalArgumentException(); } } - static ValueSetter createValueSetter(DataType elementType, Serializer serializer) { - // ordered by type root definition - switch (elementType.getTypeRoot()) { - case CHAR: - case VARCHAR: - return (writer, pos, value) -> writer.writeString(pos, (BinaryString) value); - case BOOLEAN: - return (writer, pos, value) -> writer.writeBoolean(pos, (boolean) value); - case BINARY: - case VARBINARY: - return (writer, pos, value) -> writer.writeBinary(pos, (byte[]) value); - case DECIMAL: - final int decimalPrecision = getPrecision(elementType); - return (writer, pos, value) -> - writer.writeDecimal(pos, (Decimal) value, decimalPrecision); - case TINYINT: - return (writer, pos, value) -> writer.writeByte(pos, (byte) value); - case SMALLINT: - return (writer, pos, value) -> writer.writeShort(pos, (short) value); - case INTEGER: - case DATE: - case TIME_WITHOUT_TIME_ZONE: - return (writer, pos, value) -> writer.writeInt(pos, (int) value); - case BIGINT: - return (writer, pos, value) -> writer.writeLong(pos, (long) value); - case FLOAT: - return (writer, pos, value) -> writer.writeFloat(pos, (float) value); - case DOUBLE: - return (writer, pos, value) -> writer.writeDouble(pos, (double) value); - case TIMESTAMP_WITHOUT_TIME_ZONE: - case TIMESTAMP_WITH_LOCAL_TIME_ZONE: - final int timestampPrecision = getPrecision(elementType); - return (writer, pos, value) -> - writer.writeTimestamp(pos, (Timestamp) value, timestampPrecision); - case ARRAY: - return (writer, pos, value) -> - writer.writeArray( - pos, (InternalArray) value, (InternalArraySerializer) serializer); - case MULTISET: - case MAP: - return (writer, pos, value) -> - writer.writeMap( - pos, (InternalMap) value, (InternalMapSerializer) serializer); - case ROW: - return (writer, pos, value) -> - writer.writeRow( - pos, (InternalRow) value, (InternalRowSerializer) serializer); - default: - throw new IllegalArgumentException(); + static Serializer createSerializerIfNeed(DataType elementType, Serializer serializer) { + Serializer finalSerializer = serializer; + DataTypeRoot typeRoot = elementType.getTypeRoot(); + if (finalSerializer == null + && (typeRoot == DataTypeRoot.MAP + || typeRoot == DataTypeRoot.ROW + || typeRoot == DataTypeRoot.ARRAY)) { + finalSerializer = InternalSerializers.create(elementType); } + return finalSerializer; } /** Accessor for setting the elements of a binary writer during runtime. */ diff --git a/paimon-common/src/main/java/org/apache/paimon/data/serializer/InternalRowSerializer.java b/paimon-common/src/main/java/org/apache/paimon/data/serializer/InternalRowSerializer.java index 6eefacb86881f..97a5ae31d3721 100644 --- a/paimon-common/src/main/java/org/apache/paimon/data/serializer/InternalRowSerializer.java +++ b/paimon-common/src/main/java/org/apache/paimon/data/serializer/InternalRowSerializer.java @@ -73,7 +73,7 @@ public InternalRowSerializer(DataType[] types, Serializer[] fieldSerializers) for (int i = 0; i < types.length; i++) { DataType type = types[i]; fieldGetters[i] = InternalRow.createFieldGetter(type, i); - valueSetters[i] = BinaryWriter.createValueSetter(type, this); + valueSetters[i] = BinaryWriter.createValueSetter(type, fieldSerializers[i]); } }