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: fix explicitly insert null value into timestamp column #3646

Merged
merged 6 commits into from
Jul 12, 2017

Conversation

tiancaiamao
Copy link
Contributor

create table t (ts timestamp null default current_timestamp);
insert into t values (null);
select * from t;

get:

mysql> select * from t;
+---------------------+
| ts                  |
+---------------------+
| 2017-07-06 15:57:05 |
+---------------------+
1 row in set (0.00 sec)

should get:

+------+
| ts   |
+------+
| NULL |
+------+
1 row in set (0.00 sec)

@zimulala @hanfei1991 @shenli @XuHuaiyu

create table t (ts timestamp);
insert into t values (null);
This should insert a null rather than default value.
@hanfei1991
Copy link
Member

LGTM

@hanfei1991 hanfei1991 added status/LGT1 Indicates that a PR has LGTM 1. conflicting labels Jul 6, 2017
for i, v := range vals {
offset := cols[i].Offset
row[offset] = v
if !ignoreErr {
Copy link
Contributor

Choose a reason for hiding this comment

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

what did this check for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm also curious about this, could you explain it? @zimulala

Copy link
Contributor

Choose a reason for hiding this comment

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

It's used for loading data, please keep it.

@@ -134,7 +134,7 @@ func countEntriesWithPrefix(ctx context.Context, prefix []byte) (int, error) {
}

func (ts *testSuite) TestTypes(c *C) {
_, err := ts.se.Execute("CREATE TABLE test.t (c1 tinyint, c2 smallint, c3 int, c4 bigint, c5 text, c6 blob, c7 varchar(64), c8 time, c9 timestamp not null default CURRENT_TIMESTAMP, c10 decimal(10,1))")
_, err := ts.se.Execute("CREATE TABLE test.t (c1 tinyint, c2 smallint, c3 int, c4 bigint, c5 text, c6 blob, c7 varchar(64), c8 time, c9 timestamp null default CURRENT_TIMESTAMP, c10 decimal(10,1))")
Copy link
Contributor

Choose a reason for hiding this comment

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

why change this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If table definition say the column can't be null, and we insert a null value, the operation should fail.
I just fix the test case make it behaves right.

Copy link
Contributor

Choose a reason for hiding this comment

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

so the behavior of the cases which insert null values is wrong.

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.

@XuHuaiyu
Copy link
Contributor

PTAL @zimulala

continue
for i, c := range e.Table.Cols() {
var needDefaultValue bool
if !hasValue[i] {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can use row[i] == nil instead of this code, then we can remove this argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, we can't.

row[i] == nil is ambiguous.

create table t (a int, b int);
insert into t (a) values 1;
insert into t values (1, null);

Both case, row[1] == nil here, but they are different.
In the former case, b need a default value, the later case, b should be null.

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it.

e.ctx.GetSessionVars().RetryInfo.AddAutoIncrementID(recordID)
}
} else {
needDefaultValue = false // handle it later.
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to add a TODO here? Don't you want to handle what.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems the comment is not accurate.
What I mean is if mysql.HasAutoIncrementFlag(c.Flag), row[i] don't needDefaultValue, it would be handled later, at line 915.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's confusing.

@zimulala
Copy link
Contributor

LGTM

@zimulala zimulala added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Jul 12, 2017
@tiancaiamao tiancaiamao merged commit 1e1d01a into master Jul 12, 2017
@tiancaiamao tiancaiamao deleted the tiancaiamao/null-default branch July 12, 2017 09:13
tiancaiamao added a commit that referenced this pull request Jul 13, 2017
create table t (ts timestamp);
insert into t values (null);
This should insert a null rather than default value.
tiancaiamao added a commit that referenced this pull request Jul 13, 2017
* expression, executor: fix hex(binary) (#3567)

* expression, executor: fix unhex(binary) error (#3569)

* expression: builtin now() should consider timestamp variable (#3590)

* tidb/privilege/privileges: make show databases available with any global privilege (#3666)

* privilege/privileges: skip privilege check for information_schema database (#3675)

* conn: fix database info leaking problem (#3699)

* server: escape database name "use `xxx`" in OpenCtx (#3713)

* executor: fix explicitly insert null value into timestamp column (#3646)

* util/types: fix parsing datatime 00-00-00 (#3536)

* tidb-server: fix incorrect error message when auth failed (#3696)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants