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 3fd55216e18 Preserve microseconds for LocalDateTime text rows (#37410)
3fd55216e18 is described below

commit 3fd55216e188b05a91422249a6e1ef7724529c04
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Dec 17 11:33:44 2025 +0800

    Preserve microseconds for LocalDateTime text rows (#37410)
    
    * Preserve microseconds for LocalDateTime text rows
    
    * Preserve microseconds for LocalDateTime text rows
---
 RELEASE-NOTES.md                                     |  1 +
 .../query/text/MySQLTextResultSetRowPacket.java      | 20 +++++++++++++++++++-
 .../query/text/MySQLTextResultSetRowPacketTest.java  |  8 ++++++++
 3 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md
index cd18ebd0037..85d5496bd60 100644
--- a/RELEASE-NOTES.md
+++ b/RELEASE-NOTES.md
@@ -97,6 +97,7 @@
 1. Proxy: Fix column length for PostgreSQL string binary protocol value - 
[35840](https://github.com/apache/shardingsphere/pull/35840)
 1. Proxy: Fix the connection leak caused by rollback failure in Proxy - 
[35867](https://github.com/apache/shardingsphere/pull/35867)
 1. Proxy: Fix the behavior difference of select built-in function names with 
spaces -[#36537](https://github.com/apache/shardingsphere/pull/36537)
+1. Proxy: Fix MySQL text protocol datetime fractional seconds output - 
[#37410](https://github.com/apache/shardingsphere/pull/37410)
 1. Proxy: Fix IndexOutOfBoundsException for MySQL no-FROM multi-projection 
SELECT routed to admin path - 
[#37391](https://github.com/apache/shardingsphere/pull/37391)
 1. Proxy: Fix MySQL binary protocol datetime/time fractional seconds precision 
- [#37294](https://github.com/apache/shardingsphere/pull/37294)
 1. Proxy: Fix PostgreSQL boolean text output to return `t`/`f` as per protocol 
- [#37184](https://github.com/apache/shardingsphere/pull/37184)
diff --git 
a/database/protocol/dialect/mysql/src/main/java/org/apache/shardingsphere/database/protocol/mysql/packet/command/query/text/MySQLTextResultSetRowPacket.java
 
b/database/protocol/dialect/mysql/src/main/java/org/apache/shardingsphere/database/protocol/mysql/packet/command/query/text/MySQLTextResultSetRowPacket.java
index 11fd151632a..2b73862d1ee 100644
--- 
a/database/protocol/dialect/mysql/src/main/java/org/apache/shardingsphere/database/protocol/mysql/packet/command/query/text/MySQLTextResultSetRowPacket.java
+++ 
b/database/protocol/dialect/mysql/src/main/java/org/apache/shardingsphere/database/protocol/mysql/packet/command/query/text/MySQLTextResultSetRowPacket.java
@@ -70,9 +70,27 @@ public final class MySQLTextResultSetRowPacket extends 
MySQLPacket {
         } else if (data instanceof Boolean) {
             payload.writeBytesLenenc((boolean) data ? new byte[]{1} : new 
byte[]{0});
         } else if (data instanceof LocalDateTime) {
-            
payload.writeStringLenenc(DateTimeFormatterFactory.getDatetimeFormatter().format((LocalDateTime)
 data));
+            payload.writeStringLenenc(formatLocalDateTime((LocalDateTime) 
data));
         } else {
             payload.writeStringLenenc(data.toString());
         }
     }
+    
+    private String formatLocalDateTime(final LocalDateTime value) {
+        int nanos = value.getNano();
+        if (0 == nanos) {
+            return 
DateTimeFormatterFactory.getDatetimeFormatter().format(value);
+        }
+        StringBuilder result = new 
StringBuilder(DateTimeFormatterFactory.getDatetimeFormatter().format(value)).append('.');
+        String microsecondsText = String.format("%06d", nanos / 1000);
+        int endIndex = microsecondsText.length();
+        while (endIndex > 0 && '0' == microsecondsText.charAt(endIndex - 1)) {
+            endIndex--;
+        }
+        if (0 == endIndex) {
+            return result.substring(0, result.length() - 1);
+        }
+        result.append(microsecondsText, 0, endIndex);
+        return result.toString();
+    }
 }
diff --git 
a/database/protocol/dialect/mysql/src/test/java/org/apache/shardingsphere/database/protocol/mysql/packet/command/query/text/MySQLTextResultSetRowPacketTest.java
 
b/database/protocol/dialect/mysql/src/test/java/org/apache/shardingsphere/database/protocol/mysql/packet/command/query/text/MySQLTextResultSetRowPacketTest.java
index f36c6e2635d..16ad403cbfc 100644
--- 
a/database/protocol/dialect/mysql/src/test/java/org/apache/shardingsphere/database/protocol/mysql/packet/command/query/text/MySQLTextResultSetRowPacketTest.java
+++ 
b/database/protocol/dialect/mysql/src/test/java/org/apache/shardingsphere/database/protocol/mysql/packet/command/query/text/MySQLTextResultSetRowPacketTest.java
@@ -84,4 +84,12 @@ class MySQLTextResultSetRowPacketTest {
         actual.write(payload);
         
verify(payload).writeStringLenenc(DateTimeFormatter.ofPattern("yyyy-MM-dd 
HH:mm:ss").format(LocalDateTime.parse(localDateTimeStr, 
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"))));
     }
+    
+    @Test
+    void assertLocalDateTimeWithMicros() {
+        LocalDateTime dateTime = LocalDateTime.of(2022, 2, 18, 17, 32, 38, 
123456000);
+        MySQLTextResultSetRowPacket actual = new 
MySQLTextResultSetRowPacket(Collections.singletonList(dateTime));
+        actual.write(payload);
+        verify(payload).writeStringLenenc("2022-02-18 17:32:38.123456");
+    }
 }

Reply via email to