Skip to content
Open
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 @@ -345,6 +345,34 @@ public void deleteTest() {
retArray);
}

@Test
public void countTimeAfterDeletingLastValueAtTimestamp() {
String database = "root.count_time_delete";
String device = database + ".d1";
prepareData(
new String[] {
"CREATE DATABASE " + database,
"CREATE ALIGNED TIMESERIES " + device + "(x INT64, y INT64, z INT64)",
"INSERT INTO "
+ device
+ "(time, x, y, z) ALIGNED VALUES "
+ "(10, 100, 1, NULL), (20, NULL, 1, NULL), (30, NULL, 1, 300)",
"FLUSH " + database,
"DELETE FROM " + device + ".y WHERE time >= 10"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Could we add a regression that reaches the file-level statistics check? This deletion marks the file metadata as modified, so the scan already descends to chunk statistics. I disabled only the new file-level coverage check locally, and all three assertions below still passed.

One case that distinguishes the two versions is to load an aligned TsFile with 9 physical timestamps, 4 of which have a value in at least one measurement. With no deletions, SELECT * returns 4 rows. On the PR, COUNT_TIME(*) also returns 4. Disabling just the file-level check makes it return 9. An assertion on that count would catch a regression in this branch.

});

String count = "SELECT COUNT_TIME(*) FROM " + device;
resultSetEqualTest(count, new String[] {"count_time(*)"}, new String[] {"2,"});
resultSetEqualTest(
count + " GROUP BY ([10, 40), 30ms)",
new String[] {"Time,count_time(*)"},
new String[] {"10,2,"});
resultSetEqualTest(
count + " GROUP BY ([10, 40), 10ms)",
new String[] {"Time,count_time(*)"},
new String[] {"10,1,", "20,0,", "30,1,"});
}

@Test
public void nullRowTest() {
// align by time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ public void outputResult(ColumnBuilder[] columnBuilder) {
}
}

public boolean usesTimeStatistics() {
return inputLocationList.stream()
.anyMatch(locations -> locations[0].getValueColumnIndex() == -1);
}

/** Used for SeriesAggregateScanOperator. */
public void processStatistics(Statistics timeStatistics, Statistics[] valueStatistics) {
for (InputLocation[] inputLocations : inputLocationList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.IntFunction;

import static org.apache.iotdb.db.queryengine.execution.operator.AggregationUtil.appendAggregationResult;
import static org.apache.iotdb.db.queryengine.execution.operator.AggregationUtil.calculateAggregationFromRawData;
Expand Down Expand Up @@ -72,6 +73,8 @@ public abstract class AbstractSeriesAggregationScanOperator extends AbstractData
/** Some special data types(like BLOB) cannot use statistics. */
protected final boolean canUseStatistics;

private final boolean usesTimeStatistics;

@SuppressWarnings("squid:S107")
protected AbstractSeriesAggregationScanOperator(
PlanNodeId sourceId,
Expand Down Expand Up @@ -99,6 +102,7 @@ protected AbstractSeriesAggregationScanOperator(
this.maxReturnSize = maxReturnSize;
this.outputEndTime = outputEndTime;
this.canUseStatistics = canUseStatistics;
this.usesTimeStatistics = aggregators.stream().anyMatch(TreeAggregator::usesTimeStatistics);
}

@Override
Expand Down Expand Up @@ -256,6 +260,27 @@ private boolean calcFromRawData(TsBlock tsBlock) {
}
}

private boolean tryCalcFromStatistics(
Statistics timeStatistics, IntFunction<Statistics> getValueStatistics) {
if (!curTimeRange.contains(timeStatistics.getStartTime(), timeStatistics.getEndTime())) {
return false;
}

Statistics[] valueStatistics = new Statistics[subSensorSize];
for (int i = 0; i < subSensorSize; i++) {
valueStatistics[i] = getValueStatistics.apply(i);
}
// An aligned time column can retain a timestamp after its last non-null value is deleted.
// A measurement covering every timestamp proves that COUNT_TIME can use the physical count.
if (usesTimeStatistics
&& Arrays.stream(valueStatistics)
.noneMatch(s -> s != null && s.getCount() == timeStatistics.getCount())) {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

One possible follow-up for COUNT_TIME(*): when statistics are usable and exactly one selected measurement has a positive count, that measurement's count is the exact number of visible timestamps, even if it is sparse.

I checked this with 4,096 physical timestamps. After deleting the dense measurement, only x had values, at the 2,048 even timestamps. Both COUNT_TIME(*) and COUNT(x) returned 2,048. EXPLAIN ANALYZE VERBOSE showed that the former decoded 32 aligned pages and the latter decoded zero. Using the surviving measurement's count would avoid decoding those pages.

return false;
}
calcFromStatistics(timeStatistics, valueStatistics);
return true;
}

protected void calcFromStatistics(Statistics timeStatistics, Statistics[] valueStatistics) {
long startTime = System.nanoTime();
try {
Expand Down Expand Up @@ -293,13 +318,7 @@ protected boolean readAndCalcFromFile() throws IOException {
}
}
// calc from fileMetaData
if (curTimeRange.contains(
fileTimeStatistics.getStartTime(), fileTimeStatistics.getEndTime())) {
Statistics[] statisticsList = new Statistics[subSensorSize];
for (int i = 0; i < subSensorSize; i++) {
statisticsList[i] = seriesScanUtil.currentFileStatistics(i);
}
calcFromStatistics(fileTimeStatistics, statisticsList);
if (tryCalcFromStatistics(fileTimeStatistics, seriesScanUtil::currentFileStatistics)) {
seriesScanUtil.skipCurrentFile();
if (isAllAggregatorsHasFinalResult(aggregators) && !isGroupByQuery) {
return true;
Expand Down Expand Up @@ -341,14 +360,7 @@ protected boolean readAndCalcFromChunk() throws IOException {
}
}
// calc from chunkMetaData
if (curTimeRange.contains(
chunkTimeStatistics.getStartTime(), chunkTimeStatistics.getEndTime())) {
// calc from chunkMetaData
Statistics[] statisticsList = new Statistics[subSensorSize];
for (int i = 0; i < subSensorSize; i++) {
statisticsList[i] = seriesScanUtil.currentChunkStatistics(i);
}
calcFromStatistics(chunkTimeStatistics, statisticsList);
if (tryCalcFromStatistics(chunkTimeStatistics, seriesScanUtil::currentChunkStatistics)) {
seriesScanUtil.skipCurrentChunk();
if (isAllAggregatorsHasFinalResult(aggregators) && !isGroupByQuery) {
return true;
Expand Down Expand Up @@ -384,13 +396,7 @@ protected boolean readAndCalcFromPage() throws IOException {
}
}
// can use pageHeader
if (curTimeRange.contains(
pageTimeStatistics.getStartTime(), pageTimeStatistics.getEndTime())) {
Statistics[] statisticsList = new Statistics[subSensorSize];
for (int i = 0; i < subSensorSize; i++) {
statisticsList[i] = seriesScanUtil.currentPageStatistics(i);
}
calcFromStatistics(pageTimeStatistics, statisticsList);
if (tryCalcFromStatistics(pageTimeStatistics, seriesScanUtil::currentPageStatistics)) {
seriesScanUtil.skipCurrentPage();
if (isAllAggregatorsHasFinalResult(aggregators) && !isGroupByQuery) {
return true;
Expand Down
Loading