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

executor, expression: fix current_timestamp/now not consistent in one stmt like MySQL #11342

Merged
merged 1 commit into from
Jul 23, 2019

Conversation

cfzjywxk
Copy link
Contributor

@cfzjywxk cfzjywxk commented Jul 19, 2019

What problem does this PR solve?

MySQL will set time to “”time_cache“ structure before doing one command(COM_XXX)
later expresission calculation will use this timestamp as time value

TiDB logic different and default column calculation and now expr calculation will try to get timestamp twice.

more details see jira issue#4391
eg:
TiDB

CREATE TABLE `tt4` (
  `c1` timestamp DEFAULT CURRENT_TIMESTAMP,
  `c2` int(11) DEFAULT NULL,
  `c3` timestamp DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin

insert into tt4 set c1 = current_timestamp, c2 = sleep(2);
mysql> select c1 = c3 from tt4;
+---------+
| c1 = c3 |
+---------+
|       0 |
+---------+
1 row in set (0.00 sec)

MySQL

insert into tt4 set c1 = current_timestamp, c2 = sleep(2);
select c1 = c3 from tt4;
+---------+
| c1 = c3 |
+---------+
|       1 |
+---------+
1 row in set (0.00 sec)

  • remove stmtCtx.sysTs unused variable
  • cache time value once in stmtCtx.nowTs
  • refactor nowTs usage, offer Unified function interface for get and reset nowTS. Generally, accessing variable directly is not a good way for further maintenance, refactor these usage code

What is changed and how it works?

Check List

Tests

  • Unit test

Code changes

  • Has exported function/method change
  • Has exported variable/fields change

Side effects

Related changes

  • Need to cherry-pick to the release branch

@cfzjywxk
Copy link
Contributor Author

@jackysp PTAL

@cfzjywxk
Copy link
Contributor Author

/run-all-tests

@codecov
Copy link

codecov bot commented Jul 19, 2019

Codecov Report

Merging #11342 into master will decrease coverage by 0.1875%.
The diff coverage is 86.6666%.

@@               Coverage Diff                @@
##             master     #11342        +/-   ##
================================================
- Coverage   81.4278%   81.2403%   -0.1876%     
================================================
  Files           423        423                
  Lines         90851      90172       -679     
================================================
- Hits          73978      73256       -722     
- Misses        11588      11603        +15     
- Partials       5285       5313        +28

@codecov
Copy link

codecov bot commented Jul 19, 2019

Codecov Report

Merging #11342 into master will decrease coverage by 0.0194%.
The diff coverage is 59.1836%.

@@               Coverage Diff                @@
##             master     #11342        +/-   ##
================================================
- Coverage   81.2969%   81.2774%   -0.0195%     
================================================
  Files           423        423                
  Lines         90381      90399        +18     
================================================
- Hits          73477      73474         -3     
- Misses        11598      11609        +11     
- Partials       5306       5316        +10

expression/helper.go Outdated Show resolved Hide resolved
expression/helper.go Outdated Show resolved Hide resolved
@cfzjywxk
Copy link
Contributor Author

/run-all-tests

@cfzjywxk
Copy link
Contributor Author

/run-all-tests

1 similar comment
@cfzjywxk
Copy link
Contributor Author

/run-all-tests

@cfzjywxk
Copy link
Contributor Author

@jackysp PTAL
refactor nowTs usage, offer Unified function interface for get and reset nowTS.
there are two call paths to stmt timestamp [current_timestamp/now/date like function calls] and [time type col default value calculations]

}

// ReSetNowTs resetter for nowTs, clear cached time flag
func (sc *StatementContext) ReSetNowTs() {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
func (sc *StatementContext) ReSetNowTs() {
func (sc *StatementContext) ResetNowTs() {

@@ -393,6 +409,7 @@ func (sc *StatementContext) ResetForRetry() {
sc.mu.Unlock()
sc.TableIDs = sc.TableIDs[:0]
sc.IndexIDs = sc.IndexIDs[:0]
sc.ReSetNowTs()
Copy link
Member

Choose a reason for hiding this comment

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

I think we should not reset when retrying. Transaction retry is an automatic implicit behavior. It is possible that current_timestamp has been returned to the client. If it is reset here, the user may be bothered. For example,

begin;
insert xxx current_timestamp;
select now(); -- The user might think this value is bigger than the previous insert.
commit; -- It is better do not reset current here, if the retry is needed.

Copy link
Member

Choose a reason for hiding this comment

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

PTAL @lysu

var nowTs = &b.ctx.GetSessionVars().StmtCtx.NowTs
if nowTs.Equal(time.Time{}) {
*nowTs = time.Now()
nowTs, err := getStmtTimestamp(b.ctx)
Copy link
Member

Choose a reason for hiding this comment

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

would this impact the prepare plan cache which has the NOW() builtin 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.

plan cache will not cache now function results, every time execution will recaculate now() function. This change make now/current_time func call same results within one sql statement

}

// ResetNowTs resetter for nowTs, clear cached time flag
func (sc *StatementContext) ResetNowTs() {
Copy link
Member

Choose a reason for hiding this comment

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

It seems we do not need this function anymore. It is only used in the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe leave this as it's correspond to newly added Get interface. Future devs will not write "*timeNows = xxx/time.Time{}" like code and forget to reset stmtTimeCached flag

Copy link
Member

@jackysp jackysp left a comment

Choose a reason for hiding this comment

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

rest LGTM

@cfzjywxk cfzjywxk requested a review from jackysp July 23, 2019 07:00
Copy link
Member

@jackysp jackysp 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 the status/LGT1 Indicates that a PR has LGTM 1. label Jul 23, 2019
…unction results

refactor related statement time related interface and some time function implementations
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 merged commit ca70d74 into pingcap:master Jul 23, 2019
@zz-jason zz-jason added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Jul 23, 2019
@sre-bot
Copy link
Contributor

sre-bot commented Jul 23, 2019

cherry pick to release-2.1 failed

@sre-bot
Copy link
Contributor

sre-bot commented Jul 23, 2019

cherry pick to release-3.0 failed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/expression sig/execution SIG execution status/LGT2 Indicates that a PR has LGTM 2. type/bug The issue is confirmed as a bug. type/compatibility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants