Skip to content

Commit 0f0f9c1

Browse files
committed
Fix PostgreSQL proxy timestamp text output adding trailing .0
Fixes #37437
1 parent 605db75 commit 0f0f9c1

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

database/protocol/dialect/postgresql/src/main/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacket.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
import java.nio.charset.StandardCharsets;
3131
import java.sql.SQLException;
3232
import java.sql.SQLXML;
33+
import java.sql.Timestamp;
34+
import java.time.LocalDateTime;
35+
import java.time.format.DateTimeFormatter;
3336
import java.util.Collection;
3437

3538
/**
@@ -79,13 +82,34 @@ private void writeTextValue(final PostgreSQLPacketPayload payload, final Object
7982
byte[] columnData = ((Boolean) each ? "t" : "f").getBytes(payload.getCharset());
8083
payload.writeInt4(columnData.length);
8184
payload.writeBytes(columnData);
85+
} else if (each instanceof Timestamp) {
86+
byte[] columnData = formatTimestamp((Timestamp) each).getBytes(payload.getCharset());
87+
payload.writeInt4(columnData.length);
88+
payload.writeBytes(columnData);
8289
} else {
8390
byte[] columnData = each.toString().getBytes(payload.getCharset());
8491
payload.writeInt4(columnData.length);
8592
payload.writeBytes(columnData);
8693
}
8794
}
8895

96+
private String formatTimestamp(final Timestamp timestamp) {
97+
LocalDateTime dateTime = timestamp.toLocalDateTime();
98+
int nanos = dateTime.getNano();
99+
if (0 == nanos) {
100+
return dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
101+
}
102+
int micros = nanos / 1000;
103+
StringBuilder result = new StringBuilder(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.")));
104+
result.append(String.format("%06d", micros));
105+
int length = result.length();
106+
while (result.charAt(length - 1) == '0') {
107+
length--;
108+
}
109+
result.setLength(length);
110+
return result.toString();
111+
}
112+
89113
private byte[] encodeByteaText(final byte[] value) {
90114
byte[] result = new byte[value.length * 2 + 2];
91115
result[0] = '\\';

database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacketTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.nio.charset.StandardCharsets;
3434
import java.sql.SQLException;
3535
import java.sql.SQLXML;
36+
import java.sql.Timestamp;
3637
import java.util.Collections;
3738
import java.util.stream.Stream;
3839

@@ -140,6 +141,10 @@ private static Stream<Arguments> textValueCases() {
140141
return Stream.of(
141142
Arguments.of("boolean_true", true, "t".getBytes(StandardCharsets.UTF_8)),
142143
Arguments.of("boolean_false", false, "f".getBytes(StandardCharsets.UTF_8)),
143-
Arguments.of("string_value", "value", "value".getBytes(StandardCharsets.UTF_8)));
144+
Arguments.of("string_value", "value", "value".getBytes(StandardCharsets.UTF_8)),
145+
Arguments.of("timestamp_no_fractional", Timestamp.valueOf("1973-06-03 10:30:01"), "1973-06-03 10:30:01".getBytes(StandardCharsets.UTF_8)),
146+
Arguments.of("timestamp_with_fractional", Timestamp.valueOf("2024-01-15 14:30:45.123"), "2024-01-15 14:30:45.123".getBytes(StandardCharsets.UTF_8)),
147+
Arguments.of("timestamp_trailing_zeros_stripped", Timestamp.valueOf("2024-01-15 14:30:45.120"), "2024-01-15 14:30:45.12".getBytes(StandardCharsets.UTF_8)),
148+
Arguments.of("timestamp_microsecond_precision", Timestamp.valueOf("2024-01-15 14:30:45.123456"), "2024-01-15 14:30:45.123456".getBytes(StandardCharsets.UTF_8)));
144149
}
145150
}

0 commit comments

Comments
 (0)