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

expression: add builtin json_array_append #9609

Merged
merged 17 commits into from
Mar 14, 2019

Conversation

erjiaqing
Copy link
Contributor

@erjiaqing erjiaqing commented Mar 8, 2019

What problem does this PR solve?

a part of #7546

What is changed and how it works?

  • Get the JSON object from first argument.
  • Iterate on every (path, obj) pair
    o. if path is null, do nothing and return null without checking other errors
    o. if path is illegal (invalid or contains wildcard), throw error
    o. if json object at path is not an array, warp it with a array.
    o. append the obj to that array with json.MergeBinary
    o. call Modify to commit change

Original MySQL document:

Appends values to the end of the indicated arrays within a JSON document and returns the result. Returns NULL if any argument is NULL. An error occurs if the json_doc argument is not a valid JSON document or any path argument is not a valid path expression or contains a * or ** wildcard.

The path-value pairs are evaluated left to right. The document produced by evaluating one pair becomes the new value against which the next pair is evaluated.

If a path selects a scalar or object value, that value is autowrapped within an array and the new value is added to that array. Pairs for which the path does not identify any value in the JSON document are ignored.

Observed behavior on MySQL 8.0:

MySQL [(none)]> SELECT JSON_ARRAY_APPEND("[]", null, 1, "aaaa", "aaaa");show warnings;
+--------------------------------------------------+
| JSON_ARRAY_APPEND("[]", null, 1, "aaaa", "aaaa") |
+--------------------------------------------------+
| NULL                                             |
+--------------------------------------------------+
1 row in set (0.001 sec)

Empty set (0.000 sec)
MySQL [(none)]> SELECT JSON_ARRAY_APPEND(null, null, 1, "aaaa", "aaaa");show warnings;
+--------------------------------------------------+
| JSON_ARRAY_APPEND(null, null, 1, "aaaa", "aaaa") |
+--------------------------------------------------+
| NULL                                             |
+--------------------------------------------------+
1 row in set (0.001 sec)

Empty set (0.001 sec)

MySQL [(none)]> SELECT JSON_ARRAY_APPEND("", null, 1, "aaaa", "aaaa");show warnings;
ERROR 3141 (22032): Invalid JSON text in argument 1 to function json_array_append: "The document is empty." at position 0.
+-------+------+--------------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                                |
+-------+------+--------------------------------------------------------------------------------------------------------+
| Error | 3141 | Invalid JSON text in argument 1 to function json_array_append: "The document is empty." at position 0. |
+-------+------+--------------------------------------------------------------------------------------------------------+
1 row in set (0.000 sec)

MySQL [(none)]> SELECT JSON_ARRAY_APPEND("[", null, 1, "aaaa", "aaaa");show warnings;
ERROR 3141 (22032): Invalid JSON text in argument 1 to function json_array_append: "Invalid value." at position 1.
+-------+------+------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                        |
+-------+------+------------------------------------------------------------------------------------------------+
| Error | 3141 | Invalid JSON text in argument 1 to function json_array_append: "Invalid value." at position 1. |
+-------+------+------------------------------------------------------------------------------------------------+
1 row in set (0.001 sec)

MySQL [(none)]> SELECT JSON_ARRAY_APPEND("null", "$", null);show warnings;
+--------------------------------------+
| JSON_ARRAY_APPEND("null", "$", null) |
+--------------------------------------+
| [null, null]                         |
+--------------------------------------+
1 row in set (0.001 sec)

Empty set (0.001 sec)

MySQL [(none)]> SELECT JSON_ARRAY_APPEND(null, "a", null);show warnings;
+------------------------------------+
| JSON_ARRAY_APPEND(null, "a", null) |
+------------------------------------+
| NULL                               |
+------------------------------------+
1 row in set (0.001 sec)

Empty set (0.001 sec)

Check List

Tests

  • Unit test

Code changes

  • Has exported function/method change

@morgo morgo added contribution This PR is from a community contributor. component/expression labels Mar 8, 2019
@morgo
Copy link
Contributor

morgo commented Mar 8, 2019

Thank you for your contribution! It looks like the CI is broken on errcheck.

@codecov-io
Copy link

codecov-io commented Mar 8, 2019

Codecov Report

Merging #9609 into master will increase coverage by 0.0087%.
The diff coverage is 80%.

@@               Coverage Diff               @@
##             master     #9609        +/-   ##
===============================================
+ Coverage   67.3893%   67.398%   +0.0087%     
===============================================
  Files           377       377                
  Lines         79385     79434        +49     
===============================================
+ Hits          53497     53537        +40     
- Misses        21111     21115         +4     
- Partials       4777      4782         +5

@zz-jason zz-jason removed the contribution This PR is from a community contributor. label Mar 8, 2019
@qw4990 qw4990 self-requested a review March 8, 2019 06:27
@zz-jason zz-jason requested a review from eurekaka March 8, 2019 06:29
expression/builtin_json.go Outdated Show resolved Hide resolved
expression/builtin_json_test.go Outdated Show resolved Hide resolved
expression/builtin_json.go Outdated Show resolved Hide resolved
@zhouqiang-cl
Copy link
Contributor

/run-all-tests

@morgo
Copy link
Contributor

morgo commented Mar 10, 2019

/run-all-tests

@eurekaka
Copy link
Contributor

/run-all-tests

expression/builtin_json.go Outdated Show resolved Hide resolved
expression/builtin_json.go Outdated Show resolved Hide resolved
expression/builtin_json.go Outdated Show resolved Hide resolved
expression/builtin_json.go Show resolved Hide resolved
expression/builtin_json.go Outdated Show resolved Hide resolved
expression/builtin_json.go Outdated Show resolved Hide resolved
expression/builtin_json.go Outdated Show resolved Hide resolved
@lzmhhh123
Copy link
Contributor

/run-all-tests

expression/builtin_json_test.go Outdated Show resolved Hide resolved
expression/builtin_json_test.go Outdated Show resolved Hide resolved
expression/builtin_json_test.go Outdated Show resolved Hide resolved
Copy link
Contributor

@eurekaka eurekaka left a comment

Choose a reason for hiding this comment

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

LGTM

@eurekaka eurekaka added the status/LGT1 Indicates that a PR has LGTM 1. label Mar 12, 2019
expression/builtin_json.go Outdated Show resolved Hide resolved
expression/builtin_json.go Outdated Show resolved Hide resolved
expression/builtin_json.go Outdated Show resolved Hide resolved
expression/builtin_json.go Outdated Show resolved Hide resolved
Copy link
Member

@zz-jason zz-jason left a comment

Choose a reason for hiding this comment

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

LGTM

@zz-jason zz-jason added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Mar 14, 2019
@zz-jason zz-jason merged commit ec7514f into pingcap:master Mar 14, 2019
@erjiaqing erjiaqing deleted the json_ARRAY_APPEND branch March 24, 2019 02:18
@morgo morgo mentioned this pull request Jun 24, 2019
5 tasks
@sre-bot sre-bot added the contribution This PR is from a community contributor. label Dec 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/expression contribution This PR is from a community contributor. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants