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,planner/core: support unix_timestamp() function in partition pruning #12035

Merged
merged 6 commits into from
Sep 12, 2019

Conversation

tiancaiamao
Copy link
Contributor

What problem does this PR solve?

CREATE TABLE quarterly_report_status (
    report_id INT NOT NULL,
    report_status VARCHAR(20) NOT NULL,
    report_updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP) 
PARTITION BY RANGE (UNIX_TIMESTAMP(report_updated)) (
    PARTITION p0 VALUES LESS THAN (UNIX_TIMESTAMP('2008-01-01 00:00:00')),
    PARTITION p1 VALUES LESS THAN (UNIX_TIMESTAMP('2008-04-01 00:00:00')),
    PARTITION p2 VALUES LESS THAN (UNIX_TIMESTAMP('2010-01-01 00:00:00')),
    PARTITION p3 VALUES LESS THAN (MAXVALUE)
);

Expected:

mysql> explain select * from quarterly_report_status where report_updated < '2007-05-01 00:00:00';
+---------------------+----------+------+------------------------------------------------------------------------------------------------+
| id                  | count    | task | operator info                                                                                  |
+---------------------+----------+------+------------------------------------------------------------------------------------------------+
| TableReader_8       | 3323.33  | root | data:Selection_7                                                                               |
| └─Selection_7       | 3323.33  | cop  | lt(test.quarterly_report_status.report_updated, 2007-05-01 00:00:00.000000)                    |
|   └─TableScan_6     | 10000.00 | cop  | table:quarterly_report_status, partition:p0, range:[-inf,+inf], keep order:false, stats:pseudo |
+---------------------+----------+------+------------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)

Get:

mysql> explain select * from quarterly_report_status where report_updated < '2007-05-01 00:00:00';
+--------------------------+----------+------+------------------------------------------------------------------------------------------------+
| id                       | count    | task | operator info                                                                                  |
+--------------------------+----------+------+------------------------------------------------------------------------------------------------+
| Union_10                 | 13293.33 | root |                                                                                                |
| ├─TableReader_13         | 3323.33  | root | data:Selection_12                                                                              |
| │ └─Selection_12         | 3323.33  | cop  | lt(test.quarterly_report_status.report_updated, 2007-05-01 00:00:00.000000)                    |
| │   └─TableScan_11       | 10000.00 | cop  | table:quarterly_report_status, partition:p0, range:[-inf,+inf], keep order:false, stats:pseudo |
| ├─TableReader_16         | 3323.33  | root | data:Selection_15                                                                              |
| │ └─Selection_15         | 3323.33  | cop  | lt(test.quarterly_report_status.report_updated, 2007-05-01 00:00:00.000000)                    |
| │   └─TableScan_14       | 10000.00 | cop  | table:quarterly_report_status, partition:p1, range:[-inf,+inf], keep order:false, stats:pseudo |
| ├─TableReader_19         | 3323.33  | root | data:Selection_18                                                                              |
| │ └─Selection_18         | 3323.33  | cop  | lt(test.quarterly_report_status.report_updated, 2007-05-01 00:00:00.000000)                    |
| │   └─TableScan_17       | 10000.00 | cop  | table:quarterly_report_status, partition:p2, range:[-inf,+inf], keep order:false, stats:pseudo |
| └─TableReader_22         | 3323.33  | root | data:Selection_21                                                                              |
|   └─Selection_21         | 3323.33  | cop  | lt(test.quarterly_report_status.report_updated, 2007-05-01 00:00:00.000000)                    |
|     └─TableScan_20       | 10000.00 | cop  | table:quarterly_report_status, partition:p3, range:[-inf,+inf], keep order:false, stats:pseudo |
+--------------------------+----------+------+------------------------------------------------------------------------------------------------+
13 rows in set (0.00 sec)

Partition pruning doesn't work using the unix_timestamp() function.

What is changed and how it works?

Add unix_timestamp to monotoneIncFuncs list.
We can infer the rule f(x) > xxx and x < yyy if f is monotonously increasing.

Check List

Tests

  • Unit test
  • Manual test (add detailed scripts or steps below)

@codecov
Copy link

codecov bot commented Sep 5, 2019

Codecov Report

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

@@             Coverage Diff             @@
##             master     #12035   +/-   ##
===========================================
  Coverage   81.3689%   81.3689%           
===========================================
  Files           453        453           
  Lines         97289      97289           
===========================================
  Hits          79163      79163           
  Misses        12468      12468           
  Partials       5658       5658

@tiancaiamao
Copy link
Contributor Author

PTAL @XuHuaiyu @eurekaka

@tiancaiamao
Copy link
Contributor Author

Related issue #12028

@zimulala
Copy link
Contributor

zimulala commented Sep 5, 2019

/run-common-test

}

func newTimestamp(yy, mm, dd, hh, min, ss int) *Constant {
return newTimeConst(yy, mm, dd, hh, min, ss, mysql.TypeTimestamp)
Copy link

@fatpa fatpa Sep 5, 2019

Choose a reason for hiding this comment

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

The arguments hh, min, ss dosen't use in the newTimeConst function, why still pass them into the function??

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A timestamp value has the hh, min, ss part, while a date value contains only the yy, mm, dd information. That's why the newDate function has fewer parameters than newTimestamp.

We pass 0 as arguments, but it doesn't means hh, min, ss would never be useful in the future.
@fatpa

@tiancaiamao
Copy link
Contributor Author

PTAL @XuHuaiyu @eurekaka @zz-jason

Copy link
Contributor

@eurekaka eurekaka left a comment

Choose a reason for hiding this comment

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

LGTM. Please fix CI.

@tiancaiamao
Copy link
Contributor Author

/run-all-tests

@tiancaiamao
Copy link
Contributor Author

/rebuild

1 similar comment
@tiancaiamao
Copy link
Contributor Author

/rebuild

@tiancaiamao
Copy link
Contributor Author

/run-sqllogic-test-1

@tiancaiamao
Copy link
Contributor Author

/run-integration-compatibility-test

@tiancaiamao tiancaiamao added status/LGT1 Indicates that a PR has LGTM 1. status/all tests passed labels Sep 11, 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.

LGTM

@zz-jason zz-jason added status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Sep 12, 2019
@sre-bot
Copy link
Contributor

sre-bot commented Sep 12, 2019

Your auto merge job has been accepted, waiting for 11702, 12150

@sre-bot
Copy link
Contributor

sre-bot commented Sep 12, 2019

/run-all-tests

@sre-bot
Copy link
Contributor

sre-bot commented Sep 12, 2019

@tiancaiamao merge failed.

@zz-jason
Copy link
Member

/run-unit-test

@tiancaiamao
Copy link
Contributor Author

/run-all-tests

@tiancaiamao tiancaiamao merged commit 81cc7bc into pingcap:master Sep 12, 2019
@tiancaiamao tiancaiamao deleted the prune-unix-timestamp branch September 12, 2019 05:37
@sre-bot
Copy link
Contributor

sre-bot commented Sep 12, 2019

cherry pick to release-3.0 failed

@cyliu0
Copy link
Contributor

cyliu0 commented Nov 6, 2019

@tiancaiamao Do we need to cherry pick this one to release-3.1?

@sre-bot
Copy link
Contributor

sre-bot commented Apr 7, 2020

It seems that, not for sure, we failed to cherry-pick this commit to release-3.0. Please comment '/run-cherry-picker' to try to trigger the cherry-picker if we did fail to cherry-pick this commit before. @tiancaiamao PTAL.

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/bugfix This PR fixes a bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants