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: support tidb encode key function #51678

Merged
merged 25 commits into from
Sep 6, 2024

Conversation

tangenta
Copy link
Contributor

@tangenta tangenta commented Mar 12, 2024

What problem does this PR solve?

Issue Number: close #51683

Problem Summary: See #51683.

What changed and how does it work?

This PR supports three builtin functions, namely tidb_mvcc_info(), tidb_encode_record_key() and tidb_encode_index_key():

  • tidb_mvcc_info(key) returns MVCC information of a given key.
  • tidb_encode_record_key(dbName, tableName, handle/pk columns...) returns a row record key. The table must exist.
  • tidb_encode_index_key(dbName, tableName, indexName, idx columns..., handle/pk columns...) returns a index record key. The index must exist.

Please also note that tableName can be one of formats as below:

  • t: refer to non-partitioned table t.
  • t(p1): refer to partition table t and the partition p1.
mysql> create table t (a int, b int, c timestamp, primary key (a) clustered, index idx_c(c));
Query OK, 0 rows affected (0.14 sec)

mysql> insert into t values (1, 1, now());
Query OK, 1 row affected (0.00 sec)

mysql> table t;
+---+------+---------------------+
| a | b    | c                   |
+---+------+---------------------+
| 1 |    1 | 2024-03-12 11:14:35 |
+---+------+---------------------+
1 row in set (0.00 sec)

mysql> select tidb_encode_record_key('test', 't', 1);
+----------------------------------------+
| tidb_encode_record_key('test', 't', 1) |
+----------------------------------------+
| 7480000000000000705f728000000000000001 |
+----------------------------------------+
1 row in set (0.00 sec)

mysql> select tidb_mvcc_info('7480000000000000705f728000000000000001');
+-----------------------------------------------------------------------------------------------------------------------------------+
| tidb_mvcc_info('7480000000000000705f728000000000000001')                                                                          |
+-----------------------------------------------------------------------------------------------------------------------------------+
| {"info":{"writes":[{"start_ts":448322148825038849,"commit_ts":448322148825038850,"short_value":"gAACAAAAAgMBAAkAAQAAAKMz2LIZ"}]}} |
+-----------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select tidb_encode_index_key('test', 't', 'idx_c', '2024-03-12 11:14:35', 1);
+----------------------------------------------------------------------------+
| tidb_encode_index_key('test', 't', 'idx_c', '2024-03-12 11:14:35', 1)      |
+----------------------------------------------------------------------------+
| 7480000000000000705f6980000000000000010419b2d833a3000000038000000000000001 |
+----------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select tidb_decode_key(tidb_encode_index_key('test', 't', 'idx_c', '2024-03-12 11:14:35', 1));
+----------------------------------------------------------------------------------------+
| tidb_decode_key(tidb_encode_index_key('test', 't', 'idx_c', '2024-03-12 11:14:35', 1)) |
+----------------------------------------------------------------------------------------+
| {"index_id":1,"index_vals":{"c":"2024-03-12 11:14:35"},"table_id":112}                 |
+----------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select tidb_mvcc_info(tidb_encode_index_key('test', 't', 'idx_c', '2024-03-12 11:14:35', 1));
+-----------------------------------------------------------------------------------------------------------+
| tidb_mvcc_info(tidb_encode_index_key('test', 't', 'idx_c', '2024-03-12 11:14:35', 1))                     |
+-----------------------------------------------------------------------------------------------------------+
| {"info":{"writes":[{"start_ts":448322148825038849,"commit_ts":448322148825038850,"short_value":"MA=="}]}} |
+-----------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

None

@ti-chi-bot ti-chi-bot bot added do-not-merge/needs-linked-issue do-not-merge/needs-tests-checked release-note-none Denotes a PR that doesn't merit a release note. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Mar 12, 2024
Copy link

tiprow bot commented Mar 12, 2024

Hi @tangenta. Thanks for your PR.

PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

I understand the commands that are listed here.

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 kubernetes/test-infra repository.

Copy link

codecov bot commented Mar 12, 2024

