Change return type of EXTRACT to numeric

Enterprise / PostgreSQL - Peter Eisentraut [eisentraut.org] - 6 April 2021 05:20 UTC

The previous implementation of EXTRACT mapped internally to date_part(), which returned type double precision (since it was implemented long before the numeric type existed). This can lead to imprecise output in some cases, so returning numeric would be preferrable. Changing the return type of an existing function is a bit risky, so instead we do the following: We implement a new set of functions, which are now called "extract", in parallel to the existing date_part functions. They work the same way internally but use numeric instead of float8. The EXTRACT construct is now mapped by the parser to these new extract functions. That way, dumps of views etc. from old versions (which would use date_part) continue to work unchanged, but new uses will map to the new extract functions.

Additionally, the reverse compilation of EXTRACT now reproduces the original syntax, using the new mechanism introduced in 40c24bfef92530bd846e111c1742c2a54441c62c.

The following minor changes of behavior result from the new implementation:

- The column name from an isolated EXTRACT call is now "extract" instead of "date_part".

- Extract from date now rejects inappropriate field names such as HOUR. It was previously mapped internally to extract from timestamp, so it would silently accept everything appropriate for timestamp.

- Return values when extracting fields with possibly fractional
values, such as second and epoch, now have the full scale that the
value has internally (so, for example, '1.000000' instead of just '1').

a2da77cdb4 Change return type of EXTRACT to numeric
doc/src/sgml/func.sgml | 10 +-
src/backend/parser/gram.y | 2 +-
src/backend/utils/adt/date.c | 308 ++++++++++++++++--
src/backend/utils/adt/numeric.c | 61 ++++
src/backend/utils/adt/ruleutils.c | 21 ++
src/backend/utils/adt/timestamp.c | 471 ++++++++++++++++++++--------
src/include/catalog/catversion.h | 2 +-
src/include/catalog/pg_proc.dat | 18 ++
src/include/utils/numeric.h | 1 +
src/test/regress/expected/create_view.out | 2 +-
src/test/regress/expected/date.out | 452 ++++++++++++--------------
src/test/regress/expected/interval.out | 101 +++---
src/test/regress/expected/psql_crosstab.out | 12 +-
src/test/regress/expected/time.out | 48 ++-
src/test/regress/expected/timestamp.out | 91 ++++++
src/test/regress/expected/timestamptz.out | 92 ++++++
src/test/regress/expected/timetz.out | 60 +++-
src/test/regress/sql/date.sql | 22 +-
src/test/regress/sql/interval.sql | 12 +
src/test/regress/sql/time.sql | 7 +
src/test/regress/sql/timestamp.sql | 13 +
src/test/regress/sql/timestamptz.sql | 13 +
src/test/regress/sql/timetz.sql | 7 +
23 files changed, 1330 insertions(+), 496 deletions(-)

Upstream: git.postgresql.org


  • Share