Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested arrays aren't expanded properly in 3.4.3 #820

Open
baileywickham opened this issue Mar 6, 2024 · 3 comments
Open

Nested arrays aren't expanded properly in 3.4.3 #820

baileywickham opened this issue Mar 6, 2024 · 3 comments

Comments

@baileywickham
Copy link

Using nested arrays in a where statement causes an error in

import postgres from 'postgres';

const sql = postgres('postgres:///', {
                prepare: false,
});

const values = [
	[]
];

const res = await sql`SELECT a, b FROM fner WHERE (a, b) IN (${sql(values)})`;

console.log(res);

This is the output from running this code where the first line is a console.log from sql on line 112 of index.js

[ 'SELECT a, b FROM fner WHERE (a, b) IN (', ')' ] [ Builder { first: [ [] ], rest: [] } ]
node:internal/process/esm_loader:40
      internalBinding('errors').triggerUncaughtException(
                                ^

TypeError: str.replace is not a function
    at escape (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/postgres@3.4.3/node_modules/postgres/src/types.js:217:20)
    at file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/postgres@3.4.3/node_modules/postgres/src/types.js:213:22
    at Array.map (<anonymous>)
    at escapeIdentifiers (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/postgres@3.4.3/node_modules/postgres/src/types.js:213:13)
    at Object.select [as fn] (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/postgres@3.4.3/node_modules/postgres/src/types.js:139:12)
    at Builder.build (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/postgres@3.4.3/node_modules/postgres/src/types.js:71:17)
    at stringifyValue (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/postgres@3.4.3/node_modules/postgres/src/types.js:109:38)
    at stringify (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/postgres@3.4.3/node_modules/postgres/src/types.js:100:16)
    at build (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/postgres@3.4.3/node_modules/postgres/src/connection.js:223:20)
    at execute (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/postgres@3.4.3/node_modules/postgres/src/connection.js:167:7)
    at cachedError (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/postgres@3.4.3/node_modules/postgres/src/query.js:170:23)
    at new Query (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/postgres@3.4.3/node_modules/postgres/src/query.js:36:24)
    at sql (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/postgres@3.4.3/node_modules/postgres/src/index.js:113:11)
    at file:///home/admin/proto-0.14.0-747-g66d563cf/lib/build/testanotheridea.js:11:22
    at cachedError (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/postgres@3.4.3/node_modules/postgres/src/query.js:170:23)
    at new Query (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/postgres@3.4.3/node_modules/postgres/src/query.js:36:24)
    at sql (file:///home/admin/proto-0.14.0-747-g66d563cf/node_modules/.pnpm/postgres@3.4.3/node_modules/postgres/src/index.js:113:11)

This was working on version 3.3.5 and when we upgraded to 3.4.3 this error started showing up for us. Let me know how I can help.

@alonrbar
Copy link

Seems like the same issue as #701. There's a workaround there you might find useful.

@baileywickham
Copy link
Author

Thanks for the pointer to the workaround.

@Marviel
Copy link

Marviel commented May 9, 2024

Another workaround I found is running sql manually on each subarray in the 2d arr before running sql again on the result.

const arr2D = [
   ['baz1', 'bat1'],
   ['baz2', 'bat2']
]
sql`SELECT foo FROM bar WHERE (baz, bat) IN ${sql(arr2d.map((item) => sql(item)))}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants