Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VbBA9-0007hq-7r for pgsql-bugs@arkaria.postgresql.org; Tue, 29 Oct 2013 15:24:57 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VbBA8-0001Gt-Nt for pgsql-bugs@arkaria.postgresql.org; Tue, 29 Oct 2013 15:24:56 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VbBA7-0001Gn-Rq for pgsql-bugs@postgresql.org; Tue, 29 Oct 2013 15:24:56 +0000 Received: from mail-we0-x236.google.com ([2a00:1450:400c:c03::236]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VbBA4-0004FS-Nf for pgsql-bugs@postgresql.org; Tue, 29 Oct 2013 15:24:55 +0000 Received: by mail-we0-f182.google.com with SMTP id t61so8257074wes.41 for ; Tue, 29 Oct 2013 08:24:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=BCi2oba47Tx7LCZ2NfZEhGKNSD+3nglbMKycsm95i5c=; b=oB7J57ABJedZJS0unhw2ktJmaESHSJAh80C1756AnQMLVp+Zg/3kU+HHjkZltrfG86 GloZHORX9Mmig3PTq8RpmI0/j1Wyh1bHFO5fQiZFUw5m8NwUkK1gHGY0bc7pOKE7j7wk nkwLrE2mbo2Cqz7W6yq75IH4fDOzDmNHUI+dPjIRmC8r9vcZ02Cxp7yZCY14U06HbE/o Nbj2PhN7GsUj716OXnk3Zm9Vvoni9iACpzcMmCY7421+yT5BKVt43GvugPY83iCSuCCn of6MBBK/BRJ5rC64ZwQkK7lapXyuwiZa0ShFG3vduYwFfqf50haBMHDlbJaQf1+I82Ol fvTQ== MIME-Version: 1.0 X-Received: by 10.194.95.72 with SMTP id di8mr221281wjb.74.1383060290492; Tue, 29 Oct 2013 08:24:50 -0700 (PDT) Received: by 10.227.195.73 with HTTP; Tue, 29 Oct 2013 08:24:49 -0700 (PDT) Date: Tue, 29 Oct 2013 11:24:49 -0400 Message-ID: Subject: surprising to_timestamp behavior From: Robert Haas To: Postgres-Bugs Content-Type: multipart/mixed; boundary=047d7beb90a651980304e9e2d1e2 X-Pg-Spam-Score: -2.0 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --047d7beb90a651980304e9e2d1e2 Content-Type: text/plain; charset=ISO-8859-1 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 --047d7beb90a651980304e9e2d1e2 Content-Type: text/x-patch; charset=US-ASCII; name="to_timestamp_ws.patch" Content-Disposition: attachment; filename="to_timestamp_ws.patch" Content-Transfer-Encoding: base64 X-Attachment-Id: f_hnd9o1xk0 ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL3V0aWxzL2FkdC9mb3JtYXR0aW5n LmMgYi9zcmMvYmFja2VuZC91dGlscy9hZHQvZm9ybWF0dGluZy5jCmluZGV4 IDM2MzUzYzMuLmQwMGI4MmMgMTAwNjQ0Ci0tLSBhL3NyYy9iYWNrZW5kL3V0 aWxzL2FkdC9mb3JtYXR0aW5nLmMKKysrIGIvc3JjL2JhY2tlbmQvdXRpbHMv YWR0L2Zvcm1hdHRpbmcuYwpAQCAtMjg0NiwxMyArMjg0NiwxOCBAQCBEQ0hf ZnJvbV9jaGFyKEZvcm1hdE5vZGUgKm5vZGUsIGNoYXIgKmluLCBUbUZyb21D aGFyICpvdXQpCiAJewogCQlpZiAobi0+dHlwZSAhPSBOT0RFX1RZUEVfQUNU SU9OKQogCQl7Ci0JCQlzKys7Ci0JCQkvKiBJZ25vcmUgc3BhY2VzIHdoZW4g bm90IGluIEZYIChmaXhlZCB3aWR0aCkgbW9kZSAqLworCQkJLyoKKwkJCSAq IFVubGVzcyB3ZSdyZSBpbiBmaXhlZC13aWR0aCBtb2RlLCB1cG9uIGVuY291 bnRlcmluZyBhIHNwYWNlCisJCQkgKiBpbiB0aGUgZm9ybWF0IHN0cmluZywg c2tpcCAwIG9yIG1vcmUgd2hpdGVzcGFjZSBjaGFyYWN0ZXJzCisJCQkgKiBm cm9tIHRoZSBpbnB1dCBzdHJpbmcuCisJCQkgKi8KIAkJCWlmIChpc3NwYWNl KCh1bnNpZ25lZCBjaGFyKSBuLT5jaGFyYWN0ZXIpICYmICFmeF9tb2RlKQog CQkJewogCQkJCXdoaWxlICgqcyAhPSAnXDAnICYmIGlzc3BhY2UoKHVuc2ln bmVkIGNoYXIpICpzKSkKIAkJCQkJcysrOwogCQkJfQorCQkJZWxzZQorCQkJ CXMrKzsJCS8qIE90aGVyd2lzZSwgc2tpcCBleGFjdGx5IG9uZSBjaGFyYWN0 ZXIuICovCiAJCQljb250aW51ZTsKIAkJfQogCg== --047d7beb90a651980304e9e2d1e2 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --047d7beb90a651980304e9e2d1e2--