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

%sql does not parse JSON operators appropriately #918

Closed
maciejb opened this issue Oct 17, 2023 · 6 comments · Fixed by #938
Closed

%sql does not parse JSON operators appropriately #918

maciejb opened this issue Oct 17, 2023 · 6 comments · Fixed by #938
Assignees

Comments

@maciejb
Copy link

maciejb commented Oct 17, 2023

What happens?

It seems that the -> operator which is used to access JSON fields, is incompatible with %sql and is being interpreted as an argument switch instead. I am unable to locate any methods for escaping this in the documentation, although it can be worked around by using %%sql blocks.

To Reproduce

Connect to a Postgres database, and issue the following:

%sql select '{"key": 1}'::jsonb -> 'key'

The resulting error

UsageError: unrecognized arguments: -> 'key'

OS:

macOS

JupySQL Version:

0.10.2

Full Name:

Maciej Bukczynski

Affiliation:

Darkhorse Analytics

@edublancas
Copy link

hi @maciejb, thanks for reporting this!

We've had similar issues to this one in the past. We inherited the %sql parsing logic from ipython-sql and we decided to keep it since changing it too much would make the ipython-sql -> jupysql transition more difficult, but that means we keep encountering these issues from time to time.

for now, using %%sql is the way to go. but we'll keep this open and tackle it so %sql doesn't think -> is an argument.

@maciejb
Copy link
Author

maciejb commented Oct 17, 2023

Thanks, @edublancas! And thank you for your work on this important project :)

I'm not sure about this being inherited logic, though. We encountered this only upon transitioning from ipython-sql to jupysql, where this had been working without issue.

Also, in case it's useful for anyone else, if %%sql were undesirable in some situation, the Python variable approach also works around this successfully, e.g.:

query = "select '{\"key\": 1}'::jsonb -> 'key';"
%sql {{query}}

@edublancas
Copy link

ah, thanks for sharing this. I think we modified the parser recently so I guess we introduced the bug there. alright, we'll get to it!

@bryannho
Copy link

bryannho commented Nov 6, 2023

Should have a fix for this out soon, but as I was testing I wanted to document a few more errors/details I found.

The parsing bug occurs because the operator begins with -. The same thing happens with ->>. All other JSON operators seem to operate fine using %sql and %%sql.

Interestingly, removing any whitespace before the occurrence of -> or --> avoids the parsing bug. e.g:

%sql select '{"key": 1}'::jsonb -> 'key' returns an error, but

%sql select '{"key": 1}'::jsonb-> 'key' works fine.

@maciejb
Copy link
Author

maciejb commented Dec 1, 2023

Not sure whether to open a new issue or not, but I found another case of this issue which #938 did not address: operations with negative numbers. For example:

image

@edublancas
Copy link

@maciejb: please open another issue!

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

Successfully merging a pull request may close this issue.

3 participants