porsager / porsager/postgres

nulls in result of array_agg are parsed to the string literal 'NULL' rather than the JS null

Open
#1,124 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
8.7k
Forks
374
Avg merge
11d 16h
Merged PRs (30d)
1

Description

I would expect null values in an array to end up as javascript nulls, just as they when a column is null in a row.

Here is a test demonstrating the handling of nulls in a "normal" query and what happens when array_agg is introduced.

    it.only('null values is array should be parsed as javascript nulls', async () => {
        await conn`
            CREATE TABLE test_nulls (
                id serial primary key,
                y text
            )
        `;

        await conn`
            insert into test_nulls (y) values
            ('a'), (null), ('b')
        `;
        
        const correct = await conn`
            SELECT
                y
            FROM test_nulls
        `;
        console.log(correct.map(r => r.y)); // [ 'a', null, 'b' ] <- actual null primitive
        expect(correct.find(r => r.y === null)).toBeDefined();

        const incorrect = await conn`
            SELECT
                array_agg(y)::text[] as y
            FROM test_nulls
        `;
        console.log(incorrect[0].y); // [ 'a', 'NULL', 'b' ] <-- string literal 'NULL'
        expect(incorrect[0].y.find(v => v === null)).toBeDefined();
    });

I could create a query helper as described in the Custom Types section of the README, but it's not clear how I would write a parser for an array. Especially considering that what appears to be the default (e.g., arrayParser() in the types.ts) file is rather complex; handling nested arrays, quotes, etc. I'm not sure I'm comfortable overriding that for what I would expect to be default behavior.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the test shown in the issue, comparing ordinary nullable columns with array_agg(y)::text[]. Then inspect arrayParser() in types.ts and the Custom Types section of the README. Done means PostgreSQL array elements that are NULL are returned as JavaScript null values, including in the demonstrated test.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, postgresql
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.