Clean up error messages related to bad datetime units.

Adjust the error texts used for unrecognized/unsupported datetime
units so that there are just two strings to translate, not two
per datatype.  Along the way, follow our usual error message style
of not double-quoting type names, and instead making sure that we
say the name is a type.  Fix a couple of places in date.c that
were using the wrong one of "unrecognized" and "unsupported".

Nikhil Benesch, with a bit more editing by me

Discussion: https://postgr.es/m/CAPWqQZTURGixmbMH2_Z3ZtWGA0ANjUb9bwtkkxSxSfDeFHuM6Q@mail.gmail.com
This commit is contained in:
Tom Lane 2022-01-03 14:05:03 -05:00
parent ba2bc4a7ba
commit 4b160492b9
6 changed files with 76 additions and 91 deletions

View File

@ -22,6 +22,7 @@
#include <time.h> #include <time.h>
#include "access/xact.h" #include "access/xact.h"
#include "catalog/pg_type.h"
#include "common/hashfn.h" #include "common/hashfn.h"
#include "libpq/pqformat.h" #include "libpq/pqformat.h"
#include "miscadmin.h" #include "miscadmin.h"
@ -1124,8 +1125,8 @@ extract_date(PG_FUNCTION_ARGS)
default: default:
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("date units \"%s\" not supported", errmsg("unit \"%s\" not supported for type %s",
lowunits))); lowunits, format_type_be(DATEOID))));
} }
} }
else if (type == UNITS) else if (type == UNITS)
@ -1207,8 +1208,8 @@ extract_date(PG_FUNCTION_ARGS)
default: default:
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("date units \"%s\" not supported", errmsg("unit \"%s\" not supported for type %s",
lowunits))); lowunits, format_type_be(DATEOID))));
intresult = 0; intresult = 0;
} }
} }
@ -1223,8 +1224,8 @@ extract_date(PG_FUNCTION_ARGS)
default: default:
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("date units \"%s\" not supported", errmsg("unit \"%s\" not supported for type %s",
lowunits))); lowunits, format_type_be(DATEOID))));
intresult = 0; intresult = 0;
} }
} }
@ -1232,7 +1233,8 @@ extract_date(PG_FUNCTION_ARGS)
{ {
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("date units \"%s\" not recognized", lowunits))); errmsg("unit \"%s\" not recognized for type %s",
lowunits, format_type_be(DATEOID))));
intresult = 0; intresult = 0;
} }
@ -2202,9 +2204,9 @@ time_part_common(PG_FUNCTION_ARGS, bool retnumeric)
case DTK_ISOYEAR: case DTK_ISOYEAR:
default: default:
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("\"time\" units \"%s\" not recognized", errmsg("unit \"%s\" not supported for type %s",
lowunits))); lowunits, format_type_be(TIMEOID))));
intresult = 0; intresult = 0;
} }
} }
@ -2219,8 +2221,8 @@ time_part_common(PG_FUNCTION_ARGS, bool retnumeric)
{ {
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("\"time\" units \"%s\" not recognized", errmsg("unit \"%s\" not recognized for type %s",
lowunits))); lowunits, format_type_be(TIMEOID))));
intresult = 0; intresult = 0;
} }
@ -2980,9 +2982,9 @@ timetz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
case DTK_MILLENNIUM: case DTK_MILLENNIUM:
default: default:
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("\"time with time zone\" units \"%s\" not recognized", errmsg("unit \"%s\" not supported for type %s",
lowunits))); lowunits, format_type_be(TIMETZOID))));
intresult = 0; intresult = 0;
} }
} }
@ -3001,8 +3003,8 @@ timetz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
{ {
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("\"time with time zone\" units \"%s\" not recognized", errmsg("unit \"%s\" not recognized for type %s",
lowunits))); lowunits, format_type_be(TIMETZOID))));
intresult = 0; intresult = 0;
} }

View File

