Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,6 @@ private StorageEngineMessages() {}

// ======================== Read ========================

public static final String MEM_CHUNK_READER_NOT_SUPPORT_METHOD = "mem chunk reader does not support this method";
public static final String MEM_ALIGNED_PAGE_READER_TSBLOCK = "[memAlignedPageReader] TsBlock:{}";
public static final String AFTER_FILTER_CHUNK_METADATA_LIST = "After removed by filter Chunk meta data list is: ";
public static final String AFTER_MODIFICATION_CHUNK_METADATA_LIST = "After modification Chunk meta data list is: ";
public static final String TIME_DATA_SIZE_NOT_MATCH = "Time data size not match";
Expand Down Expand Up @@ -1750,7 +1748,6 @@ private StorageEngineMessages() {}
// Additional auto-collected messages
// ---------------------------------------------------------------------------
public static final String MESSAGE_NO_LOAD_TSFILE_UUID_ARG_RECORDED_EXECUTE_LOAD_COMMAND_ARG_66722D80 = "No load TsFile uuid %s recorded for execute load command %s.";
public static final String EXCEPTION_NON_MINUS_ALIGNED_CHUNK_ONLY_HAS_ONE_MEASUREMENT_COMMA_BUT_MEASUREMENTINDEX_IS_E1A87F80 = "Non-aligned chunk only has one measurement, but measurementIndex is ";
public static final String MESSAGE_LOAD_QUERY_FALLBACK_FAILED_FOR_DEVICE_ARG_MEASUREMENTS_ARG_IN_TSFILE_ARG_50771955 = "Load: Query fallback failed for device {} measurements {} in TsFile {}. ";
public static final String MESSAGE_LOAD_FAILED_TO_INITIALIZE_QUERY_FALLBACK_FOR_TSFILE_ARG_AFTER_SCAN_PARSER_FAILURE_143B6037 = "Load: Failed to initialize query fallback for TsFile {} after scan parser failure.";
public static final String MESSAGE_LOAD_SCAN_PARSER_DETECTED_A_CORRUPTED_SECTION_IN_TSFILE_ARG_AT_DEVICE_ARG_E2632421 = "Load: Scan parser detected a corrupted section in TsFile {} at device {}. ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,8 +569,6 @@ private StorageEngineMessages() {}

// ======================== Read ========================

