RE: [PATCH] Report malformed integer aggregate states as array errors

Started by Strydom, Matthys10 days ago4 messageshackers
Jump to latest
#1Strydom, Matthys
strydom@amazon.com

Hi All,

Several SQL-callable aggregate support functions accept an int8[]
transition state. They use elog(ERROR) when the supplied state does not
have the expected two-element shape. This means a direct call such as:

SELECT int8_avg(ARRAY[1]::int8[]);

reports SQLSTATE XX000.

The attached patch reports ERRCODE_ARRAY_SUBSCRIPT_ERROR (2202E)
instead. This matches existing array-shape checks, including the
array_agg state handling.

I found these related SQLSTATE cleanup discussions helpful:

* Improve error code and messages for some SQL-reachable code paths:
/messages/by-id/94ac928b-8370-4db5-ada5-579a9ebc089d@gmail.com

* Improve reporting of invalid weight symbols in setweight() et al.:
/messages/by-id/CAON2xHNaeLAUzRCXL5AmXLcXaSE_gWAVjWQRmLzc_oZ=1_Vf4Q@mail.gmail.com

* Report specific SQLSTATEs for stats restore errors:
/messages/by-id/19629-76babc04b683594d@postgresql.org

The patch changes all eight checks for this aggregate state
representation. It also adds a regression test for the direct
int8_avg() call.

I also noticed similar aggregate-state checks elsewhere, such as
check_float8_array() in float.c. I kept this patch focused on the
repeated int8[] checks. If this change is considered useful, I can
prepare a follow-up patch for the remaining cases.

Tested with:

make -C src/test/regress check-tests \
TESTS='test_setup create_index create_aggregate aggregates'

Regards,
Matthys Strydom
Amazon Web Services

Attachments:

t259102_1
0001-Report-malformed-integer-aggregate-states-as-array-e.patchapplication/octet-stream; name=0001-Report-malformed-integer-aggregate-states-as-array-e.patchDownload+34-9
#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Strydom, Matthys (#1)
Re: [PATCH] Report malformed integer aggregate states as array errors

"Strydom, Matthys" <strydom@amazon.com> writes:

Hi All,
Several SQL-callable aggregate support functions accept an int8[]
transition state. They use elog(ERROR) when the supplied state does not
have the expected two-element shape. This means a direct call such as:

SELECT int8_avg(ARRAY[1]::int8[]);

reports SQLSTATE XX000.

The attached patch reports ERRCODE_ARRAY_SUBSCRIPT_ERROR (2202E)
instead. This matches existing array-shape checks, including the
array_agg state handling.

I don't think this is worth the trouble. It's not reasonable for
people to call these functions directly --- indeed there's been
discussion about making that impossible, and then stripping out some
of these error checks.

regards, tom lane

#3Strydom, Matthys
strydom@amazon.com
In reply to: Tom Lane (#2)
Re: [PATCH] Report malformed integer aggregate states as array errors

Tom Lane <tgl@sss.pgh.pa.us> writes:

It's not reasonable for people to call these functions directly ---
indeed there's been discussion about making that impossible, and then
stripping out some of these error checks.

Thanks for the quick response. That makes sense. Since these functions
are callable today, I thought returning a more specific error would be
a small improvement, till the functions are made not callable.

I would be interested in helping make these aggregate support functions
impossible to call directly. Could you point me to the earlier
discussion, or to the preferred direction if one has emerged?

If there is a reasonably self-contained part of that work suitable for
a first PostgreSQL patch, I would be happy to investigate it.

Kind regards,
Matthys

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Strydom, Matthys (#3)
Re: [PATCH] Report malformed integer aggregate states as array errors

"Strydom, Matthys" <strydom@amazon.com> writes:

Tom Lane <tgl@sss.pgh.pa.us> writes:

It's not reasonable for people to call these functions directly ---
indeed there's been discussion about making that impossible, and then
stripping out some of these error checks.

I would be interested in helping make these aggregate support functions
impossible to call directly. Could you point me to the earlier
discussion, or to the preferred direction if one has emerged?

Here are a couple of recent threads touching on this:

/messages/by-id/1ddc2a6f-4b26-43d9-9f3b-5b5db98a486d@gmail.com
/messages/by-id/762e811a-6584-4b03-a6ca-52600adcd6e9@gmail.com

I'm not sure there's full consensus on what to do, but the second
choice (declare these aggregates' transition type INTERNAL rather
than an array) would get the job done with a smaller blast radius.

regards, tom lane