pgsql: Fix interval_transform so it doesn't throw away non-no-op casts.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Fix interval_transform so it doesn't throw away non-no-op casts.
Date: 2016-12-27 20:44:18
Message-ID: E1cLyba-0005BB-Qs@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix interval_transform so it doesn't throw away non-no-op casts.

interval_transform() contained two separate bugs that caused it to
sometimes mistakenly decide that a cast from interval to restricted
interval is a no-op and throw it away.

First, it was wrong to rely on dt.h's field type macros to have an
ordering consistent with the field's significance; in one case they do
not. This led to mistakenly treating YEAR as less significant than MONTH,
so that a cast from INTERVAL MONTH to INTERVAL YEAR was incorrectly
discarded.

Second, fls(1<<k) produces k+1 not k, so comparing its output directly
to SECOND was wrong. This led to supposing that a cast to INTERVAL
MINUTE was really a cast to INTERVAL SECOND and so could be discarded.

To fix, get rid of the use of fls(), and make a function based on
intervaltypmodout to produce a field ID code adapted to the need here.

Per bug #14479 from Piotr Stefaniak. Back-patch to 9.2 where transform
functions were introduced, because this code was born broken.

Discussion: https://postgr.es/m/20161227172307.10135.7747@wrigleys.postgresql.org

Branch
------
REL9_3_STABLE

Details
-------
http://git.postgresql.org/pg/commitdiff/58359983909ae2c4038da21c907d606fd6da9bdf

Modified Files
--------------
src/backend/utils/adt/timestamp.c | 111 ++++++++++++++++++++++++---------
src/test/regress/expected/interval.out | 18 ++++++
src/test/regress/sql/interval.sql | 5 ++
3 files changed, 105 insertions(+), 29 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Peter Eisentraut 2016-12-29 15:57:22 pgsql: Make more use of RoleSpec struct
Previous Message Andrew Dunstan 2016-12-27 16:30:28 pgsql: Explain unaccounted for space in pgstattuple.