This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 25033f400b1 Fix bytea text encoding in PostgreSQL data rows (#37772)
25033f400b1 is described below

commit 25033f400b1304d938b8e0ff03422b37dc4de326
Author: jonasHanhan <[email protected]>
AuthorDate: Tue Jan 20 00:46:13 2026 +0800

    Fix bytea text encoding in PostgreSQL data rows (#37772)
---
 .../command/query/PostgreSQLDataRowPacket.java       | 20 ++++++++++++++++++--
 .../command/query/PostgreSQLDataRowPacketTest.java   | 18 ++++++++++++++++--
 2 files changed, 34 insertions(+), 4 deletions(-)

diff --git 
a/database/protocol/dialect/postgresql/src/main/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacket.java
 
b/database/protocol/dialect/postgresql/src/main/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacket.java
index 590a845e92f..1e1829f43cb 100644
--- 
a/database/protocol/dialect/postgresql/src/main/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacket.java
+++ 
b/database/protocol/dialect/postgresql/src/main/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacket.java
@@ -27,6 +27,7 @@ import 
org.apache.shardingsphere.database.protocol.postgresql.packet.identifier.
 import 
org.apache.shardingsphere.database.protocol.postgresql.packet.identifier.PostgreSQLMessagePacketType;
 import 
org.apache.shardingsphere.database.protocol.postgresql.payload.PostgreSQLPacketPayload;
 
+import java.nio.charset.StandardCharsets;
 import java.sql.SQLException;
 import java.sql.SQLXML;
 import java.util.Collection;
@@ -38,6 +39,8 @@ import java.util.Collection;
 @Getter
 public final class PostgreSQLDataRowPacket extends PostgreSQLIdentifierPacket {
     
+    private static final byte[] HEX_DIGITS = 
"0123456789abcdef".getBytes(StandardCharsets.US_ASCII);
+    
     private final Collection<Object> data;
     
     @Override
@@ -67,8 +70,9 @@ public final class PostgreSQLDataRowPacket extends 
PostgreSQLIdentifierPacket {
         if (null == each) {
             payload.writeInt4(0xFFFFFFFF);
         } else if (each instanceof byte[]) {
-            payload.writeInt4(((byte[]) each).length);
-            payload.writeBytes((byte[]) each);
+            byte[] columnData = encodeByteaText((byte[]) each);
+            payload.writeInt4(columnData.length);
+            payload.writeBytes(columnData);
         } else if (each instanceof SQLXML) {
             writeSQLXMLData(payload, each);
         } else if (each instanceof Boolean) {
@@ -82,6 +86,18 @@ public final class PostgreSQLDataRowPacket extends 
PostgreSQLIdentifierPacket {
         }
     }
     
+    private byte[] encodeByteaText(final byte[] value) {
+        byte[] result = new byte[value.length * 2 + 2];
+        result[0] = '\\';
+        result[1] = 'x';
+        for (int i = 0; i < value.length; i++) {
+            int unsignedByte = value[i] & 0xFF;
+            result[2 + i * 2] = HEX_DIGITS[unsignedByte >>> 4];
+            result[3 + i * 2] = HEX_DIGITS[unsignedByte & 0x0F];
+        }
+        return result;
+    }
+    
     private void writeSQLXMLData(final PostgreSQLPacketPayload payload, final 
Object data) {
         try {
             byte[] dataBytes = ((SQLXML) 
data).getString().getBytes(payload.getCharset());
diff --git 
a/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacketTest.java
 
b/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacketTest.java
index 1ba3630b26f..cf0bd494662 100644
--- 
a/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacketTest.java
+++ 
b/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacketTest.java
@@ -68,8 +68,9 @@ class PostgreSQLDataRowPacketTest {
     void assertWriteWithBytes() {
         PostgreSQLDataRowPacket actual = new 
PostgreSQLDataRowPacket(Collections.singleton(new byte[]{'a'}));
         actual.write(payload);
-        verify(payload).writeInt4(new byte[]{'a'}.length);
-        verify(payload).writeBytes(new byte[]{'a'});
+        byte[] expectedBytes = buildExpectedByteaText(new byte[]{'a'});
+        verify(payload).writeInt4(expectedBytes.length);
+        verify(payload).writeBytes(expectedBytes);
     }
     
     @Test
@@ -122,4 +123,17 @@ class PostgreSQLDataRowPacketTest {
     void assertGetIdentifier() {
         assertThat(new 
PostgreSQLDataRowPacket(Collections.emptyList()).getIdentifier(), 
is(PostgreSQLMessagePacketType.DATA_ROW));
     }
+    
+    private byte[] buildExpectedByteaText(final byte[] value) {
+        byte[] result = new byte[value.length * 2 + 2];
+        result[0] = '\\';
+        result[1] = 'x';
+        byte[] hexDigits = 
"0123456789abcdef".getBytes(StandardCharsets.US_ASCII);
+        for (int i = 0; i < value.length; i++) {
+            int unsignedByte = value[i] & 0xFF;
+            result[2 + i * 2] = hexDigits[unsignedByte >>> 4];
+            result[3 + i * 2] = hexDigits[unsignedByte & 0x0F];
+        }
+        return result;
+    }
 }

Reply via email to