Skip to content

Fix DatabaseMetaData.getIndexInfo() CachedRowSet invalid cursor position exception#2763

Merged
Ananya2 merged 9 commits intomainfrom
user/anagarg/issue#2758
Sep 23, 2025
Merged

Fix DatabaseMetaData.getIndexInfo() CachedRowSet invalid cursor position exception#2763
Ananya2 merged 9 commits intomainfrom
user/anagarg/issue#2758

Conversation

@Ananya2
Copy link
Copy Markdown
Contributor

@Ananya2 Ananya2 commented Sep 10, 2025

Overview
This PR updates DatabaseMetaData.getIndexInfo() to address an issue where calling ResultSet.next() after exhaustion could throw an invalid cursor position exception.
The fix removes the use of CachedRowSet for combining sp_statistics and columnstore index results, and instead uses a UNION ALL query. This ensures standard JDBC cursor behavior while keeping columnstore index support intact.

Problem Description
The current implementation uses CachedRowSet to merge results from sp_statistics and Columnstore index queries. However, CachedRowSet has stricter cursor validation than SQLServerResultSet:

if (cursorPos < 0 || cursorPos >= numRows + 1) {
    throw new SQLException("Invalid cursor position");
}

This causes SQLException: Invalid cursor position when next() is called after the ResultSet has been fully enumerated, which differs from standard JDBC behavior where such calls should simply return false.

Root Cause
Columnstore index support was first added in #2598
to address missing indexes in sp_statistics results. That implementation relied on CachedRowSet to merge sp_statistics output with additional Columnstore index query results, which in turn caused the cursor positioning issue.

Solution

  • SQL Server: Replaced CachedRowSet merging with a single UNION ALL query (INDEX_INFO_COMBINED_QUERY) that combines sp_statistics results with Columnstore index data from sys.indexes.
  • Azure Synapse DW: Execute sp_statistics separately and combine with sys.indexes via UNION ALL ensuring comprehensive index coverage including columnstore indexes.

This eliminates intermediate result set handling and ensures consistent behavior across environments.

Testing

  • Validated cursor behavior on both SQL Server and Azure Synapse DW.
  • Added comprehensive JUnit tests for ResultSet navigation patterns.
  • Verified Columnstore indexes are properly included.
  • Confirmed sp_statistics compatibility maintained.

Closes #2758 #2778

@Ananya2 Ananya2 self-assigned this Sep 10, 2025
@Ananya2 Ananya2 added this to the 13.2.1 milestone Sep 10, 2025
@codecov
Copy link
Copy Markdown

codecov Bot commented Sep 10, 2025

Codecov Report

❌ Patch coverage is 14.63415% with 70 lines in your changes missing coverage. Please review.
✅ Project coverage is 52.27%. Comparing base (51074c0) to head (bb94a36).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...soft/sqlserver/jdbc/SQLServerDatabaseMetaData.java 14.63% 68 Missing and 2 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #2763      +/-   ##
============================================
- Coverage     52.30%   52.27%   -0.04%     
- Complexity     4147     4159      +12     
============================================
  Files           149      149              
  Lines         34241    34306      +65     
  Branches       5718     5723       +5     
============================================
+ Hits          17910    17933      +23     
- Misses        13838    13901      +63     
+ Partials       2493     2472      -21     

☔ View full report in Codecov by Sentry.
📢 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.

@Ananya2 Ananya2 force-pushed the user/anagarg/issue#2758 branch from fc4c9f7 to 38b8252 Compare September 12, 2025 07:25
…e as per sp_statistics official documentation
divang
divang previously approved these changes Sep 12, 2025
muskan124947
muskan124947 previously approved these changes Sep 12, 2025
@Ananya2 Ananya2 modified the milestones: 13.2.1, 13.3.0 Sep 15, 2025
@Ananya2 Ananya2 dismissed stale reviews from muskan124947 and divang via 31a0f2a September 22, 2025 08:11
@Ananya2 Ananya2 modified the milestones: 13.3.0, 13.2.1 Sep 22, 2025
@Ananya2 Ananya2 linked an issue Sep 23, 2025 that may be closed by this pull request
@Ananya2 Ananya2 merged commit e783ae4 into main Sep 23, 2025
17 of 19 checks passed
Ananya2 added a commit that referenced this pull request Sep 23, 2025
…ion exception (#2763)

* Fix DatabaseMetaData.getIndexInfo() CachedRowSet invalid cursor position exception

* removed unused imports

* Fix ADO getIndexInfo test failures

* Reverted INDEX_QUALIFIER to return table name instead of database name as per sp_statistics official documentation

* Fix catalog switching in getIndexInfo to handle cross-database queries and fix ADO regression test failure

* Fix Azure DW getIndexInfo to include columnstore indexes via UNION ALL

* Fix getIndexInfo ORDER BY clause to match sp_statistics server implementation
Ananya2 added a commit that referenced this pull request Sep 23, 2025
…ion exception (#2763) (#2780)

* Fix DatabaseMetaData.getIndexInfo() CachedRowSet invalid cursor position exception

* removed unused imports

* Fix ADO getIndexInfo test failures

* Reverted INDEX_QUALIFIER to return table name instead of database name as per sp_statistics official documentation

* Fix catalog switching in getIndexInfo to handle cross-database queries and fix ADO regression test failure

* Fix Azure DW getIndexInfo to include columnstore indexes via UNION ALL

* Fix getIndexInfo ORDER BY clause to match sp_statistics server implementation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

4 participants