Skip to content

fix(cpp): keep NULL column for series with no data in query range - #1146

Merged
ColinLeeo merged 8 commits into
apache:developfrom
ColinLeeo:fix-empty-measure-misalign
Nov 30, 2026
Merged

ColinLeeo merged 8 commits into
apache:developfrom
ColinLeeo:fix-empty-measure-misalign

Conversation

@ColinLeeo

Copy link
Copy Markdown
Contributor

Problem

A multi-path query in the C++ (and Python) SDK drops a selected column when the measurement exists in the file but has no data inside the query time range. The following columns then shift into the dropped column's position, silently returning wrong data. Java keeps the column and returns NULL for it.

Root cause

QDSWithoutTimeGenerator::init_internal treated every E_NO_MORE_DATA returned by alloc_ssi as "measurement does not exist" and dropped the path. But alloc_ssi also returns E_NO_MORE_DATA when the measurement exists yet has no data in the range (its timeseries index statistic is pruned by the time filter).

Fix

  • alloc_ssi now reports a bloom-filter miss (measurement truly absent) as E_NOT_EXIST instead of E_NO_MORE_DATA, so callers can tell the two cases apart. It also exposes the series data type via a new optional out-param for the exists-but-empty-in-range case.
  • QDSWithoutTimeGenerator::init_internal keeps the column (with a NULL value and the correct data type) when the series exists but is empty in range, and only drops the column when the series does not exist.
  • QDSWithTimeGenerator::construct_node_tree keeps its leaf for the not-found codes as well, preserving the existing behavior for missing series.

Test

Added TsFileReaderTest.MultiPathKeepsNullColumnForSeriesEmptyInRange: a two-series query where v1 has data only outside the range and v2 has data inside it; asserts 3 columns (time, root.t.v1, root.t.v2) with v1 NULL and v2 values aligned.

A multi-path query dropped the column of a selected measurement that
exists in the file but has no data inside the query time range, shifting
the values of the following columns into the wrong positions. This aligns
C++ with the Java behavior: keep the column and return NULL for it.

alloc_ssi now reports a bloom-filter miss (measurement truly absent) as
E_NOT_EXIST instead of E_NO_MORE_DATA, and exposes the series data type
for the exists-but-empty-in-range case. QDSWithoutTimeGenerator keeps a
NULL column with the correct type for the latter and only drops the
column when the measurement does not exist.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The existing missing-series test never reaches the new predicate branch, leaving OR/AND behavior, safe result destruction, and metadata not-found paths without regression coverage.

1 open issue
What changed in this PR

This PR fixes column shifting in C++ range-query results, also used by the Python SDK, by retaining NULL columns for existing series with no data in the requested range.

Changes:

  • Distinguishes missing series from existing series pruned by the time filter and exposes their data types.
  • Preserves empty-column positions and handles missing-series predicate leaves.
  • Adds a regression test for column names, types, NULL values, and value alignment.
File Description
cpp/​test/​reader/​tsfile_reader_test.cc Adds coverage for retaining an empty-in-range column.
cpp/​src/​reader/​qds_without_timegenerator.cc Retains typed NULL columns and safely handles null iterator placeholders.
cpp/​src/​reader/​qds_with_timegenerator.cc Treats missing predicate series as empty leaves.
cpp/​src/​file/​tsfile_io_reader.h Adds an optional series-type output parameter.
cpp/​src/​file/​tsfile_io_reader.cc Separates bloom-filter misses from range pruning and reports pruned series types.

Review effort: Balanced

