Skip to content

[Python] Fix swapped session and statement IDs in query result sets - #18842

Merged
JackieTien97 merged 1 commit into
apache:masterfrom
ColinLeeo:fix/python-query-session-ids
Oct 13, 2026
Merged

JackieTien97 merged 1 commit into
apache:masterfrom
ColinLeeo:fix/python-query-session-ids

Conversation

@ColinLeeo

Copy link
Copy Markdown
Collaborator

Description

Fix a Python client bug that swaps sessionId and statementId when constructing query result sets. execute_query_statement, execute_statement, and execute_raw_data_query send the correct IDs in the initial execution request, but pass them in reverse order to SessionDataSet. Subsequent fetchResultsV2 and closeOperation requests therefore use the wrong IDs. execute_last_data_query already uses the correct order.

Compatibility with newer IoTDB servers

Older Python client versions containing this bug can fail when connecting to IoTDB servers with the query ownership validation introduced by #18643. When sessionId != statementId, the initial result batch can be returned successfully, but fetching the next batch is rejected with NO_PERMISSION because the supplied statement ID does not own the query. Closing a query before all results have been consumed can also be rejected while the query is still running.

If the two IDs happen to be equal, the swap is invisible. This explains why simple connections and existing tests may appear to work, while other connection/statement allocation histories expose the problem. The server validation exposes an existing client bug; users encountering this combination need a Python client containing this fix. Upgrading only the server does not correct the IDs sent by an older client.

Changes

  • Pass SessionDataSet arguments by name in all four query entry points, assigning the correct connection session_id and statement_id explicitly.
  • Add 16 regression scenarios using deliberately different session and statement IDs. Cover all four entry points, fetching another result batch, closing before exhaustion, and retrying query execution after reconnecting with a new client and new IDs.
  • Keep the public client API and server-side query ownership checks unchanged.

Validation

  • Before the fix, the new regression suite reproduced 12 failures in the three affected entry points; the four latest-value query scenarios passed.
  • Python unit suite: python3 -m pytest tests/unit -q --tb=short — 50 passed.
  • Black check on the changed Python files and git diff --check passed.
  • Live-server integration tests were not run; regression tests exercise the actual dataset iteration, TsBlock deserialization, and reconnect paths, and inspect the Thrift request IDs using mocked RPC clients.

This PR has:

  • been self-reviewed.
  • added unit tests to cover the corrected behavior.
Key changed/added classes
  • iotdb.Session.Session
  • tests/unit/test_session_query_ids.py

@ColinLeeo
ColinLeeo requested a review from HTHou October 13, 2026 03:48
@JackieTien97
JackieTien97 merged commit ecf526a into apache:master Oct 13, 2026
43 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.

3 participants