Skip to content

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

@AlexMayleRdn

Description

@AlexMayleRdn

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions