Skip to content

All array element accessors require non-array items to be wrapped #3

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

Closed
wants to merge 74 commits into from

Conversation

pstef
Copy link
Contributor

@pstef pstef commented Nov 5, 2017

Both SELECT JSON_QUERY(jsonb '{}', '{"a":"b"}[*]'); and SELECT JSON_QUERY(jsonb '{}', '{}["bread"]'); should return values (the latter query should return a null value).

Nikita Glukhov and others added 30 commits November 4, 2017 01:27
Main TODO
	- indexed arrays [1,4, 6 to 18]
	- variables execution ( $varname )
	- function support
	- ariphmetic support
	- correct error support

It provides two debugging functions: _jsonpath_object and _jsonpath_exist
Some examples are avaliable in src/test/regress/sql/sql_json.sql
@pstef
Copy link
Contributor Author

pstef commented Nov 5, 2017

This is a separate pull request, because I discovered this issue the next day I created PR #3. I didn't unify the two commits by creating a new PR, because this change could use some discussion.

My main concern is that '{}["bread"]'::jsonpath is allowed at the parsing level. The technical report I'm using to learn the SQL/JSON path language specifically says:

The subscripts can be specified in either of two forms:

  1. A single numeric value.
  2. A range between two numeric values (inclusive) indicated by the keyword to .

and specifically forbids strings:

In both strict and lax mode, non-numeric subscripts such as $["hello"] are an error.

On the other hand, the BNF they give just above the paragraph I selectively quoted says that JSON array accessor is a bracketed JSON subscript list which if a list of JSON path wff - and JSON path wff appears to be the entire language specification, excluding the mode name.

Even if you decide to allow strings to be elements of array subscripts, I'm not sure why unquoted character sequences like in the following example are allowed:
SELECT JSON_QUERY(jsonb '{}', '{}[bread]');

@glukhovn
Copy link

glukhovn commented Nov 8, 2017

I agree that unquoted string literals should not be allowed in jsonpath grammar. Now they are not distinguished from quoted literals in grammar, so I'll have to fix this issue.

Object subscripting with string subscripts is our extension (see commit 63113dc where processing of jpiIndexArray had been moved into recursiveExecuteNoUnwrap()).

Here are examples showing differences in behavior between lax and strict mode:

postgres=# SELECT JSON_QUERY(jsonb '[]', 'strict $[0]' ERROR ON EMPTY ERROR ON ERROR);
ERROR: Invalid SQL/JSON subscript
postgres=# SELECT JSON_QUERY(jsonb '[]', 'lax $[5]' ERROR ON EMPTY ERROR ON ERROR);
ERROR: no SQL/JSON item

postgres=# SELECT JSON_QUERY(jsonb '{}', 'strict $[5]' ERROR ON EMPTY ERROR ON ERROR);
ERROR: Invalid SQL/JSON subscript
postgres=# SELECT JSON_QUERY(jsonb '{}', 'lax $[5]' ERROR ON EMPTY ERROR ON ERROR);
ERROR: no SQL/JSON item
postgres=# SELECT JSON_QUERY(jsonb '{}', 'lax $[0]' ERROR ON EMPTY ERROR ON ERROR);
?column?
----------
{}
(1 row)

postgres=# SELECT JSON_QUERY(jsonb '{"a": 1}', 'lax $["b"]' ERROR ON EMPTY ERROR ON ERROR);
ERROR: no SQL/JSON item
postgres=# SELECT JSON_QUERY(jsonb '{"a": 1}', 'strict $["b"]' ERROR ON EMPTY ERROR ON ERROR);
ERROR: SQL/JSON member not found
postgres=# SELECT JSON_QUERY(jsonb '{"a": 1}', '$["a"]' ERROR ON EMPTY ERROR ON ERROR);
?column?
----------
1
(1 row)

@pstef
Copy link
Contributor Author

pstef commented Nov 10, 2017

This is a separate pull request, because I discovered this issue the next day I created PR #3.

I meant PR #2 there.

Object subscripting with string subscripts is our extension

I see. This should be documented somewhere.

I guess this PR boils down to "SELECT JSON_QUERY(jsonb '{}', '{}["bread"]'); shouldn't segfault".

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 this pull request may close these issues.

3 participants