@ -3972,8 +3972,8 @@ timestamp_trunc(PG_FUNCTION_ARGS)
default: default:
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("timestamp units \"%s\" not supported", errmsg("unit \"%s\" not supported for type %s",
lowunits))); lowunits, format_type_be(TIMESTAMPOID))));
result = 0; result = 0;
} }
@ -3986,8 +3986,8 @@ timestamp_trunc(PG_FUNCTION_ARGS)
{ {
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("timestamp units \"%s\" not recognized", errmsg("unit \"%s\" not recognized for type %s",
lowunits))); lowunits, format_type_be(TIMESTAMPOID))));
result = 0; result = 0;
} }
@ -4165,8 +4165,8 @@ timestamptz_trunc_internal(text *units, TimestampTz timestamp, pg_tz *tzp)
default: default:
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("timestamp with time zone units \"%s\" not " errmsg("unit \"%s\" not supported for type %s",
"supported", lowunits))); lowunits, format_type_be(TIMESTAMPTZOID))));
result = 0; result = 0;
} }
@ -4182,8 +4182,8 @@ timestamptz_trunc_internal(text *units, TimestampTz timestamp, pg_tz *tzp)
{ {
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("timestamp with time zone units \"%s\" not recognized", errmsg("unit \"%s\" not recognized for type %s",
lowunits))); lowunits, format_type_be(TIMESTAMPTZOID))));
result = 0; result = 0;
} }
@ -4337,17 +4337,11 @@ interval_trunc(PG_FUNCTION_ARGS)
break; break;
default: default:
if (val == DTK_WEEK) ereport(ERROR,
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("unit \"%s\" not supported for type %s",
errmsg("interval units \"%s\" not supported " lowunits, format_type_be(INTERVALOID)),
"because months usually have fractional weeks", (val == DTK_WEEK) ? errdetail("Months usually have fractional weeks.") : 0));
lowunits)));
else
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("interval units \"%s\" not supported",
lowunits)));
} }
if (tm2interval(tm, fsec, result) != 0) if (tm2interval(tm, fsec, result) != 0)
@ -4362,8 +4356,8 @@ interval_trunc(PG_FUNCTION_ARGS)
{ {
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("interval units \"%s\" not recognized", errmsg("unit \"%s\" not recognized for type %s",
lowunits))); lowunits, format_type_be(INTERVALOID))));
} }
PG_RETURN_INTERVAL_P(result); PG_RETURN_INTERVAL_P(result);
@ -4559,18 +4553,11 @@ NonFiniteTimestampTzPart(int type, int unit, char *lowunits,
bool isNegative, bool isTz) bool isNegative, bool isTz)
{ {
if ((type != UNITS) && (type != RESERV)) if ((type != UNITS) && (type != RESERV))
{ ereport(ERROR,
if (isTz) (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
ereport(ERROR, errmsg("unit \"%s\" not recognized for type %s",
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), lowunits,
errmsg("timestamp with time zone units \"%s\" not recognized", format_type_be(isTz ? TIMESTAMPTZOID : TIMESTAMPOID))));
lowunits)));
else
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("timestamp units \"%s\" not recognized",
lowunits)));
}
switch (unit) switch (unit)
{ {
@ -4606,16 +4593,11 @@ NonFiniteTimestampTzPart(int type, int unit, char *lowunits,
return get_float8_infinity(); return get_float8_infinity();
default: default:
if (isTz) ereport(ERROR,
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("unit \"%s\" not supported for type %s",
errmsg("timestamp with time zone units \"%s\" not supported", lowunits,
lowunits))); format_type_be(isTz ? TIMESTAMPTZOID : TIMESTAMPOID))));
else
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("timestamp units \"%s\" not supported",
lowunits)));
return 0.0; /* keep compiler quiet */ return 0.0; /* keep compiler quiet */
} }
} }
@ -4814,8 +4796,8 @@ timestamp_part_common(PG_FUNCTION_ARGS, bool retnumeric)
default: default:
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("timestamp units \"%s\" not supported", errmsg("unit \"%s\" not supported for type %s",
lowunits))); lowunits, format_type_be(TIMESTAMPOID))));
intresult = 0; intresult = 0;
} }
} }
@ -4861,8 +4843,8 @@ timestamp_part_common(PG_FUNCTION_ARGS, bool retnumeric)
default: default:
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("timestamp units \"%s\" not supported", errmsg("unit \"%s\" not supported for type %s",
lowunits))); lowunits, format_type_be(TIMESTAMPOID))));
intresult = 0; intresult = 0;
} }
@ -4871,7 +4853,8 @@ timestamp_part_common(PG_FUNCTION_ARGS, bool retnumeric)
{ {
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("timestamp units \"%s\" not recognized", lowunits))); errmsg("unit \"%s\" not recognized for type %s",
lowunits, format_type_be(TIMESTAMPOID))));
intresult = 0; intresult = 0;
} }
@ -5085,8 +5068,8 @@ timestamptz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
default: default:
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("timestamp with time zone units \"%s\" not supported", errmsg("unit \"%s\" not supported for type %s",
lowunits))); lowunits, format_type_be(TIMESTAMPTZOID))));
intresult = 0; intresult = 0;
} }
@ -5133,8 +5116,8 @@ timestamptz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
default: default:
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("timestamp with time zone units \"%s\" not supported", errmsg("unit \"%s\" not supported for type %s",
lowunits))); lowunits, format_type_be(TIMESTAMPTZOID))));
intresult = 0; intresult = 0;
} }
} }
@ -5142,8 +5125,8 @@ timestamptz_part_common(PG_FUNCTION_ARGS, bool retnumeric)
{ {
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("timestamp with time zone units \"%s\" not recognized", errmsg("unit \"%s\" not recognized for type %s",
lowunits))); lowunits, format_type_be(TIMESTAMPTZOID))));
intresult = 0; intresult = 0;
} }
@ -5265,8 +5248,8 @@ interval_part_common(PG_FUNCTION_ARGS, bool retnumeric)
default: default:
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("interval units \"%s\" not supported", errmsg("unit \"%s\" not supported for type %s",
lowunits))); lowunits, format_type_be(INTERVALOID))));
intresult = 0; intresult = 0;
} }
} }
@ -5326,8 +5309,8 @@ interval_part_common(PG_FUNCTION_ARGS, bool retnumeric)
{ {
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("interval units \"%s\" not recognized", errmsg("unit \"%s\" not recognized for type %s",
lowunits))); lowunits, format_type_be(INTERVALOID))));
intresult = 0; intresult = 0;
} }

View File

@ -1129,15 +1129,15 @@ SELECT EXTRACT(DECADE FROM DATE '0012-12-31 BC'); -- -2
-- all possible fields -- all possible fields
-- --
SELECT EXTRACT(MICROSECONDS FROM DATE '2020-08-11'); SELECT EXTRACT(MICROSECONDS FROM DATE '2020-08-11');
ERROR: date units "microseconds" not supported ERROR: unit "microseconds" not supported for type date
SELECT EXTRACT(MILLISECONDS FROM DATE '2020-08-11'); SELECT EXTRACT(MILLISECONDS FROM DATE '2020-08-11');
ERROR: date units "milliseconds" not supported ERROR: unit "milliseconds" not supported for type date
SELECT EXTRACT(SECOND FROM DATE '2020-08-11'); SELECT EXTRACT(SECOND FROM DATE '2020-08-11');
ERROR: date units "second" not supported ERROR: unit "second" not supported for type date
SELECT EXTRACT(MINUTE FROM DATE '2020-08-11'); SELECT EXTRACT(MINUTE FROM DATE '2020-08-11');
ERROR: date units "minute" not supported ERROR: unit "minute" not supported for type date
SELECT EXTRACT(HOUR FROM DATE '2020-08-11'); SELECT EXTRACT(HOUR FROM DATE '2020-08-11');
ERROR: date units "hour" not supported ERROR: unit "hour" not supported for type date
SELECT EXTRACT(DAY FROM DATE '2020-08-11'); SELECT EXTRACT(DAY FROM DATE '2020-08-11');
extract extract
--------- ---------
@ -1235,11 +1235,11 @@ SELECT EXTRACT(DOY FROM DATE '2020-08-11');
(1 row) (1 row)
SELECT EXTRACT(TIMEZONE FROM DATE '2020-08-11'); SELECT EXTRACT(TIMEZONE FROM DATE '2020-08-11');
ERROR: date units "timezone" not supported ERROR: unit "timezone" not supported for type date
SELECT EXTRACT(TIMEZONE_M FROM DATE '2020-08-11'); SELECT EXTRACT(TIMEZONE_M FROM DATE '2020-08-11');
ERROR: date units "timezone_m" not supported ERROR: unit "timezone_m" not supported for type date
SELECT EXTRACT(TIMEZONE_H FROM DATE '2020-08-11'); SELECT EXTRACT(TIMEZONE_H FROM DATE '2020-08-11');
ERROR: date units "timezone_h" not supported ERROR: unit "timezone_h" not supported for type date
SELECT EXTRACT(EPOCH FROM DATE '2020-08-11'); SELECT EXTRACT(EPOCH FROM DATE '2020-08-11');
extract extract
------------ ------------
@ -1462,7 +1462,7 @@ SELECT EXTRACT(EPOCH FROM DATE 'infinity'); -- Infinity
-- wrong fields from non-finite date: -- wrong fields from non-finite date:
-- --
SELECT EXTRACT(MICROSEC FROM DATE 'infinity'); -- error SELECT EXTRACT(MICROSEC FROM DATE 'infinity'); -- error
ERROR: date units "microsec" not recognized ERROR: unit "microsec" not recognized for type date
-- test constructors -- test constructors
select make_date(2013, 7, 15); select make_date(2013, 7, 15);
make_date make_date

View File

@ -963,9 +963,9 @@ SELECT f1,
(10 rows) (10 rows)
SELECT EXTRACT(FORTNIGHT FROM INTERVAL '2 days'); -- error SELECT EXTRACT(FORTNIGHT FROM INTERVAL '2 days'); -- error
ERROR: interval units "fortnight" not recognized ERROR: unit "fortnight" not recognized for type interval
SELECT EXTRACT(TIMEZONE FROM INTERVAL '2 days'); -- error SELECT EXTRACT(TIMEZONE FROM INTERVAL '2 days'); -- error
ERROR: interval units "timezone" not supported ERROR: unit "timezone" not supported for type interval
SELECT EXTRACT(DECADE FROM INTERVAL '100 y'); SELECT EXTRACT(DECADE FROM INTERVAL '100 y');
extract extract
--------- ---------

View File

@ -161,11 +161,11 @@ SELECT EXTRACT(HOUR FROM TIME '2020-05-26 13:30:25.575401');
(1 row) (1 row)
SELECT EXTRACT(DAY FROM TIME '2020-05-26 13:30:25.575401'); -- error SELECT EXTRACT(DAY FROM TIME '2020-05-26 13:30:25.575401'); -- error
ERROR: "time" units "day" not recognized ERROR: unit "day" not supported for type time without time zone
SELECT EXTRACT(FORTNIGHT FROM TIME '2020-05-26 13:30:25.575401'); -- error SELECT EXTRACT(FORTNIGHT FROM TIME '2020-05-26 13:30:25.575401'); -- error
ERROR: "time" units "fortnight" not recognized ERROR: unit "fortnight" not recognized for type time without time zone
SELECT EXTRACT(TIMEZONE FROM TIME '2020-05-26 13:30:25.575401'); -- error SELECT EXTRACT(TIMEZONE FROM TIME '2020-05-26 13:30:25.575401'); -- error
ERROR: "time" units "timezone" not recognized ERROR: unit "timezone" not supported for type time without time zone
SELECT EXTRACT(EPOCH FROM TIME '2020-05-26 13:30:25.575401'); SELECT EXTRACT(EPOCH FROM TIME '2020-05-26 13:30:25.575401');
extract extract
-------------- --------------

View File

@ -178,9 +178,9 @@ SELECT EXTRACT(HOUR FROM TIME WITH TIME ZONE '2020-05-26 13:30:25.575401-
(1 row) (1 row)
SELECT EXTRACT(DAY FROM TIME WITH TIME ZONE '2020-05-26 13:30:25.575401-04'); -- error SELECT EXTRACT(DAY FROM TIME WITH TIME ZONE '2020-05-26 13:30:25.575401-04'); -- error
ERROR: "time with time zone" units "day" not recognized ERROR: unit "day" not supported for type time with time zone
SELECT EXTRACT(FORTNIGHT FROM TIME WITH TIME ZONE '2020-05-26 13:30:25.575401-04'); -- error SELECT EXTRACT(FORTNIGHT FROM TIME WITH TIME ZONE '2020-05-26 13:30:25.575401-04'); -- error
ERROR: "time with time zone" units "fortnight" not recognized ERROR: unit "fortnight" not recognized for type time with time zone
SELECT EXTRACT(TIMEZONE FROM TIME WITH TIME ZONE '2020-05-26 13:30:25.575401-04:30'); SELECT EXTRACT(TIMEZONE FROM TIME WITH TIME ZONE '2020-05-26 13:30:25.575401-04:30');
extract extract
--------- ---------