diff --git a/src/backend/parser/parse_coerce.c b/src/backend/parser/parse_coerce.c index bfaac4229ea..2e0b927851c 100644 --- a/src/backend/parser/parse_coerce.c +++ b/src/backend/parser/parse_coerce.c @@ -153,7 +153,7 @@ coerce_to_target_type_extended(ParseState *pstate, Node *expr, Oid exprtype, /* * coerce_type_with_format() * - * Cocerce the CAST(expr AS type FORMAT 'fmt') construct to the target type. + * Coerce the CAST(expr AS type FORMAT 'fmt') construct to the target type. * * This is a subroutine of coerce_type_extended. The caller must ensure that * the coercion is possible via can_coerce_type. @@ -187,6 +187,12 @@ coerce_type_with_format(ParseState *pstate, Node *node, Node *fmt, t_typcategory = TypeCategory(targetBaseTypeId); fmtcategory = TypeCategory(formatBaseTypeId); + if (fmtcategory != TYPCATEGORY_STRING && fmtcategory != TYPCATEGORY_UNKNOWN) + ereport(ERROR, + errcode(ERRCODE_CANNOT_COERCE), + errmsg("CAST FORMAT expression is not of type text"), + parser_errposition(pstate, exprLocation(fmt))); + /* * Since the caller (coerce_type_extended) does not handle 'Unknown' * constants, we must explicitly coerce them to TEXT in the source @@ -254,12 +260,6 @@ coerce_type_with_format(ParseState *pstate, Node *node, Node *fmt, errhint("Only timestamptz, text, numeric and date data type are supported for formatted type casting"), parser_coercion_errposition(pstate, location, node)); - if (fmtcategory != TYPCATEGORY_STRING && fmtcategory != TYPCATEGORY_UNKNOWN) - ereport(ERROR, - errcode(ERRCODE_CANNOT_COERCE), - errmsg("CAST FORMAT expression is not of type text"), - parser_errposition(pstate, exprLocation(fmt))); - /* * Internally, CAST FORMAT delegates to functions (e.g., to_char, to_date) * where the format string parameter is typed as TEXT. Consequently, the @@ -293,8 +293,7 @@ coerce_type_with_format(ParseState *pstate, Node *node, Node *fmt, elog(ERROR, "failed to find conversion function from %s to %s while using a format template", format_type_be(inputTypeId), format_type_be(targetTypeId)); - args = list_make1(node); - args = lappend(args, format); + args = list_make2(node, format); fn = makeFuncCall(funcname, args, COERCE_SQL_SYNTAX, -1); diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index d1960799ee5..bffaddc920d 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -2742,7 +2742,7 @@ transformTypeCast(ParseState *pstate, TypeCast *tc) if (tc->format) ereport(ERROR, errcode(ERRCODE_CANNOT_COERCE), - errmsg("formmatted type cast is not supported for array type"), + errmsg("formatted type cast is not supported for array type"), parser_coercion_errposition(pstate, exprLocation(arg), arg)); /* diff --git a/src/test/regress/expected/misc.out b/src/test/regress/expected/misc.out index 4aed7e8a498..1804ec7de31 100644 --- a/src/test/regress/expected/misc.out +++ b/src/test/regress/expected/misc.out @@ -458,7 +458,7 @@ LINE 1: select cast('-34,338,492' as bigint format '99G999G999'); ^ HINT: Only timestamptz, text, numeric and date data type are supported for formatted type casting select cast(array[1] as text format 'YYYY'); -ERROR: formmatted type cast is not supported for array type +ERROR: formatted type cast is not supported for array type LINE 1: select cast(array[1] as text format 'YYYY'); ^ select cast('1' as timestamp[] format 'YYYY-MM-DD');