Comment on lines +434 to +435
} else if (ret == E_NO_MORE_DATA || ret == E_MEASUREMENT_NOT_EXIST ||
ret == E_DEVICE_NOT_EXIST || ret == E_NOT_EXIST) {
The construct_node_tree change keeps a leaf (empty stream) for a series
that does not exist in the file but appears in the query predicate. Add
regression tests for that path: OR preserves the live series' timestamps,
AND returns no rows, and the result set stays destroyable. Cover both a
missing measurement and a missing device.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The regression tests leave the single-path/null-iterator branch and all-empty dataset lifecycle uncovered, including metadata retention, rowless iteration, and safe destruction.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Low severity Add tests for single-path and all-empty query lifecycles

cpp/​test/​reader/​tsfile_reader_test.cc:2367

This regression covers an empty column only alongside a live iterator, leaving the new single-path/null-iterator branch and the all-empty dataset lifecycle untested. Please add single-path and all-selected-paths-empty queries that check retained column names and types, next() returning E_OK with no rows, and safe destruction.

Review effort: Balanced

…misalign

# Conflicts:
#	cpp/test/reader/tsfile_reader_test.cc

@ColinLeeo ColinLeeo left a comment

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.

I verified the column-position fix on dd0981df. The result kept the declared types when one or all selected series were empty in the requested range. It also kept the live value in the correct column when the projection included an empty series, a missing series, and a live series. Please document this distinction in the SDK skill under skills/tsfile/ so callers can rely on the result schema.

// parent AND/OR nodes still have a child; its stream reports
// INVALID_NEXT_TIMESTAMP for every lookup.
} else if (ret == E_NO_MORE_DATA || ret == E_MEASUREMENT_NOT_EXIST ||
ret == E_DEVICE_NOT_EXIST || ret == E_NOT_EXIST) {

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 errors while loading predicate metadata. do_load_timeseries_index() overwrites the return code with E_NOT_EXIST whenever found is false, including when TimeseriesIndex::deserialize_from() failed with E_OOM. This branch then turns that error into a successful empty predicate leaf. For a query that selects a and filters on a OR b, a failure to load the index for b can silently drop timestamps matched only by b.

Please return E_NOT_EXIST only after a complete scan of the index range finds no match, and propagate deserialization errors. A regression test should check that a metadata failure on a predicate-only series reaches the caller while a genuinely missing series still yields an empty leaf. Index scan and error handling

Comment thread cpp/src/file/tsfile_io_reader.h Outdated
TsFileSeriesScanIterator*& ssi, common::PageArena& pa,
Filter* time_filter = nullptr);
Filter* time_filter = nullptr,
common::TSDataType* out_series_type = nullptr);

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.

[P3] Keep the five-argument alloc_ssi symbol for existing C++ clients. I compiled a caller against the previous header. The same object linked with the previous library and failed to link with this PR's library because the five-argument symbol was missing. This affects clients that call TsFileIOReader directly through the installed header. Keeping an out-of-line five-argument overload that forwards nullptr to the new six-argument overload would preserve binary compatibility. The six-argument overload can require its final argument to keep overload resolution unambiguous.

do_load_timeseries_index() only maps a fully-scanned, no-match index range
to E_NOT_EXIST; deserialization errors such as E_OOM now propagate instead
of being swallowed as a missing series.

Keep the five-argument alloc_ssi() overload so clients compiled against the
previous header keep linking; the new six-argument overload carries the
optional series-type out-param.
Add regression tests for a single selected series that is empty in the
query range and for all-selected-series-empty queries, asserting retained
column names/types, no rows, and safe destruction.

Document the tree-model result-column semantics (empty-in-range keeps a
typed NULL column; a missing measurement drops the column) in the SDK
skill so callers can rely on the result schema.
Comment thread cpp/src/file/tsfile_io_reader.cc Outdated
}
} // end while
if (!found) {
if (!found && deserialize_buf_not_enough(ret)) {

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] Propagate partial reads inside a timeseries index entry. deserialize_buf_not_enough(ret) also matches E_PARTIAL_READ when the buffer ends halfway through the current entry. Returning E_NOT_EXIST in that case makes the query treat an unreadable predicate series as absent. For an a OR b query, this can silently omit timestamps matched only by b.

Please check bs.has_remaining() before starting each entry, as do_load_timeseries_index_range() does below. Return E_NOT_EXIST only after scanning complete entries without a match, and propagate errors from any entry whose deserialization has started. A regression test should cover both a complete range with no matching measurement and a range ending inside an entry.

do_load_timeseries_index() now only scans complete entries, checking
bs.has_remaining() before each one, and returns E_NOT_EXIST solely after a
full scan without a match. A deserialization error mid-entry
(E_PARTIAL_READ, E_OOM, ...) is propagated instead of being masked as a
missing series.
Start each merged row at the first path with its timestamp. Keep the reader on predicate streams so cleanup releases their scan iterators through the owning reader.

Cover the difference between a missing measurement and an incomplete index entry, and document tree-query projection semantics.
Track the statistic allocation when replacing it so short reads and index reuse release the correct storage. Cover the failure path and the all-null row on a shared aligned timestamp, and clarify the tree query contract.
@ColinLeeo
ColinLeeo merged commit 786a9d1 into apache:develop Nov 30, 2026
49 checks passed
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.

2 participants