Skip to content

Length-encode the info field of the MySQL OK packet - #107693

Merged
alexey-milovidov merged 3 commits into
masterfrom
fix-mysql-protocol-connector-j-ok-packet
Jun 18, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
fix-mysql-protocol-connector-j-ok-packet

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jun 17, 2026 •

Copy link
Copy Markdown
Member

Closes: #84542

MySQL Connector/J 8.2.0 and newer (including 9.x) could not connect to the ClickHouse MySQL interface. The connection failed during driver setup with Buffer length is less than expected payload length. (the driver fails while running its loadServerVariables query, before any user query runs).

Root cause: OKPacket::writePayloadImpl wrote the info (human-readable status) field of the OK packet as a plain rest-of-packet string whenever CLIENT_SESSION_TRACK was not negotiated. The protocol documentation describes the field that way, but a real MySQL server length-encodes info unconditionally — I verified this on the wire against MySQL 8.0, which sends e.g. ... 0x28 "Rows matched: 2 Changed: 2 Warnings: 0" even when the client did not request CLIENT_SESSION_TRACK. Connector/J >= 8.2 likewise reads info as a length-encoded string regardless of the negotiated capabilities. Because ClickHouse sends a non-empty summary (Read N rows, ... in ... sec.) in the result-set terminating OK packet, the driver interpreted the first byte of that summary (R, 0x52) as a length and failed.

Fix: always length-encode info, matching what a real MySQL server sends. session_state_changes is still only present when CLIENT_SESSION_TRACK and SERVER_SESSION_STATE_CHANGED are set, and the human-readable summary is preserved.

The mysql-java-client integration image was pinned to Connector/J 8.1.0 as a workaround for this bug; it is bumped to the current release (9.7.0) to cover the regression. Verified that MySQLJavaClientTest produces output identical to java_client.reference with Connector/J 8.1.0, 9.3.0 and 9.7.0 (text and binary protocols), and that the bytes produced by the fix match a real MySQL server, so Connector/J 9.3.0/9.7.0 and PyMySQL all connect successfully.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed the MySQL interface being unusable with MySQL Connector/J 8.2.0 and newer (including 9.x). The info field of the OK packet is now length-encoded, matching the MySQL server, so the JDBC driver can connect.

Documentation entry for user-facing changes

  • Documentation is unchanged (protocol compatibility fix, no user-facing documentation change required).

Version info

  • Merged into: 26.6.1.964 (included in 26.6 and later)
  • Backported to: 26.5.3.45, 26.4.5.51, 26.3.17.12, 25.8.26.10

alexey-milovidov and others added 2 commits June 17, 2026 01:06
`MySQL Connector/J` 8.2.0 and newer (including 9.x) could not connect to the
ClickHouse MySQL interface; the connection failed during driver setup with
`Buffer length is less than expected payload length.`

Root cause: `OKPacket::writePayloadImpl` wrote the `info` (human-readable
status) field as a plain rest-of-packet string whenever `CLIENT_SESSION_TRACK`
was not negotiated. The MySQL protocol documentation describes the field that
way, but a real MySQL server length-encodes `info` unconditionally (verified
on the wire against MySQL 8.0, which sends e.g. `... 0x28 "Rows matched: 2
Changed: 2 Warnings: 0"` even when the client did not request
`CLIENT_SESSION_TRACK`). `Connector/J` >= 8.2 likewise reads `info` as a
length-encoded string regardless of the negotiated capabilities. Because
ClickHouse sends a non-empty summary (`Read N rows, ... in ... sec.`) in the
result-set terminating `OK` packet, `Connector/J` interpreted the first byte
of that summary (`R`, 0x52) as a length and failed. The driver issues the
triggering query (`loadServerVariables`) while opening the connection, so the
failure happens before any user query runs.

Fix: always length-encode `info`, matching what a real MySQL server sends.
`session_state_changes` is still only present when `CLIENT_SESSION_TRACK` and
`SERVER_SESSION_STATE_CHANGED` are set. The human-readable summary is kept.
Verified that `Connector/J` 9.3.0, `Connector/J` 8.1.0 and `PyMySQL` all
connect successfully, and that the produced bytes match a real MySQL server.

Closes: #84542

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The `mysql-java-client` integration image was pinned to `Connector/J` 8.1.0
because 8.2.0+ could not connect to the ClickHouse MySQL interface (the `info`
field of the `OK` packet was written as a plain string while newer drivers read
it length-encoded). Now that `info` is length-encoded, bump the driver to the
current release (9.7.0) so the regression is covered.

Verified that `MySQLJavaClientTest` produces output identical to
`java_client.reference` with `Connector/J` 8.1.0, 9.3.0 and 9.7.0 (text and
binary protocols), so no reference update is needed.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f0ec58a]

Summary: ✅


AI Review

Summary

This PR fixes the MySQL wire OK packet encoding by making the info field length-encoded consistently, and updates the mysql-java-client integration image to Connector/J 9.7.0. I found no code-level blockers or majors; the change is narrow, the affected write paths remain size/serialization-consistent, and the existing Java integration test should exercise connection setup plus text and server-prepared binary query paths with the new driver.

