Fix tag index misalignment in read_tsfile when projection pruning removes tag columns#18191
Conversation
…oves tag columns When projection pruning removes tag columns from the scan node's assignments, constructExternalTsFileDeviceFilter builds a TsTable from the pruned assignments and recomputes tag indices via getTagColumnIndex. This causes tag2 to be assigned index 0 instead of 1, and ExternalTsFileDeviceFilterVisitor reads the wrong deviceID segment (tag1 instead of tag2), effectively evaluating the wrong tag. Fix: Use ExternalTsFileQueryResource.getTableColumnSchema() which always holds the complete, unpruned column schema with correct tag ordering, instead of the pruned scan node assignments.
There was a problem hiding this comment.
Pull request overview
Fixes incorrect tag filtering for read_tsfile(...) when optimizer projection pruning removes tag columns from an ExternalTsFileScanNode’s assignments, which previously caused tag index recomputation against a pruned schema and led to mis-addressing deviceID.segment(tagIndex + 1).
Changes:
- Update
PushPredicateIntoTableScan.constructExternalTsFileDeviceFilterto build theTsTablefromExternalTsFileQueryResource.getTableColumnSchema()(complete, unpruned schema) instead of the scan node’s pruned assignments. - Add an integration test that reproduces the tag-index misalignment under projection pruning and verifies correct tag filtering behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/optimizations/PushPredicateIntoTableScan.java | Builds device-filter TsTable from the full external TsFile table schema to preserve correct tag ordering/indices after column pruning. |
| integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBReadTsFileTableFunctionIT.java | Adds an IT case ensuring tag predicates still match the correct tag values when tag columns are pruned from projection. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #18191 +/- ##
============================================
+ Coverage 41.93% 42.41% +0.47%
Complexity 318 318
============================================
Files 5312 5321 +9
Lines 375370 376605 +1235
Branches 48474 48716 +242
============================================
+ Hits 157423 159720 +2297
+ Misses 217947 216885 -1062 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|



Problem
When projection pruning removes tag columns from the scan node's assignments,
constructExternalTsFileDeviceFilterbuilds aTsTablefrom the pruned assignments and recomputes tag indices viagetTagColumnIndex. This causes subsequent tag columns to be assigned wrong indices.For example, with tags
[tag1, tag2]and a query like:The optimizer:
tag1andtag2from the scan node's assignments (they are not in SELECT)TsTablecontaining onlytag2tag2→ 0 (should be 1)ExternalTsFileDeviceFilterVisitorreadsdeviceID.segment(0 + 1)=tag1instead ofdeviceID.segment(1 + 1)=tag2Result:
WHERE tag2 = 'x'actually evaluatesWHERE tag1 = 'x'.Fix
Use
ExternalTsFileQueryResource.getTableColumnSchema()instead of the pruned scan nodegetAssignments()when constructing theTsTablefor device filter. The query resource always holds the complete, unpruned column schema with correct tag ordering from the initial planning phase.Changes
PushPredicateIntoTableScan.java: ChangeconstructExternalTsFileDeviceFilterto buildTsTablefrom the complete table column schemaIoTDBReadTsFileTableFunctionIT.java: AddtestReadTsFileWithTagFilterAndProjectionPruningto verify correct tag filtering when projection prunes tag columns