JackieTien97 opened a new issue, #18846:
URL: https://github.com/apache/iotdb/issues/18846

   ### Search before asking
   
   - [x] I searched the existing issues and found no report of this 
result-column mapping problem.
   
   ### Version
   
   - Apache IoTDB `2.0.11-SNAPSHOT`, `master` at commit 
`17b95c8360e534e647fd233dfabd2a92e9dff2bc`.
   - Reproduction environment: macOS 27.0.1, OpenJDK 24.0.2; the relevant 
current-source JDBC/RPC classes were compiled with `--release 17`.
   - Validation scope: a client-only Java probe using simulated RPC responses 
and real serialized TsBlocks. No live IoTDB server or cluster was used.
   
   ### Describe the bug and provide the minimal reproduce step
   
   `IoTDBTablePreparedStatement.executeQuery()` discards the server-provided 
`columnIndex2TsBlockColumnIndexList` when constructing its result set. The 
constructor it selects replaces that mapping with `[0, 1, 2, ...]`.
   
   Whenever the logical result columns do not match the physical TsBlock column 
order, this can silently return values from the wrong columns. When multiple 
result columns share a physical TsBlock column, it can instead throw an 
array-bounds exception. Both index-based and name-based reads are affected.
   
   The following client-only setup reproduced the problem:
   
   1. Construct and serialize a one-row TsBlock with two `INT32` value columns 
containing `10` and `20`, respectively.
   2. Configure a mocked RPC client so that `prepareStatement` succeeds with 
`parameterCount = 1`, and `executePreparedStatement` returns a successful 
`TSExecuteStatementResp` with:
      - `columns = ["b", "a"]`
      - `dataTypeList = ["INT32", "INT32"]`
      - `columnNameIndexMap = {"b": 1, "a": 0}`
      - `columnIndex2TsBlockColumnIndexList = [1, 0]`
      - `ignoreTimeStamp = true`, `tableModel = true`, `moreData = false`
      - `queryResult` containing the serialized TsBlock.
   3. Create an `IoTDBTablePreparedStatement` for `SELECT b, a FROM t WHERE a > 
?`, bind parameter 1 to `0`, and call `executeQuery()`.
   4. Advance to the first row and read both columns by index and by name.
   5. As a control, pass an equivalent fresh response through ordinary 
`IoTDBStatement.executeQuery()`.
   
   This is a simulated response fixture: the SQL identifies the intended 
projection, while the response explicitly supplies the physical column layout. 
It does not assume that this particular SQL always produces the same physical 
layout on a live server.
   
   A second fixture uses one physical `INT32` column containing `10`, two 
logical result columns `["x", "y"]`, and mapping `[0, 0]`, representing a 
shared physical column such as `SELECT a AS x, a AS y ...`. Reading the second 
result column through the prepared statement throws 
`ArrayIndexOutOfBoundsException`.
   
   ### What did you expect to see?
   
   For the same server response, `PreparedStatement` should return the same 
values as ordinary `Statement` and honor the server's logical-to-physical 
column mapping.
   
   | Response columns | Physical values | Mapping | Expected values |
   | --- | --- | --- | --- |
   | `[b, a]` | `[10, 20]` | `[1, 0]` | `[20, 10]` |
   | `[x, y]` | `[10]` | `[0, 0]` | `[10, 10]` |
   
   ### What did you see instead?
   
   The current-source probe produced:
   
   ```text
   Statement control: columns=[b,a], mapping=[1,0], values=[20,10] OK
   PreparedStatement: columns=[b,a], mapping=[1,0], expected=[20,10], 
actual=[10,20]; named b=10,a=20 BUG
   Statement duplicate control: columns=[x,y], mapping=[0,0], physical=[10], 
values=[10,10] OK
   PreparedStatement duplicate: expected second column=10, 
actual=java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for 
length 1 BUG
   ```
   
   The first case returns incorrect values without reporting an error. The 
second fails while reading a valid logical result column.
   
   ### Anything else?
   
   The source path explaining these results is:
   
   1. 
[IoTDBTablePreparedStatement.processQueryResult()](https://github.com/apache/iotdb/blob/17b95c8360e534e647fd233dfabd2a92e9dff2bc/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBTablePreparedStatement.java#L215-L232)
 calls the shorter `IoTDBJDBCResultSet` constructor and does not forward 
`resp.getColumnIndex2TsBlockColumnIndexList()`.
   2. [That 
constructor](https://github.com/apache/iotdb/blob/17b95c8360e534e647fd233dfabd2a92e9dff2bc/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBJDBCResultSet.java#L155-L174)
 supplies `false` for `tableModel` and creates an identity column mapping using 
`IntStream.range(0, columnNameList.size())`.
   3. 
[IoTDBRpcDataSet](https://github.com/apache/iotdb/blob/17b95c8360e534e647fd233dfabd2a92e9dff2bc/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/IoTDBRpcDataSet.java#L170-L184)
 builds its name-based mapping from this same positional list. Passing 
`columnNameIndexMap` alone therefore does not preserve the correct mapping.
   4. [Ordinary 
IoTDBStatement](https://github.com/apache/iotdb/blob/17b95c8360e534e647fd233dfabd2a92e9dff2bc/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBStatement.java#L540-L558)
 already forwards the server-provided mapping correctly.
   
   Suggested fix: use the same full `IoTDBJDBCResultSet` constructor as 
ordinary `IoTDBStatement`, passing the following arguments after `zoneId`:
   
   ```java
   charset,
   resp.isSetTableModel() && resp.isTableModel(),
   resp.getColumnIndex2TsBlockColumnIndexList()
   ```
   
   Regression coverage should verify actual values for reordered columns and 
repeated projections/aliases, using both column-index and column-name getters. 
Checking only result-set metadata would not detect the incorrect values.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to