Skip to content

Commit 4d6c2d2

Browse files
morningmanYour Name
authored andcommitted
[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 d96662c commit 4d6c2d2

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;
@@ -557,7 +558,17 @@ protected void handleFieldList(String tableName) throws ConnectionException {
557558
// only Mysql protocol
558559
protected ByteBuffer getResultPacket() {
559560
Preconditions.checkState(connectType.equals(ConnectType.MYSQL));
560-
MysqlPacket packet = ctx.getState().toResponsePacket();
561+
MysqlPacket packet;
562+
// When CLIENT_DEPRECATE_EOF is set and the state is EOF (end of result set),
563+
// we need to send a "ResultSet OK" packet (0xFE header with payload > 5 bytes)
564+
// instead of the traditional EOF packet. This is required by the MySQL protocol
565+
// and expected by MySQL Connector/J 9.5.0+.
566+
if (ctx.getState().getStateType() == QueryState.MysqlStateType.EOF
567+
&& ctx.getMysqlChannel().clientDeprecatedEOF()) {
568+
packet = new MysqlResultSetEndPacket(ctx.getState());
569+
} else {
570+
packet = ctx.getState().toResponsePacket();
571+
}
561572
if (packet == null) {
562573
// possible two cases:
563574
// 1. handler has send request
@@ -647,6 +658,12 @@ public TMasterOpResult proxyExecute(TMasterOpRequest request) throws TException
647658
// set compute group
648659
ctx.setComputeGroup(Env.getCurrentEnv().getAuth().getComputeGroup(ctx.getQualifiedUser()));
649660

661+
// Propagate the client's CLIENT_DEPRECATE_EOF capability to the proxy channel.
662+
// This ensures the master generates packets matching the original client's protocol.
663+
if (request.isSetClientDeprecatedEOF() && request.isClientDeprecatedEOF()) {
664+
ctx.getMysqlChannel().setClientDeprecatedEOF();
665+
}
666+
650667
ctx.setThreadLocalInfo();
651668
StmtExecutor executor = null;
652669
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;
@@ -1538,11 +1537,15 @@ private void sendMetaData(ResultSetMetaData metaData, List<FieldInfo> fieldInfos
15381537
}
15391538
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
15401539
}
1541-
// send EOF
1542-
serializer.reset();
1543-
MysqlEofPacket eofPacket = new MysqlEofPacket(context.getState());
1544-
eofPacket.writeTo(serializer);
1545-
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
1540+
// When CLIENT_DEPRECATE_EOF is set, the server should not send the intermediate
1541+
// EOF packet after column definitions. The client will go directly from column
1542+
// definitions to reading data rows.
1543+
if (!context.getMysqlChannel().clientDeprecatedEOF()) {
1544+
serializer.reset();
1545+
MysqlEofPacket eofPacket = new MysqlEofPacket(context.getState());
1546+
eofPacket.writeTo(serializer);
1547+
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
1548+
}
15461549
}
15471550

15481551
private List<PrimitiveType> exprToStringType(List<Expr> exprs) {
@@ -1584,15 +1587,15 @@ public void sendStmtPrepareOK(int stmtId, List<String> labels, List<Slot> output
15841587
serializer.writeField(colNames.get(i), Type.STRING);
15851588
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
15861589
}
1587-
serializer.reset();
1590+
// When CLIENT_DEPRECATE_EOF is set, no EOF/OK packet should be sent after
1591+
// parameter definitions. The driver knows how many params to expect from the
1592+
// prepare OK packet and simply stops reading after that count.
15881593
if (!context.getMysqlChannel().clientDeprecatedEOF()) {
1594+
serializer.reset();
15891595
MysqlEofPacket eofPacket = new MysqlEofPacket(context.getState());
15901596
eofPacket.writeTo(serializer);
1591-
} else {
1592-
MysqlOkPacket okPacket = new MysqlOkPacket(context.getState());
1593-
okPacket.writeTo(serializer);
1597+
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
15941598
}
1595-
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
15961599
}
15971600
if (numColumns > 0) {
15981601
for (Slot slot : output) {
@@ -1611,15 +1614,15 @@ public void sendStmtPrepareOK(int stmtId, List<String> labels, List<Slot> output
16111614
}
16121615
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
16131616
}
1614-
serializer.reset();
1617+
// When CLIENT_DEPRECATE_EOF is set, no EOF/OK packet should be sent after
1618+
// column definitions. The driver knows how many columns to expect from the
1619+
// prepare OK packet and simply stops reading after that count.
16151620
if (!context.getMysqlChannel().clientDeprecatedEOF()) {
1621+
serializer.reset();
16161622
MysqlEofPacket eofPacket = new MysqlEofPacket(context.getState());
16171623
eofPacket.writeTo(serializer);
1618-
} else {
1619-
MysqlOkPacket okPacket = new MysqlOkPacket(context.getState());
1620-
okPacket.writeTo(serializer);
1624+
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
16211625
}
1622-
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
16231626
}
16241627
context.getMysqlChannel().flush();
16251628
context.getState().setNoop();
@@ -1668,11 +1671,15 @@ private void sendFields(List<String> colNames, List<FieldInfo> fieldInfos, List<
16681671
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
16691672
}
16701673
}
1671-
// send EOF
1672-
serializer.reset();
1673-
MysqlEofPacket eofPacket = new MysqlEofPacket(context.getState());
1674-
eofPacket.writeTo(serializer);
1675-
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
1674+
// When CLIENT_DEPRECATE_EOF is set, the server should not send the intermediate
1675+
// EOF packet after column definitions. The client will go directly from column
1676+
// definitions to reading data rows.
1677+
if (!context.getMysqlChannel().clientDeprecatedEOF()) {
1678+
serializer.reset();
1679+
MysqlEofPacket eofPacket = new MysqlEofPacket(context.getState());
1680+
eofPacket.writeTo(serializer);
1681+
context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
1682+
}
16761683
}
16771684

16781685
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)