Missing context / blind spots
  • ⚠️ Current GitHub status for f0ec58a230382e62771cc41f5bc3eb608c48a911 still has Fast test, Build (arm_tidy), and CI Tests queued, while the direct Praktika PR report has no test results. A completed CI run, especially the MySQL protocol integration test using the rebuilt mysql-java-client image, would close this gap.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 17, 2026
@tiandiwonder tiandiwonder self-assigned this Jun 17, 2026
pull Bot pushed a commit to admariner/ClickHouse that referenced this pull request Jun 17, 2026
The "new tests" check (`new_tests_check.py`) requires a bug-fix PR to add or
modify a test. It previously only recognized changes to `tests/queries/0_stateless`
(functional), `tests/integration/test_*` (integration), and `src/.../tests/`
(unit) files.

Docker images under `ci/docker/integration/` define the client and server
environments that integration tests spin up (e.g. the MySQL and PostgreSQL
client images). Bumping a client library version there - to exercise a fixed
bug through an existing integration test - is effectively a test change, even
though no `test_*.py` file is touched. Count such changes for this gate.

This is added as a separate predicate rather than extending
`has_new_integration_tests`, because the latter is also consulted by
`filter_job.py` to enable the integration flaky and bugfix-validate jobs, which
derive the test modules to run from changed `test_*.py` files and would have
nothing to run for a Docker-image-only change.

Found via the failed check in ClickHouse#107693

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.20% 85.20% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.50% 77.50% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 5/14 (35.71%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 7 line(s) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Jun 18, 2026
@alexey-milovidov
alexey-milovidov merged commit 73e97de into master Jun 18, 2026
164 of 165 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-mysql-protocol-connector-j-ok-packet branch June 18, 2026 01:28
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 18, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jun 18, 2026
robot-ch-test-poll3 added a commit that referenced this pull request Jun 18, 2026
Cherry pick #107693 to 26.3: Length-encode the `info` field of the MySQL `OK` packet
robot-clickhouse added a commit that referenced this pull request Jun 18, 2026
robot-ch-test-poll3 added a commit that referenced this pull request Jun 18, 2026
Cherry pick #107693 to 26.4: Length-encode the `info` field of the MySQL `OK` packet
robot-clickhouse added a commit that referenced this pull request Jun 18, 2026
robot-ch-test-poll3 added a commit that referenced this pull request Jun 18, 2026
Cherry pick #107693 to 26.5: Length-encode the `info` field of the MySQL `OK` packet
robot-clickhouse added a commit that referenced this pull request Jun 18, 2026
alexey-milovidov added a commit that referenced this pull request Jun 18, 2026
Backport #107693 to 26.4: Length-encode the `info` field of the MySQL `OK` packet
alexey-milovidov added a commit that referenced this pull request Jun 18, 2026
Backport #107693 to 26.5: Length-encode the `info` field of the MySQL `OK` packet
robot-ch-test-poll4 added a commit that referenced this pull request Jun 18, 2026
Cherry pick #107693 to 25.8: Length-encode the `info` field of the MySQL `OK` packet
robot-clickhouse added a commit that referenced this pull request Jun 18, 2026
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jun 18, 2026
alexey-milovidov added a commit that referenced this pull request Jun 30, 2026
…eclipse-temurin:21-jdk-alpine`

The `Dockers Build (amd)`/`Dockers Build (arm)` jobs failed because the
`clickhouse/mysql-java-client` image (rebuilt here due to the `Connector/J`
bump) still used the deprecated `openjdk:8-jdk-alpine` base, which the CI
`dockerhub-proxy` can no longer serve:

    failed to resolve source metadata for docker.io/library/openjdk:8-jdk-alpine:
    unexpected status from HEAD request to .../openjdk/manifests/8-jdk-alpine: 500 Internal Server Error

The failure is deterministic (it reproduced identically on a re-run ~2.5h
later), not a transient flake.

On `master` the base image had already been migrated to
`eclipse-temurin:21-jdk-alpine` (and `CLASSPATH` set to `.:`) in an earlier
commit that was never backported to `25.8`; #107693 was developed and verified
on top of that. The cherry-pick brought only the `Connector/J` 9.7.0 bump,
leaving `25.8` on the broken base image, so its image could not be rebuilt.

Align the `25.8` Dockerfile with the verified `master` configuration. Verified
locally: the image builds, the `Connector/J` 9.7.0 jar downloads, and
`MySQLJavaClientTest.java` (identical to `master`) compiles under JDK 21.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107889&sha=29e01838604f8a31671c7090e7ebcfa725b2a192&name_0=BackportPR&name_1=Dockers%20Build%20%28amd%29

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
alexey-milovidov added a commit that referenced this pull request Jun 30, 2026
Backport #107693 to 26.3: Length-encode the `info` field of the MySQL `OK` packet
alexey-milovidov added a commit that referenced this pull request Jul 2, 2026
Backport #107693 to 25.8: Length-encode the `info` field of the MySQL `OK` packet
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…7693

Backport ClickHouse#107693 to 26.4: Length-encode the `info` field of the MySQL `OK` packet
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to connect to ClickHouse's MySQL Interface via MySQL JDBC Driver

4 participants