postgres.git / summary / log / commit / refs

commit    165d581f146b09543b832513ee00fead132ba6b1
Author:   Michael Paquier <michael@paquier.xyz>
Date:     Mon Aug 28 04:49:55 2023 +0000
First released in: 17.0

    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
    5b3c5953553b or bcc704b524904.
    
    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


src/backend/utils/adt/datetime.c | 7 +++++++ src/test/regress/expected/interval.out | 9 +++++++++ src/test/regress/sql/interval.sql | 4 ++++ 3 files changed, 20 insertions(+) diff --git a/src/backend/utils/adt/datetime.c b/src/backend/utils/adt/datetime.c index 2a5dddc43f3..178b3f47803 100644 --- a/src/backend/utils/adt/datetime.c +++ b/src/backend/utils/adt/datetime.c @@ -3578,6 +3578,13 @@ DecodeInterval(char **field, int *ftype, int nf, int range, break; case AGO: + + /* + * "ago" is only allowed to appear at the end of the + * interval. + */ + if (i != nf - 1) + return DTERR_BAD_FORMAT; is_before = true; type = uval; break; diff --git a/src/test/regress/expected/interval.out b/src/test/regress/expected/interval.out index 28b71d96817..01d43b58687 100644 --- a/src/test/regress/expected/interval.out +++ b/src/test/regress/expected/interval.out @@ -1787,3 +1787,12 @@ SELECT extract(epoch from interval '1000000000 days'); 86400000000000.000000 (1 row) +-- "ago" can only appear once at the end of an interval. +SELECT INTERVAL '42 days 2 seconds ago ago'; +ERROR: invalid input syntax for type interval: "42 days 2 seconds ago ago" +LINE 1: SELECT INTERVAL '42 days 2 seconds ago ago'; + ^ +SELECT INTERVAL '2 minutes ago 5 days'; +ERROR: invalid input syntax for type interval: "2 minutes ago 5 days" +LINE 1: SELECT INTERVAL '2 minutes ago 5 days'; + ^ diff --git a/src/test/regress/sql/interval.sql b/src/test/regress/sql/interval.sql index 56feda1a3d8..fb1ef304904 100644 --- a/src/test/regress/sql/interval.sql +++ b/src/test/regress/sql/interval.sql @@ -582,3 +582,7 @@ SELECT f1, -- internal overflow test case SELECT extract(epoch from interval '1000000000 days'); + +-- "ago" can only appear once at the end of an interval. +SELECT INTERVAL '42 days 2 seconds ago ago'; +SELECT INTERVAL '2 minutes ago 5 days'; [parent: 9a0ddc39c64b]