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 tidb_decode_key() #12193

Merged
merged 21 commits into from
Sep 24, 2019

Conversation

crazycs520
Copy link
Contributor

@crazycs520 crazycs520 commented Sep 16, 2019

What problem does this PR solve?

  • Add tidb_decode_key() function to decode key.

Related Parser PR: pingcap/parser#560

What is changed and how it works?

-- decode region key.
mysql>select region_id, tidb_decode_key(start_key) as k from `TIKV_REGION_STATUS` where tidb_decode_key(start_key) like 'tableID=%' limit 3;
+-----------+---------------------------------------------------------------+
| region_id | k                                                             |
+-----------+---------------------------------------------------------------+
| 107       | tableID=43, handle=300000000                                  |
| 128       | tableID=43, indexID=1, indexValues=0380000000165A0BC003000000 |
| 154       | tableID=43, indexID=1, indexValues=03800000000EE6B28003000000 |
+-----------+---------------------------------------------------------------+

mysql>select tidb_decode_key( '7480000000000000FF2E5F698000000000FF0000010380000000FF05F5E10003000000FF0000000000000000F7')
+----------------------------------------------------------------------------------------------------------------+
| tidb_decode_key( '7480000000000000FF2E5F698000000000FF0000010380000000FF05F5E10003000000FF0000000000000000F7') |
+----------------------------------------------------------------------------------------------------------------+
| tableID=46, indexID=1, indexValues=038000000005F5E10003000000                                                  |
+----------------------------------------------------------------------------------------------------------------+

mysql>select tidb_decode_key( '7480000000000000FF2E5F728000000011FFE1A3000000000000FA')
+----------------------------------------------------------------------------+
| tidb_decode_key( '7480000000000000FF2E5F728000000011FFE1A3000000000000FA') |
+----------------------------------------------------------------------------+
| tableID=46, handle=300000000                                               |
+----------------------------------------------------------------------------+

-- if the key is not record/index kye, just do nothing.
mysql>select tidb_decode_key( '7480000000000000FF2E5F698000000000FF0000010380000000FF05F5E10003000000FF0000000000000000F')
+---------------------------------------------------------------------------------------------------------------+
| tidb_decode_key( '7480000000000000FF2E5F698000000000FF0000010380000000FF05F5E10003000000FF0000000000000000F') |
+---------------------------------------------------------------------------------------------------------------+
| 7480000000000000FF2E5F698000000000FF0000010380000000FF05F5E10003000000FF0000000000000000F                     |
+---------------------------------------------------------------------------------------------------------------+

Other discuss

The decoded key format currently maybe not very well, If you have any suggestions, Please comment, Thanks very much.

Maybe we can use the format in show table region.

+-----------+-------------------------------------+-------------------------------------+-----------+-----------------+---------------+------------+---------------+------------+----------------------+------------------+
| REGION_ID | START_KEY                           | END_KEY                             | LEADER_ID | LEADER_STORE_ID | PEERS         | SCATTERING | WRITTEN_BYTES | READ_BYTES | APPROXIMATE_SIZE(MB) | APPROXIMATE_KEYS |
+-----------+-------------------------------------+-------------------------------------+-----------+-----------------+---------------+------------+---------------+------------+----------------------+------------------+
| 95        | t_43_r                              | t_43_r_100000000                    | 112       | 9               | 98, 112, 111  | 0          | 129           | 0          | 1                    | 0                |
| 99        | t_43_r_100000000                    | t_43_r_200000000                    | 113       | 3               | 101, 113, 165 | 0          | 0             | 0          | 1                    | 0                |
| 103       | t_43_r_200000000                    | t_43_r_300000000                    | 114       | 9               | 105, 106, 114 | 0          | 126           | 0          | 1                    | 0                |
| 107       | t_43_r_300000000                    | t_43_r_400000000                    | 116       | 3               | 108, 116, 115 | 0          | 35            | 0          | 1                    | 0                |
| 2         | t_43_r_400000000                    |                                     | 73        | 8               | 73, 79, 162   | 0          | 0             | 0          | 1                    | 0                |
| 146       | t_43_i_1_                           | t_43_i_1_03800000000773594003000000 | 149       | 9               | 148, 149, 166 | 0          | 0             | 0          | 1                    | 0                |
| 150       | t_43_i_1_03800000000773594003000000 | t_43_i_1_03800000000ee6b28003000000 | 159       | 8               | 152, 153, 159 | 0          | 35            | 0          | 1                    | 0                |
| 154       | t_43_i_1_03800000000ee6b28003000000 | t_43_i_1_0380000000165a0bc003000000 | 160       | 1               | 155, 160, 161 | 0          | 35            | 0          | 1                    | 0                |
| 128       | t_43_i_1_0380000000165a0bc003000000 | t_43_i_2_                           | 145       | 3               | 131, 145, 144 | 0          | 0             | 0          | 1                    | 0                |
| 91        | t_43_i_2_                           | t_43_r                              | 93        | 8               | 92, 93, 94    | 0          | 35            | 0          | 1                    | 0                |
+-----------+-------------------------------------+-------------------------------------+-----------+-----------------+---------------+------------+---------------+------------+----------------------+------------------+

