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

Projects
None yet
8 participants
@erjiaqing
Copy link
Contributor

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

This comment has been minimized.

Copy link
Contributor

commented Mar 8, 2019

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

@codecov-io

This comment has been minimized.

Copy link

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

@erjiaqing erjiaqing force-pushed the erjiaqing:json_ARRAY_APPEND branch from 61a89d5 to 1c3269c Mar 8, 2019

@zz-jason zz-jason removed the contribution label Mar 8, 2019

@qw4990 qw4990 self-requested a review Mar 8, 2019

@zz-jason zz-jason requested a review from eurekaka Mar 8, 2019

Show resolved Hide resolved expression/builtin_json.go Outdated
Show resolved Hide resolved expression/builtin_json_test.go Outdated
Show resolved Hide resolved expression/builtin_json.go Outdated
@zhouqiang-cl

This comment has been minimized.

Copy link
Member

commented Mar 9, 2019

/run-all-tests

@morgo

This comment has been minimized.

Copy link
Contributor

commented Mar 10, 2019

/run-all-tests

erjiaqing and others added some commits Mar 11, 2019

@eurekaka

This comment has been minimized.

Copy link
Contributor

commented Mar 11, 2019

/run-all-tests

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 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

erjiaqing added some commits Mar 11, 2019

@lzmhhh123

This comment has been minimized.

Copy link
Member

commented Mar 12, 2019

/run-all-tests

Show resolved Hide resolved 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

@erjiaqing erjiaqing force-pushed the erjiaqing:json_ARRAY_APPEND branch from 75a5aa8 to 09026d7 Mar 12, 2019

@erjiaqing erjiaqing force-pushed the erjiaqing:json_ARRAY_APPEND branch from 09026d7 to 2c7506c Mar 12, 2019

@eurekaka
Copy link
Contributor

left a comment

LGTM

@eurekaka eurekaka requested review from qw4990 and zz-jason Mar 12, 2019

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 expression/builtin_json.go Outdated
@zz-jason
Copy link
Member

left a comment

LGTM

@zz-jason zz-jason added status/LGT2 and removed status/LGT1 labels Mar 14, 2019

@zz-jason zz-jason merged commit ec7514f into pingcap:master Mar 14, 2019

8 checks passed

ci/circleci Your tests passed on CircleCI!
Details
codecov/patch 80% of diff hit (target 0%)
Details
codecov/project 67.3678% (+0.002%) compared to 9cc5733
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
idc-jenkins-ci-tidb/build Jenkins job succeeded.
Details
idc-jenkins-ci-tidb/check_dev Jenkins job succeeded.
Details
idc-jenkins-ci-tidb/code_coverage Jenkins job succeeded.
Details
license/cla Contributor License Agreement is signed.
Details

@erjiaqing erjiaqing deleted the erjiaqing:json_ARRAY_APPEND branch Mar 24, 2019

@morgo morgo referenced this pull request Jun 24, 2019

Merged

*: recent 3.0 fixes #1257

5 of 5 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.