From ee9b9ad318719b715caeebd20875692e80ff82dc Mon Sep 17 00:00:00 2001 From: Aleksander Alekseev Date: Fri, 11 Feb 2022 00:21:25 +0300 Subject: [PATCH] time_bucket_ng() bugfix time_bucket_ng() may return wrong results for monthly buckets with overwritten origin. As an example: time_bucket_ng('1 month', '2001-01-01' :: date, origin => '2000-06-01') ... returns 2000-01-01 instead of 2001-01-01. In fact, our tests showed this but we overlooked the fact that the results are incorrect. The reason is that the month of the origin is not accounted for when calculating the resulting year. This patch fixes this. Also it updates linux-32bit-build-and-test.yaml workflow due to todays 14.2/13.6/12.10 releases. "Memoize" test doesn't pass on 14.2 thus the patch adds it to the list of skipped tests. The patch doesn't rename the checks because we list "PG14.1 Debug linux-i386" etc as required checks on GitHub. This will be addressed in the follow-up patches. --- .github/workflows/linux-32bit-build-and-test.yaml | 2 +- src/time_bucket.c | 2 +- test/expected/timestamp.out | 14 +++++++------- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/linux-32bit-build-and-test.yaml b/.github/workflows/linux-32bit-build-and-test.yaml index 3bbafe70f..2455b4b2c 100644 --- a/.github/workflows/linux-32bit-build-and-test.yaml +++ b/.github/workflows/linux-32bit-build-and-test.yaml @@ -29,7 +29,7 @@ jobs: ignores: append-13 debug_notice transparent_decompression-13 transparent_decompress_chunk-13 pg_dump pg_major: 13 - pg: "14.1" - ignores: append-14 debug_notice transparent_decompression-14 transparent_decompress_chunk-14 pg_dump + ignores: append-14 debug_notice transparent_decompression-14 transparent_decompress_chunk-14 pg_dump memoize pg_major: 14 steps: diff --git a/src/time_bucket.c b/src/time_bucket.c index 83b0a034b..593a71548 100644 --- a/src/time_bucket.c +++ b/src/time_bucket.c @@ -438,7 +438,7 @@ ts_time_bucket_ng_date(PG_FUNCTION_ARGS) delta = (year * 12 + month) - (origin_year * 12 + origin_month); bucket_number = delta / interval->month; - year = origin_year + (bucket_number * interval->month) / 12; + year = origin_year + (origin_month - 1 + bucket_number * interval->month) / 12; month = (((origin_year * 12 + (origin_month - 1)) + (bucket_number * interval->month)) % 12) + 1; diff --git a/test/expected/timestamp.out b/test/expected/timestamp.out index b378f49d7..f19d2a486 100644 --- a/test/expected/timestamp.out +++ b/test/expected/timestamp.out @@ -1630,13 +1630,13 @@ FROM generate_series('2020-01-01' :: date, '2020-12-01', '1 month') AS ts, unnest(array[ts :: date]) AS d; d | m1 | m2 | m3 | m4 | m5 ------------+------------+------------+------------+------------+------------ - 2020-01-01 | 2019-01-01 | 2019-01-01 | 2019-11-01 | 2019-01-01 | 2019-10-01 - 2020-02-01 | 2019-02-01 | 2019-01-01 | 2019-02-01 | 2019-01-01 | 2019-10-01 - 2020-03-01 | 2019-03-01 | 2019-03-01 | 2019-02-01 | 2019-01-01 | 2019-03-01 - 2020-04-01 | 2019-04-01 | 2019-03-01 | 2019-02-01 | 2019-01-01 | 2019-03-01 - 2020-05-01 | 2020-05-01 | 2020-05-01 | 2020-05-01 | 2020-05-01 | 2019-03-01 - 2020-06-01 | 2020-06-01 | 2020-05-01 | 2020-05-01 | 2020-05-01 | 2019-03-01 - 2020-07-01 | 2020-07-01 | 2020-07-01 | 2020-05-01 | 2020-05-01 | 2019-03-01 + 2020-01-01 | 2020-01-01 | 2020-01-01 | 2019-11-01 | 2020-01-01 | 2019-10-01 + 2020-02-01 | 2020-02-01 | 2020-01-01 | 2020-02-01 | 2020-01-01 | 2019-10-01 + 2020-03-01 | 2020-03-01 | 2020-03-01 | 2020-02-01 | 2020-01-01 | 2020-03-01 + 2020-04-01 | 2020-04-01 | 2020-03-01 | 2020-02-01 | 2020-01-01 | 2020-03-01 + 2020-05-01 | 2020-05-01 | 2020-05-01 | 2020-05-01 | 2020-05-01 | 2020-03-01 + 2020-06-01 | 2020-06-01 | 2020-05-01 | 2020-05-01 | 2020-05-01 | 2020-03-01 + 2020-07-01 | 2020-07-01 | 2020-07-01 | 2020-05-01 | 2020-05-01 | 2020-03-01 2020-08-01 | 2020-08-01 | 2020-07-01 | 2020-08-01 | 2020-05-01 | 2020-08-01 2020-09-01 | 2020-09-01 | 2020-09-01 | 2020-08-01 | 2020-09-01 | 2020-08-01 2020-10-01 | 2020-10-01 | 2020-09-01 | 2020-08-01 | 2020-09-01 | 2020-08-01