1

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?

2
  • postgresql-archive.org/… please accept @Lukasz answer - as it the right one (at least today) and indeed is not documented (at least today) properly Commented Dec 29, 2017 at 19:09
  • 1
    @VaoTsun, done. Commented Apr 29, 2019 at 8:53

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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?..
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.