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

array searches produce broken SQL (even with arrayFields) #14

Closed
mattbishop opened this issue May 6, 2021 · 3 comments
Closed

array searches produce broken SQL (even with arrayFields) #14

mattbishop opened this issue May 6, 2021 · 3 comments

Comments

@mattbishop
Copy link

I have table with a 'data' JSONB column that has an object like this:

{ "players": [ "Mark", "Matt", "Minhwa", "Melinda" ] }

Instead of using $in, I am using a query like this:

const sql = mongoToPostgres("data", {players: ["Matt", "Melinda"]}, ["players"])

The resulting SQL looks like this:

(data @> '{ "players": Matt,Melinda }' OR EXISTS (SELECT * FROM jsonb_array_elements(data->'players') WHERE jsonb_typeof(data->'players')='array' AND value @> '["Matt","Melinda"]'::jsonb))

Postgres throws an error:

invalid input syntax for type json
...
      "name": "error",
      "severity": "ERROR",
      "code": "22P02",
      "detail": "Token \"Matt\" is invalid.",
      "position": "233",
      "where": "JSON data, line 1: { \"players\": Matt...",
      "file": "json.c",
      "line": "1248",
      "routine": "report_invalid_token"

the first part of the SQL should be omitted because it's an array select, which is covered by the EXISTS... statement.

@mattbishop
Copy link
Author

Or, the first part fixed to emit a proper JSON array. This query works as expected:

select * from events where (data @> '{ "players": ["Matt","Melinda"] }' OR EXISTS (SELECT * FROM jsonb_array_elements(data->'players') WHERE jsonb_typeof(data->'players')='array' AND value @> '["Matt","Melinda"]'::jsonb))

Expected: data @> '{ "players": ["Matt","Melinda"] }'
Actual: data @> '{ "players": Matt,Melinda }'

@thomas4019
Copy link
Owner

@mattbishop Thanks for filling this. I think I fixed this and pushed out a new release. Can you try and let me know if it solves your issue?

@mattbishop
Copy link
Author

Latest version fixes this for me. Thanks!

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

2 participants