Dodge a compiler bug affecting timetz_zone/timetz_izone.
authorTom Lane <[email protected]>
Tue, 17 Oct 2023 17:10:32 +0000 (13:10 -0400)
committerTom Lane <[email protected]>
Tue, 17 Oct 2023 17:10:35 +0000 (13:10 -0400)
Use a modulo operator instead of implementing the same behavior
with a loop.  The loop solution is doubtless microscopically
faster for the typical case of only wrapping into the very next
day, but maybe not so much for large interval values.  In any
case, timetz is such a backwater that it's doubtful anybody
would notice any performance change anyway.

This avoids a compiler bug occurring in AIX's xlc, even in pretty
late-model revisions.

We did not have test coverage for the case where the initial
result->time value is negative, so add that.

For the moment, install this only in HEAD.  My plan is to
back-patch the test case, and then the code change assuming that
buildfarm testing proves the bug occurs in the back branches.
(That seems pretty likely, but let's find out for sure.)

Per buildfarm results from commits 97957fdba and 2f0472030.
Thanks to Michael Paquier for the idea to use a modulo operation
to replace the faulty loop.

Discussion: https://postgr.es/m/CA+hUKGK=DOC+hE-62FKfZy=Ybt5uLkrg3zCZD-jFykM-iPn8yw@mail.gmail.com

src/backend/utils/adt/date.c
src/test/regress/expected/timetz.out
src/test/regress/sql/timetz.sql

index c4da10d47a9f0be70e197ffe60816d61487c8f28..56c7746c11fcf283ef1e06dd9b4894be1592a148 100644 (file)
@@ -3083,10 +3083,11 @@ timetz_zone(PG_FUNCTION_ARGS)
    result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
 
    result->time = t->time + (t->zone - tz) * USECS_PER_SEC;
+   /* C99 modulo has the wrong sign convention for negative input */
    while (result->time < INT64CONST(0))
        result->time += USECS_PER_DAY;
-   while (result->time >= USECS_PER_DAY)
-       result->time -= USECS_PER_DAY;
+   if (result->time >= USECS_PER_DAY)
+       result->time %= USECS_PER_DAY;
 
    result->zone = tz;
 
@@ -3116,10 +3117,11 @@ timetz_izone(PG_FUNCTION_ARGS)
    result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
 
    result->time = time->time + (time->zone - tz) * USECS_PER_SEC;
+   /* C99 modulo has the wrong sign convention for negative input */
    while (result->time < INT64CONST(0))
        result->time += USECS_PER_DAY;
-   while (result->time >= USECS_PER_DAY)
-       result->time -= USECS_PER_DAY;
+   if (result->time >= USECS_PER_DAY)
+       result->time %= USECS_PER_DAY;
 
    result->zone = tz;
 
index 3f8e005ce70c100c9baee6a67949d14e7fd8c78f..cbab6cfe5d7f1dcde966a60f6816137584674226 100644 (file)
@@ -304,4 +304,25 @@ TABLE timetz_local_view;
  23:59:59.99-07 | 06:59:59.99+00 | 06:59:59.99+00 | 06:59:59.99+00 | 06:59:59.99+00
 (12 rows)
 
+SELECT f1 AS dat,
+       f1 AT TIME ZONE 'UTC+10' AS dat_at_tz,
+       f1 AT TIME ZONE INTERVAL '-10:00' AS dat_at_int
+  FROM TIMETZ_TBL
+  ORDER BY f1;
+      dat       |   dat_at_tz    |   dat_at_int   
+----------------+----------------+----------------
+ 00:01:00-07    | 21:01:00-10    | 21:01:00-10
+ 01:00:00-07    | 22:00:00-10    | 22:00:00-10
+ 02:03:00-07    | 23:03:00-10    | 23:03:00-10
+ 08:08:00-04    | 02:08:00-10    | 02:08:00-10
+ 07:07:00-08    | 05:07:00-10    | 05:07:00-10
+ 11:59:00-07    | 08:59:00-10    | 08:59:00-10
+ 12:00:00-07    | 09:00:00-10    | 09:00:00-10
+ 12:01:00-07    | 09:01:00-10    | 09:01:00-10
+ 15:36:39-04    | 09:36:39-10    | 09:36:39-10
+ 15:36:39-05    | 10:36:39-10    | 10:36:39-10
+ 23:59:00-07    | 20:59:00-10    | 20:59:00-10
+ 23:59:59.99-07 | 20:59:59.99-10 | 20:59:59.99-10
+(12 rows)
+
 ROLLBACK;
index 33f7f8aafb4ba3941c67cc00979b7e580299c2f4..d797f478f0db5c7521e1794612f032becb91c2df 100644 (file)
@@ -100,4 +100,9 @@ CREATE VIEW timetz_local_view AS
   ORDER BY f1;
 SELECT pg_get_viewdef('timetz_local_view', true);
 TABLE timetz_local_view;
+SELECT f1 AS dat,
+       f1 AT TIME ZONE 'UTC+10' AS dat_at_tz,
+       f1 AT TIME ZONE INTERVAL '-10:00' AS dat_at_int
+  FROM TIMETZ_TBL
+  ORDER BY f1;
 ROLLBACK;