Skip to content

Fix tag index misalignment in read_tsfile when projection pruning removes tag columns#18191

Merged
JackieTien97 merged 1 commit into
masterfrom
fixBug-0713
Jul 13, 2026
Merged

Fix tag index misalignment in read_tsfile when projection pruning removes tag columns#18191
JackieTien97 merged 1 commit into
masterfrom
fixBug-0713

Conversation

@shuwenwei

Copy link
Copy Markdown
Member

Problem

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 subsequent tag columns to be assigned wrong indices.

For example, with tags [tag1, tag2] and a query like:

SELECT time, value FROM read_tsfile(...) WHERE tag2 = 'x'

The optimizer:

  1. Prunes tag1 and tag2 from the scan node's assignments (they are not in SELECT)
  2. Rebuilds a TsTable containing only tag2
  3. Recomputes tag index: tag2 → 0 (should be 1)
  4. ExternalTsFileDeviceFilterVisitor reads deviceID.segment(0 + 1) = tag1 instead of deviceID.segment(1 + 1) = tag2

Result: WHERE tag2 = 'x' actually evaluates WHERE tag1 = 'x'.

Fix

Use ExternalTsFileQueryResource.getTableColumnSchema() instead of the pruned scan node getAssignments() when constructing the TsTable for device filter. The query resource always holds the complete, unpruned column schema with correct tag ordering from the initial planning phase.

Changes

  • PushPredicateIntoTableScan.java: Change constructExternalTsFileDeviceFilter to build TsTable from the complete table column schema
  • IoTDBReadTsFileTableFunctionIT.java: Add testReadTsFileWithTagFilterAndProjectionPruning to verify correct tag filtering when projection prunes tag columns

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

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.

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.constructExternalTsFileDeviceFilter to build the TsTable from ExternalTsFileQueryResource.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.

@sonarqubecloud

Copy link
Copy Markdown

@JackieTien97 JackieTien97 merged commit ce74949 into master Jul 13, 2026
44 checks passed
@JackieTien97 JackieTien97 deleted the fixBug-0713 branch July 13, 2026 07:15
@codecov

codecov Bot commented Jul 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 42.41%. Comparing base (350c6c7) to head (9b31558).
⚠️ Report is 7 commits behind head on master.

Files with missing lines Patch % Lines
...nner/optimizations/PushPredicateIntoTableScan.java 0.00% 1 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

3 participants