Check List

Tests

  • Unit test

Code changes

Side effects

Related changes

Release note

  • Add tidb_decode_key build-in function to decode the key of TiDB used.

@crazycs520
Copy link
Contributor Author

@winkyao PTAL

@codecov
Copy link

codecov bot commented Sep 16, 2019

Codecov Report

Merging #12193 into master will not change coverage.
The diff coverage is n/a.

@@             Coverage Diff             @@
##             master     #12193   +/-   ##
===========================================
  Coverage   81.1245%   81.1245%           
===========================================
  Files           454        454           
  Lines         99351      99351           
===========================================
  Hits          80598      80598           
  Misses        12950      12950           
  Partials       5803       5803

@crazycs520 crazycs520 requested a review from imtbkcat September 16, 2019 06:00
@crazycs520 crazycs520 changed the title Encode key ast/functions: add tidb_decode_key() function to decode key Sep 16, 2019
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

Copy link
Member

@bb7133 bb7133 left a comment

Choose a reason for hiding this comment

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

If tidb_decode_key() failed to decode the input, IMHO it's better to return a warning to the client, for example, given the following query:

>select region_id, tidb_decode_key(start_key) as k from `TIKV_REGION_STATUS` where tidb_decode_key(start_key) like 'tableID=%' limit 3

If the decoding failed for some unexpected reason, the end-user sees nothing result. In such cases, a warning can remind the user that something is wrong because of the failed decodes, rather than the real result does not exist actually.

@zz-jason
Copy link
Member

@crazycs520 Please supplement the release-note section in the PR description. BTW, we also need to update the document about this new builtin function.

@zz-jason zz-jason changed the title ast/functions: add tidb_decode_key() function to decode key expression: add builtin function tidb_decode_key() Sep 17, 2019
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.

Maybe tidb_decode_region_key is preciser, although tidb_decode_key() is simpler,

expression/builtin_info.go Outdated Show resolved Hide resolved
expression/builtin_info.go Outdated Show resolved Hide resolved
expression/builtin_info.go Outdated Show resolved Hide resolved
expression/builtin_info.go Outdated Show resolved Hide resolved
@crazycs520
Copy link
Contributor Author

@zz-jason , tidb_decode_region_key is too narrow, this function not only use to decode the region key, sometime in the TiDB log/TiKV log maybe record the conflict key, then we can use this function to decode too.

Copy link
Member

@bb7133 bb7133 left a comment

Choose a reason for hiding this comment

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

LGTM

bb7133
bb7133 previously approved these changes Sep 23, 2019
@crazycs520 crazycs520 added status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2. labels Sep 23, 2019
@sre-bot
Copy link
Contributor

sre-bot commented Sep 23, 2019

Your auto merge job has been accepted, waiting for 12254

@sre-bot
Copy link
Contributor

sre-bot commented Sep 23, 2019

/run-all-tests

expression/builtin.go Show resolved Hide resolved
expression/builtin_info.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
Copy link
Member

/run-all-tests

@crazycs520 crazycs520 merged commit 1051949 into pingcap:master Sep 24, 2019
// Try to decode it as a record key.
tableID, handle, err := tablecodec.DecodeRecordKey(key)
if err == nil {
return "tableID=" + strconv.FormatInt(tableID, 10) + ", _tidb_rowid=" + strconv.FormatInt(handle, 10)
Copy link
Member

Choose a reason for hiding this comment

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

Why we don't return json object?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will change this tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/expression status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2. type/usability
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants