pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Robert Haas <robertmhaas@gmail.com>
To: Postgres-Bugs <pgsql-bugs@postgresql.org>
Subject: surprising to_timestamp behavior
Date: Tue, 29 Oct 2013 11:24:49 -0400
Message-ID: <CA+TgmobO2Jc2meW0tWpcNcX0Q3AoddcfWwKvBD+tp3VNPef_7w@mail.gmail.com> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

It turns out that when you use the to_timestamp function, a space in
the format mask can result in skipping any character at all, even a
digit, in the input string.  Consider this example, where 10 hours are
lost:

rhaas=# select to_timestamp('2013-10-29 10:47:18', 'YYYY-MM-DD  HH24:MI:SS');
      to_timestamp
------------------------
 2013-10-29 00:47:18-04
(1 row)

There's already some code in the relevant function (DCH_from_char) to
skip whitespace characters, but that happens *in addition* to the
mandatory one-character skip.  I suggest that we revamp the logic so
that we *either* skip whitespace *or* skip exactly one character from
the input string - but not both.  That fixes this case, because the
first space in the mask matches the input space, and the second one is
allowed to match nothing instead of being forced to consume the "1"
following the space.  See attached patch.

One problem with this is that it breaks the regression tests.  Right
now, this works:

  SELECT to_timestamp('My birthday-> Year: 1976, Month: May, Day: 16',
                      '"My birthday-> Year" YYYY, "Month:" FMMonth, "Day:" DD');

First, the quoted string matches exactly.  Next, the subsequent space
in the format mask skips the colon in the input.  And now it's trying
to match YYYY against " 1976", and it's happy to ignore that leading
space as part of decoding a numeric field.  With the proposed patch,
we're no longer willing to match the colon in the input to the space
in the format mask, so it errors out, complaining that the colon
doesn't look like a number.  I tend to think that's more correct than
what we're doing now.

The other regression test that fails is:

  SELECT to_timestamp('15 "text between quote marks" 98 54 45',
                      E'HH24 "\\text between quote marks\\"" YY MI SS');

With the patch, this fails with:

ERROR:  invalid value "rk" for "YY"

The problem is basically that we're quite happy to skip quoted text
that doesn't match at all, as long as it's the same length.  And the
format string has extra cruft in it, so they're not, and we end up in
the wrong spot.  Either I'm missing something, or our current behavior
here is nothing short of bizarre.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

to_timestamp_ws.patch (text/x-patch, 793B) download diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index 36353c3..d00b82c 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -2846,13 +2846,18 @@ DCH_from_char(FormatNode *node, char *in, TmFromChar *out) { if (n->type != NODE_TYPE_ACTION) { - s++; - /* Ignore spaces when not in FX (fixed width) mode */ + /* + * Unless we're in fixed-width mode, upon encountering a space + * in the format string, skip 0 or more whitespace characters + * from the input string. + */ if (isspace((unsigned char) n->character) && !fx_mode) { while (*s != '\0' && isspace((unsigned char) *s)) s++; } + else + s++; /* Otherwise, skip exactly one character. */ continue; }

view thread (8+ messages)  latest in thread

Message-ID: <CA+TgmobO2Jc2meW0tWpcNcX0Q3AoddcfWwKvBD+tp3VNPef_7w@mail.gmail.com>
Permalink:  ../CA+TgmobO2Jc2meW0tWpcNcX0Q3AoddcfWwKvBD+tp3VNPef_7w@mail.gmail.com/
Also on:    postgresql.org/message-id/CA+TgmobO2Jc2meW0tWpcNcX0Q3AoddcfWwKvBD+tp3VNPef_7w@mail.gmail.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: robertmhaas@gmail.com
  Subject: Re: surprising to_timestamp behavior
  In-Reply-To: <CA+TgmobO2Jc2meW0tWpcNcX0Q3AoddcfWwKvBD+tp3VNPef_7w@mail.gmail.com>
  References: <CA+TgmobO2Jc2meW0tWpcNcX0Q3AoddcfWwKvBD+tp3VNPef_7w@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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