Skip to content

Commit

Permalink
[to dev/1.1] Tablet.serialize() may throw an exception due to null va…
Browse files Browse the repository at this point in the history
…lues in the Date column (#330)

* Tablet.serialize() may throw an exception due to null values in the Date column

* fix ut
  • Loading branch information
shuwenwei authored Dec 11, 2024
1 parent 670a0f5 commit cfc8819
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

public class DateUtils {
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public static final int EMPTY_DATE_INT = 10000101;

public static String formatDate(int date) {
return date / 10000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,11 @@ private void serializeColumn(TSDataType dataType, Object column, DataOutputStrea
case DATE:
LocalDate[] dateValues = (LocalDate[]) column;
for (int j = 0; j < rowSize; j++) {
ReadWriteIOUtils.write(DateUtils.parseDateExpressionToInt(dateValues[j]), stream);
ReadWriteIOUtils.write(
dateValues[j] == null
? DateUtils.EMPTY_DATE_INT
: DateUtils.parseDateExpressionToInt(dateValues[j]),
stream);
}
break;
case INT64:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
import org.apache.tsfile.utils.BitMap;
import org.apache.tsfile.write.schema.MeasurementSchema;

import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -107,4 +110,27 @@ public void testSerializationAndDeSerializationWithMoreData() {
fail();
}
}

@Test
public void testSerializeDateColumnWithNullValue() throws IOException {
final List<MeasurementSchema> measurementSchemas = new ArrayList<>();
measurementSchemas.add(new MeasurementSchema("s1", TSDataType.DATE, TSEncoding.PLAIN));
measurementSchemas.add(new MeasurementSchema("s2", TSDataType.DATE, TSEncoding.PLAIN));
Tablet tablet = new Tablet("root.testsg.d1", measurementSchemas);
tablet.addTimestamp(0, 0);
tablet.addValue("s1", 0, LocalDate.now());
tablet.addValue("s2", 0, null);
tablet.addTimestamp(1, 1);
tablet.addValue("s1", 1, null);
tablet.addValue("s2", 1, LocalDate.now());
tablet.rowSize = 2;
ByteBuffer serialized = tablet.serialize();
Tablet deserializeTablet = Tablet.deserialize(serialized);
Assert.assertEquals(
((LocalDate[]) tablet.values[0])[0], ((LocalDate[]) deserializeTablet.values[0])[0]);
Assert.assertTrue(deserializeTablet.bitMaps[1].isMarked(0));
Assert.assertEquals(
((LocalDate[]) tablet.values[1])[1], ((LocalDate[]) deserializeTablet.values[1])[1]);
Assert.assertTrue(deserializeTablet.bitMaps[0].isMarked(1));
}
}

0 comments on commit cfc8819

Please sign in to comment.