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 function json_pretty #24675

Merged
merged 8 commits into from
May 20, 2021

Conversation

jianzhiyao
Copy link
Contributor

@jianzhiyao jianzhiyao commented May 14, 2021

What problem does this PR solve?

Issue Number: part of #14486

Problem Summary:

add builtin function json_pretty

What is changed and how it works?

add builtin function json_pretty

Check List

Tests

  • Unit test
  • Integration test

Integration test

Preparation

use test;
drop table if exists t;
CREATE TABLE t  (`id` int NOT NULL AUTO_INCREMENT, `j` json,vc VARCHAR(500) ,  PRIMARY KEY (`id`));
INSERT INTO t ( id, j, vc )
VALUES
	( 1, '{"a":1,"b":"qwe","c":[1,2,3,"123",null],"d":{"d1":1,"d2":2}}', '{"a":1,"b":"qwe","c":[1,2,3,"123",null],"d":{"d1":1,"d2":2}}' ),
	( 2, '[1,2,34]', '{' );

Run in mysql

select JSON_PRETTY(t.j),JSON_PRETTY(vc) from  t where id = 1;
select JSON_PRETTY(t.j),JSON_PRETTY(vc) from  t where id = 2;
select JSON_PRETTY("[1,2,3]}");
Database changed
Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

+------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------+
| JSON_PRETTY(t.j)                                                                                           | JSON_PRETTY(vc)                                                                                            |
+------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------+
| {
  "a": 1,
  "b": "qwe",
  "c": [
    1,
    2,
    3,
    "123",
    null
  ],
  "d": {
    "d1": 1,
    "d2": 2
  }
} | {
  "a": 1,
  "b": "qwe",
  "c": [
    1,
    2,
    3,
    "123",
    null
  ],
  "d": {
    "d1": 1,
    "d2": 2
  }
} |
+------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------+
1 row in set (0.06 sec)

Invalid JSON text in argument 1 to function json_pretty: "Missing a name for object member." at position 1.
Invalid JSON text in argument 1 to function json_pretty: "The document root must not be followed by other values." at position 7.

Run in tidb

select JSON_PRETTY(t.j),JSON_PRETTY(vc) from  t where id = 1;
select JSON_PRETTY(t.j),JSON_PRETTY(vc) from  t where id = 2;
select JSON_PRETTY("[1,2,3]}");
Database changed
Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

+------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------+
| JSON_PRETTY(t.j)                                                                                           | JSON_PRETTY(vc)                                                                                            |
+------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------+
| {
  "a": 1,
  "b": "qwe",
  "c": [
    1,
    2,
    3,
    "123",
    null
  ],
  "d": {
    "d1": 1,
    "d2": 2
  }
} | {
  "a": 1,
  "b": "qwe",
  "c": [
    1,
    2,
    3,
    "123",
    null
  ],
  "d": {
    "d1": 1,
    "d2": 2
  }
} |
+------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------+
1 row in set (0.05 sec)

3140 - Invalid JSON text: The document root must not be followed by other values.
3140 - Invalid JSON text: The document root must not be followed by other values.

Side effects

  • Performance regression
    • Consumes more CPU
    • Consumes more MEM

Release note

  • add builtin function json_pretty

@jianzhiyao jianzhiyao requested a review from a team as a code owner May 14, 2021 15:06
@jianzhiyao jianzhiyao requested review from lzmhhh123 and removed request for a team May 14, 2021 15:06
@ti-chi-bot ti-chi-bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label May 14, 2021
@ti-srebot
Copy link
Contributor

@ti-srebot
Copy link
Contributor

@jianzhiyao jianzhiyao changed the title *: add builtin function json_pretty executor: add builtin function json_pretty May 14, 2021
@jianzhiyao
Copy link
Contributor Author

/run-check_release_note

@morgo
Copy link
Contributor

morgo commented May 14, 2021

@jiangyuzhao Thank you for adding this, it's very useful!

May I ask that you add some tests, such as:

  • An integration test showing json_pretty applied on a column with the JSON data type.
  • An integration test showing json_pretty applied to a varchar column (with inconsistent formatting)
  • An integration test showing json_pretty applied to invalid JSON data, showing the same error as MySQL.

