From d17168a500f833ccb4139b9ab4b0ea2a40baa76e Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <ashutosh.bapat@enterprisedb.com>
Date: Fri, 6 Oct 2023 11:58:21 +0530
Subject: [PATCH v27 8/8] Add tests for "invalid preceding or following size in
 window function"

Ashutosh Bapat
---
 src/test/regress/expected/window.out | 55 ++++++++++++++++++++++++++++
 src/test/regress/sql/window.sql      | 55 ++++++++++++++++++++++++++++
 2 files changed, 110 insertions(+)

diff --git a/src/test/regress/expected/window.out b/src/test/regress/expected/window.out
index 6b8c3c3413..2201740c18 100644
--- a/src/test/regress/expected/window.out
+++ b/src/test/regress/expected/window.out
@@ -2424,6 +2424,11 @@ window w as (order by f_time desc range between
   0 | 10:00:00 |           1 |          0
 (12 rows)
 
+select id, f_time, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_time desc range between
+             '-70 min' preceding and '2 hours' following);  -- error, negative offset disallowed
+ERROR:  invalid preceding or following size in window function
 select id, f_time, first_value(id) over w, last_value(id) over w
 from datetimes
 window w as (order by f_time range between
@@ -2484,6 +2489,12 @@ window w as (order by f_time range between
  11 | 21:00:00 |             |           
 (12 rows)
 
+select id, f_time, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_time range between
+             '-infinity'::interval following and
+             'infinity'::interval following);  -- error, negative offset disallowed
+ERROR:  invalid preceding or following size in window function
 select id, f_timetz, first_value(id) over w, last_value(id) over w
 from datetimes
 window w as (order by f_timetz range between
@@ -2524,6 +2535,11 @@ window w as (order by f_timetz desc range between
   0 | 10:00:00+01 |           1 |          0
 (12 rows)
 
+select id, f_timetz, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_timetz desc range between
+             '70 min' preceding and '-2 hours' following);  -- error, negative offset disallowed
+ERROR:  invalid preceding or following size in window function
 select id, f_timetz, first_value(id) over w, last_value(id) over w
 from datetimes
 window w as (order by f_timetz range between
@@ -2584,6 +2600,12 @@ window w as (order by f_timetz range between
  11 | 21:00:00+01 |             |           
 (12 rows)
 
+select id, f_timetz, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_timetz range between
+             'infinity'::interval following and
+             '-infinity'::interval following);  -- error, negative offset disallowed
+ERROR:  invalid preceding or following size in window function
 select id, f_interval, first_value(id) over w, last_value(id) over w
 from datetimes
 window w as (order by f_interval range between
@@ -2624,6 +2646,11 @@ window w as (order by f_interval desc range between
   0 | -infinity  |           0 |          0
 (12 rows)
 
+select id, f_interval, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_interval desc range between
+             '-1 year' preceding and '1 year' following);  -- error, negative offset disallowed
+ERROR:  invalid preceding or following size in window function
 select id, f_interval, first_value(id) over w, last_value(id) over w
 from datetimes
 window w as (order by f_interval range between
@@ -2684,6 +2711,12 @@ window w as (order by f_interval range between
  11 | infinity   |          11 |         11
 (12 rows)
 
+select id, f_interval, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_interval range between
+             '-infinity'::interval following and
+             'infinity'::interval following);  -- error, negative offset disallowed
+ERROR:  invalid preceding or following size in window function
 select id, f_timestamptz, first_value(id) over w, last_value(id) over w
 from datetimes
 window w as (order by f_timestamptz range between
@@ -2724,6 +2757,11 @@ window w as (order by f_timestamptz desc range between
   0 | -infinity                    |           0 |          0
 (12 rows)
 
+select id, f_timestamptz, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_timestamptz desc range between
+             '1 year' preceding and '-1 year' following);  -- error, negative offset disallowed
+ERROR:  invalid preceding or following size in window function
 select id, f_timestamptz, first_value(id) over w, last_value(id) over w
 from datetimes
 window w as (order by f_timestamptz range between
@@ -2784,6 +2822,12 @@ window w as (order by f_timestamptz range between
  11 | infinity                     |          11 |         11
 (12 rows)
 
+select id, f_timestamptz, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_timestamptz range between
+             '-infinity'::interval following and
+             'infinity'::interval following);  -- error, negative offset disallowed
+ERROR:  invalid preceding or following size in window function
 select id, f_timestamp, first_value(id) over w, last_value(id) over w
 from datetimes
 window w as (order by f_timestamp range between
@@ -2824,6 +2868,11 @@ window w as (order by f_timestamp desc range between
   0 | -infinity                |           0 |          0
 (12 rows)
 
+select id, f_timestamp, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_timestamp desc range between
+             '-1 year' preceding and '1 year' following);  -- error, negative offset disallowed
+ERROR:  invalid preceding or following size in window function
 select id, f_timestamp, first_value(id) over w, last_value(id) over w
 from datetimes
 window w as (order by f_timestamp range between
@@ -2884,6 +2933,12 @@ window w as (order by f_timestamp range between
  11 | infinity                 |          11 |         11
 (12 rows)
 
+select id, f_timestamp, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_timestamp range between
+             '-infinity'::interval following and
+             'infinity'::interval following);  -- error, negative offset disallowed
+ERROR:  invalid preceding or following size in window function
 -- RANGE offset PRECEDING/FOLLOWING error cases
 select sum(salary) over (order by enroll_date, salary range between '1 year'::interval preceding and '2 years'::interval following
 	exclude ties), salary, enroll_date from empsalary;
diff --git a/src/test/regress/sql/window.sql b/src/test/regress/sql/window.sql
index c2a4cb8d64..437e948d6c 100644
--- a/src/test/regress/sql/window.sql
+++ b/src/test/regress/sql/window.sql
@@ -696,6 +696,11 @@ from datetimes
 window w as (order by f_time desc range between
              '70 min' preceding and '2 hours' following);
 
+select id, f_time, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_time desc range between
+             '-70 min' preceding and '2 hours' following);  -- error, negative offset disallowed
+
 select id, f_time, first_value(id) over w, last_value(id) over w
 from datetimes
 window w as (order by f_time range between
@@ -711,6 +716,12 @@ from datetimes
 window w as (order by f_time range between
              'infinity'::interval following and 'infinity'::interval following);
 
+select id, f_time, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_time range between
+             '-infinity'::interval following and
+             'infinity'::interval following);  -- error, negative offset disallowed
+
 select id, f_timetz, first_value(id) over w, last_value(id) over w
 from datetimes
 window w as (order by f_timetz range between
@@ -721,6 +732,11 @@ from datetimes
 window w as (order by f_timetz desc range between
              '70 min' preceding and '2 hours' following);
 
+select id, f_timetz, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_timetz desc range between
+             '70 min' preceding and '-2 hours' following);  -- error, negative offset disallowed
+
 select id, f_timetz, first_value(id) over w, last_value(id) over w
 from datetimes
 window w as (order by f_timetz range between
@@ -736,6 +752,12 @@ from datetimes
 window w as (order by f_timetz range between
              'infinity'::interval following and 'infinity'::interval following);
 
+select id, f_timetz, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_timetz range between
+             'infinity'::interval following and
+             '-infinity'::interval following);  -- error, negative offset disallowed
+
 select id, f_interval, first_value(id) over w, last_value(id) over w
 from datetimes
 window w as (order by f_interval range between
@@ -746,6 +768,11 @@ from datetimes
 window w as (order by f_interval desc range between
              '1 year' preceding and '1 year' following);
 
+select id, f_interval, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_interval desc range between
+             '-1 year' preceding and '1 year' following);  -- error, negative offset disallowed
+
 select id, f_interval, first_value(id) over w, last_value(id) over w
 from datetimes
 window w as (order by f_interval range between
@@ -761,6 +788,12 @@ from datetimes
 window w as (order by f_interval range between
              'infinity'::interval following and 'infinity'::interval following);
 
+select id, f_interval, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_interval range between
+             '-infinity'::interval following and
+             'infinity'::interval following);  -- error, negative offset disallowed
+
 select id, f_timestamptz, first_value(id) over w, last_value(id) over w
 from datetimes
 window w as (order by f_timestamptz range between
@@ -771,6 +804,11 @@ from datetimes
 window w as (order by f_timestamptz desc range between
              '1 year' preceding and '1 year' following);
 
+select id, f_timestamptz, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_timestamptz desc range between
+             '1 year' preceding and '-1 year' following);  -- error, negative offset disallowed
+
 select id, f_timestamptz, first_value(id) over w, last_value(id) over w
 from datetimes
 window w as (order by f_timestamptz range between
@@ -786,6 +824,12 @@ from datetimes
 window w as (order by f_timestamptz range between
              'infinity'::interval following and 'infinity'::interval following);
 
+select id, f_timestamptz, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_timestamptz range between
+             '-infinity'::interval following and
+             'infinity'::interval following);  -- error, negative offset disallowed
+
 select id, f_timestamp, first_value(id) over w, last_value(id) over w
 from datetimes
 window w as (order by f_timestamp range between
@@ -796,6 +840,11 @@ from datetimes
 window w as (order by f_timestamp desc range between
              '1 year' preceding and '1 year' following);
 
+select id, f_timestamp, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_timestamp desc range between
+             '-1 year' preceding and '1 year' following);  -- error, negative offset disallowed
+
 select id, f_timestamp, first_value(id) over w, last_value(id) over w
 from datetimes
 window w as (order by f_timestamp range between
@@ -811,6 +860,12 @@ from datetimes
 window w as (order by f_timestamp range between
              'infinity'::interval following and 'infinity'::interval following);
 
+select id, f_timestamp, first_value(id) over w, last_value(id) over w
+from datetimes
+window w as (order by f_timestamp range between
+             '-infinity'::interval following and
+             'infinity'::interval following);  -- error, negative offset disallowed
+
 -- RANGE offset PRECEDING/FOLLOWING error cases
 select sum(salary) over (order by enroll_date, salary range between '1 year'::interval preceding and '2 years'::interval following
 	exclude ties), salary, enroll_date from empsalary;
-- 
2.35.3

