From 5a64e64bf03e6ab634f2fe3ee2e7d56e8b3a00cd Mon Sep 17 00:00:00 2001 From: Tian Jiang Date: Thu, 24 Apr 2025 15:21:37 +0800 Subject: [PATCH 1/3] Remove redundant conversion in TableResultSet --- .../common/block/column/BinaryColumn.java | 2 +- .../read/query/dataset/TableResultSet.java | 98 ++++++++++++++----- 2 files changed, 72 insertions(+), 28 deletions(-) diff --git a/java/tsfile/src/main/java/org/apache/tsfile/read/common/block/column/BinaryColumn.java b/java/tsfile/src/main/java/org/apache/tsfile/read/common/block/column/BinaryColumn.java index 8a794508a..e3c308c91 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/read/common/block/column/BinaryColumn.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/read/common/block/column/BinaryColumn.java @@ -146,7 +146,7 @@ public boolean mayHaveNull() { @Override public boolean isNull(int position) { - return valueIsNull != null && valueIsNull[position + arrayOffset]; + return values[position] == null || valueIsNull != null && valueIsNull[position + arrayOffset]; } @Override diff --git a/java/tsfile/src/main/java/org/apache/tsfile/read/query/dataset/TableResultSet.java b/java/tsfile/src/main/java/org/apache/tsfile/read/query/dataset/TableResultSet.java index 5e0507742..d13439b4b 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/read/query/dataset/TableResultSet.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/read/query/dataset/TableResultSet.java @@ -20,58 +20,102 @@ package org.apache.tsfile.read.query.dataset; import org.apache.tsfile.enums.TSDataType; -import org.apache.tsfile.read.TimeValuePair; -import org.apache.tsfile.read.common.Field; -import org.apache.tsfile.read.common.RowRecord; import org.apache.tsfile.read.common.block.TsBlock; -import org.apache.tsfile.read.reader.IPointReader; import org.apache.tsfile.read.reader.block.TsBlockReader; +import org.apache.tsfile.utils.DateUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; +import java.time.LocalDate; import java.util.List; public class TableResultSet extends AbstractResultSet { private static final Logger LOG = LoggerFactory.getLogger(TableResultSet.class); - private TsBlockReader tsBlockReader; - private IPointReader tsBlockPointReader; - private List columnNameList; - private List dataTypeList; + private final TsBlockReader tsBlockReader; + private TsBlock currentTsBlock; + private int currentTsBlockIndex; public TableResultSet( TsBlockReader tsBlockReader, List columnNameList, List dataTypeList) { super(columnNameList, dataTypeList); this.tsBlockReader = tsBlockReader; - this.columnNameList = columnNameList; - this.dataTypeList = dataTypeList; } @Override public boolean next() throws IOException { - while ((tsBlockPointReader == null || !tsBlockPointReader.hasNextTimeValuePair()) + while ((currentTsBlock == null || currentTsBlockIndex >= currentTsBlock.getPositionCount() - 1) && tsBlockReader.hasNext()) { - TsBlock currentTsBlock = tsBlockReader.next(); - tsBlockPointReader = currentTsBlock.getTsBlockAlignedRowIterator(); + currentTsBlock = tsBlockReader.next(); + currentTsBlockIndex = -1; } - if (tsBlockPointReader == null || !tsBlockPointReader.hasNextTimeValuePair()) { - return false; - } - TimeValuePair currentTimeValuePair = tsBlockPointReader.nextTimeValuePair(); - currentRow = convertTimeValuePairToRowRecord(currentTimeValuePair); - return true; + currentTsBlockIndex++; + return currentTsBlock != null && currentTsBlockIndex < currentTsBlock.getPositionCount(); } - private RowRecord convertTimeValuePairToRowRecord(TimeValuePair timeValuePair) { - RowRecord rowRecord = new RowRecord(timeValuePair.getValues().length); - rowRecord.setTimestamp(timeValuePair.getTimestamp()); - for (int i = 0; i < timeValuePair.getValues().length; i++) { - Object value = timeValuePair.getValues()[i]; - rowRecord.addField(Field.getField(value, dataTypeList.get(i))); - } - return rowRecord; + @Override + public int getInt(int columnIndex) { + // -2 because the columnIndex starts from 1 and the first column is fixed as the time column + return currentTsBlock.getValueColumns()[columnIndex - 2].getInt(currentTsBlockIndex); + } + + @Override + public long getLong(int columnIndex) { + return columnIndex == 1 + ? currentTsBlock.getTimeByIndex(currentTsBlockIndex) + // -2 because the columnIndex starts from 1 and the first column is fixed as the time column + : currentTsBlock.getValueColumns()[columnIndex - 2].getLong(currentTsBlockIndex); + } + + @Override + public double getDouble(int columnIndex) { + // -2 because the columnIndex starts from 1 and the first column is fixed as the time column + return currentTsBlock.getValueColumns()[columnIndex - 2].getDouble(currentTsBlockIndex); + } + + @Override + public float getFloat(int columnIndex) { + // -2 because the columnIndex starts from 1 and the first column is fixed as the time column + return currentTsBlock.getValueColumns()[columnIndex - 2].getFloat(currentTsBlockIndex); + } + + @Override + public boolean getBoolean(int columnIndex) { + // -2 because the columnIndex starts from 1 and the first column is fixed as the time column + return currentTsBlock.getValueColumns()[columnIndex - 2].getBoolean(currentTsBlockIndex); + } + + @Override + public LocalDate getDate(int columnIndex) { + // -2 because the columnIndex starts from 1 and the first column is fixed as the time column + return DateUtils.parseIntToLocalDate( + currentTsBlock.getValueColumns()[columnIndex - 2].getInt(currentTsBlockIndex)); + } + + @Override + public byte[] getBinary(int columnIndex) { + // -2 because the columnIndex starts from 1 and the first column is fixed as the time column + return currentTsBlock + .getValueColumns()[columnIndex - 2] + .getBinary(currentTsBlockIndex) + .getValues(); + } + + @Override + public String getString(int columnIndex) { + // -2 because the columnIndex starts from 1 and the first column is fixed as the time column + return currentTsBlock + .getValueColumns()[columnIndex - 2] + .getBinary(currentTsBlockIndex) + .toString(); + } + + @Override + public boolean isNull(int columnIndex) { + // -2 because the columnIndex starts from 1 and the first column is fixed as the time column + return currentTsBlock.getValueColumns()[columnIndex - 2].isNull(currentTsBlockIndex); } @Override From 3b0ba9d11d41989b6d99fd3c5145b8f5e29642fd Mon Sep 17 00:00:00 2001 From: Jiang Tian Date: Thu, 24 Apr 2025 17:26:48 +0800 Subject: [PATCH 2/3] Update java/tsfile/src/main/java/org/apache/tsfile/read/common/block/column/BinaryColumn.java Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../apache/tsfile/read/common/block/column/BinaryColumn.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/tsfile/src/main/java/org/apache/tsfile/read/common/block/column/BinaryColumn.java b/java/tsfile/src/main/java/org/apache/tsfile/read/common/block/column/BinaryColumn.java index e3c308c91..d891829b5 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/read/common/block/column/BinaryColumn.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/read/common/block/column/BinaryColumn.java @@ -146,7 +146,7 @@ public boolean mayHaveNull() { @Override public boolean isNull(int position) { - return values[position] == null || valueIsNull != null && valueIsNull[position + arrayOffset]; + return values[position + arrayOffset] == null || valueIsNull != null && valueIsNull[position + arrayOffset]; } @Override From 5e9e7368eabea6d07c1a92979bd06afa7c41c8be Mon Sep 17 00:00:00 2001 From: Tian Jiang Date: Fri, 25 Apr 2025 11:06:15 +0800 Subject: [PATCH 3/3] spotless --- .../apache/tsfile/read/common/block/column/BinaryColumn.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/java/tsfile/src/main/java/org/apache/tsfile/read/common/block/column/BinaryColumn.java b/java/tsfile/src/main/java/org/apache/tsfile/read/common/block/column/BinaryColumn.java index d891829b5..ecc1b66ed 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/read/common/block/column/BinaryColumn.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/read/common/block/column/BinaryColumn.java @@ -146,7 +146,8 @@ public boolean mayHaveNull() { @Override public boolean isNull(int position) { - return values[position + arrayOffset] == null || valueIsNull != null && valueIsNull[position + arrayOffset]; + return values[position + arrayOffset] == null + || valueIsNull != null && valueIsNull[position + arrayOffset]; } @Override