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: support non-deterministic functions (e.g., now) in the prepared plan cache #8105

Merged
merged 1 commit into from
Nov 1, 2018
Merged

expression,planner: support non-deterministic functions (e.g., now) in the prepared plan cache #8105

merged 1 commit into from
Nov 1, 2018

Conversation

dbjoa
Copy link
Contributor

@dbjoa dbjoa commented Oct 30, 2018

What problem does this PR solve?

Support non-deterministic functions (e.g., now) in the prepared plan cache.

What is changed and how it works?

Make a non-deterministic function expression to be a constant expression with the deferred expression (i.e., the non-deterministic function expression).

Check List

Tests

  • Unit test

Code changes

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

Side effects

  • No

Related changes

  • Need to be included in the release note

@sre-bot
Copy link
Contributor

sre-bot commented Oct 30, 2018

Hi contributor, thanks for your PR.

This patch needs to be approved by someone of admins. They should reply with "/ok-to-test" to accept this PR for running test automatically.

@dbjoa
Copy link
Contributor Author

dbjoa commented Oct 30, 2018

/run-all-tests

@shenli shenli added the contribution This PR is from a community contributor. label Oct 30, 2018
@shenli
Copy link
Member

shenli commented Oct 30, 2018

@dbjoa Thanks!

@eurekaka eurekaka added type/enhancement The issue or PR belongs to an enhancement. sig/planner SIG: Planner status/all tests passed labels Oct 30, 2018
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.

Nice work, LGTM.

@@ -70,8 +70,8 @@ func (sf *ScalarFunction) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%s\"", sf)), nil
}

// NewFunction creates a new scalar function or constant.
func NewFunction(ctx sessionctx.Context, funcName string, retType *types.FieldType, args ...Expression) (Expression, error) {
// newFunction creates a new scalar function or constant.
Copy link
Contributor

Choose a reason for hiding this comment

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

s/newFunction/newFunctionImpl

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the comment. I've updated the PR.

@eurekaka eurekaka added the status/LGT1 Indicates that a PR has LGTM 1. label Oct 31, 2018
"github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testleak"
dto "github.com/prometheus/client_model/go"
"time"
Copy link
Contributor

Choose a reason for hiding this comment

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

put this line to line 17 and leave a blank line behind it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the detailed comment. I've updated the PR.

// NewFunction creates a new scalar function or constant.
func NewFunction(ctx sessionctx.Context, funcName string, retType *types.FieldType, args ...Expression) (Expression, error) {
// newFunctionImpl creates a new scalar function or constant.
func newFunctionImpl(fold bool, ctx sessionctx.Context, funcName string, retType *types.FieldType, args ...Expression) (Expression, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

ctx is always the first argument of a function,
we'd better keep this style.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the detailed comment. I've updated the PR.

@XuHuaiyu
Copy link
Contributor

MySQL:

mysql> prepare stmt from "select now(), sleep(1), now(), sleep(1), now()";
Query OK, 0 rows affected (0.00 sec)
Statement prepared

mysql> execute stmt;
+---------------------+----------+---------------------+----------+----------------$----+
| now()               | sleep(1) | now()               | sleep(1) | now()
    |
+---------------------+----------+---------------------+----------+----------------$----+
| 2018-10-31 16:00:14 |        0 | 2018-10-31 16:00:14 |        0 | 2018-10-31 16:0$:14 |
+---------------------+----------+---------------------+----------+----------------$----+
1 row in set (2.01 sec)

mysql> execute stmt;
+---------------------+----------+---------------------+----------+----------------$----+
| now()               | sleep(1) | now()               | sleep(1) | now()
    |
+---------------------+----------+---------------------+----------+----------------$----+
| 2018-10-31 16:00:19 |        0 | 2018-10-31 16:00:19 |        0 | 2018-10-31 16:0$:19 |
+---------------------+----------+---------------------+----------+----------------$----+
1 row in set (2.01 sec)

In MySQL, now is a foldable function,
but It seems we will get different results for different now() with this commit?

@dbjoa
Copy link
Contributor Author

dbjoa commented Oct 31, 2018

@XuHuaiyu, Thank you for the comment. The updated PR should address the issue.

@dbjoa
Copy link
Contributor Author

dbjoa commented Oct 31, 2018

/run-all-tests

1 similar comment
@dbjoa
Copy link
Contributor Author

dbjoa commented Oct 31, 2018

/run-all-tests

@dbjoa dbjoa changed the title expression,planner: support non-deterministic functions (e.g., now) in the prepared plan cache [WIP] expression,planner: support non-deterministic functions (e.g., now) in the prepared plan cache Oct 31, 2018
@dbjoa
Copy link
Contributor Author

dbjoa commented Oct 31, 2018

I should also handle the synonyms of NOW() (i.e., CURDATE(), CURTIME(), UTC_DATE(), UTC_TIME(), UTC_TIMESTAMP(), CURRENT_TIMESTAMP(), UNIX_TIMESTAMP()).

@CLAassistant
Copy link

CLAassistant commented Oct 31, 2018

CLA assistant check
All committers have signed the CLA.

@dbjoa
Copy link
Contributor Author

dbjoa commented Oct 31, 2018

/run-all-tests

@dbjoa
Copy link
Contributor Author

dbjoa commented Oct 31, 2018

The updated PR should handle the synonyms of NOW().

@dbjoa dbjoa changed the title [WIP] expression,planner: support non-deterministic functions (e.g., now) in the prepared plan cache expression,planner: support non-deterministic functions (e.g., now) in the prepared plan cache Oct 31, 2018
@dbjoa
Copy link
Contributor Author

dbjoa commented Oct 31, 2018

/run-all-tests

@@ -14,7 +14,13 @@
package core_test

import (
dto "github.com/prometheus/client_model/go"
Copy link
Member

Choose a reason for hiding this comment

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

please move this package to the lower block. separate it from the golang standard packages.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the comment. I've updated the PR.

@@ -52,6 +41,23 @@ var unFoldableFunctions = map[string]struct{}{
ast.GetParam: {},
}

// DeferredFunctions stores non-deterministic functions which can be deferred.
Copy link
Member

Choose a reason for hiding this comment

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

How adding another comment to specify that the deferred functions take effects only when plan cache is enabled?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the comment. I've added the comment to the updated the PR.

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

zz-jason commented Nov 1, 2018

/run-all-tests

@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 Nov 1, 2018
@ngaut ngaut merged commit 5374ff8 into pingcap:master Nov 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution This PR is from a community contributor. sig/planner SIG: Planner status/LGT2 Indicates that a PR has LGTM 2. type/enhancement The issue or PR belongs to an enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants