Skip to content

Fix tree COUNT_TIME overcount on sparse aligned timestamps - #19446

Open
ColinLeeo wants to merge 1 commit into
apache:masterfrom
ColinLeeo:fix/tree-count-time-aligned-statistics
Open

ColinLeeo wants to merge 1 commit into
apache:masterfrom
ColinLeeo:fix/tree-count-time-aligned-statistics

Conversation

@ColinLeeo

@ColinLeeo ColinLeeo commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Reproducer

An aligned device has three timestamps. After a flush, deleting the only non-null value at time 20 leaves two visible timestamps.

CREATE DATABASE root.count_demo;
CREATE ALIGNED TIMESERIES root.count_demo.d1(x INT64, y INT64);
INSERT INTO root.count_demo.d1(time, x, y) ALIGNED VALUES
  (10, 100, 1), (20, NULL, 1), (30, 300, 1);
FLUSH root.count_demo;
DELETE FROM root.count_demo.d1.y WHERE time >= 10;
Tree query on root.count_demo.d1 Before After
SELECT * timestamps 10 and 30 timestamps 10 and 30
SELECT COUNT_TIME(*) 3 2
SELECT COUNT_TIME(*) … GROUP BY ([10, 40), 30ms) 3 2
SELECT COUNT_TIME(*) … GROUP BY ([10, 40), 10ms) 1, 0, 1 1, 0, 1

Fix

The aligned time column can retain physical timestamps after their values have been deleted. A tree aggregation that consumes time-column statistics now uses a file, chunk, or page count only when a selected measurement has a value at every timestamp. If the statistics cannot establish that, the scan descends to finer-grained statistics or reads the rows. The regression test leaves the two surviving values in different columns so it also checks that both timestamps contribute to the result.

Verification

  • Compared 14 tree SQL queries on separate single-DataNode instances running master before and after the fix, including ungrouped, grouped and align-by-device counts, sparse and dense measurements, and deletion of the final value.
  • SimpleIT ran all 8 tests in IoTDBCountTimeAlignedDeviceIT, including the new regression, with no failures, errors, or skips.
  • ClusterIT (1 ConfigNode and 3 DataNodes) ran all 14 tests in IoTDBCountTimeAlignedDeviceIT and IoTDBCountTimeIT. All passed, with no skips.
  • All 9 FragmentInstanceContextTest unit tests passed. Spotless and integration test selection checks passed.

@ColinLeeo ColinLeeo left a comment

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.

The fix looks correct to me. I compared 26 result queries on the base and PR versions, including partial deletions, out-of-order writes, and ascending and descending group-by windows. Both comments below are non-blocking.

+ "(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.

// 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant