pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Parameter marker swapping in {fn timestampdiff()}
3+ messages / 2 participants
[nested] [flat]

* Parameter marker swapping in {fn timestampdiff()}
@ 2023-03-26 18:14 Matthew Bellew <matthewb@labkey.com>
  2023-03-26 18:54 ` Re: Parameter marker swapping in {fn timestampdiff()} Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 3+ messages in thread

From: Matthew Bellew @ 2023-03-26 18:14 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org

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

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Parameter marker swapping in {fn timestampdiff()}
  2023-03-26 18:14 Parameter marker swapping in {fn timestampdiff()} Matthew Bellew <matthewb@labkey.com>
@ 2023-03-26 18:54 ` Tom Lane <tgl@sss.pgh.pa.us>
  2023-03-26 22:24   ` Re: Parameter marker swapping in {fn timestampdiff()} Matthew Bellew <matthewb@labkey.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Tom Lane @ 2023-03-26 18:54 UTC (permalink / raw)
  To: Matthew Bellew <matthewb@labkey.com>; +Cc: pgsql-bugs@lists.postgresql.org

Matthew Bellew <matthewb@labkey.com> writes:
> 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.

You probably want to report that on the pgsql-jdbc list.  This list
mainly deals in bugs in core Postgres, so I'm not very sure whether
the JDBC maintainers read it.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Parameter marker swapping in {fn timestampdiff()}
  2023-03-26 18:14 Parameter marker swapping in {fn timestampdiff()} Matthew Bellew <matthewb@labkey.com>
  2023-03-26 18:54 ` Re: Parameter marker swapping in {fn timestampdiff()} Tom Lane <tgl@sss.pgh.pa.us>
@ 2023-03-26 22:24   ` Matthew Bellew <matthewb@labkey.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Matthew Bellew @ 2023-03-26 22:24 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs@lists.postgresql.org

Thanks, reposted.

On Sun, Mar 26, 2023 at 11:54 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:

> Matthew Bellew <matthewb@labkey.com> writes:
> > 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.
>
> You probably want to report that on the pgsql-jdbc list.  This list
> mainly deals in bugs in core Postgres, so I'm not very sure whether
> the JDBC maintainers read it.
>
>                         regards, tom lane
>

^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2023-03-26 22:24 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2023-03-26 18:14 Parameter marker swapping in {fn timestampdiff()} Matthew Bellew <matthewb@labkey.com>
2023-03-26 18:54 ` Tom Lane <tgl@sss.pgh.pa.us>
2023-03-26 22:24   ` Matthew Bellew <matthewb@labkey.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox