pgtyped icon indicating copy to clipboard operation
pgtyped copied to clipboard

Dynamic OR query causes processSQLQueryIR to fail

Open filipecatraia opened this issue 3 years ago • 1 comments

Given a simple query with dynamic parameters as such:

SELECT id, email
FROM entity.users
WHERE (id = :id OR email = :email)
AND account_deleted IS NOT TRUE;

Before upgrading to 1.0.1 from 0.13, the generated query ran without issues. The query is run via (simplified):

async function findUser({id, email}: {id?: string; email?: string}) {
    findUserByIdOrEmail.run({id, email}, pool)
    …
}

With the upgrade, all queries with dynamic OR parameters crash if one of the parameters is undefined. We have a few queries like that and they all fail.

I've started digging in, and traced it down to processSQLQueryIR in preprocessor-sql.js. When you filter down for usedParams, parameters with a value of undefined might need to be skipped?

const usedParams = queryIR.params.filter((p) => p.name in queryIR.usedParamSet);
…
let i = 1;
const intervals = [];
for (const usedParam of usedParams) {
    …
    // 🚨 The problem is here, as locs is undefined for this usedParam
    usedParam.locs.forEach… 
    …
}

UPDATE: Turns out that filtering for undefined values doesn't change anything, as locs is still missing for valid parameters passed to the query.

Any idea why this could be happening, i.e. why is locs in each usedParam missing?

filipecatraia avatar Sep 15 '22 10:09 filipecatraia

Downgrading to 0.13 fixes the issue, so we at least have that temporary solution :)

filipecatraia avatar Sep 15 '22 10:09 filipecatraia

Not able to reproduce this in v1.1.0. An additional improvement for optional parameter UX was added in https://github.com/adelsz/pgtyped/pull/482. Please reopen if issue persists.

adelsz avatar Jan 28 '23 14:01 adelsz