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

types/json: fix an over-quoted bug in BinaryJSON.Unquote function #11934

Merged
merged 2 commits into from
Aug 30, 2019

Conversation

bb7133
Copy link
Member

@bb7133 bb7133 commented Aug 29, 2019

What problem does this PR solve?

This PR tries to fix the following issue:

create table a(a json);
insert into a values ("{\"low_keywords\":\"[{\\\"x\\\":\\\"{\\\\\\\"y\\\\\\\":12}\\\"}]\"}");

In current TiDB(master/3.0/2.1):

tidb> select a->>"$.low_keywords" from a;
+----------------------+
| a->>"$.low_keywords" |
+----------------------+
| [{"x":"{"y":12}"}] |
+----------------------+
1 rows in set (0.02 sec)

In MySQL 5.7/8.0:

tidb> select a->>"$.low_keywords" from a;
+----------------------+
| a->>"$.low_keywords" |
+----------------------+
| [{"x":"{\"y\":12}"}] |
+----------------------+
1 rows in set (0.02 sec)

What is changed and how it works?

The issue is caused by the over-unquote behaviors of BinaryJSON.Unquote: for a string literal of JSON, it should be unquoted only when enclosed by double quotes(")

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
tidb> select json_unquote("\"[{\\\"x\\\":\\\"{\\\\\\\"y\\\\\\\":12}\\\"}]\"");
+------------------------------------------------------------------+
| json_unquote("\"[{\\\"x\\\":\\\"{\\\\\\\"y\\\\\\\":12}\\\"}]\"") |
+------------------------------------------------------------------+
| [{"x":"{\"y\":12}"}]                                             |
+------------------------------------------------------------------+
1 row in set (0.00 sec)

tidb> select json_unquote("\\\\");
+----------------------+
| json_unquote("\\\\") |
+----------------------+
| \\                   |
+----------------------+
1 row in set (0.00 sec)
  • No code

Code changes

  • Has exported function/method change

Side effects

N/A

Related changes

  • Need to cherry-pick to the release branch

Release note

Fix an over-quoted bug for JSON_UNQUOTE function: only values enclosed by the double quote marks(") should be unquoted. For example, result of SELECT JSON_UNQUOTE("\\\\") should be \\(not changed).

@bb7133 bb7133 force-pushed the bb7133/fix_json_unquote branch from 34fcb9f to 9190621 Compare August 29, 2019 09:40
@codecov
Copy link

codecov bot commented Aug 29, 2019

Codecov Report

Merging #11934 into master will decrease coverage by 0.0055%.
The diff coverage is 100%.

@@               Coverage Diff                @@
##             master     #11934        +/-   ##
================================================
- Coverage   81.2155%   81.2099%   -0.0056%     
================================================
  Files           443        443                
  Lines         95004      95013         +9     
================================================
+ Hits          77158      77160         +2     
- Misses        12351      12356         +5     
- Partials       5495       5497         +2

@bb7133 bb7133 added the type/bugfix This PR fixes a bug. label Aug 29, 2019
@bb7133
Copy link
Member Author

bb7133 commented Aug 29, 2019

/run-all-tests

Copy link
Contributor

@winkyao winkyao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

// Remove prefix and suffix '"'.
s, err := unquoteString(tmp[1 : tlen-1])
if err != nil {
return s, errors.Trace(err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove the Trace.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error is returned by unquoteString, so I think it is better to use Trace? @lonng

Copy link
Contributor

@lonng lonng Aug 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bb7133 All errors returned by unquoteString have been wrapt withTrace. (anyway, not a strong opinion)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

Copy link
Contributor

@lonng lonng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rest LGTM

lonng
lonng previously approved these changes Aug 30, 2019
@bb7133
Copy link
Member Author

bb7133 commented Aug 30, 2019

/run-all-tests

@lonng lonng added the status/can-merge Indicates a PR has been approved by a committer. label Aug 30, 2019
@sre-bot
Copy link
Contributor

sre-bot commented Aug 30, 2019

/run-all-tests

@lonng
Copy link
Contributor

lonng commented Aug 30, 2019

@bb7133 Do we need to cherry-pick to release branches?

@sre-bot sre-bot merged commit e415ced into pingcap:master Aug 30, 2019
@sre-bot
Copy link
Contributor

sre-bot commented Aug 30, 2019

cherry pick to release-2.1 failed

@sre-bot
Copy link
Contributor

sre-bot commented Aug 30, 2019

cherry pick to release-3.0 in PR #11955

@cyliu0
Copy link
Contributor

cyliu0 commented Nov 6, 2019

@bb7133 Do we need to cherry pick this one to release-3.1 ?

@bb7133 bb7133 deleted the bb7133/fix_json_unquote branch December 5, 2019 09:42
@sre-bot
Copy link
Contributor

sre-bot commented Apr 7, 2020

It seems that, not for sure, we failed to cherry-pick this commit to release-2.1. Please comment '/run-cherry-picker' to try to trigger the cherry-picker if we did fail to cherry-pick this commit before. @bb7133 PTAL.

@bb7133
Copy link
Member Author

bb7133 commented Apr 9, 2020

/run-cherry-picker

@sre-bot
Copy link
Contributor

sre-bot commented Apr 9, 2020

cherry pick to release-2.1 failed

@you06
Copy link
Contributor

you06 commented Apr 9, 2020

Already cherry picked in #12096.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/json status/can-merge Indicates a PR has been approved by a committer. type/bugfix This PR fixes a bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants