Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 165d581

Browse files
committed
Tighten handling of "ago" in interval values
This commit Restrict the unit "ago" to only appear at the end of the interval. According to the documentation, a direction can only be defined at the end of an interval, but it was possible to define it in the middle of the string or define it multiple times. In spirit, this is similar to the error handling improvements done in 5b3c595 or bcc704b. Author: Joseph Koshakow Reviewed-by: Jacob Champion, Gurjeet Singh, Reid Thompson Discussion: https://postgr.es/m/CAAvxfHd-yNO+XYnUxL=GaNZ1n+eE0V-oE0+-cC1jdjdU0KS3iw@mail.gmail.com
1 parent 9a0ddc3 commit 165d581

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

src/backend/utils/adt/datetime.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3578,6 +3578,13 @@ DecodeInterval(char **field, int *ftype, int nf, int range,
35783578
break;
35793579

35803580
case AGO:
3581+
3582+
/*
3583+
* "ago" is only allowed to appear at the end of the
3584+
* interval.
3585+
*/
3586+
if (i != nf - 1)
3587+
return DTERR_BAD_FORMAT;
35813588
is_before = true;
35823589
type = uval;
35833590
break;

src/test/regress/expected/interval.out

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,3 +1787,12 @@ SELECT extract(epoch from interval '1000000000 days');
17871787
86400000000000.000000
17881788
(1 row)
17891789

1790+
-- "ago" can only appear once at the end of an interval.
1791+
SELECT INTERVAL '42 days 2 seconds ago ago';
1792+
ERROR: invalid input syntax for type interval: "42 days 2 seconds ago ago"
1793+
LINE 1: SELECT INTERVAL '42 days 2 seconds ago ago';
1794+
^
1795+
SELECT INTERVAL '2 minutes ago 5 days';
1796+
ERROR: invalid input syntax for type interval: "2 minutes ago 5 days"
1797+
LINE 1: SELECT INTERVAL '2 minutes ago 5 days';
1798+
^

src/test/regress/sql/interval.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,3 +582,7 @@ SELECT f1,
582582

583583
-- internal overflow test case
584584
SELECT extract(epoch from interval '1000000000 days');
585+
586+
-- "ago" can only appear once at the end of an interval.
587+
SELECT INTERVAL '42 days 2 seconds ago ago';
588+
SELECT INTERVAL '2 minutes ago 5 days';

0 commit comments

Comments
 (0)