On Mon, Jul 8, 2024 at 10:42 PM Tom Lane <t...@sss.pgh.pa.us> wrote: > > jian he <jian.universal...@gmail.com> writes: > > while reviewing the json query doc, > > I found out the following error message was not quite right. > > > select '[1,2]'::int[]; > > ERROR: malformed array literal: "[1,2]" > > LINE 1: select '[1,2]'::int[]; > > ^ > > DETAIL: Missing "]" after array dimensions. > > > should it be: > > "Missing delimiter ":" while specifying array dimensions." > > That's presuming quite a lot about the nature of the error. > All the code knows is that what follows the "1" should be > either ":" or "]", and when it sees "," instead it throws > this error. I agree the existing message isn't great, but > trying to be more specific could confuse people even more > if the more-specific message doesn't apply either. > > One possibility could be > > if (*p != ']') > ereturn(escontext, false, > (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), > errmsg("malformed array literal: \"%s\"", origStr), > + (strchr(p, ']') != NULL) ? > + errdetail("Array dimensions have invalid syntax.") : > errdetail("Missing \"%s\" after array dimensions.", > "]"))); > > that is, only say "Missing "]"" if there's no ']' anywhere, and > otherwise just say the dimensions are wrong. This could be fooled > by a ']' that's part of some string in the data, but even then the > errdetail isn't wrong. > > Or we could just say "Array dimensions have invalid syntax." > unconditionally. > > regards, tom lane
we can if (*p == ':') { .... if (*p != ']') ereturn(escontext, false, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("malformed array literal: \"%s\"", origStr), errdetail("Missing \"%s\" after array dimensions.", "]"))); } else { if (*p != ']') ereturn(escontext, false, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("malformed array literal: \"%s\"", origStr), errdetail("Missing delimiter \"%s\" while specifying array dimensions.", ":"))); } different error message in IF ELSE blocks. please check attached.
diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c index d6641b57..c5653a6a 100644 --- a/src/backend/utils/adt/arrayfuncs.c +++ b/src/backend/utils/adt/arrayfuncs.c @@ -453,19 +453,25 @@ ReadArrayDimensions(char **srcptr, int *ndim_p, int *dim, int *lBound, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("malformed array literal: \"%s\"", origStr), errdetail("Missing array dimension value."))); + if (*p != ']') + ereturn(escontext, false, + (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), + errmsg("malformed array literal: \"%s\"", origStr), + errdetail("Missing \"%s\" after array dimensions.", + "]"))); } else { /* [n] format */ lBound[ndim] = 1; ub = i; + if (*p != ']') + ereturn(escontext, false, + (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), + errmsg("malformed array literal: \"%s\"", origStr), + errdetail("Missing delimiter \"%s\" while specifying array dimensions.", + ":"))); } - if (*p != ']') - ereturn(escontext, false, - (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), - errmsg("malformed array literal: \"%s\"", origStr), - errdetail("Missing \"%s\" after array dimensions.", - "]"))); p++; /*