Cleaning up array_in()

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alexander Lakhin <exclusion(at)gmail(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Cleaning up array_in()
Date: 2023-05-02 15:41:27
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

This is in response to Alexander's observation at [1], but I'm
starting a fresh thread to keep this patch separate from the plperl
fixes in the cfbot's eyes.

Alexander Lakhin <exclusion(at)gmail(dot)com> writes:
> I continue watching the array handling bugs dancing Sirtaki too. Now it's
> another asymmetry:
> select '{{1},{{2}}}'::int[];
>  {{{1}},{{2}}}
> but:
> select '{{{1}},{2}}'::int[];
>  {}

Bleah. Both of those should be rejected, for sure, but it's the same
situation as in the PLs: we weren't doing anything to enforce that all
the scalar elements appear at the same nesting depth.

I spent some time examining array_in(), and was pretty disheartened
by what a mess it is. It looks like back in the dim mists of the
Berkeley era, there was an intentional attempt to allow
non-rectangular array input, with the missing elements automatically
filled out as NULLs. Since that was undocumented, we concluded it was
a bug and plastered on some code to check for rectangularity of the
input. I don't quibble with enforcing rectangularity, but the
underlying logic should have been simplified while we were at it.
The element-counting logic was basically magic (why is it okay to
increment temp[ndim - 1] when the current nest_level might be
different from that?) and the extra layers of checks didn't make it
any more intelligible. Plus, ReadArrayStr was expending far more
cycles than it needs to given the assumption of rectangularity.

So, here's a rewrite.

Although I view this as a bug fix, AFAICT the only effects are to
accept input that should be rejected. So again I don't advocate
back-patching. But should we sneak it into v16, or wait for v17?

regards, tom lane


Attachment Content-Type Size
v1-0001-Simplify-and-speed-up-ReadArrayStr.patch text/x-diff 6.8 KB
v1-0002-Rewrite-ArrayCount-to-make-dimensionality-checks-.patch text/x-diff 19.8 KB


Browse pgsql-hackers by date

  From Date Subject
Next Message Robert Haas 2023-05-02 17:49:47 code cleanup for CREATE STATISTICS
Previous Message Noah Misch 2023-05-02 14:29:38 Re: ICU locale validation / canonicalization