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

*: support default expression value for sequence #14589

Merged
merged 22 commits into from
Feb 13, 2020

Conversation

AilinKid
Copy link
Contributor

@AilinKid AilinKid commented Feb 3, 2020

What problem does this PR solve?

linked Parser PR: #731
Problem 1:
before this pr, when we create a table with sequence's next value as it's default value like

create sequence seq
create table t (a int default next value for seq)

will get a crash on expression rewrite.

Problem 2:
In some cases, the default value is dynamic, needing to be evaluated every time be called.

Problem 3:
Now, the static default value will be cache in the case like insert into t values (),(),()....

What is changed and how it works?

1: rewrite the tableName (specified sequence to constant string args)
2: store the expr_string in default value as the generated column does.
3: parser the expr_string and evaluate it every time call it
4: when DefaultIsExpr is true, we should ignore the default cache in insert cases and evaluate the value every time be called.

Check List

Tests

  • Unit test
  • Integration test

Release note

  • support dynamic default value in columnInfo for sequence (store the expr string and evaluate every time call it).

@AilinKid AilinKid requested a review from a team as a code owner February 3, 2020 02:24
@ghost ghost requested review from francis0407 and lzmhhh123 and removed request for a team February 3, 2020 02:24
@AilinKid AilinKid requested review from wjhuang2016, tangenta, djshow832 and zimulala and removed request for francis0407 and lzmhhh123 February 3, 2020 03:09
ddl/ddl_api.go Outdated Show resolved Hide resolved
ddl/ddl_api.go Show resolved Hide resolved
ddl/sequence_test.go Show resolved Hide resolved
sessionctx/variable/sequence_state.go Outdated Show resolved Hide resolved
table/column.go Outdated
var defaultExpr ast.ExprNode
expr := fmt.Sprintf("select %s", x)
charset, collation := charset.GetDefaultCharsetAndCollate()
stmts, _, err := parser.New().Parse(expr, charset, collation)
Copy link
Contributor

Choose a reason for hiding this comment

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

It is enough to pass empty strings to charset and collation.

A question: is it necessary to parse the expression once again?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. nice idea.
  2. Actually we can do the expr cache like colDefaultVals does, Given the sequence function hasn't been implemented now, it's hard to do the test, so I push it to the next PR.

Copy link
Contributor Author

@AilinKid AilinKid left a comment

Choose a reason for hiding this comment

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

Rest addressed

ddl/ddl_api.go Outdated Show resolved Hide resolved
ddl/ddl_api.go Show resolved Hide resolved
ddl/sequence_test.go Show resolved Hide resolved
table/column.go Outdated
var defaultExpr ast.ExprNode
expr := fmt.Sprintf("select %s", x)
charset, collation := charset.GetDefaultCharsetAndCollate()
stmts, _, err := parser.New().Parse(expr, charset, collation)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. nice idea.
  2. Actually we can do the expr cache like colDefaultVals does, Given the sequence function hasn't been implemented now, it's hard to do the test, so I push it to the next PR.

@AilinKid
Copy link
Contributor Author

AilinKid commented Feb 4, 2020

/rebuild

@AilinKid
Copy link
Contributor Author

AilinKid commented Feb 4, 2020

/build

Value: types.NewDatum(v.Name.L),
RetType: types.NewFieldType(mysql.TypeString),
}
er.ctxStackAppend(val, types.EmptyName)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does any place use this element in the stack?

Copy link
Contributor Author

@AilinKid AilinKid Feb 4, 2020

Choose a reason for hiding this comment

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

only nextval / lastval / setval function will take it as string arg. (this function will be added in next pr)

Copy link
Contributor

Choose a reason for hiding this comment

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

Should we guarantee the stack empty at the end of the rewrite stage? If we should, does this element break our guarantee?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes we can, take nextval(tableName) for example, tableName will be rewritten as a constant string, then the upper FuncCallExpr will consume it.

ddl/sequence_test.go Outdated Show resolved Hide resolved
@AilinKid
Copy link
Contributor Author

AilinKid commented Feb 5, 2020

/run-unit-test

ddl/ddl_api.go Outdated Show resolved Hide resolved
ddl/ddl_api.go Outdated Show resolved Hide resolved
ddl/ddl_api.go Outdated Show resolved Hide resolved
ddl/ddl_api.go Outdated Show resolved Hide resolved
ddl/sequence_test.go Outdated Show resolved Hide resolved
table/column.go Outdated Show resolved Hide resolved
Copy link
Contributor

@tangenta tangenta left a comment

Choose a reason for hiding this comment

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

LGTM

@tangenta
Copy link
Contributor

/merge

@sre-bot sre-bot added the status/can-merge Indicates a PR has been approved by a committer. label Feb 13, 2020
@sre-bot
Copy link
Contributor

sre-bot commented Feb 13, 2020

/run-all-tests

@sre-bot
Copy link
Contributor

sre-bot commented Feb 13, 2020

@AilinKid merge failed.

@AilinKid
Copy link
Contributor Author

/run-integration-common-test

@AilinKid
Copy link
Contributor Author

/build

@AilinKid
Copy link
Contributor Author

/run-integration-common-test

1 similar comment
@AilinKid
Copy link
Contributor Author

/run-integration-common-test

@AilinKid
Copy link
Contributor Author

/run-unit-test

@AilinKid
Copy link
Contributor Author

/run-all-tests

@AilinKid
Copy link
Contributor Author

/run-unit-test

@AilinKid
Copy link
Contributor Author

/merge

@sre-bot
Copy link
Contributor

sre-bot commented Feb 13, 2020

Your auto merge job has been accepted, waiting for 14682

@sre-bot
Copy link
Contributor

sre-bot commented Feb 13, 2020

/run-all-tests

@sre-bot sre-bot merged commit 007c0e6 into pingcap:master Feb 13, 2020
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants