From 3f1e529e7897a307ff3431a06b739fa7069d792b Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Mon, 22 Jun 2009 17:54:30 +0000
Subject: [PATCH] Make to_timestamp and friends skip leading spaces before an
 integer field, even when not in FM mode.  This improves compatibility with
 Oracle and with our pre-8.4 behavior, as per bug #4862.

Brendan Jurd

Add a couple of regression test cases for this.  In passing, get rid of the
labeling of the individual test cases; doesn't seem to be good for anything
except causing extra work when inserting a test...

Tom Lane
---
 src/backend/utils/adt/formatting.c     |   9 +-
 src/test/regress/expected/horology.out | 202 ++++++++++++++-----------
 src/test/regress/sql/horology.sql      |  66 ++++----
 3 files changed, 157 insertions(+), 120 deletions(-)

diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c
index 064993bd87b..66e75995fd0 100644
--- a/src/backend/utils/adt/formatting.c
+++ b/src/backend/utils/adt/formatting.c
@@ -1,7 +1,7 @@
 /* -----------------------------------------------------------------------
  * formatting.c
  *
- * $PostgreSQL: pgsql/src/backend/utils/adt/formatting.c,v 1.157 2009/06/11 14:49:03 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/formatting.c,v 1.158 2009/06/22 17:54:30 tgl Exp $
  *
  *
  *	 Portions Copyright (c) 1999-2009, PostgreSQL Global Development Group
@@ -1817,7 +1817,7 @@ from_char_set_int(int *dest, const int value, const FormatNode *node)
  * 'dest'. If 'dest' is NULL, the result is discarded.
  *
  * In fixed-width mode (the node does not have the FM suffix), consume at most
- * 'len' characters.
+ * 'len' characters.  However, any leading whitespace isn't counted in 'len'.
  *
  * We use strtol() to recover the integer value from the source string, in
  * accordance with the given FormatNode.
@@ -1840,6 +1840,11 @@ from_char_parse_int_len(int *dest, char **src, const int len, FormatNode *node)
 	char	   *init = *src;
 	int			used;
 
+	/*
+	 * Skip any whitespace before parsing the integer.
+	 */
+	*src += strspace_len(*src);
+
 	Assert(len <= DCH_MAX_ITEM_SIZ);
 	used = (int) strlcpy(copy, *src, len + 1);
 
diff --git a/src/test/regress/expected/horology.out b/src/test/regress/expected/horology.out
index a32895c6e5f..26d7541b720 100644
--- a/src/test/regress/expected/horology.out
+++ b/src/test/regress/expected/horology.out
@@ -2708,157 +2708,181 @@ RESET DateStyle;
 --
 -- to_timestamp()
 --
-SELECT '' AS to_timestamp_1, to_timestamp('0097/Feb/16 --> 08:14:30', 'YYYY/Mon/DD --> HH:MI:SS');
- to_timestamp_1 |         to_timestamp         
-----------------+------------------------------
-                | Sat Feb 16 08:14:30 0097 PST
+SELECT to_timestamp('0097/Feb/16 --> 08:14:30', 'YYYY/Mon/DD --> HH:MI:SS');
+         to_timestamp         
+------------------------------
+ Sat Feb 16 08:14:30 0097 PST
 (1 row)
 
-SELECT '' AS to_timestamp_2, to_timestamp('97/2/16 8:14:30', 'FMYYYY/FMMM/FMDD FMHH:FMMI:FMSS');
- to_timestamp_2 |         to_timestamp         
-----------------+------------------------------
-                | Sat Feb 16 08:14:30 0097 PST
+SELECT to_timestamp('97/2/16 8:14:30', 'FMYYYY/FMMM/FMDD FMHH:FMMI:FMSS');
+         to_timestamp         
+------------------------------
+ Sat Feb 16 08:14:30 0097 PST
 (1 row)
 
-SELECT '' AS to_timestamp_3, to_timestamp('1985 January 12', 'YYYY FMMonth DD');
- to_timestamp_3 |         to_timestamp         
-----------------+------------------------------
-                | Sat Jan 12 00:00:00 1985 PST
+SELECT to_timestamp('1985 January 12', 'YYYY FMMonth DD');
+         to_timestamp         
+------------------------------
+ Sat Jan 12 00:00:00 1985 PST
 (1 row)
 
-SELECT '' AS to_timestamp_4, to_timestamp('My birthday-> Year: 1976, Month: May, Day: 16',
-                                          '"My birthday-> Year" YYYY, "Month:" FMMonth, "Day:" DD');
- to_timestamp_4 |         to_timestamp         
-----------------+------------------------------
-                | Sun May 16 00:00:00 1976 PDT
+SELECT to_timestamp('My birthday-> Year: 1976, Month: May, Day: 16',
+                    '"My birthday-> Year" YYYY, "Month:" FMMonth, "Day:" DD');
+         to_timestamp         
+------------------------------
+ Sun May 16 00:00:00 1976 PDT
 (1 row)
 
-SELECT '' AS to_timestamp_5, to_timestamp('1,582nd VIII 21', 'Y,YYYth FMRM DD');
- to_timestamp_5 |         to_timestamp         
-----------------+------------------------------
-                | Sat Aug 21 00:00:00 1582 PST
+SELECT to_timestamp('1,582nd VIII 21', 'Y,YYYth FMRM DD');
+         to_timestamp         
+------------------------------
+ Sat Aug 21 00:00:00 1582 PST
 (1 row)
 
-SELECT '' AS to_timestamp_6, to_timestamp('15 "text between quote marks" 98 54 45',
-                                          E'HH24 "\\text between quote marks\\"" YY MI SS');
- to_timestamp_6 |         to_timestamp         
-----------------+------------------------------
-                | Thu Jan 01 15:54:45 1998 PST
+SELECT to_timestamp('15 "text between quote marks" 98 54 45',
+                    E'HH24 "\\text between quote marks\\"" YY MI SS');
+         to_timestamp         
+------------------------------
+ Thu Jan 01 15:54:45 1998 PST
 (1 row)
 
-SELECT '' AS to_timestamp_7, to_timestamp('05121445482000', 'MMDDHH24MISSYYYY');
- to_timestamp_7 |         to_timestamp         
-----------------+------------------------------
-                | Fri May 12 14:45:48 2000 PDT
+SELECT to_timestamp('05121445482000', 'MMDDHH24MISSYYYY');
+         to_timestamp         
+------------------------------
+ Fri May 12 14:45:48 2000 PDT
 (1 row)
 
-SELECT '' AS to_timestamp_8, to_timestamp('2000January09Sunday', 'YYYYFMMonthDDFMDay');
- to_timestamp_8 |         to_timestamp         
-----------------+------------------------------
-                | Sun Jan 09 00:00:00 2000 PST
+SELECT to_timestamp('2000January09Sunday', 'YYYYFMMonthDDFMDay');
+         to_timestamp         
+------------------------------
+ Sun Jan 09 00:00:00 2000 PST
 (1 row)
 
-SELECT '' AS to_timestamp_9, to_timestamp('97/Feb/16', 'YYMonDD');
+SELECT to_timestamp('97/Feb/16', 'YYMonDD');
 ERROR:  invalid value "/Fe" for "Mon"
 DETAIL:  The given value did not match any of the allowed values for this field.
-SELECT '' AS to_timestamp_10, to_timestamp('19971116', 'YYYYMMDD');
- to_timestamp_10 |         to_timestamp         
------------------+------------------------------
-                 | Sun Nov 16 00:00:00 1997 PST
+SELECT to_timestamp('19971116', 'YYYYMMDD');
+         to_timestamp         
+------------------------------
+ Sun Nov 16 00:00:00 1997 PST
 (1 row)
 
-SELECT '' AS to_timestamp_11, to_timestamp('20000-1116', 'YYYY-MMDD');
- to_timestamp_11 |         to_timestamp          
------------------+-------------------------------
-                 | Thu Nov 16 00:00:00 20000 PST
+SELECT to_timestamp('20000-1116', 'YYYY-MMDD');
+         to_timestamp          
+-------------------------------
+ Thu Nov 16 00:00:00 20000 PST
 (1 row)
 
-SELECT '' AS to_timestamp_12, to_timestamp('9-1116', 'Y-MMDD');
- to_timestamp_12 |         to_timestamp         
------------------+------------------------------
-                 | Mon Nov 16 00:00:00 2009 PST
+SELECT to_timestamp('9-1116', 'Y-MMDD');
+         to_timestamp         
+------------------------------
+ Mon Nov 16 00:00:00 2009 PST
 (1 row)
 
-SELECT '' AS to_timestamp_13, to_timestamp('95-1116', 'YY-MMDD');
- to_timestamp_13 |         to_timestamp         
------------------+------------------------------
-                 | Thu Nov 16 00:00:00 1995 PST
+SELECT to_timestamp('95-1116', 'YY-MMDD');
+         to_timestamp         
+------------------------------
+ Thu Nov 16 00:00:00 1995 PST
 (1 row)
 
-SELECT '' AS to_timestamp_14, to_timestamp('995-1116', 'YYY-MMDD');
- to_timestamp_14 |         to_timestamp         
------------------+------------------------------
-                 | Thu Nov 16 00:00:00 1995 PST
+SELECT to_timestamp('995-1116', 'YYY-MMDD');
+         to_timestamp         
+------------------------------
+ Thu Nov 16 00:00:00 1995 PST
 (1 row)
 
-SELECT '' AS to_timestamp_15, to_timestamp('2005426', 'YYYYWWD');
- to_timestamp_15 |         to_timestamp         
------------------+------------------------------
-                 | Sat Oct 15 00:00:00 2005 PDT
+SELECT to_timestamp('2005426', 'YYYYWWD');
+         to_timestamp         
+------------------------------
+ Sat Oct 15 00:00:00 2005 PDT
 (1 row)
 
-SELECT '' AS to_timestamp_16, to_timestamp('2005300', 'YYYYDDD');
- to_timestamp_16 |         to_timestamp         
------------------+------------------------------
-                 | Thu Oct 27 00:00:00 2005 PDT
+SELECT to_timestamp('2005300', 'YYYYDDD');
+         to_timestamp         
+------------------------------
+ Thu Oct 27 00:00:00 2005 PDT
 (1 row)
 
-SELECT '' AS to_timestamp_17, to_timestamp('2005527', 'IYYYIWID');
- to_timestamp_17 |         to_timestamp         
------------------+------------------------------
-                 | Sun Jan 01 00:00:00 2006 PST
+SELECT to_timestamp('2005527', 'IYYYIWID');
+         to_timestamp         
+------------------------------
+ Sun Jan 01 00:00:00 2006 PST
 (1 row)
 
-SELECT '' AS to_timestamp_18, to_timestamp('005527', 'IYYIWID');
- to_timestamp_18 |         to_timestamp         
------------------+------------------------------
-                 | Sun Jan 01 00:00:00 2006 PST
+SELECT to_timestamp('005527', 'IYYIWID');
+         to_timestamp         
+------------------------------
+ Sun Jan 01 00:00:00 2006 PST
 (1 row)
 
-SELECT '' AS to_timestamp_19, to_timestamp('05527', 'IYIWID');
- to_timestamp_19 |         to_timestamp         
------------------+------------------------------
-                 | Sun Jan 01 00:00:00 2006 PST
+SELECT to_timestamp('05527', 'IYIWID');
+         to_timestamp         
+------------------------------
+ Sun Jan 01 00:00:00 2006 PST
+(1 row)
+
+SELECT to_timestamp('5527', 'IIWID');
+         to_timestamp         
+------------------------------
+ Sun Jan 01 00:00:00 2006 PST
 (1 row)
 
-SELECT '' AS to_timestamp_20, to_timestamp('5527', 'IIWID');
- to_timestamp_20 |         to_timestamp         
------------------+------------------------------
-                 | Sun Jan 01 00:00:00 2006 PST
+SELECT to_timestamp('2005364', 'IYYYIDDD');
+         to_timestamp         
+------------------------------
+ Sun Jan 01 00:00:00 2006 PST
+(1 row)
+
+SELECT to_timestamp('20050302', 'YYYYMMDD');
+         to_timestamp         
+------------------------------
+ Wed Mar 02 00:00:00 2005 PST
 (1 row)
 
-SELECT '' AS to_timestamp_21, to_timestamp('2005364', 'IYYYIDDD');
- to_timestamp_21 |         to_timestamp         
------------------+------------------------------
-                 | Sun Jan 01 00:00:00 2006 PST
+SELECT to_timestamp('2005 03 02', 'YYYYMMDD');
+         to_timestamp         
+------------------------------
+ Wed Mar 02 00:00:00 2005 PST
+(1 row)
+
+SELECT to_timestamp(' 2005 03 02', 'YYYYMMDD');
+         to_timestamp         
+------------------------------
+ Wed Mar 02 00:00:00 2005 PST
+(1 row)
+
+SELECT to_timestamp('  20050302', 'YYYYMMDD');
+         to_timestamp         
+------------------------------
+ Wed Mar 02 00:00:00 2005 PST
 (1 row)
 
 --
 -- Check errors for some incorrect usages of to_timestamp()
 --
 -- Mixture of date conventions (ISO week and Gregorian):
-SELECT '' AS to_timestamp_22, to_timestamp('2005527', 'YYYYIWID');
+SELECT to_timestamp('2005527', 'YYYYIWID');
 ERROR:  invalid combination of date conventions
 HINT:  Do not mix Gregorian and ISO week date conventions in a formatting template.
 -- Insufficient characters in the source string:
-SELECT '' AS to_timestamp_23, to_timestamp('19971', 'YYYYMMDD');
+SELECT to_timestamp('19971', 'YYYYMMDD');
 ERROR:  source string too short for "MM" formatting field
 DETAIL:  Field requires 2 characters, but only 1 remain.
 HINT:  If your source string is not fixed-width, try using the "FM" modifier.
 -- Insufficient digit characters for a single node:
-SELECT '' AS to_timestamp_24, to_timestamp('19971)24', 'YYYYMMDD');
+SELECT to_timestamp('19971)24', 'YYYYMMDD');
 ERROR:  invalid value "1)" for "MM"
 DETAIL:  Field requires 2 characters, but only 1 could be parsed.
 HINT:  If your source string is not fixed-width, try using the "FM" modifier.
 -- Value clobbering:
-SELECT '' AS to_timestamp_25, to_timestamp('1997-11-Jan-16', 'YYYY-MM-Mon-DD');
+SELECT to_timestamp('1997-11-Jan-16', 'YYYY-MM-Mon-DD');
 ERROR:  conflicting values for "Mon" field in formatting string
 DETAIL:  This value contradicts a previous setting for the same field type.
 -- Non-numeric input:
-SELECT '' AS to_timestamp_26, to_timestamp('199711xy', 'YYYYMMDD');
+SELECT to_timestamp('199711xy', 'YYYYMMDD');
 ERROR:  invalid value "xy" for "DD"
 DETAIL:  Value must be an integer.
 -- Input that doesn't fit in an int:
-SELECT '' AS to_timestamp_27, to_timestamp('10000000000', 'FMYYYY');
+SELECT to_timestamp('10000000000', 'FMYYYY');
 ERROR:  value for "YYYY" in source string is out of range
 DETAIL:  Value must be in the range -2147483648 to 2147483647.
diff --git a/src/test/regress/sql/horology.sql b/src/test/regress/sql/horology.sql
index ae412bfbc8e..615755e3de0 100644
--- a/src/test/regress/sql/horology.sql
+++ b/src/test/regress/sql/horology.sql
@@ -379,68 +379,76 @@ RESET DateStyle;
 -- to_timestamp()
 --
 
-SELECT '' AS to_timestamp_1, to_timestamp('0097/Feb/16 --> 08:14:30', 'YYYY/Mon/DD --> HH:MI:SS');
+SELECT to_timestamp('0097/Feb/16 --> 08:14:30', 'YYYY/Mon/DD --> HH:MI:SS');
 
-SELECT '' AS to_timestamp_2, to_timestamp('97/2/16 8:14:30', 'FMYYYY/FMMM/FMDD FMHH:FMMI:FMSS');
+SELECT to_timestamp('97/2/16 8:14:30', 'FMYYYY/FMMM/FMDD FMHH:FMMI:FMSS');
 
-SELECT '' AS to_timestamp_3, to_timestamp('1985 January 12', 'YYYY FMMonth DD');
+SELECT to_timestamp('1985 January 12', 'YYYY FMMonth DD');
 
-SELECT '' AS to_timestamp_4, to_timestamp('My birthday-> Year: 1976, Month: May, Day: 16',
-                                          '"My birthday-> Year" YYYY, "Month:" FMMonth, "Day:" DD');
+SELECT to_timestamp('My birthday-> Year: 1976, Month: May, Day: 16',
+                    '"My birthday-> Year" YYYY, "Month:" FMMonth, "Day:" DD');
 
-SELECT '' AS to_timestamp_5, to_timestamp('1,582nd VIII 21', 'Y,YYYth FMRM DD');
+SELECT to_timestamp('1,582nd VIII 21', 'Y,YYYth FMRM DD');
 
-SELECT '' AS to_timestamp_6, to_timestamp('15 "text between quote marks" 98 54 45',
-                                          E'HH24 "\\text between quote marks\\"" YY MI SS');
+SELECT to_timestamp('15 "text between quote marks" 98 54 45',
+                    E'HH24 "\\text between quote marks\\"" YY MI SS');
 
-SELECT '' AS to_timestamp_7, to_timestamp('05121445482000', 'MMDDHH24MISSYYYY');
+SELECT to_timestamp('05121445482000', 'MMDDHH24MISSYYYY');
 
-SELECT '' AS to_timestamp_8, to_timestamp('2000January09Sunday', 'YYYYFMMonthDDFMDay');
+SELECT to_timestamp('2000January09Sunday', 'YYYYFMMonthDDFMDay');
 
-SELECT '' AS to_timestamp_9, to_timestamp('97/Feb/16', 'YYMonDD');
+SELECT to_timestamp('97/Feb/16', 'YYMonDD');
 
-SELECT '' AS to_timestamp_10, to_timestamp('19971116', 'YYYYMMDD');
+SELECT to_timestamp('19971116', 'YYYYMMDD');
 
-SELECT '' AS to_timestamp_11, to_timestamp('20000-1116', 'YYYY-MMDD');
+SELECT to_timestamp('20000-1116', 'YYYY-MMDD');
 
-SELECT '' AS to_timestamp_12, to_timestamp('9-1116', 'Y-MMDD');
+SELECT to_timestamp('9-1116', 'Y-MMDD');
 
-SELECT '' AS to_timestamp_13, to_timestamp('95-1116', 'YY-MMDD');
+SELECT to_timestamp('95-1116', 'YY-MMDD');
 
-SELECT '' AS to_timestamp_14, to_timestamp('995-1116', 'YYY-MMDD');
+SELECT to_timestamp('995-1116', 'YYY-MMDD');
 
-SELECT '' AS to_timestamp_15, to_timestamp('2005426', 'YYYYWWD');
+SELECT to_timestamp('2005426', 'YYYYWWD');
 
-SELECT '' AS to_timestamp_16, to_timestamp('2005300', 'YYYYDDD');
+SELECT to_timestamp('2005300', 'YYYYDDD');
 
-SELECT '' AS to_timestamp_17, to_timestamp('2005527', 'IYYYIWID');
+SELECT to_timestamp('2005527', 'IYYYIWID');
 
-SELECT '' AS to_timestamp_18, to_timestamp('005527', 'IYYIWID');
+SELECT to_timestamp('005527', 'IYYIWID');
 
-SELECT '' AS to_timestamp_19, to_timestamp('05527', 'IYIWID');
+SELECT to_timestamp('05527', 'IYIWID');
 
-SELECT '' AS to_timestamp_20, to_timestamp('5527', 'IIWID');
+SELECT to_timestamp('5527', 'IIWID');
 
-SELECT '' AS to_timestamp_21, to_timestamp('2005364', 'IYYYIDDD');
+SELECT to_timestamp('2005364', 'IYYYIDDD');
+
+SELECT to_timestamp('20050302', 'YYYYMMDD');
+
+SELECT to_timestamp('2005 03 02', 'YYYYMMDD');
+
+SELECT to_timestamp(' 2005 03 02', 'YYYYMMDD');
+
+SELECT to_timestamp('  20050302', 'YYYYMMDD');
 
 --
 -- Check errors for some incorrect usages of to_timestamp()
 --
 
 -- Mixture of date conventions (ISO week and Gregorian):
-SELECT '' AS to_timestamp_22, to_timestamp('2005527', 'YYYYIWID');
+SELECT to_timestamp('2005527', 'YYYYIWID');
 
 -- Insufficient characters in the source string:
-SELECT '' AS to_timestamp_23, to_timestamp('19971', 'YYYYMMDD');
+SELECT to_timestamp('19971', 'YYYYMMDD');
 
 -- Insufficient digit characters for a single node:
-SELECT '' AS to_timestamp_24, to_timestamp('19971)24', 'YYYYMMDD');
+SELECT to_timestamp('19971)24', 'YYYYMMDD');
 
 -- Value clobbering:
-SELECT '' AS to_timestamp_25, to_timestamp('1997-11-Jan-16', 'YYYY-MM-Mon-DD');
+SELECT to_timestamp('1997-11-Jan-16', 'YYYY-MM-Mon-DD');
 
 -- Non-numeric input:
-SELECT '' AS to_timestamp_26, to_timestamp('199711xy', 'YYYYMMDD');
+SELECT to_timestamp('199711xy', 'YYYYMMDD');
 
 -- Input that doesn't fit in an int:
-SELECT '' AS to_timestamp_27, to_timestamp('10000000000', 'FMYYYY');
+SELECT to_timestamp('10000000000', 'FMYYYY');
-- 
GitLab