From: hutiefang76 (@hutiefang76) <noreply+hutiefang76@github.com>
To: pgjdbc/pgjdbc <noreply+pgjdbc-pgjdbc@github.com>
Subject: [pgjdbc/pgjdbc] PR #4528: fix: retain BC era in legacy date and timestamp arrays
Date: Thu, 12 Nov 2026 09:18:25 +0000
Message-ID: <gh-pgjdbc-pgjdbc-4528@github.com> (raw)
A `java.sql.Date` for 501 BC is sent as 501 AD when used in a `date[]`; the scalar path keeps the era. The same loss affects `java.sql.Timestamp` arrays. Restore the missing BC marker while keeping the original element text, including fractional seconds and existing session-time-zone interpretation.
Fixes #4493. The `java.time` work in #4487 remains separate.
### All Submissions:
* [x] Have you followed the guidelines in our [Contributing](https://github.com/pgjdbc/pgjdbc/blob/master/CONTRIBUTING.md) document?
* [x] Have you checked to ensure there aren't other open [Pull Requests](../../pulls) for the same update/change?
### Changes to Existing Features:
* [ ] Does this break existing behaviour? If so please explain.
BC array elements now retain their intended era. AD text, timestamp fractions, `java.sql.Time` rendering and the existing `timestamptz[]` session-zone interpretation are preserved. Encoder signatures and scalar setters are unchanged.
* [x] Have you added an explanation of what your changes do and why you'd like us to include them?
* [x] Have you written new tests for your core changes, as applicable?
* [x] Have you successfully run tests with your changes locally?
Validation uses the native Gradle test task with `SqlDateArrayEraTest`, the JDBC2/JDBC4 `ArrayTest` classes, `DateTest`, `TimeTest` and `TimestampTest`:
| Test JVM | PostgreSQL | Tests | Passed | Existing conditional skips | Failures/errors |
| --- | --- | --- | --- | --- | --- |
| 21.0.10 | 17.11 | 353 | 339 | 14 | 0 |
| 1.8.0_492 | 13.23 | 353 | 339 | 14 | 0 |
The 48 new cases all pass without skips in both environments. On the original production source, the initial 32-case regression/control set reports 16 BC assertion failures and 16 passing controls, with no errors or skips. The cases cover two JVM time zones, both binary-mode settings, null/nested arrays, mixed objects, a pre-existing BC marker, and session-zone behavior.
`./gradlew styleCheck` and `./gradlew -PenableErrorprone -PenableCheckerframework :postgresql:classes` pass. The affected production classes retain Java 8 bytecode. The full test suite was not run.
AI assistance: DeepSeek assisted implementation, regression tests and first-pass review. Codex reviewed and corrected the patch and tests, and ran the native validation.
inline patch 1: CHANGELOG.md, pgjdbc/src/main/java/org/postgresql/jdbc/ArrayEncoding.java, pgjdbc/src/test/java/org/postgresql/test/jdbc2/SqlDateArrayEraTest.java (+352/-1 lines) download inline-1.patchdiff --git a/CHANGELOG.md b/CHANGELOG.mdindex a85ff9edcf..1505585139 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
## [Unreleased]
### Fixed
+* fix: preserve the BC era of `java.sql.Date` and `java.sql.Timestamp` array elements while retaining their existing AD text and time zone behavior [Issue #4493](https://github.com/pgjdbc/pgjdbc/issues/4493)
* fix: `GSSInputStream.available()` now returns the number of decrypted bytes the stream holds and has not returned yet, instead of 0. Under GSS encryption, `PGConnection.getNotifications()` and non-blocking `COPY` reads no longer set a 1 ms socket timeout to probe for data while the stream still holds decrypted bytes [PR #4383](https://github.com/pgjdbc/pgjdbc/pull/4383)
* fix: `PGStream.skip` no longer loops forever when the connection ends while the driver is discarding bytes, and no longer mistakes a stream that skips nothing for one that has ended. `InputStream.skip` may return zero with more data still coming, which a stream supplied through the `socketFactory` property is free to do; the driver now reads a byte in that case and treats only `-1` as the end [PR #4358](https://github.com/pgjdbc/pgjdbc/pull/4358)
* fix: `LargeObjectManager` operations now work inside an active XA transaction. Since 42.7.13 an XA branch keeps the caller's `autoCommit=true`, and the guard rejected large objects as if the connection were idle. It now checks the server transaction state (`getTransactionState()`) instead of `autoCommit`, so a genuine auto-commit connection with no open transaction is still refused [Issue #4309](https://github.com/pgjdbc/pgjdbc/issues/4309) [PR #4310](https://github.com/pgjdbc/pgjdbc/pull/4310)
diff --git a/pgjdbc/src/main/java/org/postgresql/jdbc/ArrayEncoding.java b/pgjdbc/src/main/java/org/postgresql/jdbc/ArrayEncoding.javaindex 01d3ca3592..018d9b320c 100644--- a/pgjdbc/src/main/java/org/postgresql/jdbc/ArrayEncoding.java+++ b/pgjdbc/src/main/java/org/postgresql/jdbc/ArrayEncoding.java@@ -15,13 +15,17 @@
import org.postgresql.util.PSQLState;
import org.checkerframework.checker.index.qual.Positive;
+import org.checkerframework.checker.nullness.qual.Nullable;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Array;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
+import java.util.Calendar;+import java.util.GregorianCalendar;
import java.util.HashMap;
+import java.util.Locale;
import java.util.Map;
/**
@@ -976,6 +980,7 @@ byte[] toSingleDimensionBinaryRepresentation(BaseConnection connection, Object[]
@Override
public void appendArray(StringBuilder sb, char delim, Object[] array) {
sb.append('{');
+ @Nullable GregorianCalendar calendar = null;
for (int i = 0; i < array.length; i++) {
if (i > 0) {
sb.append(delim);
@@ -993,7 +998,18 @@ public void appendArray(StringBuilder sb, char delim, Object[] array) {
throw new IllegalStateException(e);
}
} else {
- PgArray.escapeArrayElement(sb, array[i].toString());+ String element = array[i].toString();+ if (array[i] instanceof java.sql.Date || array[i] instanceof java.sql.Timestamp) {+ if (calendar == null) {+ // Match the JVM default time zone used by the element's toString().+ calendar = new GregorianCalendar(Locale.ROOT);+ }+ calendar.setTime((java.util.Date) array[i]);+ if (calendar.get(Calendar.ERA) == GregorianCalendar.BC && !element.endsWith(" BC")) {+ element += " BC";+ }+ }+ PgArray.escapeArrayElement(sb, element);
}
}
sb.append('}');
diff --git a/pgjdbc/src/test/java/org/postgresql/test/jdbc2/SqlDateArrayEraTest.java b/pgjdbc/src/test/java/org/postgresql/test/jdbc2/SqlDateArrayEraTest.javanew file mode 100644index 0000000000..f9e6e1280c--- /dev/null+++ b/pgjdbc/src/test/java/org/postgresql/test/jdbc2/SqlDateArrayEraTest.java@@ -0,0 +1,334 @@+/*+ * Copyright (c) 2004, PostgreSQL Global Development Group+ * See the LICENSE file in the project root for more information.+ */++package org.postgresql.test.jdbc2;++import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertTrue;++import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.parallel.Isolated;+import org.junit.jupiter.params.ParameterizedClass;+import org.junit.jupiter.params.provider.MethodSource;++import java.sql.Array;+import java.sql.Date;+import java.sql.PreparedStatement;+import java.sql.ResultSet;+import java.sql.SQLException;+import java.sql.Time;+import java.sql.Timestamp;+import java.time.LocalDate;+import java.time.LocalDateTime;+import java.util.ArrayList;+import java.util.Arrays;+import java.util.Collection;+import java.util.List;+import java.util.TimeZone;++/**+ * Regression coverage for era (BC) preservation of java.sql.Date / java.sql.Timestamp elements when+ * bound as array parameters via {@link java.sql.Connection#createArrayOf(String, Object[])}.+ *+ * <p>Array elements are expected to keep the same era marker as the driver's own scalar+ * {@code setObject} path produces for the same value. Controls (scalar paths, AD values, text[] of+ * java.sql values, java.sql.Time rendering) must pass regardless of any array-era change.</p>+ */+@ParameterizedClass+@MethodSource("data")+@Isolated("Uses TimeZone.setDefault")+public class SqlDateArrayEraTest extends BaseTest4 {++ private static final String SESSION_ZONE = "Europe/Paris";++ private final String zoneId;++ private TimeZone savedDefaultZone;++ public SqlDateArrayEraTest(String zoneId, BinaryMode binaryMode) {+ this.zoneId = zoneId;+ setBinaryMode(binaryMode);+ }++ public static Iterable<Object[]> data() {+ Collection<Object[]> ids = new ArrayList<>();+ for (String zoneId : Arrays.asList("UTC", "America/Los_Angeles")) {+ for (BinaryMode binaryMode : BinaryMode.values()) {+ ids.add(new Object[]{zoneId, binaryMode});+ }+ }+ return ids;+ }++ @Override+ protected void setUp() throws Exception {+ savedDefaultZone = TimeZone.getDefault();+ TimeZone.setDefault(TimeZone.getTimeZone(zoneId));+ super.setUp();+ }++ @Override+ public void tearDown() throws SQLException {+ try {+ super.tearDown();+ } finally {+ TimeZone.setDefault(savedDefaultZone);+ }+ }++ @Test+ public void dateArrayKeepsBcEra() throws SQLException {+ Date bc501 = Date.valueOf(LocalDate.of(-500, 1, 1));+ Date bc1 = Date.valueOf(LocalDate.of(0, 1, 1));+ Date ad1 = Date.valueOf(LocalDate.of(1, 1, 1));+ Date ad2024 = Date.valueOf(LocalDate.of(2024, 2, 29));++ List<String> values = arrayElements("date", "date[]", new Date[]{bc501, null, bc1, ad1, ad2024});++ // Real scalar SQL result text establishes the expected era rendering.+ assertEquals("0501-01-01 BC", scalarText("date", bc501));+ assertEquals("0001-01-01 BC", scalarText("date", bc1));+ assertEquals(+ Arrays.asList("0501-01-01 BC", null, "0001-01-01 BC", "0001-01-01", "2024-02-29"),+ values);+ }++ @Test+ public void timestampArrayKeepsBcEraAndFractions() throws SQLException {+ Timestamp bc501 = Timestamp.valueOf(LocalDateTime.of(-500, 1, 1, 0, 0));+ Timestamp bcFrac =+ Timestamp.valueOf(LocalDateTime.of(-1, 12, 31, 23, 59, 59, 123_456_000));+ Timestamp bc1 = Timestamp.valueOf(LocalDateTime.of(0, 1, 1, 12, 34, 56, 500_000_000));+ Timestamp ad1 = Timestamp.valueOf(LocalDateTime.of(1, 1, 1, 0, 0, 0, 0));+ Timestamp ad2024 = Timestamp.valueOf(LocalDateTime.of(2024, 2, 29, 1, 2, 3, 7_000_000));++ List<String> values = arrayElements("timestamp", "timestamp[]",+ new Timestamp[]{bc501, null, bcFrac, bc1, ad1, ad2024});++ assertEquals("0501-01-01 00:00:00 BC", scalarText("timestamp", bc501));+ assertEquals("0002-12-31 23:59:59.123456 BC", scalarText("timestamp", bcFrac));+ assertEquals(+ Arrays.asList(+ "0501-01-01 00:00:00 BC",+ null,+ "0002-12-31 23:59:59.123456 BC",+ "0001-01-01 12:34:56.5 BC",+ "0001-01-01 00:00:00",+ "2024-02-29 01:02:03.007"),+ values);+ }++ @Test+ public void nestedDateArraysKeepBcEraWhenUnnested() throws SQLException {+ Date bc501 = Date.valueOf(LocalDate.of(-500, 1, 1));+ Date bc1 = Date.valueOf(LocalDate.of(0, 6, 15));+ Date ad2024 = Date.valueOf(LocalDate.of(2024, 2, 29));++ Date[][] nested = new Date[][]{{bc501, null}, {bc1, ad2024}};++ Array sqlArray = null;+ try {+ sqlArray = con.createArrayOf("date", nested);+ List<String> rows = new ArrayList<>();+ try (PreparedStatement ps = con.prepareStatement(+ "SELECT unnest(CAST(? AS date[][]))::text")) {+ ps.setArray(1, sqlArray);+ try (ResultSet rs = ps.executeQuery()) {+ while (rs.next()) {+ rows.add(rs.getString(1));+ }+ }+ }+ assertEquals(Arrays.asList("0501-01-01 BC", null, "0001-06-15 BC", "2024-02-29"), rows);+ } finally {+ if (sqlArray != null) {+ sqlArray.free();+ }+ }+ }++ @Test+ public void objectArrayOfDatesKeepsBcEraWhenUnnested() throws SQLException {+ Date bc1 = Date.valueOf(LocalDate.of(0, 1, 1));+ Date adDate = Date.valueOf(LocalDate.of(2024, 2, 29));++ List<String> values =+ arrayElements("date", "date[]", new Object[]{bc1, null, adDate, "2024-02-29"});++ assertEquals(+ Arrays.asList("0001-01-01 BC", null, "2024-02-29", "2024-02-29"), values);+ }++ @Test+ public void scalarDateAndTimestampKeepBcEra() throws SQLException {+ Date date = Date.valueOf(LocalDate.of(-500, 1, 1));+ Timestamp ts = Timestamp.valueOf(LocalDateTime.of(-500, 1, 1, 0, 0));++ assertEquals("0501-01-01 BC", scalarText("date", date));+ assertEquals("0501-01-01 00:00:00 BC", scalarText("timestamp", ts));+ }++ @Test+ public void adTimestampArrayBoundAsTimestamptzComparesInstant() throws SQLException {+ Timestamp ad = Timestamp.valueOf(LocalDateTime.of(2024, 3, 10, 12, 0, 0, 0));++ execute("SET TIME ZONE '" + SESSION_ZONE + "'");++ Array sqlArray = null;+ try {+ sqlArray = con.createArrayOf("timestamptz", new Timestamp[]{ad});+ try (PreparedStatement ps = con.prepareStatement(+ "SELECT (CAST(? AS timestamptz[]))[1] = TIMESTAMPTZ '2024-03-10 12:00:00 Europe/Paris'")) {+ ps.setArray(1, sqlArray);+ try (ResultSet rs = ps.executeQuery()) {+ assertTrue(rs.next());+ assertTrue(rs.getBoolean(1));+ }+ }+ } finally {+ if (sqlArray != null) {+ sqlArray.free();+ }+ }+ }++ @Test+ public void textArrayOfAdDateAndTimestampRetainsValues() throws SQLException {+ Date adDate = Date.valueOf(LocalDate.of(2024, 2, 29));+ Timestamp adTs = Timestamp.valueOf(LocalDateTime.of(2024, 2, 29, 1, 2, 3, 123_456_789));++ List<String> values = textElements(new Object[]{adDate, adTs});++ assertEquals(+ Arrays.asList("2024-02-29", "2024-02-29 01:02:03.123456789"), values);+ }++ @Test+ public void textArrayOfTimeRetainsSecondOnlyRendering() throws SQLException {+ Time time = new Time(Time.valueOf("12:34:56").getTime() + 4);++ List<String> values = textElements(new Time[]{time});++ assertEquals(Arrays.asList("12:34:56"), values);+ }++ @Test+ public void dateSubclassWithBcMarkerIsNotDuplicated() throws SQLException {+ final long millis = Date.valueOf(LocalDate.of(-500, 1, 1)).getTime();+ Date bcDate = new Date(millis) {+ @Override+ public String toString() {+ return "0501-01-01 BC";+ }+ };++ List<String> values = arrayElements("date", "date[]", new Date[]{bcDate});++ assertEquals(Arrays.asList("0501-01-01 BC"), values);+ }++ @Test+ public void bcTimestampArrayUsesSessionTimeZone() throws SQLException {+ Timestamp bcTs = Timestamp.valueOf(LocalDateTime.of(-500, 1, 1, 12, 34, 56, 123_456_000));++ execute("SET TIME ZONE '" + SESSION_ZONE + "'");++ Array sqlArray = null;+ try {+ sqlArray = con.createArrayOf("timestamptz", new Timestamp[]{bcTs});+ try (PreparedStatement ps = con.prepareStatement(+ "SELECT (CAST(? AS timestamptz[]))[1] = "+ + "(TIMESTAMP '0501-01-01 12:34:56.123456 BC' AT TIME ZONE '" + SESSION_ZONE+ + "')")) {+ ps.setArray(1, sqlArray);+ try (ResultSet rs = ps.executeQuery()) {+ assertTrue(rs.next());+ assertTrue(rs.getBoolean(1));+ }+ }+ } finally {+ if (sqlArray != null) {+ sqlArray.free();+ }+ }+ }++ @Test+ public void timeArrayKeepsSecondOnlyRendering() throws SQLException {+ Time time = new Time(Time.valueOf("12:34:56").getTime() + 4);++ List<String> values = arrayElements("time", "time[]", new Time[]{time});++ assertEquals(Arrays.asList("12:34:56"), values);+ }++ @Test+ public void scalarTimestampValueWithBcEraRendersFraction() throws SQLException {+ Timestamp bc1 = Timestamp.valueOf(LocalDateTime.of(0, 1, 1, 12, 34, 56, 500_000_000));++ assertEquals("0001-01-01 12:34:56.5 BC", scalarText("timestamp", bc1));+ }++ private void execute(String sql) throws SQLException {+ try (java.sql.Statement st = con.createStatement()) {+ st.execute(sql);+ }+ }++ private String scalarText(String type, Object value) throws SQLException {+ try (PreparedStatement ps = con.prepareStatement("SELECT CAST(? AS " + type + ")::text")) {+ ps.setObject(1, value);+ try (ResultSet rs = ps.executeQuery()) {+ assertTrue(rs.next());+ return rs.getString(1);+ }+ }+ }++ private List<String> arrayElements(String arrayType, String castType, Object[] value)+ throws SQLException {+ Array sqlArray = null;+ try {+ sqlArray = con.createArrayOf(arrayType, value);+ List<String> result = new ArrayList<>();+ try (PreparedStatement ps = con.prepareStatement(+ "SELECT unnest(CAST(? AS " + castType + "))::text")) {+ ps.setArray(1, sqlArray);+ try (ResultSet rs = ps.executeQuery()) {+ while (rs.next()) {+ result.add(rs.getString(1));+ }+ }+ }+ return result;+ } finally {+ if (sqlArray != null) {+ sqlArray.free();+ }+ }+ }++ private List<String> textElements(Object[] value) throws SQLException {+ Array sqlArray = null;+ try {+ sqlArray = con.createArrayOf("text", value);+ List<String> result = new ArrayList<>();+ try (PreparedStatement ps = con.prepareStatement(+ "SELECT unnest(CAST(? AS text[]))")) {+ ps.setArray(1, sqlArray);+ try (ResultSet rs = ps.executeQuery()) {+ while (rs.next()) {+ result.add(rs.getString(1));+ }+ }+ }+ return result;+ } finally {+ if (sqlArray != null) {+ sqlArray.free();+ }+ }+ }+}