@jianzhiyao
Copy link
Contributor Author

@jiangyuzhao Thank you for adding this, it's very useful!

May I ask that you add some tests, such as:

  • An integration test showing json_pretty applied on a column with the JSON data type.
  • An integration test showing json_pretty applied to a varchar column (with inconsistent formatting)
  • An integration test showing json_pretty applied to invalid JSON data, showing the same error as MySQL.

I make some test in tidb and mysql.
And I found their behavior is different when applying to invalid JSON data, which seems belong to another compatibility problem.

@morgo
Copy link
Contributor

morgo commented May 17, 2021

@jiangyuzhao Thank you for adding this, it's very useful!
May I ask that you add some tests, such as:

  • An integration test showing json_pretty applied on a column with the JSON data type.
  • An integration test showing json_pretty applied to a varchar column (with inconsistent formatting)
  • An integration test showing json_pretty applied to invalid JSON data, showing the same error as MySQL.

I make some test in tidb and mysql.
And I found their behavior is different when applying to invalid JSON data, which seems belong to another compatibility problem.

Can you add the tests in the PR description to expression/integration_test.go. This will allow us to run the tests automatically. Thanks!

@ti-chi-bot ti-chi-bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels May 17, 2021
@jianzhiyao jianzhiyao force-pushed the frt/builtin_json_pretty branch 2 times, most recently from 95f1e53 to e599119 Compare May 17, 2021 06:34
@jianzhiyao jianzhiyao force-pushed the frt/builtin_json_pretty branch from e599119 to a3e5a9d Compare May 17, 2021 06:48
@jianzhiyao
Copy link
Contributor Author

PTAL @morgo

@morgo
Copy link
Contributor

morgo commented May 17, 2021

LGTM (non binding)

Sorry, I am not a component/expression reviewer. I will bring it up in #sig-exec on TiDB community slack.

@jianzhiyao jianzhiyao changed the title executor: add builtin function json_pretty expression: add builtin function json_pretty May 18, 2021
@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label May 18, 2021
expression/builtin_json_vec.go Outdated Show resolved Hide resolved
expression/integration_test.go Outdated Show resolved Hide resolved
expression/builtin_json_test.go Outdated Show resolved Hide resolved
@ti-chi-bot ti-chi-bot removed the status/LGT1 Indicates that a PR has LGTM 1. label May 19, 2021
@jianzhiyao
Copy link
Contributor Author

/run-check_dev_2

@jianzhiyao
Copy link
Contributor Author

PTAL again @pingyu

@pingyu
Copy link
Contributor

pingyu commented May 19, 2021

/lgtm

@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label May 19, 2021
@jianzhiyao
Copy link
Contributor Author

PTAL again @lzmhhh123

@ti-chi-bot
Copy link
Member

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • lzmhhh123
  • pingyu

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by writing /lgtm in a comment.
Reviewer can cancel approval by writing /lgtm cancel in a comment.

@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels May 20, 2021
@lzmhhh123
Copy link
Contributor

/merge

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: b690785

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label May 20, 2021
@jianzhiyao
Copy link
Contributor Author

jianzhiyao commented May 20, 2021

Why the uint-test fails so many time? i can not get any useful info in error message
@lzmhhh123

@ti-chi-bot
Copy link
Member

@jianzhiyao: Your PR was out of date, I have automatically updated it for you.

At the same time I will also trigger all tests for you:

/run-all-tests

If the CI test fails, you just re-trigger the test that failed and the bot will merge the PR for you after the CI passes.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

@jianzhiyao
Copy link
Contributor Author

/run-unit-test

@ti-chi-bot ti-chi-bot merged commit ca23f55 into pingcap:master May 20, 2021
Howie59 pushed a commit to Howie59/tidb that referenced this pull request May 21, 2021
…ingcap#24052)

* *: fix revoke statement for CURRENT_USER() and refine error message

planner: support set tidb_allow_mpp to `2` or `ENFORCE` to enforce use mpp mode. (pingcap#24516)

store/tikv: remove use of SchemaAmender option in store/tikv (pingcap#24408)

*: the value of tikv-client.store-liveness-timeout should not less than 0 (pingcap#24244)

store/tikv: remove use of EnableAsyncCommit option in store/tikv (pingcap#24462)

txn: Add txn state's view (pingcap#22908)

planner: ignore lock for temporary table of PointGet and BatchPointGet (pingcap#24540)

store/tikv: remove use of ReplicaRead transaction option in store/tikv (pingcap#24409)

store/driver: move error to single package (pingcap#24549)

ddl: add check table compatibility for temporary table (pingcap#24501)

store/tikv: remove use of IsStatenessReadOnly option in store/tikv (pingcap#24464)

store/tikv: change backoff type for missed tiflash peer. (pingcap#24577)

store/tikv: remove use of MatchStoreLabels transaction option in store/tikv (pingcap#24465)

executor, meta: Allocate auto id for global temporary tables (pingcap#24506)

store/tikv: remove use of SampleStep option in store/tikv (pingcap#24461)

executor: add partition pruning tests for adding and dropping partition operations (pingcap#24573)

ddl: forbid partition on temporary mode before put into queue (pingcap#24565)

ddl: speedup test case TestIndexOnMultipleGeneratedColumn (pingcap#24487)

execution: Fix issue 24439 Inconsistent error with MySQL for GRANT CREATE USER ON <specific db>.* (pingcap#24485)

*: fix errcheck (pingcap#24463)

test: make TestExtractStartTs stable (pingcap#24585)

ddl: forbid recover/flashback temporary tables (pingcap#24518)

executor: fix point_get result on clustered index when new-row-format disabled but new-collation enabled (pingcap#24544)

executor: Improve the performance of appending not fixed columns (pingcap#20969)

*: typo fix (pingcap#24564)

planner/core: refresh stale regions in cache for batch cop response (pingcap#24457)

binlog: DML on temporary tables do not write binlog (pingcap#24570)

store/tikv: remove use of GuaranteeLinearizability option in store/tikv (pingcap#24605)

store/tikv: remove use of CollectRuntimeStats option in store/tikv (pingcap#24604)

store/tikv: move Backoffer into a single package (pingcap#24525)

variables: init cte max recursive deeps in a new session (pingcap#24609)

store/tikv: move transaction options out to /kv (pingcap#24619)

store/driver: move backoff driver into single package so we can use i… (pingcap#24624)

server: close the temporary session in HTTP API to avoid memory leak (pingcap#24339)

store/tikv: use latest PD TS plus one as min commit ts (pingcap#24579)

planner: fix incorrect TableDual plan built from nulleq (pingcap#24596)

ranger: fix the case which could have duplicate ranges (pingcap#24590)

 executor, store: Pass the SQL digest down to pessimistic lock request (pingcap#24380)

kv: remove UnionStore interface (pingcap#24625)

*: enable gosimple linter (pingcap#24617)

txn: avoid the gc resolving pessimistic locks of ongoing transactions (pingcap#24601)

util: fix wrong enum building for index range  (pingcap#24632)

sessionctx: change innodb large prefix default (pingcap#24555)

store: fix data race about KVStore.tikvClient (pingcap#24655)

executor, privileges: Add dynamic privileges to SHOW PRIVILEGES (pingcap#24646)

ddl: refactor rule [4/6] (pingcap#24007)

cmd: ddl_test modify retryCnt from 5 to 20 (pingcap#24662)

executor: add correctness tests about direct reading with ORDER BY and LIMIT (pingcap#24455)

store/tikv: remove options from unionstore (pingcap#24629)

planner: fix wrongly check for update statement (pingcap#24614)

store/tikv: remove CompareTS (pingcap#24657)

planner, privilege: Add security enhanced mode part 4 (pingcap#24416)

executor: add some test cases about partition table dynamic-mode with split-region (pingcap#24665)

planner: fix wrong column offsets when processing dynamic pruning for IndexJoin (pingcap#24659)

*: Add security enhanced mode part 3 (pingcap#24412)

store/tikv: resolve ReplicaReadType dependencies (pingcap#24653)

executor: add test cases about partition table with `expression` (pingcap#24628)

tablecodec: fix write wrong prefix index value when collation is ascii_bin/latin1_bin (pingcap#24578)

*: compatibility with staleread (pingcap#24285)

session: test that temporary tables will also be retried (pingcap#24505)

domain, session: Add new sysvarcache to replace global values cache (pingcap#24359)

ddl, transaction: DDL on temporary tables won't affect transactions (pingcap#24534)

*: implement tidb_bounded_staleness built-in function (pingcap#24328)

executor: add correctness tests for partition table with different joins (pingcap#24673)

expression: fix the spelling of word arithmetical (pingcap#24713)

store/copr: balance region for batch cop task (pingcap#24521)

store, metrics: Add metrics for safetTS updating (pingcap#24687)

sem: add tidbredact log to restricted variables (pingcap#24701)

session: fix dml_batch_size doesn't load the global variable (pingcap#24710)

store/tikv: retry TSO RPC (pingcap#24682)

expression, planner: push cast down to control function with enum type. (pingcap#24542)

executor: add correctness tests about IndexMerge (pingcap#24674)

variable: change default for DefDMLBatchSize tidbOptInt64 call (pingcap#24697)

planner: add partitioning pruning tests for range partitioning (pingcap#24554)

*: add option for enum push down (pingcap#24685)

txn: break dependency from store/tikv to tidb/kv cause by TransactionOption (pingcap#24656)

executor: enhancement for ListInDisk(support writing after reading) (pingcap#24379)

kv: move TxnScope into kv (pingcap#24715)

execution: fix the incorrect use of cached plan for point get (pingcap#24749)

executor: add correctness tests about direct reading with indexJoin (pingcap#24497)

variable:  fix sysvar datarace with deep copy (pingcap#24732)

*: Implementing RENAME USER (pingcap#24413)

infoschema, executor: Add the deadlock table (pingcap#24524)

docs: Some proposal for renaming and configurations for Lock View (pingcap#24718)

planner: add range partition boundaries tests with BETWEEN expression (pingcap#24598)

oracle: simplify timestamp utilities (pingcap#24688)

executor: fix wrong enum key in point get (pingcap#24618)

ranger: fix incorrect enum range for xxx_ci collation (pingcap#24661)

executor: add some test cases about dynamic-mode and apply operator (pingcap#24683)

store/tikv: remove Variables.Hook (pingcap#24758)

ddl: speed up the execution time of `TestBackwardCompatibility`. (pingcap#24704)

*: prepare errors for CTE (pingcap#24763)

expression: support cast real/int as real (pingcap#24670)

executor: add table name in log (pingcap#24666)

expression: add builtin function ``json_pretty`` (pingcap#24675)

ddl: make `TestDropLastVisibleColumns` stable (pingcap#24790)

* ddl: make `TestDropLastVisibleColumns` stable

*: support AS OF TIMESTAMP read-only begin statement (pingcap#24740)

executor: Fix unstable TestTiDBLastTxnInfoCommitMode (pingcap#24779)

planner: add tests for partition range boundaries for LT/GT (pingcap#24574)

test: record random seed in TestIssue20658 (pingcap#24782)

store/tikv/retry: define Config instead of BackoffType (pingcap#24692)

config: ignore tiflash when show config (pingcap#24770)

privileges: improve dynamic privs registration and tests (pingcap#24773)

README: remove the link of TiDB Monthly Update (pingcap#24791)

region_cache: filter peers on tombstone or dropped stores (pingcap#24726)

util/stmtsummary: remove import package tikv (pingcap#24776)

ddl: grammar check for create unsupported temporary table (pingcap#24723)

*: update go.etcd.io/bbolt (pingcap#24799)

ddl: speed up the execution time of `ddl test` and `Test Chunk pingcap#7 ddl-other` (pingcap#24780)

executor: remove the unnecessary use of fmt.Sprintf (pingcap#24815)

executor: fix index join panic on prefix index on some cases (pingcap#24568)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/expression size/L Denotes a PR that changes 100-499 lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants