Skip to content

Commit

Permalink
Long.MAX_VALUE may cause npe in SingleDeviceTsBlockReader (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
shuwenwei authored Dec 18, 2024
1 parent 39c32d7 commit 9ae9b27
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class SingleDeviceTsBlockReader implements TsBlockReader {
private final Map<String, MeasurementColumnContext> measureColumnContextMap;
private final Map<String, IdColumnContext> idColumnContextMap;

private long nextTime;
private Long nextTime;

public SingleDeviceTsBlockReader(
DeviceQueryTask task,
Expand Down Expand Up @@ -142,15 +142,15 @@ public boolean hasNext() {
}

currentBlock.reset();
nextTime = Long.MAX_VALUE;
nextTime = null;
List<MeasurementColumnContext> minTimeColumns = new ArrayList<>();

while (currentBlock.getPositionCount() < blockSize) {
// find the minimum time among the batches and the associated columns
for (Entry<String, MeasurementColumnContext> entry : measureColumnContextMap.entrySet()) {
final BatchData batchData = entry.getValue().currentBatch;
final long currentTime = batchData.currentTime();
if (nextTime > currentTime) {
if (nextTime == null || nextTime > currentTime) {
nextTime = currentTime;
minTimeColumns.clear();
minTimeColumns.add(entry.getValue());
Expand All @@ -161,7 +161,7 @@ public boolean hasNext() {

try {
fillMeasurements(minTimeColumns);
nextTime = Long.MAX_VALUE;
nextTime = null;
} catch (IOException e) {
LOGGER.error("Cannot fill measurements", e);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,77 @@ public void testQueryTable() throws Exception {
}
}

@Test
public void testQueryWithMaxValue() throws Exception {
TableSchema tableSchema =
new TableSchema(
"t1",
Arrays.asList(
new MeasurementSchema("id1", TSDataType.STRING),
new MeasurementSchema("id2", TSDataType.STRING),
new MeasurementSchema("s1", TSDataType.BOOLEAN),
new MeasurementSchema("s2", TSDataType.BOOLEAN)),
Arrays.asList(
Tablet.ColumnCategory.ID,
Tablet.ColumnCategory.ID,
Tablet.ColumnCategory.MEASUREMENT,
Tablet.ColumnCategory.MEASUREMENT));
Tablet tablet =
new Tablet(
Arrays.asList("id1", "id2", "s1", "s2"),
Arrays.asList(
TSDataType.STRING, TSDataType.STRING, TSDataType.BOOLEAN, TSDataType.BOOLEAN),
1024);
tablet.addTimestamp(0, Long.MAX_VALUE - 1);
tablet.addValue("id1", 0, "id_field1");
tablet.addValue("id2", 0, "id_field2");
tablet.addValue("s1", 0, true);
tablet.addValue("s2", 0, false);

tablet.addTimestamp(1, Long.MAX_VALUE);
tablet.addValue("id1", 1, "id_field1");
tablet.addValue("id2", 1, "id_field2");
tablet.addValue("s1", 1, true);
tablet.addValue("s2", 1, false);
try (ITsFileWriter writer =
new TsFileWriterBuilder().file(tsfile).tableSchema(tableSchema).build()) {
writer.write(tablet);
}

try (DeviceTableModelReader tsFileReader = new DeviceTableModelReader(tsfile);
ResultSet resultSet =
tsFileReader.query(
"T1", Arrays.asList("id1", "id2", "S2", "S1"), 0, Long.MAX_VALUE); ) {
// id1 id2 s2 s1
ResultSetMetadata resultSetMetadata = resultSet.getMetadata();
// Time id1 id2 s2 s1
Assert.assertEquals("Time", resultSetMetadata.getColumnName(1));
Assert.assertEquals(TSDataType.INT64, resultSetMetadata.getColumnType(1));
Assert.assertEquals("id1", resultSetMetadata.getColumnName(2));
Assert.assertEquals(TSDataType.STRING, resultSetMetadata.getColumnType(2));
Assert.assertEquals("id2", resultSetMetadata.getColumnName(3));
Assert.assertEquals(TSDataType.STRING, resultSetMetadata.getColumnType(3));
Assert.assertEquals("S2", resultSetMetadata.getColumnName(4));
Assert.assertEquals(TSDataType.BOOLEAN, resultSetMetadata.getColumnType(4));
Assert.assertEquals("S1", resultSetMetadata.getColumnName(5));
Assert.assertEquals(TSDataType.BOOLEAN, resultSetMetadata.getColumnType(5));

Assert.assertTrue(resultSet.next());
Assert.assertEquals(Long.MAX_VALUE - 1, resultSet.getLong(1));
Assert.assertEquals("id_field1", resultSet.getString(2));
Assert.assertEquals("id_field2", resultSet.getString(3));
Assert.assertFalse(resultSet.getBoolean(4));
Assert.assertTrue(resultSet.getBoolean(5));

Assert.assertTrue(resultSet.next());
Assert.assertEquals(Long.MAX_VALUE, resultSet.getLong(1));
Assert.assertEquals("id_field1", resultSet.getString(2));
Assert.assertEquals("id_field2", resultSet.getString(3));
Assert.assertFalse(resultSet.getBoolean(4));
Assert.assertTrue(resultSet.getBoolean(5));
}
}

@Test
public void testQueryTableWithPartialNullValueInChunk() throws Exception {
TableSchema tableSchema =
Expand Down

0 comments on commit 9ae9b27

Please sign in to comment.