Codecov Report

Attention: Patch coverage is 59.80392% with 205 lines in your changes missing coverage. Please review.

Project coverage is 56.6772%. Comparing base (782f08c) to head (bd28383).
Report is 22 commits behind head on master.

Additional details and impacted files
@@                Coverage Diff                @@
##             master     #51678         +/-   ##
=================================================
- Coverage   72.8726%   56.6772%   -16.1955%     
=================================================
  Files          1598       1729        +131     
  Lines        444224     630892     +186668     
=================================================
+ Hits         323718     357572      +33854     
- Misses       100549     249723     +149174     
- Partials      19957      23597       +3640     
Flag Coverage Δ
integration 37.9496% <16.8627%> (?)
unit 72.6300% <58.4313%> (+0.6490%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 52.9567% <ø> (ø)
parser ∅ <ø> (∅)
br 52.9080% <ø> (+7.1145%) ⬆️

@ti-chi-bot ti-chi-bot bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Mar 29, 2024
Copy link

ti-chi-bot bot commented Mar 29, 2024

PR needs rebase.

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 kubernetes/test-infra repository.

@ekexium
Copy link
Contributor

ekexium commented Apr 26, 2024

I love this
We need this
Please do continue this

@D3Hunter
Copy link
Contributor

D3Hunter commented Jun 7, 2024

i hope those functions would works in normal query like :

select tidb_handle(), tidb_idx_key(index-name) from test.t where id = 1
select tidb_mvcc(tidb_handle()) from test.t where id = 1

@tangenta
Copy link
Contributor Author

@D3Hunter If we put this in normal query, I think we cannot get the MVCC of deleted rows/indexes?

@kennytm
Copy link
Contributor

kennytm commented Jun 14, 2024

@D3Hunter I don't think a function should need or be able to know the current plan is a SELECT operating on exactly one physical table.

Copy link
Contributor

@kennytm kennytm left a comment

Choose a reason for hiding this comment

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

This must not be merged before the privilege check bypass is fixed.

See #51683 (comment) for a PoC.

@D3Hunter
Copy link
Contributor

D3Hunter commented Jun 18, 2024

i hope those functions would works in normal query like :

select tidb_handle(), tidb_idx_key(index-name) from test.t where id = 1
select tidb_mvcc(tidb_handle()) from test.t where id = 1

ok, for downvoters, get from query is much simplier, i don't like to input so many parameters to just get the key of it, and it's error-prone to do that as we have to remember the definition of the pk/index. also privilige check is easier, by reuse existing one.

If we put this in normal query, I think we cannot get the MVCC of deleted rows/indexes?

can use as of timestamp?

I don't think a function should need or be able to know the current plan is a SELECT operating on exactly one physical table.

don't get your point, where is a function should need or be able to know the current plan is a SELECT operating on exactly one physical table. rule came from?

@kennytm
Copy link
Contributor

kennytm commented Jun 18, 2024

@D3Hunter what is the result of tidb_handle() in the following?

select tidb_handle() from a, b;
select tidb_handle() from dual;
select tidb_handle() from (select * from t) _;

create view v as select 1;
select tidb_handle() from v;

@D3Hunter
Copy link
Contributor

@D3Hunter what is the result of tidb_handle() in the following?

select tidb_handle() from a, b;
select tidb_handle() from dual;
select tidb_handle() from (select * from t) _;

create view v as select 1;
select tidb_handle() from v;

if we use the pattern, just check and report error in this case

@D3Hunter
Copy link
Contributor

D3Hunter commented Sep 4, 2024

please ask PM for approval for new feature

@D3Hunter
Copy link
Contributor

D3Hunter commented Sep 4, 2024

/hold

unhold it after PM approve

@ti-chi-bot ti-chi-bot bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Sep 4, 2024
Copy link

ti-chi-bot bot commented Sep 4, 2024

@tangenta: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-br-integration-test 78c5c63 link true /test pull-br-integration-test
pull-lightning-integration-test 78c5c63 link true /test pull-lightning-integration-test

Full PR test history. Your PR dashboard.

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 kubernetes/test-infra repository. I understand the commands that are listed here.

Copy link

ti-chi-bot bot commented Sep 5, 2024

@Frank945946: adding LGTM is restricted to approvers and reviewers in OWNERS files.

In response to this:

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 kubernetes/test-infra repository.

@tangenta
Copy link
Contributor Author

tangenta commented Sep 5, 2024

/retest

Copy link

tiprow bot commented Sep 5, 2024

@tangenta: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

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 kubernetes-sigs/prow repository.

@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Sep 5, 2024
Copy link

ti-chi-bot bot commented Sep 5, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-06-14 15:52:24.810574996 +0000 UTC m=+739098.863886920: ✖️🔁 reset by kennytm.
  • 2024-09-03 09:36:06.780380988 +0000 UTC m=+350691.298433913: ☑️ agreed by kennytm.
  • 2024-09-05 09:25:51.146288226 +0000 UTC m=+522875.664341150: ☑️ agreed by wjhuang2016.

@D3Hunter
Copy link
Contributor

D3Hunter commented Sep 5, 2024

/unhold

@ti-chi-bot ti-chi-bot bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Sep 5, 2024
Copy link
Contributor

@D3Hunter D3Hunter left a comment

Choose a reason for hiding this comment

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

add some test for user privileges?

@tangenta
Copy link
Contributor Author

tangenta commented Sep 6, 2024

/approve

Copy link

ti-chi-bot bot commented Sep 6, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Frank945946, kennytm, tangenta, time-and-fate, wjhuang2016, XuHuaiyu

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

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot bot added the approved label Sep 6, 2024
@tangenta
Copy link
Contributor Author

tangenta commented Sep 6, 2024

/ok-to-test

@ti-chi-bot ti-chi-bot bot added the ok-to-test Indicates a PR is ready to be tested. label Sep 6, 2024
@tangenta tangenta removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 6, 2024
@ti-chi-bot ti-chi-bot bot merged commit 5dae1a3 into pingcap:master Sep 6, 2024
21 of 23 checks passed
breezewish added a commit to breezewish/tidb that referenced this pull request Sep 8, 2024
* origin/master: (33 commits)
  build(deps): bump github.com/prometheus/common from 0.55.0 to 0.57.0 (pingcap#55762)
  build(deps): bump golang.org/x/sys from 0.24.0 to 0.25.0 (pingcap#55894)
  planner: fix incorrect estRows with global index and json column (pingcap#55842)
  ddl, stat: switch to new struct for add/truncate/drop partition (pingcap#55893)
  planner: hide instance plan cache eviction log if no plan is evicted (pingcap#55918)
  expression: support tidb encode key function (pingcap#51678)
  planner: fix incorrect maintenance of `handleColHelper` for recursive CTE (pingcap#55732)
  executor: some code refine of hash join v2 (pingcap#55887)
  infoschema, meta: fix wrong auto id after `rename table` (pingcap#55847)
  ddl/ingest: set `minCommitTS` when detect remote duplicate keys (pingcap#55588)
  planner: move index advisor into the kernel (pingcap#55874)
  ddl, stats: switch to new struct for create/truncate table (pingcap#55811)
  executor: avoid new small objects in probe stage of hash join v2 (pingcap#55855)
  *: Add tidbCPU/tikvCPU into system tables (pingcap#55455)
  ddl: use static contexts in `NewReorgCopContext` (pingcap#55823)
  executor: fix index out of range bug in hash join v2 (pingcap#55864)
  executor: record index usage for the clustered primary keys (pingcap#55602)
  domain: load all non-public tables into infoschema (pingcap#55142)
  test: fix unstable TestShowViewPriv (pingcap#55868)
  ttl: add `varbinary` case for `TestSplitTTLScanRangesWithBytes` (pingcap#55863)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm ok-to-test Indicates a PR is ready to be tested. release-note-none Denotes a PR that doesn't merit a release note. sig/planner SIG: Planner size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support querying MVCC information through builtin function
8 participants