Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pgYln-0004KR-0s for pgsql-jdbc@arkaria.postgresql.org; Sun, 26 Mar 2023 22:23:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pgYlk-0001hs-UE for pgsql-jdbc@arkaria.postgresql.org; Sun, 26 Mar 2023 22:23:20 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pgYlk-0001hi-I9 for pgsql-jdbc@lists.postgresql.org; Sun, 26 Mar 2023 22:23:20 +0000 Received: from mail-pf1-x431.google.com ([2607:f8b0:4864:20::431]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pgYld-0007kC-Iu for pgsql-jdbc@postgresql.org; Sun, 26 Mar 2023 22:23:19 +0000 Received: by mail-pf1-x431.google.com with SMTP id c4so4402280pfl.0 for ; Sun, 26 Mar 2023 15:23:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=labkey.com; s=google; t=1679869392; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=zaYv0eqV4R0DRBmurK7rPVzRAiOAd4LUMdlhZwkVrlU=; b=LFhBKdqThJciKUbkz6qWT7GNoOf3txXsAwF4R5Uj25YXEU4LQEs38OkhqsRL9DXCRs nN0O7NRj3zhUIH4gHrVMJfzAUkBBdx1uxiGjeZLwvd3Coh9qiq9nGATrF5bm9HNf0Itx miCIOX0Ut4YZg4cSfS0NdixXvA4fN6NmVEY7AvzghNgSqgAoK43ptzS/+u0lcyDHeGBC PkbG/aglRgZGD8V/wqFy1KmDfhKKJIq1THUW9ULayZ0KxWLO32jt9yxjy5mGW+WW4wDn QdDJ/Be28f5kTQrgFAPFyb8gIp/HrdqLaGMCrLz0Jg1BPGgHjfHv1MtgdGcnyXoKPzXE MKPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679869392; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=zaYv0eqV4R0DRBmurK7rPVzRAiOAd4LUMdlhZwkVrlU=; b=aAUfHAeaGfntfWaUFc5V3YGI/tss2Dl+kpJXtHARD/CVCH1LXdOeMWC40vF8G0QTZ3 KZs0VAX/enHmnSuJ6Id8u37HixTNrYpNLxQ+EmG+V/pn+okWnNPRXL1egXeqTihaI4ka S2UPofs59aIqNOw40JOkbnF5BrxlTc8dJCSuX/ZX6mGuSEJG4OKXctCNWEcvlPj19Vrd hbJOuYMPudNSfkLwp7eVElf5fVYRDzoRPxXqBpSxkdI79Nnv37GKbo+Gn9OENLaC5RoG oemgcAMs4U2re0Ai7gtgbmbQeO9u2CXYbLGI/TehcptgTSEA0fKoeajukKZ9/qDFrS3n iH6w== X-Gm-Message-State: AAQBX9c32nESDdX857fUTTBle29pOoNPPracTJQdibBXAzEbSonQwvVw 6SQuEfTH13OUFNNMrz8E9KqlZsEB9IwU/7pe5j5kMVVnqPfCWFkchRZm6nuc X-Google-Smtp-Source: AKy350Z74j8qvBcwNLAIjUmBLnbE1uTBjXTHWWadRcOi0OXMP+0FsPz2IyHJcWcCCSHnYtl0jGFmL72KL7OXd3dbq2c= X-Received: by 2002:a05:6a00:1a86:b0:625:563e:7d17 with SMTP id e6-20020a056a001a8600b00625563e7d17mr4677974pfv.0.1679869391957; Sun, 26 Mar 2023 15:23:11 -0700 (PDT) MIME-Version: 1.0 From: Matthew Bellew Date: Sun, 26 Mar 2023 15:23:01 -0700 Message-ID: Subject: Parameter marker swapping in {fn timestampdiff()} To: pgsql-jdbc@postgresql.org Content-Type: multipart/alternative; boundary="000000000000609e9905f7d51217" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000609e9905f7d51217 Content-Type: text/plain; charset="UTF-8" (moved/reposted from pgsql-bugs) version info: Database Product Name PostgreSQL Database Product Version 14.2 JDBC Driver Name PostgreSQL JDBC Driver JDBC Driver Version 42.5.3 I recently made a small change to a core utility and our sql tests flagged some unexpected results. I traced these to timestampdiff(). Below is a running code function (except for creating the JDBC Connection). I expect the same result for all three executeQuery() calls. The version that uses string literals return 366 and the versions that use parameter markers return -366. Output: w/o parameters: 366 w/ parameters: -366 w/ parameters varchar: -366 void testTimestampDiffParameters(Connection conn) throws SQLException { // WITHOUT PARAMETERS try (PreparedStatement stmt = conn.prepareStatement("SELECT {fn TIMESTAMPDIFF(SQL_TSI_DAY, CAST('01 Jan 2000 12:00' AS TIMESTAMP), CAST('01 Jan 2001 12:00' AS TIMESTAMP))")) { try (ResultSet rs = stmt.executeQuery()) { rs.next(); System.out.println("w/o parameters: " + rs.getInt(1)); } } // WITH PARAMETERS try (PreparedStatement stmt = conn.prepareStatement("SELECT {fn TIMESTAMPDIFF(SQL_TSI_DAY, CAST(? AS TIMESTAMP), CAST(? AS TIMESTAMP))")) { stmt.setString(1,"01 Jan 2000 12:00"); stmt.setString(2,"01 Jan 2001 12:00"); try (ResultSet rs = stmt.executeQuery()) { rs.next(); System.out.println("w/ parameters: " + rs.getInt(1)); } } // WITH PARAMETERS try (PreparedStatement stmt = conn.prepareStatement("SELECT {fn TIMESTAMPDIFF(SQL_TSI_DAY, CAST(CAST(? AS VARCHAR(50)) AS TIMESTAMP), CAST(CAST(? AS VARCHAR(50)) AS TIMESTAMP))")) { stmt.setString(1,"01 Jan 2000 12:00"); stmt.setString(2,"01 Jan 2001 12:00"); try (ResultSet rs = stmt.executeQuery()) { rs.next(); System.out.println("w/ parameters varchar: " + rs.getInt(1)); } } } Since the parameters to DateDiff are swapped from {fn timestampdiff()}, the obvious guess would be that the driver is swapping the arguments, but not remapping the JDBC parameter indexes to the new swapped location. Thank you, Matt --000000000000609e9905f7d51217 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
(moved/reposted from pgsql-bugs)

=
version info:
=C2=A0=C2=A0=C2=A0 Datab= ase Product Name PostgreSQL
=C2=A0=C2=A0=C2=A0 Database Product Version = 14.2
=C2=A0=C2=A0=C2=A0 JDBC Driver Name PostgreSQL JDBC Driver
=C2= =A0=C2=A0=C2=A0 JDBC Driver Version 42.5.3

I recently made a small change to a core utility and our sql tests=20 flagged some unexpected results.=C2=A0 I traced these to timestampdiff().= =C2=A0=20 Below is a running code function (except for creating the JDBC=20 Connection).=C2=A0 I expect the same result for all three executeQuery()=20 calls.=C2=A0 The version that uses string literals return 366 and the=20 versions that use parameter markers return -366.

O= utput:
=C2=A0=C2=A0=C2=A0 w/o parameters: 366
=C2=A0=C2=A0=C2= =A0 w/ parameters: -366
=C2=A0=C2=A0=C2=A0 w/ parameters varchar: -366<= /div>

=C2=A0 =C2=A0 void testTimestampDiffParameters(Con= nection conn) throws SQLException
=C2=A0 =C2=A0 {
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 // WITHOUT PARAMETERS
=C2=A0 =C2=A0 =C2=A0 =C2=A0 try (PreparedStatement stmt =3D conn.prepareStatement= ("SELECT {fn=20 TIMESTAMPDIFF(SQL_TSI_DAY, CAST('01 Jan 2000 12:00' AS TIMESTAMP),= =20 CAST('01 Jan 2001 12:00' AS TIMESTAMP))"))
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 try (ResultSet= rs =3D stmt.executeQuery())
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 {=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 rs.next();
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 System.out.println(= "w/o parameters: " + rs.getInt(1));
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }

=C2=A0 =C2=A0 = =C2=A0 =C2=A0 // WITH PARAMETERS
=C2=A0 =C2=A0 =C2=A0 =C2=A0 try (PreparedStatement stmt =3D conn.prepareStatement= ("SELECT {fn=20 TIMESTAMPDIFF(SQL_TSI_DAY, CAST(? AS TIMESTAMP), CAST(? AS=20 TIMESTAMP))"))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 {
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 stmt.setString(1,"01 Jan 2000 12:00");=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 stmt.setString(2,"01 Jan 2= 001 12:00");
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 try (ResultS= et rs =3D stmt.executeQuery())
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 rs.next();=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 System.out.println= ("w/ parameters: " + rs.getInt(1));
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }

=C2=A0 =C2=A0 = =C2=A0 =C2=A0 // WITH PARAMETERS
=C2=A0 =C2=A0 =C2=A0 =C2=A0 try (PreparedStatement stmt =3D conn.prepareStatement= ("SELECT {fn=20 TIMESTAMPDIFF(SQL_TSI_DAY, CAST(CAST(? AS VARCHAR(50)) AS TIMESTAMP),=20 CAST(CAST(? AS VARCHAR(50)) AS TIMESTAMP))"))
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 stmt.setString(1,&quo= t;01 Jan 2000 12:00");
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 st= mt.setString(2,"01 Jan 2001 12:00");
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 try (ResultSet rs =3D stmt.executeQuery())
=C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 rs.next();
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 System.out.println("w/ parameters varchar: " + rs.g= etInt(1));
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 }

Since the parameters to DateDiff are swapped from {fn timestampdiff()}, the=20 obvious guess would be that the driver is swapping the arguments, but=20 not remapping the JDBC parameter indexes to the new swapped location.
=

Thank you,
Matt

--000000000000609e9905f7d51217--