Skip to content

Commit

Permalink
Fix tiny int convert error in TextSerializationSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
ruanwenjun committed Jan 2, 2024
1 parent dd64ed5 commit 1aa2c1d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import org.apache.commons.lang3.StringUtils;

import com.google.common.annotations.VisibleForTesting;
import lombok.NonNull;

import java.io.IOException;
Expand Down Expand Up @@ -144,7 +145,8 @@ private Map<Integer, String> splitLineBySeaTunnelRowType(
return splitsMap;
}

private Object convert(String field, SeaTunnelDataType<?> fieldType, int level) {
@VisibleForTesting
Object convert(String field, SeaTunnelDataType<?> fieldType, int level) {
if (StringUtils.isBlank(field)) {
return null;
}
Expand Down Expand Up @@ -201,7 +203,8 @@ private Object convert(String field, SeaTunnelDataType<?> fieldType, int level)
case BOOLEAN:
return Boolean.parseBoolean(field);
case TINYINT:
return Byte.parseByte(field);
byte b = Byte.parseByte(field);
return b & 0xff;
case SMALLINT:
return Short.parseShort(field);
case INT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.seatunnel.format.text.constant.TextFormatConstant;
import org.apache.seatunnel.format.text.exception.SeaTunnelTextFormatException;

import com.google.common.annotations.VisibleForTesting;
import lombok.NonNull;

import java.time.LocalDate;
Expand Down Expand Up @@ -124,7 +125,8 @@ public byte[] serialize(SeaTunnelRow element) {
return String.join(separators[0], strings).getBytes();
}

private String convert(Object field, SeaTunnelDataType<?> fieldType, int level) {
@VisibleForTesting
String convert(Object field, SeaTunnelDataType<?> fieldType, int level) {
if (field == null) {
return "";
}
Expand All @@ -135,6 +137,9 @@ private String convert(Object field, SeaTunnelDataType<?> fieldType, int level)
case STRING:
case BOOLEAN:
case TINYINT:
int value = (int) field;
byte byteValue = (byte) value;
return String.valueOf(byteValue);
case SMALLINT:
case BIGINT:
case DECIMAL:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,26 @@ public void testParse() throws IOException {
Assertions.assertEquals(seaTunnelRow.getField(2), "tyrantlucifer");
Assertions.assertEquals(data, content);
}

@Test
public void testConvertTinyInt() {
TextDeserializationSchema textDeserializationSchema =
TextDeserializationSchema.builder().seaTunnelRowType(seaTunnelRowType).build();
TextSerializationSchema textSerializationSchema =
TextSerializationSchema.builder().seaTunnelRowType(seaTunnelRowType).build();
String tinyIntSerialization = textSerializationSchema.convert(0, BasicType.BYTE_TYPE, 0);
Object tinyIntDeserialization =
textDeserializationSchema.convert(tinyIntSerialization, BasicType.BYTE_TYPE, 0);
Assertions.assertEquals(0, tinyIntDeserialization);

tinyIntSerialization = textSerializationSchema.convert(122, BasicType.BYTE_TYPE, 0);
tinyIntDeserialization =
textDeserializationSchema.convert(tinyIntSerialization, BasicType.BYTE_TYPE, 0);
Assertions.assertEquals(122, tinyIntDeserialization);

tinyIntSerialization = textSerializationSchema.convert(255, BasicType.BYTE_TYPE, 0);
tinyIntDeserialization =
textDeserializationSchema.convert(tinyIntSerialization, BasicType.BYTE_TYPE, 0);
Assertions.assertEquals(255, tinyIntDeserialization);
}
}

0 comments on commit 1aa2c1d

Please sign in to comment.