From b9492499b89d9fe002450703b11039b0ac5aff94 Mon Sep 17 00:00:00 2001 From: Nikhil Benesch Date: Sun, 2 Jan 2022 21:59:29 -0500 Subject: [PATCH v2] Improve unsupported units error for extract/date_part Reword the error message when extract/date_part are supplied with unsupported units from e.g.: timestamp with time zone units "foo" not supported to: unit "foo" not supported for type timestamp with time zone The new message avoids a grammatical ambiguity around "time zone units" and consistently avoids quotes around the type name, per standard error message style. It also injects the type name via format_type_be to reduce the number of strings which must be translated. --- src/backend/utils/adt/date.c | 32 ++++++------ src/backend/utils/adt/timestamp.c | 71 +++++++++++++------------- src/test/regress/expected/date.out | 18 +++---- src/test/regress/expected/interval.out | 4 +- src/test/regress/expected/time.out | 6 +-- src/test/regress/expected/timetz.out | 4 +- 6 files changed, 69 insertions(+), 66 deletions(-) diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 9e8baeaa9c..b6decc631e 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -22,6 +22,7 @@ #include #include "access/xact.h" +#include "catalog/pg_type.h" #include "common/hashfn.h" #include "libpq/pqformat.h" #include "miscadmin.h" @@ -1124,8 +1125,8 @@ extract_date(PG_FUNCTION_ARGS) default: ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("date units \"%s\" not supported", - lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(DATEOID)))); } } else if (type == UNITS) @@ -1207,8 +1208,8 @@ extract_date(PG_FUNCTION_ARGS) default: ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("date units \"%s\" not supported", - lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(DATEOID)))); intresult = 0; } } @@ -1223,8 +1224,8 @@ extract_date(PG_FUNCTION_ARGS) default: ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("date units \"%s\" not supported", - lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(DATEOID)))); intresult = 0; } } @@ -1232,7 +1233,8 @@ extract_date(PG_FUNCTION_ARGS) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("date units \"%s\" not recognized", lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(DATEOID)))); intresult = 0; } @@ -2203,8 +2205,8 @@ time_part_common(PG_FUNCTION_ARGS, bool retnumeric) default: ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("\"time\" units \"%s\" not recognized", - lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(TIMEOID)))); intresult = 0; } } @@ -2219,8 +2221,8 @@ time_part_common(PG_FUNCTION_ARGS, bool retnumeric) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("\"time\" units \"%s\" not recognized", - lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(TIMEOID)))); intresult = 0; } @@ -2981,8 +2983,8 @@ timetz_part_common(PG_FUNCTION_ARGS, bool retnumeric) default: ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("\"time with time zone\" units \"%s\" not recognized", - lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(TIMETZOID)))); intresult = 0; } } @@ -3001,8 +3003,8 @@ timetz_part_common(PG_FUNCTION_ARGS, bool retnumeric) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("\"time with time zone\" units \"%s\" not recognized", - lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(TIMETZOID)))); intresult = 0; } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index cb9faff0bb..9d638fceaf 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -3972,8 +3972,8 @@ timestamp_trunc(PG_FUNCTION_ARGS) default: ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("timestamp units \"%s\" not supported", - lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(TIMESTAMPOID)))); result = 0; } @@ -3986,8 +3986,8 @@ timestamp_trunc(PG_FUNCTION_ARGS) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("timestamp units \"%s\" not recognized", - lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(TIMESTAMPOID)))); result = 0; } @@ -4182,8 +4182,8 @@ timestamptz_trunc_internal(text *units, TimestampTz timestamp, pg_tz *tzp) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("timestamp with time zone units \"%s\" not recognized", - lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(TIMESTAMPTZOID)))); result = 0; } @@ -4340,14 +4340,14 @@ interval_trunc(PG_FUNCTION_ARGS) if (val == DTK_WEEK) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("interval units \"%s\" not supported " + errmsg("unit \"%s\" not supported for type %s " "because months usually have fractional weeks", - lowunits))); + lowunits, format_type_be(INTERVALOID)))); else ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("interval units \"%s\" not supported", - lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(INTERVALOID)))); } if (tm2interval(tm, fsec, result) != 0) @@ -4362,8 +4362,8 @@ interval_trunc(PG_FUNCTION_ARGS) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("interval units \"%s\" not recognized", - lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(INTERVALOID)))); } PG_RETURN_INTERVAL_P(result); @@ -4563,13 +4563,13 @@ NonFiniteTimestampTzPart(int type, int unit, char *lowunits, if (isTz) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("timestamp with time zone units \"%s\" not recognized", - lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(TIMESTAMPTZOID)))); else ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("timestamp units \"%s\" not recognized", - lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(TIMESTAMPOID)))); } switch (unit) @@ -4609,13 +4609,13 @@ NonFiniteTimestampTzPart(int type, int unit, char *lowunits, if (isTz) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("timestamp with time zone units \"%s\" not supported", - lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(TIMESTAMPTZOID)))); else ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("timestamp units \"%s\" not supported", - lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(TIMESTAMPOID)))); return 0.0; /* keep compiler quiet */ } } @@ -4814,8 +4814,8 @@ timestamp_part_common(PG_FUNCTION_ARGS, bool retnumeric) default: ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("timestamp units \"%s\" not supported", - lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(TIMESTAMPOID)))); intresult = 0; } } @@ -4861,8 +4861,8 @@ timestamp_part_common(PG_FUNCTION_ARGS, bool retnumeric) default: ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("timestamp units \"%s\" not supported", - lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(TIMESTAMPOID)))); intresult = 0; } @@ -4871,7 +4871,8 @@ timestamp_part_common(PG_FUNCTION_ARGS, bool retnumeric) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("timestamp units \"%s\" not recognized", lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(TIMESTAMPOID)))); intresult = 0; } @@ -5085,8 +5086,8 @@ timestamptz_part_common(PG_FUNCTION_ARGS, bool retnumeric) default: ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("timestamp with time zone units \"%s\" not supported", - lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(TIMESTAMPTZOID)))); intresult = 0; } @@ -5133,8 +5134,8 @@ timestamptz_part_common(PG_FUNCTION_ARGS, bool retnumeric) default: ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("timestamp with time zone units \"%s\" not supported", - lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(TIMESTAMPTZOID)))); intresult = 0; } } @@ -5142,8 +5143,8 @@ timestamptz_part_common(PG_FUNCTION_ARGS, bool retnumeric) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("timestamp with time zone units \"%s\" not recognized", - lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(TIMESTAMPTZOID)))); intresult = 0; } @@ -5265,8 +5266,8 @@ interval_part_common(PG_FUNCTION_ARGS, bool retnumeric) default: ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("interval units \"%s\" not supported", - lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(INTERVALOID)))); intresult = 0; } } @@ -5326,8 +5327,8 @@ interval_part_common(PG_FUNCTION_ARGS, bool retnumeric) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("interval units \"%s\" not recognized", - lowunits))); + errmsg("unit \"%s\" not supported for type %s", + lowunits, format_type_be(INTERVALOID)))); intresult = 0; } diff --git a/src/test/regress/expected/date.out b/src/test/regress/expected/date.out index c8b0566ff4..252cae71e7 100644 --- a/src/test/regress/expected/date.out +++ b/src/test/regress/expected/date.out @@ -1129,15 +1129,15 @@ SELECT EXTRACT(DECADE FROM DATE '0012-12-31 BC'); -- -2 -- all possible fields -- 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'); -ERROR: date units "milliseconds" not supported +ERROR: unit "milliseconds" not supported for type date 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'); -ERROR: date units "minute" not supported +ERROR: unit "minute" not supported for type date 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'); extract --------- @@ -1235,11 +1235,11 @@ SELECT EXTRACT(DOY FROM DATE '2020-08-11'); (1 row) 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'); -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'); -ERROR: date units "timezone_h" not supported +ERROR: unit "timezone_h" not supported for type date SELECT EXTRACT(EPOCH FROM DATE '2020-08-11'); extract ------------ @@ -1462,7 +1462,7 @@ SELECT EXTRACT(EPOCH FROM DATE 'infinity'); -- Infinity -- wrong fields from non-finite date: -- SELECT EXTRACT(MICROSEC FROM DATE 'infinity'); -- error -ERROR: date units "microsec" not recognized +ERROR: unit "microsec" not supported for type date -- test constructors select make_date(2013, 7, 15); make_date diff --git a/src/test/regress/expected/interval.out b/src/test/regress/expected/interval.out index e4b1246f45..be797cb219 100644 --- a/src/test/regress/expected/interval.out +++ b/src/test/regress/expected/interval.out @@ -963,9 +963,9 @@ SELECT f1, (10 rows) SELECT EXTRACT(FORTNIGHT FROM INTERVAL '2 days'); -- error -ERROR: interval units "fortnight" not recognized +ERROR: unit "fortnight" not supported for type interval 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'); extract --------- diff --git a/src/test/regress/expected/time.out b/src/test/regress/expected/time.out index 39b409feca..f55cdd5ac0 100644 --- a/src/test/regress/expected/time.out +++ b/src/test/regress/expected/time.out @@ -161,11 +161,11 @@ SELECT EXTRACT(HOUR FROM TIME '2020-05-26 13:30:25.575401'); (1 row) 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 -ERROR: "time" units "fortnight" not recognized +ERROR: unit "fortnight" not supported for type time without time zone 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'); extract -------------- diff --git a/src/test/regress/expected/timetz.out b/src/test/regress/expected/timetz.out index f4960c0166..3990b4356a 100644 --- a/src/test/regress/expected/timetz.out +++ b/src/test/regress/expected/timetz.out @@ -178,9 +178,9 @@ SELECT EXTRACT(HOUR FROM TIME WITH TIME ZONE '2020-05-26 13:30:25.575401- (1 row) 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 -ERROR: "time with time zone" units "fortnight" not recognized +ERROR: unit "fortnight" not supported for type time with time zone SELECT EXTRACT(TIMEZONE FROM TIME WITH TIME ZONE '2020-05-26 13:30:25.575401-04:30'); extract --------- -- 2.25.1