diff --git a/java/tsfile/src/main/java/org/apache/tsfile/read/reader/block/SingleDeviceTsBlockReader.java b/java/tsfile/src/main/java/org/apache/tsfile/read/reader/block/SingleDeviceTsBlockReader.java index 6c7a43b62..6b6d25011 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/read/reader/block/SingleDeviceTsBlockReader.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/read/reader/block/SingleDeviceTsBlockReader.java @@ -59,7 +59,7 @@ public class SingleDeviceTsBlockReader implements TsBlockReader { private final Map measureColumnContextMap; private final Map idColumnContextMap; - private long nextTime; + private Long nextTime; public SingleDeviceTsBlockReader( DeviceQueryTask task, @@ -142,7 +142,7 @@ public boolean hasNext() { } currentBlock.reset(); - nextTime = Long.MAX_VALUE; + nextTime = null; List minTimeColumns = new ArrayList<>(); while (currentBlock.getPositionCount() < blockSize) { @@ -150,7 +150,7 @@ public boolean hasNext() { for (Entry 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()); @@ -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; diff --git a/java/tsfile/src/test/java/org/apache/tsfile/read/query/ResultSetTest.java b/java/tsfile/src/test/java/org/apache/tsfile/read/query/ResultSetTest.java index 6ab9dad18..8233a7017 100644 --- a/java/tsfile/src/test/java/org/apache/tsfile/read/query/ResultSetTest.java +++ b/java/tsfile/src/test/java/org/apache/tsfile/read/query/ResultSetTest.java @@ -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 =