public static final String MEM_CHUNK_READER_NOT_SUPPORT_METHOD = "内存 Chunk 读取器不支持此方法";
public static final String MEM_ALIGNED_PAGE_READER_TSBLOCK = "[memAlignedPageReader] TsBlock:{}";
public static final String AFTER_FILTER_CHUNK_METADATA_LIST = "按过滤器移除后的 Chunk 元数据列表: ";
public static final String AFTER_MODIFICATION_CHUNK_METADATA_LIST = "修改后的 Chunk 元数据列表: ";
public static final String TIME_DATA_SIZE_NOT_MATCH = "时间数据大小不匹配";
Expand Down Expand Up @@ -1660,7 +1658,6 @@ private StorageEngineMessages() {}
// Additional auto-collected messages
// ---------------------------------------------------------------------------
public static final String MESSAGE_NO_LOAD_TSFILE_UUID_ARG_RECORDED_EXECUTE_LOAD_COMMAND_ARG_66722D80 = "未记录 Load TsFile uuid %s,执行 Load 命令为 %s。";
public static final String EXCEPTION_NON_MINUS_ALIGNED_CHUNK_ONLY_HAS_ONE_MEASUREMENT_COMMA_BUT_MEASUREMENTINDEX_IS_E1A87F80 = "非对齐 chunk 只有一个 measurement,但 measurementIndex 为 ";
public static final String MESSAGE_LOAD_QUERY_FALLBACK_FAILED_FOR_DEVICE_ARG_MEASUREMENTS_ARG_IN_TSFILE_ARG_50771955 = "Load:设备 {} 测点 {} 在 TsFile {} 中的查询回退失败。";
public static final String MESSAGE_LOAD_FAILED_TO_INITIALIZE_QUERY_FALLBACK_FOR_TSFILE_ARG_AFTER_SCAN_PARSER_FAILURE_143B6037 = "Load:scan parser 失败后,TsFile {} 的查询回退初始化失败。";
public static final String MESSAGE_LOAD_SCAN_PARSER_DETECTED_A_CORRUPTED_SECTION_IN_TSFILE_ARG_AT_DEVICE_ARG_E2632421 = "Load:scan parser 在 TsFile {} 的设备 {} 处检测到损坏的数据段。";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,9 +445,6 @@ public class IoTDBConfig implements DefaultEncodingProvider {
*/
private int tvListSortThreshold = 0;

/** Enable streaming query mem chunk */
private boolean streamingQueryMemChunk = true;

/** Enable inner space compaction for sequence files */
private volatile boolean enableSeqSpaceCompaction = true;

Expand Down Expand Up @@ -2536,14 +2533,6 @@ public void setTVListSortThreshold(int tvListSortThreshold) {
this.tvListSortThreshold = tvListSortThreshold;
}

public boolean isStreamingQueryMemChunk() {
return streamingQueryMemChunk;
}

public void setStreamingQueryMemChunk(boolean streamingQueryMemChunk) {
this.streamingQueryMemChunk = streamingQueryMemChunk;
}

public boolean isRpcThriftCompressionEnable() {
return rpcThriftCompressionEnable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ public void processStatistics(Statistics timeStatistics, Statistics[] valueStati
}
}

public boolean usesTimeStatistics() {
for (InputLocation[] inputLocations : inputLocationList) {
if (inputLocations[0].getValueColumnIndex() == -1) {
return true;
}
}
return false;
}

public TSDataType[] getOutputType() {
if (step.isOutputPartial()) {
return accumulator.getIntermediateType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public abstract class AbstractSeriesAggregationScanOperator extends AbstractData
/** Some special data types(like BLOB) cannot use statistics. */
protected final boolean canUseStatistics;

private final boolean hasTimeStatisticsAggregator;

@SuppressWarnings("squid:S107")
protected AbstractSeriesAggregationScanOperator(
PlanNodeId sourceId,
Expand Down Expand Up @@ -99,6 +101,14 @@ protected AbstractSeriesAggregationScanOperator(
this.maxReturnSize = maxReturnSize;
this.outputEndTime = outputEndTime;
this.canUseStatistics = canUseStatistics;
boolean usesTimeStatistics = false;
for (TreeAggregator aggregator : aggregators) {
if (aggregator.usesTimeStatistics()) {
usesTimeStatistics = true;
break;
}
}
this.hasTimeStatisticsAggregator = usesTimeStatistics;
}

@Override
Expand Down Expand Up @@ -282,7 +292,10 @@ protected boolean readAndCalcFromFile() throws IOException {
if (!b.get()) {
break;
}
if (canUseStatistics && seriesScanUtil.canUseCurrentFileStatistics()) {
if (canUseStatistics
&& (hasTimeStatisticsAggregator
? seriesScanUtil.canUseCurrentFileTimeStatistics()
: seriesScanUtil.canUseCurrentFileStatistics())) {
Statistics fileTimeStatistics = seriesScanUtil.currentFileTimeStatistics();
if (fileTimeStatistics.getStartTime() > curTimeRange.getMax()) {
if (ascending) {
Expand Down Expand Up @@ -330,7 +343,10 @@ protected boolean readAndCalcFromChunk() throws IOException {
if (!b.get()) {
break;
}
if (canUseStatistics && seriesScanUtil.canUseCurrentChunkStatistics()) {
if (canUseStatistics
&& (hasTimeStatisticsAggregator
? seriesScanUtil.canUseCurrentChunkTimeStatistics()
: seriesScanUtil.canUseCurrentChunkStatistics())) {
Statistics chunkTimeStatistics = seriesScanUtil.currentChunkTimeStatistics();
if (chunkTimeStatistics.getStartTime() > curTimeRange.getMax()) {
if (ascending) {
Expand Down Expand Up @@ -372,7 +388,10 @@ protected boolean readAndCalcFromPage() throws IOException {
long start = System.nanoTime();
try {
while (System.nanoTime() - start < leftRuntimeOfOneNextCall && seriesScanUtil.hasNextPage()) {
if (canUseStatistics && seriesScanUtil.canUseCurrentPageStatistics()) {
if (canUseStatistics
&& (hasTimeStatisticsAggregator
? seriesScanUtil.canUseCurrentPageTimeStatistics()
: seriesScanUtil.canUseCurrentPageStatistics())) {
Statistics pageTimeStatistics = seriesScanUtil.currentPageTimeStatistics();
// There is no more eligible points in current time range
if (pageTimeStatistics.getStartTime() > curTimeRange.getMax()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,13 @@ public AlignedSeriesScanOperator(
AlignedFullPath seriesPath,
Ordering scanOrder,
SeriesScanOptions seriesScanOptions,
boolean queryAllSensors,
List<TSDataType> dataTypes,
int maxTsBlockLineNum) {
this.sourceId = sourceId;
this.operatorContext = context;
this.seriesScanUtil =
new AlignedSeriesScanUtil(
seriesPath,
scanOrder,
seriesScanOptions,
context.getInstanceContext(),
queryAllSensors,
dataTypes);
seriesPath, scanOrder, seriesScanOptions, context.getInstanceContext(), dataTypes);
this.valueColumnCount = seriesPath.getColumnNum();
this.maxReturnSize =
Math.min(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,22 @@
import org.apache.tsfile.enums.TSDataType;
import org.apache.tsfile.file.metadata.AbstractAlignedTimeSeriesMetadata;
import org.apache.tsfile.file.metadata.IMetadata;
import org.apache.tsfile.file.metadata.statistics.Statistics;
import org.apache.tsfile.read.common.block.TsBlock;
import org.apache.tsfile.read.reader.IPointReader;
import org.apache.tsfile.write.schema.IMeasurementSchema;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class AlignedSeriesScanUtil extends SeriesScanUtil {

private final List<TSDataType> dataTypes;

// only used for limit and offset push down optimizer, if we select all columns from aligned
// device, we can use statistics to skip.
// it's only exact while using limit & offset push down
// for table scan, it should always be true
private final boolean queryAllSensors;

// for table model, it will be false
// for tree model, it will be true
private final boolean ignoreAllNullRows;
Expand All @@ -60,15 +58,14 @@ public AlignedSeriesScanUtil(
Ordering scanOrder,
SeriesScanOptions scanOptions,
FragmentInstanceContext context) {
this(seriesPath, scanOrder, scanOptions, context, false, null);
this(seriesPath, scanOrder, scanOptions, context, null);
}

public AlignedSeriesScanUtil(
AlignedFullPath seriesPath,
Ordering scanOrder,
SeriesScanOptions scanOptions,
FragmentInstanceContext context,
boolean queryAllSensors,
List<TSDataType> givenDataTypes) {
super(seriesPath, scanOrder, scanOptions, context);
isAligned = true;
Expand All @@ -78,7 +75,6 @@ public AlignedSeriesScanUtil(
: seriesPath.getSchemaList().stream()
.map(IMeasurementSchema::getType)
.collect(Collectors.toList());
this.queryAllSensors = queryAllSensors;
this.ignoreAllNullRows = context.isIgnoreAllNullRows();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

queryAllSensors is no longer read anywhere, but it is still a constructor parameter and AlignedSeriesScanOperator and ExternalTsFileSeriesScanUtil still pass it. Suggest removing it so that nobody assumes the old shortcut can still be switched on.

}

Expand Down Expand Up @@ -116,10 +112,153 @@ protected IPointReader getPointReader(TsBlock tsBlock) {
}

@Override
protected boolean timeAllSelected(IMetadata metadata) {
if (queryAllSensors || metadata.getMeasurementCount() == 0) {
public boolean canUseCurrentFileTimeStatistics() {
return canUseCurrentFileStatistics() && canUseAlignedTimeStatistics(firstTimeSeriesMetadata);
}

@Override
public boolean canUseCurrentChunkTimeStatistics() {
return canUseCurrentChunkStatistics() && canUseAlignedTimeStatistics(firstChunkMetadata);
}

@Override
public boolean canUseCurrentPageTimeStatistics() throws IOException {
return canUseCurrentPageStatistics()
&& firstPageReader != null
&& !(firstPageReader instanceof LazyMemVersionPageReader)
&& canUseAlignedTimeStatistics(firstPageReader.getPageReader());
}

@Override
@SuppressWarnings("squid:S3740")
public Statistics currentFileTimeStatistics() {
return getAlignedTimeStatistics(firstTimeSeriesMetadata, super.currentFileTimeStatistics());
}

@Override
@SuppressWarnings("squid:S3740")
public Statistics currentChunkTimeStatistics() {
return getAlignedTimeStatistics(firstChunkMetadata, super.currentChunkTimeStatistics());
}

@Override
@SuppressWarnings("squid:S3740")
public Statistics currentPageTimeStatistics() {
if (firstPageReader == null || firstPageReader instanceof LazyMemVersionPageReader) {
return super.currentPageTimeStatistics();
}
return getAlignedTimeStatistics(
firstPageReader.getPageReader(), super.currentPageTimeStatistics());
}

@Override
protected boolean canUseTimeStatistics(IMetadata metadata) {
return canUseAlignedTimeStatistics(metadata);
}

private boolean canUseAlignedTimeStatistics(IMetadata metadata) {
if (!ignoreAllNullRows || metadata == null) {
return true;
}
return timeAllSelected(metadata) || hasSingleNonNullMeasurement(metadata);
}

private boolean hasSingleNonNullMeasurement(IMetadata metadata) {
int nonNullCount = 0;
for (int i = 0, count = metadata.getMeasurementCount(); i < count; i++) {
Statistics<?> valueStats = metadata.getMeasurementStatistics(i).orElse(null);
if (isNonEmpty(valueStats) && ++nonNullCount > 1) {
return false;
}
}
return nonNullCount == 1;
}

@SuppressWarnings("squid:S3740")
private Statistics getAlignedTimeStatistics(IMetadata metadata, Statistics timeStatistics) {
if (!ignoreAllNullRows || metadata == null || timeStatistics == null) {
return timeStatistics;
}
int measurementCount = metadata.getMeasurementCount();
if (measurementCount <= 0) {
return timeStatistics;
}
Statistics singleNonNullMeasurementStatistics = null;
int nonNullCount = 0;
for (int i = 0; i < measurementCount; i++) {
Statistics valueStats = metadata.getMeasurementStatistics(i).orElse(null);
if (isNonEmpty(valueStats)) {
if (++nonNullCount > 1) {
return timeStatistics;
}
singleNonNullMeasurementStatistics = valueStats;
}
}
return nonNullCount == 1 ? singleNonNullMeasurementStatistics : timeStatistics;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Reject nonNullCount == 0 instead of falling back to timeStatistics when measurementCount > 0

hasAtMostOneNonNullMeasurement(IMetadata) (lines 159–168) returns true when nonNullCount == 0 (every queried column has null or empty statistics while ignoreAllNullRows == true and measurementCount > 0), so canUseAlignedTimeStatistics(metadata) reports that time statistics are usable. However, getAlignedTimeStatistics(...) here evaluates return nonNullCount == 1 ? singleNonNullMeasurementStatistics : timeStatistics;, returning the unprojected timeStatistics (non-zero count) instead of 0.

For example, on a MemAlignedPageReader (which now exposes getMeasurementCount()) or aligned metadata whose queried subset of columns is all-null over those timestamps, COUNT_TIME would over-count rows from timeStatistics. Consider tightening hasAtMostOneNonNullMeasurement to require nonNullCount == 1 when measurementCount > 0 (or returning an empty Statistics when nonNullCount == 0).

}

private boolean timeAllSelected(IMetadata metadata) {
if (!ignoreAllNullRows || metadata.getMeasurementCount() == 0) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Preserve OFFSET skipping for a single sparse column

After dropping s1, SELECT * expands to s0, but an old file can still contain timestamps where only s1 had a value. The non-null count of s0 is the exact number of visible rows in that file. With s0 present at every second timestamp, LIMIT 1 OFFSET 1024 returned timestamp 2049 on both revisions. The base decoded 1 page and this head decoded 65. Please use the selected column's count for pagination when its statistics are usable.

return true;
}
return metadata.timeAllSelected();
return metadata.timeAllSelected() || allTimestampsCoveredByDisjointMeasurements(metadata);
}

private boolean allTimestampsCoveredByDisjointMeasurements(IMetadata metadata) {
Statistics<?> timeStats = metadata.getTimeStatistics();
int measurementCount = metadata.getMeasurementCount();
if (timeStats == null || timeStats.isEmpty() || measurementCount < 2) {
return false;
}
long expectedCount = timeStats.getCount();
if (measurementCount == 2) {
Statistics<?> first = metadata.getMeasurementStatistics(0).orElse(null);
Statistics<?> second = metadata.getMeasurementStatistics(1).orElse(null);
return isNonEmpty(first)
&& isNonEmpty(second)
&& first.getCount() == expectedCount - second.getCount()
&& areDisjoint(first, second);
}
List<Statistics<?>> nonEmptyStats = collectCoveredMeasurementStats(metadata, expectedCount);
return !nonEmptyStats.isEmpty() && areAllDisjoint(nonEmptyStats);
}

private static List<Statistics<?>> collectCoveredMeasurementStats(
IMetadata metadata, long expectedCount) {
int measurementCount = metadata.getMeasurementCount();
List<Statistics<?>> nonEmptyStats = new ArrayList<>(measurementCount);
long countSum = 0;
for (int i = 0; i < measurementCount; i++) {
Statistics<?> valueStats = metadata.getMeasurementStatistics(i).orElse(null);
if (!isNonEmpty(valueStats)) {
continue;
}
if (valueStats.getCount() > expectedCount - countSum) {
return Collections.emptyList();
}
countSum += valueStats.getCount();
nonEmptyStats.add(valueStats);
}
return countSum == expectedCount && nonEmptyStats.size() >= 2
? nonEmptyStats
: Collections.emptyList();
}

private static boolean areAllDisjoint(List<Statistics<?>> statsList) {
statsList.sort(Comparator.comparingLong(Statistics::getStartTime));
for (int i = 1; i < statsList.size(); i++) {
if (statsList.get(i - 1).getEndTime() >= statsList.get(i).getStartTime()) {
return false;
}
}
return true;
}

private static boolean areDisjoint(Statistics<?> first, Statistics<?> second) {
return first.getEndTime() < second.getStartTime() || second.getEndTime() < first.getStartTime();
}

private static boolean isNonEmpty(Statistics<?> statistics) {
return statistics != null && !statistics.isEmpty();
}
}
Loading
Loading