Thread: libpq: empty arrays have rank 0 in binary results? whatever the type's rank?

libpq: empty arrays have rank 0 in binary results? whatever the type's rank?

From
Dominique Devienne
Date:
Hi. Selecting/fetching an empty 1D array using a binary-mode PGresult,
gives me back 12 bytes, and the first 4, the rank, is 0, something I was not expecting.
I was expecting dims_rank = 1, then first_dim = 0

Normal? Next two ints are kinda useless given the 0 rank?
It's easy to fix, to support rank=0 for empty arrays, I just want to confirm.

Thanks, --DD

PS: On a side note; where can I find the sending code for arrays?
I didn't find the usual _send() and _recv() functions (but looking on github online...)

PPS: The relevant part of my code is below, for context:

    uint32_t dims_rank = read();
    if (dims_rank != 1) {
        throw std::runtime_error(fmt::format(
            "Unsupported array rank: Got {}; Want 1", dims_rank
        ));
    }
    uint32_t has_nulls = read();
    uint32_t actual_elem_oid = read();
    uint32_t first_dim = read();
    uint32_t lower_idx = read();

    ensure_oid(Oid{ actual_elem_oid }, expected_elem_oid);
    UNUSED(has_nulls);
    assert(lower_idx == 1);
Dominique Devienne <ddevienne@gmail.com> writes:
> Hi. Selecting/fetching an empty 1D array using a binary-mode PGresult,
> gives me back 12 bytes, and the first 4, the rank, is 0, something I was
> not expecting.

Yeah, empty arrays have zero dimensions.

> PS: On a side note; where can I find the sending code for arrays?
> I didn't find the usual _send() and _recv() functions (but looking on
> github online...)


https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/utils/adt/arrayfuncs.c;h=87c987fb2704761c59333bf8c1fee47e5c14c598;hb=HEAD#l1583

            regards, tom lane



Re: libpq: empty arrays have rank 0 in binary results? whatever the type's rank?

From
Dominique Devienne
Date:
On Wed, Mar 29, 2023 at 3:45 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
Dominique Devienne <ddevienne@gmail.com> writes:
> Hi. Selecting/fetching an empty 1D array using a binary-mode PGresult,
> gives me back 12 bytes, and the first 4, the rank, is 0, something I was
> not expecting.

Yeah, empty arrays have zero dimensions.

> PS: On a side note; where can I find the sending code for arrays?
> I didn't find the usual _send() and _recv() functions (but looking on
> github online...)

https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/utils/adt/arrayfuncs.c;h=87c987fb2704761c59333bf8c1fee47e5c14c598;hb=HEAD#l1583

Great. Thanks Tom. --DD