Message-ID: From: "jerrinot (@jerrinot)" To: "pgjdbc/pgjdbc" Date: Mon, 17 Mar 2025 11:15:14 +0000 Subject: [pgjdbc/pgjdbc] issue #3567: Surprising behaviour with jagged arrays List-Id: X-GitHub-Author-Id: 158619 X-GitHub-Author-Login: jerrinot X-GitHub-Issue: 3567 X-GitHub-Repo: pgjdbc/pgjdbc X-GitHub-State: open X-GitHub-Type: issue X-GitHub-Url: https://github.com/pgjdbc/pgjdbc/issues/3567 Content-Type: text/plain; charset=utf-8 **Describe the issue** It appears codecs for 2D primitive arrays implicitly assume rectangular arrays. This leads to surprising behaviour when jagged arrays are used. In some cases, there are hard-to-interpret errors. In other instances, arrays are silently transformed into a regular shape. **Driver Version?** 42.7.5 **Java Version?** JDK 23 **OS Version?** Linux **PostgreSQL Version?** 16.4 **To Reproduce** TestContainers reproducer: ```java package info.jerrinot.sanbox.pgjdbc; import org.junit.Test; import java.sql.*; public class JaggedArraysTest { private static final String JDBC_URL = "jdbc:tc:postgresql:16.4:///postgres"; @Test public void testJaggedArrayBadMessage() throws SQLException { // this creates a bad binding message, server can detect it and throw an error // still, the error is not very informative and hard to tell what's wrong: // ERROR: insufficient data left in message // Where: unnamed portal parameter $1 try (Connection conn = DriverManager.getConnection(JDBC_URL); PreparedStatement stmt = conn.prepareStatement("select ? ")) { stmt.setArray(1, conn.createArrayOf("int8", new long[][]{{1L, 2L}, {3L}, {4L}})); try (ResultSet rs = stmt.executeQuery()) { } } } @Test public void testJaggedArraysMetamorphosis() throws SQLException { // changes shape of the array, creates a rectangular array from a jagged array // there is no error, no warning. with binary encoding the server has no way // to tell anything is wrong. try (Connection conn = DriverManager.getConnection(JDBC_URL); PreparedStatement stmt = conn.prepareStatement("select ? ")) { stmt.setArray(1, conn.createArrayOf("int8", new long[][]{ {1L, 2L}, {3L}, {4L, 5L, 6L} })); try (ResultSet rs = stmt.executeQuery()) { assert rs.next(); Array array = rs.getArray(1); Long[][] items = (Long[][]) array.getArray(); printArray(items); } } } private static void printArray(Long[][] items) { for (Long[] row : items) { for (Long item : row) { System.out.print(item + " "); } System.out.println(); } } } ``` deps: ```xml org.testcontainers postgresql 1.20.6 test junit junit 4.13.2 test org.postgresql postgresql 42.7.5 junit junit 4.13.2 test ch.qos.logback logback-classic 1.5.11 ``` logback.xml ``` %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n ``` **Expected behaviour** I'd expect client to validate the array is not jagged.