Skip to content

Commit d4cc96a

Browse files
authored
[fix](protocol) Support CLIENT_DEPRECATE_EOF to fix empty result with MySQL driver 9.5.0 (#61050)
### What problem does this PR solve? Issue Number: close #60634 Problem Summary: When using MySQL Connector/J 9.5.0+ with useServerPrepStmts=true, queries return empty result sets. This is caused by a behavior change in the driver's BinaryResultsetReader that no longer consumes the intermediate EOF packet after column definitions when no cursor exists. The fix properly implements the CLIENT_DEPRECATE_EOF capability: 1. Advertise CLIENT_DEPRECATE_EOF in server handshake capability flags (MysqlCapability.java), so the driver negotiates it and uses the isEOFDeprecated()=true code path, bypassing the buggy 9.5.0 logic. 2. Skip intermediate EOF packets after column definitions when CLIENT_DEPRECATE_EOF is negotiated (StmtExecutor.sendMetaData, sendFields, sendStmtPrepareOK). 3. Send a 'ResultSet OK' packet (0xFE header, payload > 5 bytes) as the final end-of-result-set marker instead of the traditional EOF packet, so the driver's isResultSetOKPacket() correctly identifies it (MysqlResultSetEndPacket.java, ConnectProcessor.getResultPacket). 4. Always write the info field in MysqlOkPacket (even when empty) to prevent ArrayIndexOutOfBoundsException when the driver parses OK packets via OkPacket.parse() which unconditionally reads STRING_LENENC.
1 parent 88396fb commit d4cc96a

10 files changed

Lines changed: 266 additions & 31 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/mysql/MysqlCapability.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,14 @@ public String toString() {
7474

7575
private static final int DEFAULT_FLAGS = Flag.CLIENT_PROTOCOL_41.getFlagBit()
7676
| Flag.CLIENT_CONNECT_WITH_DB.getFlagBit() | Flag.CLIENT_SECURE_CONNECTION.getFlagBit()
77-
| Flag.CLIENT_PLUGIN_AUTH.getFlagBit() | Flag.CLIENT_LOCAL_FILES.getFlagBit() | Flag.CLIENT_LONG_FLAG
78-
.getFlagBit();
77+
| Flag.CLIENT_PLUGIN_AUTH.getFlagBit() | Flag.CLIENT_LOCAL_FILES.getFlagBit()
78+
| Flag.CLIENT_LONG_FLAG.getFlagBit() | Flag.CLIENT_DEPRECATE_EOF.getFlagBit();
7979

8080
private static final int SSL_FLAGS = Flag.CLIENT_PROTOCOL_41.getFlagBit()
8181
| Flag.CLIENT_CONNECT_WITH_DB.getFlagBit() | Flag.CLIENT_SECURE_CONNECTION.getFlagBit()
8282
| Flag.CLIENT_PLUGIN_AUTH.getFlagBit() | Flag.CLIENT_LOCAL_FILES.getFlagBit()
83-
| Flag.CLIENT_LONG_FLAG.getFlagBit() | Flag.CLIENT_SSL.getFlagBit();
83+
| Flag.CLIENT_LONG_FLAG.getFlagBit() | Flag.CLIENT_SSL.getFlagBit()
84+
| Flag.CLIENT_DEPRECATE_EOF.getFlagBit();
8485

8586
public static final MysqlCapability DEFAULT_CAPABILITY = new MysqlCapability(DEFAULT_FLAGS);
8687
public static final MysqlCapability SSL_CAPABILITY = new MysqlCapability(SSL_FLAGS);

‎fe/fe-core/src/main/java/org/apache/doris/mysql/MysqlOkPacket.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,13 @@ public void writeTo(MysqlSerializer serializer) {
5959
// if ((STATUS_FLAGS & MysqlStatusFlag.SERVER_SESSION_STATE_CHANGED) != 0) {
6060
// }
6161
} else {
62-
if (!Strings.isNullOrEmpty(infoMessage)) {
63-
// NOTE: in datasheet, use EOF string, but in the code, mysql use length encoded string
62+
// Always write the info field as a length-encoded string.
63+
// When CLIENT_DEPRECATE_EOF is negotiated, the driver's OkPacket.parse()
64+
// unconditionally reads STRING_LENENC for info, so an empty string must
65+
// still be written (as a single 0x00 byte representing length 0).
66+
if (Strings.isNullOrEmpty(infoMessage)) {
67+
serializer.writeVInt(0);
68+
} else {
6469
serializer.writeLenEncodedString(infoMessage);
6570
}
6671
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.doris.mysql;
19+
20+
import org.apache.doris.qe.QueryState;
21+
22+
/**
23+
* MySQL OK packet with 0xFE header, used as the end-of-result-set marker
24+
* when CLIENT_DEPRECATE_EOF is set.
25+
*
26+
* When CLIENT_DEPRECATE_EOF capability is negotiated, the traditional EOF packet (0xFE, ≤5 bytes)
27+
* is replaced by an OK packet that also uses 0xFE as its header byte but has a payload larger
28+
* than 5 bytes. This is called a "ResultSet OK" packet in the MySQL protocol documentation.
29+
*
30+
* See: https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_basic_ok_packet.html
31+
*/
32+
public class MysqlResultSetEndPacket extends MysqlPacket {
33+
// 0xFE header to distinguish from regular OK (0x00)
34+
private static final int RESULTSET_OK_INDICATOR = 0xFE;
35+
private static final long AFFECTED_ROWS = 0;
36+
private static final long LAST_INSERT_ID = 0;
37+
38+
private int serverStatus = 0;
39+
private int warningCount = 0;
40+
41+
public MysqlResultSetEndPacket(QueryState state) {
42+
this.serverStatus = state.serverStatus;
43+
}
44+
45+
@Override
46+
public void writeTo(MysqlSerializer serializer) {
47+
// header: 0xFE (same as EOF, but the payload length > 5 distinguishes it)
48+
serializer.writeInt1(RESULTSET_OK_INDICATOR);
49+
// affected_rows: int<lenenc>
50+
serializer.writeVInt(AFFECTED_ROWS);
51+
// last_insert_id: int<lenenc>
52+
serializer.writeVInt(LAST_INSERT_ID);
53+
// status_flags: int<2>
54+
serializer.writeInt2(serverStatus);
55+
// warnings: int<2>
56+
serializer.writeInt2(warningCount);
57+
// info: string<lenenc> (empty string, written as a single 0x00 byte for length 0)
58+
// The driver's OkPacket.parse() unconditionally reads this field.
59+
serializer.writeVInt(0);
60+
}
61+
}

‎fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.apache.doris.mysql.MysqlChannel;
4545
import org.apache.doris.mysql.MysqlCommand;
4646
import org.apache.doris.mysql.MysqlPacket;
47+
import org.apache.doris.mysql.MysqlResultSetEndPacket;
4748
import org.apache.doris.mysql.MysqlSerializer;
4849
import org.apache.doris.mysql.MysqlServerStatusFlag;
4950
import org.apache.doris.nereids.SqlCacheContext;
@@ -553,7 +554,17 @@ protected void handleFieldList(String tableName) throws ConnectionException {
553554
// only Mysql protocol
554555
protected ByteBuffer getResultPacket() {
555556
Preconditions.checkState(connectType.equals(ConnectType.MYSQL));
556-
MysqlPacket packet = ctx.getState().toResponsePacket();
557+
MysqlPacket packet;
558+
// When CLIENT_DEPRECATE_EOF is set and the state is EOF (end of result set),
559+
// we need to send a "ResultSet OK" packet (0xFE header with payload > 5 bytes)
560+
// instead of the traditional EOF packet. This is required by the MySQL protocol
561+
// and expected by MySQL Connector/J 9.5.0+.
562+
if (ctx.getState().getStateType() == QueryState.MysqlStateType.EOF
563+
&& ctx.getMysqlChannel().clientDeprecatedEOF()) {
564+
packet = new MysqlResultSetEndPacket(ctx.getState());
565+
} else {
566+
packet = ctx.getState().toResponsePacket();
567+
}
557568
if (packet == null) {
558569
// possible two cases:
559570
// 1. handler has send request
@@ -643,6 +654,12 @@ public TMasterOpResult proxyExecute(TMasterOpRequest request) throws TException
643654
// set compute group
644655
ctx.setComputeGroup(Env.getCurrentEnv().getAuth().getComputeGroup(ctx.getQualifiedUser()));
645656

657+
// Propagate the client's CLIENT_DEPRECATE_EOF capability to the proxy channel.
658+
// This ensures the master generates packets matching the original client's protocol.
659+
if (request.isSetClientDeprecatedEOF() && request.isClientDeprecatedEOF()) {
660+
ctx.getMysqlChannel().setClientDeprecatedEOF();
661+
}
662+
646663
ctx.setThreadLocalInfo();
647664
StmtExecutor executor = null;
648665
try {

‎fe/fe-core/src/main/java/org/apache/doris/qe/FEOpExecutor.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ protected TMasterOpRequest buildStmtForwardParams() throws AnalysisException {
206206
}
207207
}
208208

209+
// Propagate the client's CLIENT_DEPRECATE_EOF capability so the master FE
210+
// generates packets matching the original client's protocol expectations.
211+
params.setClientDeprecatedEOF(ctx.getMysqlChannel().clientDeprecatedEOF());
212+
209213
return params;
210214
}
211215

‎fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java‎

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
import org.apache.doris.mysql.MysqlChannel;
6868
import org.apache.doris.mysql.MysqlCommand;
6969
import org.apache.doris.mysql.MysqlEofPacket;
70-
import org.apache.doris.mysql.MysqlOkPacket;
7170
import org.apache.doris.mysql.MysqlSerializer;
7271
import org.apache.doris.mysql.ProxyMysqlChannel;
7372
import org.apache.doris.nereids.NereidsPlanner;
@@ -1595,11 +1594,15 @@ private void sendMetaData(ResultSetMetaData metaData, List<FieldInfo> fieldInfos
15951594
}
15961595
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
15971596
}
1598-
// send EOF
1599-
serializer.reset();
1600-
MysqlEofPacket eofPacket = new MysqlEofPacket(context.getState());
1601-
eofPacket.writeTo(serializer);
1602-
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
1597+
// When CLIENT_DEPRECATE_EOF is set, the server should not send the intermediate
1598+
// EOF packet after column definitions. The client will go directly from column
1599+
// definitions to reading data rows.
1600+
if (!context.getMysqlChannel().clientDeprecatedEOF()) {
1601+
serializer.reset();
1602+
MysqlEofPacket eofPacket = new MysqlEofPacket(context.getState());
1603+
eofPacket.writeTo(serializer);
1604+
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
1605+
}
16031606
}
16041607

16051608
private List<PrimitiveType> exprToStringType(List<Expr> exprs) {
@@ -1641,15 +1644,15 @@ public void sendStmtPrepareOK(int stmtId, List<String> labels, List<Slot> output
16411644
serializer.writeField(colNames.get(i), Type.STRING);
16421645
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
16431646
}
1644-
serializer.reset();
1647+
// When CLIENT_DEPRECATE_EOF is set, no EOF/OK packet should be sent after
1648+
// parameter definitions. The driver knows how many params to expect from the
1649+
// prepare OK packet and simply stops reading after that count.
16451650
if (!context.getMysqlChannel().clientDeprecatedEOF()) {
1651+
serializer.reset();
16461652
MysqlEofPacket eofPacket = new MysqlEofPacket(context.getState());
16471653
eofPacket.writeTo(serializer);
1648-
} else {
1649-
MysqlOkPacket okPacket = new MysqlOkPacket(context.getState());
1650-
okPacket.writeTo(serializer);
1654+
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
16511655
}
1652-
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
16531656
}
16541657
if (numColumns > 0) {
16551658
for (Slot slot : output) {
@@ -1668,15 +1671,15 @@ public void sendStmtPrepareOK(int stmtId, List<String> labels, List<Slot> output
16681671
}
16691672
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
16701673
}
1671-
serializer.reset();
1674+
// When CLIENT_DEPRECATE_EOF is set, no EOF/OK packet should be sent after
1675+
// column definitions. The driver knows how many columns to expect from the
1676+
// prepare OK packet and simply stops reading after that count.
16721677
if (!context.getMysqlChannel().clientDeprecatedEOF()) {
1678+
serializer.reset();
16731679
MysqlEofPacket eofPacket = new MysqlEofPacket(context.getState());
16741680
eofPacket.writeTo(serializer);
1675-
} else {
1676-
MysqlOkPacket okPacket = new MysqlOkPacket(context.getState());
1677-
okPacket.writeTo(serializer);
1681+
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
16781682
}
1679-
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
16801683
}
16811684
context.getMysqlChannel().flush();
16821685
context.getState().setNoop();
@@ -1726,11 +1729,15 @@ private void sendFields(List<String> colNames, List<FieldInfo> fieldInfos, List<
17261729
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
17271730
}
17281731
}
1729-
// send EOF
1730-
serializer.reset();
1731-
MysqlEofPacket eofPacket = new MysqlEofPacket(context.getState());
1732-
eofPacket.writeTo(serializer);
1733-
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
1732+
// When CLIENT_DEPRECATE_EOF is set, the server should not send the intermediate
1733+
// EOF packet after column definitions. The client will go directly from column
1734+
// definitions to reading data rows.
1735+
if (!context.getMysqlChannel().clientDeprecatedEOF()) {
1736+
serializer.reset();
1737+
MysqlEofPacket eofPacket = new MysqlEofPacket(context.getState());
1738+
eofPacket.writeTo(serializer);
1739+
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
1740+
}
17341741
}
17351742

17361743
public void sendResultSet(ResultSet resultSet) throws IOException {

‎fe/fe-core/src/test/java/org/apache/doris/mysql/MysqlCapabilityTest.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ public void testToString() {
4444
public void testDefaultFlags() {
4545
MysqlCapability capability = MysqlCapability.DEFAULT_CAPABILITY;
4646
Assert.assertEquals("CLIENT_LONG_FLAG | CLIENT_CONNECT_WITH_DB | CLIENT_LOCAL_FILES | CLIENT_PROTOCOL_41"
47-
+ " | CLIENT_SECURE_CONNECTION | CLIENT_PLUGIN_AUTH",
47+
+ " | CLIENT_SECURE_CONNECTION | CLIENT_PLUGIN_AUTH | CLIENT_DEPRECATE_EOF",
4848
capability.toString());
4949
Assert.assertTrue(capability.supportClientLocalFile());
50+
Assert.assertTrue(capability.isDeprecatedEOF());
5051
}
5152
}

‎fe/fe-core/src/test/java/org/apache/doris/mysql/MysqlOkPacketTest.java‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,27 @@ public void testWrite() {
5656
// assert warnings, int2: 0
5757
Assert.assertEquals(0x00, MysqlProto.readInt2(buffer));
5858

59-
// assert info, eof string: "OK"
60-
// Assert.assertEquals("OK", new String(MysqlProto.readEofString(buffer)));
61-
59+
// When infoMessage is empty, an empty len-encoded string (0x00) should still be written.
60+
// This is required because OkPacket.parse() in MySQL Connector/J unconditionally reads
61+
// STRING_LENENC for info. Without this byte, the driver throws
62+
// ArrayIndexOutOfBoundsException when CLIENT_DEPRECATE_EOF is negotiated.
63+
Assert.assertEquals(0x00, MysqlProto.readVInt(buffer));
6264
Assert.assertEquals(0, buffer.remaining());
6365
}
6466

67+
@Test
68+
public void testWritePayloadSizeGreaterThan5() {
69+
// When CLIENT_DEPRECATE_EOF is negotiated, the driver distinguishes between
70+
// EOF packets (payload <= 5) and ResultSet OK packets (payload > 5).
71+
// MysqlOkPacket payload must be > 5 to avoid being misidentified as EOF.
72+
// Payload: 0x00(1) + affected_rows(1) + last_insert_id(1) + status(2) + warnings(2) + info_len(1) = 8
73+
MysqlOkPacket packet = new MysqlOkPacket(new QueryState());
74+
MysqlSerializer serializer = MysqlSerializer.newInstance(capability);
75+
packet.writeTo(serializer);
76+
77+
ByteBuffer buffer = serializer.toByteBuffer();
78+
int payloadLength = buffer.remaining();
79+
Assert.assertTrue("OK packet payload should be > 5 for CLIENT_DEPRECATE_EOF compatibility, got: "
80+
+ payloadLength, payloadLength > 5);
81+
}
6582
}

0 commit comments

Comments
 (0)