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

planner: make var_samp can be used as a window function #53130

Merged
merged 4 commits into from May 15, 2024

Conversation

hi-rustin
Copy link
Member

@hi-rustin hi-rustin commented May 9, 2024

What problem does this PR solve?

Issue Number: close #52933

Problem Summary:

What changed and how does it work?

From the MySQL plan:

mysql> explain analyze select var_samp(c1) over (partition by c1) from t1;
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN                                                                                                                                                                                                                                                                                                                |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| -> Window aggregate with buffering: var_samp(t1.c1) OVER (PARTITION BY t1.c1 )   (cost=0 rows=1) (actual time=0.139..0.139 rows=0 loops=1)
    -> Sort: t1.c1  (cost=0.35 rows=1) (actual time=0.133..0.133 rows=0 loops=1)
        -> Table scan on t1  (cost=0.35 rows=1) (actual time=0.116..0.116 rows=0 loops=1)
 |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 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.

Make `var_samp` can be used as a window function.
使 `var_samp` 可以用作窗口函数。

@ti-chi-bot ti-chi-bot bot added release-note do-not-merge/needs-triage-completed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed do-not-merge/needs-triage-completed labels May 9, 2024
Copy link

codecov bot commented May 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 74.7449%. Comparing base (4b91fee) to head (06146bb).
Report is 10 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #53130        +/-   ##
================================================
+ Coverage   74.4490%   74.7449%   +0.2959%     
================================================
  Files          1493       1493                
  Lines        356315     430968     +74653     
================================================
+ Hits         265273     322127     +56854     
- Misses        71770      89017     +17247     
- Partials      19272      19824       +552     
Flag Coverage Δ
integration 49.4982% <0.0000%> (?)
unit 71.4551% <100.0000%> (-1.7617%) ⬇️

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

Components Coverage Δ
dumpling 53.9957% <ø> (-2.3014%) ⬇️
parser ∅ <ø> (∅)
br 50.4651% <ø> (+7.4738%) ⬆️

@hi-rustin
Copy link
Member Author

/retest

Signed-off-by: hi-rustin <rustin.liu@gmail.com>
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
@ti-chi-bot ti-chi-bot bot added sig/planner SIG: Planner size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels May 14, 2024
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
@hi-rustin
Copy link
Member Author

Tested locally:

  1. Start the cluster: tiup playground v8.0.0 --db.binpath /Users/hi-rustin/vsc/tidb/bin/tidb-server
  2. Create the table and run the query:
mysql> use test;
msqll> create table t1 (c1 int)
mysql> select c1, var_samp(c1) over (partition by c1) from t1;
Empty set (0.00 sec)

mysql> explain select c1, var_samp(c1) over (partition by c1) from t1;
+-------------------------------+----------+-----------+---------------+-----------------------------------------------------------------------------------+
| id                            | estRows  | task      | access object | operator info                                                                     |
+-------------------------------+----------+-----------+---------------+-----------------------------------------------------------------------------------+
| Shuffle_11                    | 10000.00 | root      |               | execution info: concurrency:5, data sources:[TableReader_9]                       |
| └─Window_7                    | 10000.00 | root      |               | var_samp(cast(test.t1.c1, double BINARY))->Column#4 over(partition by test.t1.c1) |
|   └─Sort_10                   | 10000.00 | root      |               | test.t1.c1                                                                        |
|     └─ShuffleReceiver_12      | 10000.00 | root      |               |                                                                                   |
|       └─TableReader_9         | 10000.00 | root      |               | data:TableFullScan_8                                                              |
|         └─TableFullScan_8     | 10000.00 | cop[tikv] | table:t1      | keep order:false, stats:pseudo                                                    |
+-------------------------------+----------+-----------+---------------+-----------------------------------------------------------------------------------+
6 rows in set (0.00 sec)

mysql> explain select c1, sum(c1) over (partition by c1) from t1;
+-------------------------------+----------+-----------+---------------+-------------------------------------------------------------------------------------+
| id                            | estRows  | task      | access object | operator info                                                                       |
+-------------------------------+----------+-----------+---------------+-------------------------------------------------------------------------------------+
| Shuffle_11                    | 10000.00 | root      |               | execution info: concurrency:5, data sources:[TableReader_9]                         |
| └─Window_7                    | 10000.00 | root      |               | sum(cast(test.t1.c1, decimal(10,0) BINARY))->Column#4 over(partition by test.t1.c1) |
|   └─Sort_10                   | 10000.00 | root      |               | test.t1.c1                                                                          |
|     └─ShuffleReceiver_12      | 10000.00 | root      |               |                                                                                     |
|       └─TableReader_9         | 10000.00 | root      |               | data:TableFullScan_8                                                                |
|         └─TableFullScan_8     | 10000.00 | cop[tikv] | table:t1      | keep order:false, stats:pseudo                                                      |
+-------------------------------+----------+-----------+---------------+-------------------------------------------------------------------------------------+
6 rows in set (0.01 sec)

Copy link
Member Author

@hi-rustin hi-rustin left a comment

Choose a reason for hiding this comment

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

🔢 Self-check (PR reviewed by myself and ready for feedback.)

Copy link

ti-chi-bot bot commented May 14, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-05-14 09:40:23.923885188 +0000 UTC m=+1559777.681020761: ☑️ agreed by qw4990.
  • 2024-05-14 12:25:15.552696005 +0000 UTC m=+1569669.309831580: ☑️ agreed by winoros.

@easonn7
Copy link

easonn7 commented May 15, 2024

/approve

Copy link

ti-chi-bot bot commented May 15, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: easonn7, qw4990, tangenta, winoros

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 May 15, 2024
@ti-chi-bot ti-chi-bot bot merged commit 2d552c0 into pingcap:master May 15, 2024
23 checks passed
ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request May 15, 2024
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-6.5: #53285.

@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-7.1: #53286.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request May 15, 2024
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-7.5: #53287.

@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-8.1: #53288.

terry1purcell pushed a commit to terry1purcell/tidb that referenced this pull request May 17, 2024
RidRisR pushed a commit to RidRisR/tidb that referenced this pull request May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

use var_samp as window function cause unexpected error
6 participants