I'm trying to test for an empty array. I understand the reasoning behind array_length returing NULL since it's a 0-dimensional array, but why is array_ndims(array[]::int array) not 0, but rather NULL?
-
postgresql-archive.org/… please accept @Lukasz answer - as it the right one (at least today) and indeed is not documented (at least today) properlyVao Tsun– Vao Tsun2017-12-29 19:09:53 +00:00Commented Dec 29, 2017 at 19:09
-
1@VaoTsun, done.David Macek– David Macek2019-04-29 08:53:46 +00:00Commented Apr 29, 2019 at 8:53
Add a comment
|
1 Answer
Looks like it will never return 0:
/* Sanity check: does it look like an array at all? */
if (AARR_NDIM(v) <= 0 || AARR_NDIM(v) > MAXDIM)
PG_RETURN_NULL();
You could write your own C function for this, simply change <= 0 into < 0. As for reasoning behind this, I can't find anything.
Source is in: https://www.postgresql.org/ftp/source/v10.1/ (same thing for 9.6.2, with is what I checked initially)
File: ./src/backend/utils/adt/arrayfuncs.c
Look for simple, 10 line function: array_ndims
3 Comments
Vao Tsun
please put a link to codebase. I also would expect to see the number of empty array to be zero. Maybe smbd just wanted to avoid checking if empty array is array?..
Łukasz Kamiński
@VaoTsun I suppose counting dimensions again (function doesn't hold it in variable) and returning them is a bit more time consuming than just returning NULL and someone writing this function figured no one needed to check 0 dimension arrays.
Vao Tsun
sorry, I meant smth like github.com/postgres/postgres/blob/master/src/backend/utils/adt/… :)