Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[to dev/1.1] Tablet.serialize() may throw an exception due to null values in the Date column #330

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}
Loading