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

json: extract the $[last] entry from json object should also return itself #59687

Merged
merged 1 commit into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/types/json_binary_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,13 @@ func (bj BinaryJSON) extractTo(buf []BinaryJSON, pathExpr JSONPathExpression, du
if currentLeg.typ == jsonPathLegArraySelection {
if bj.TypeCode != JSONTypeCodeArray {
// If the current object is not an array, still append them if the selection includes
// 0. But for asterisk, it still returns NULL.
// 0 or last. But for asterisk, it still returns NULL.
//
// don't call `getIndexRange` or `getIndexFromStart`, they will panic if the argument
// is not array.
switch selection := currentLeg.arraySelection.(type) {
case jsonPathArraySelectionIndex:
if selection.index == 0 {
if selection.index == 0 || selection.index == -1 {
buf = bj.extractTo(buf, subPathExpr, dup, one)
}
case jsonPathArraySelectionRange:
Expand Down
9 changes: 9 additions & 0 deletions tests/integrationtest/r/expression/json.result
Original file line number Diff line number Diff line change
Expand Up @@ -852,3 +852,12 @@ json_extract("{\"\\b\":\"\"}", "$")
select json_extract("{\"\\f\":\"\"}", "$");
json_extract("{\"\\f\":\"\"}", "$")
{"\f": ""}
select json_extract('{"a":"b"}', '$[0]');
json_extract('{"a":"b"}', '$[0]')
{"a": "b"}
select json_extract('{"a":"b"}', '$[last]');
json_extract('{"a":"b"}', '$[last]')
{"a": "b"}
select json_set('{"a":"b"}', '$[last]', 1);
json_set('{"a":"b"}', '$[last]', 1)
1
5 changes: 5 additions & 0 deletions tests/integrationtest/t/expression/json.test
Original file line number Diff line number Diff line change
Expand Up @@ -560,3 +560,8 @@ select json_extract('[1E27]', '$');
# TestIssue58897
select json_extract("{\"\\b\":\"\"}", "$");
select json_extract("{\"\\f\":\"\"}", "$");

# TestJSONExtractObjectFromLast
select json_extract('{"a":"b"}', '$[0]');
select json_extract('{"a":"b"}', '$[last]');
select json_set('{"a":"b"}', '$[last]', 1);