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

parser, ast, expression: fix bug on DATE literal #4362

Merged
merged 21 commits into from Sep 15, 2017

Conversation

Projects
None yet
6 participants
@spongedu
Contributor

spongedu commented Aug 29, 2017

Currently TiDB reuse builtin function DATE(arg) to evalute Date literal, which is not consistent with mysql under certain circumstance.

1. DATE literal don't accept DATETIME/TIMESTAMP formats.

mysql> select date('2017-01-01 01:01:01');
Field   1:  `date('2017-01-01 01:01:01')`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       DATE
Collation:  binary (63)
Length:     10
Max_length: 10
Decimals:   0
Flags:      BINARY 


+-----------------------------+
| date('2017-01-01 01:01:01') |
+-----------------------------+
| 2017-01-01                  |
+-----------------------------+
1 row in set (0.00 sec)

mysql> select date '2017-01-01 01:01:01';
ERROR 1525 (HY000): Incorrect DATE value: '2017-01-01 01:01:01'

**2. select date 'str' throws error on illegal inputs, whileselect date(str)` returns null and throws a warning under certain sql_mode **

mysql> set sql_mode="";
Query OK, 0 rows affected (0.00 sec)

mysql> select date('abc');
Field   1:  `date('abc')`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       DATE
Collation:  binary (63)
Length:     10
Max_length: 0
Decimals:   0
Flags:      BINARY 


+-------------+
| date('abc') |
+-------------+
| NULL        |
+-------------+
1 row in set, 1 warning (0.00 sec)

mysql> select date 'abc';
ERROR 1525 (HY000): Incorrect DATE value: 'abc'

spongedu added some commits Aug 30, 2017

Show outdated Hide outdated expression/integration_test.go
@zz-jason

This comment has been minimized.

Show comment
Hide comment
@zz-jason
Member

zz-jason commented Aug 31, 2017

@breeswish PTAL

@breeswish

This comment has been minimized.

Show comment
Hide comment
@breeswish

breeswish Aug 31, 2017

Member

Add type infer tests to verify the return type of DATE literal.

Member

breeswish commented Aug 31, 2017

Add type infer tests to verify the return type of DATE literal.

@breeswish

This comment has been minimized.

Show comment
Hide comment
@breeswish

breeswish Aug 31, 2017

Member

Is it possible to evaluate the DATE literal before execution? From my understanding, it seems that the DATE literal will be evaluated for every row? You may take a look at other literals, e.g. hex literal, to see how this is achieved.

Member

breeswish commented Aug 31, 2017

Is it possible to evaluate the DATE literal before execution? From my understanding, it seems that the DATE literal will be evaluated for every row? You may take a look at other literals, e.g. hex literal, to see how this is achieved.

@spongedu

This comment has been minimized.

Show comment
Hide comment
@spongedu

spongedu Aug 31, 2017

Contributor

Done @breeswish . PTAL

Contributor

spongedu commented Aug 31, 2017

Done @breeswish . PTAL

@breeswish

This comment has been minimized.

Show comment
Hide comment
@breeswish

breeswish Aug 31, 2017

Member

@spongedu Can we totally avoid the date function call? I guess it is possible to directly return a date literal (datum / constant expr) at the parsing stage.

Member

breeswish commented Aug 31, 2017

@spongedu Can we totally avoid the date function call? I guess it is possible to directly return a date literal (datum / constant expr) at the parsing stage.

@spongedu

This comment has been minimized.

Show comment
Hide comment
@spongedu

spongedu Sep 1, 2017

Contributor

@breeswish I think it should be done in constant-folding stage in optimizer uniformly

Contributor

spongedu commented Sep 1, 2017

@breeswish I think it should be done in constant-folding stage in optimizer uniformly

@zz-jason

This comment has been minimized.

Show comment
Hide comment
@zz-jason

zz-jason Sep 1, 2017

Member

@spongedu plz resolve conflicts.

Member

zz-jason commented Sep 1, 2017

@spongedu plz resolve conflicts.

@sre-bot

This comment has been minimized.

Show comment
Hide comment
@sre-bot

sre-bot Sep 9, 2017

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.

sre-bot commented Sep 9, 2017

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.

@spongedu

This comment has been minimized.

Show comment
Hide comment
@spongedu

spongedu Sep 9, 2017

Contributor

Done @zz-jason PTAL

Contributor

spongedu commented Sep 9, 2017

Done @zz-jason PTAL

Show outdated Hide outdated expression/builtin_time.go
}
constant, ok := args[0].(*Constant)
if !ok {
return nil, errors.Trace(types.ErrInvalidTimeFormat)

This comment has been minimized.

@zz-jason

zz-jason Sep 13, 2017

Member

should we handle this error to respect to sql mode ?

@zz-jason

zz-jason Sep 13, 2017

Member

should we handle this error to respect to sql mode ?

This comment has been minimized.

@spongedu

spongedu Sep 13, 2017

Contributor

nope.

@spongedu

spongedu Sep 13, 2017

Contributor

nope.

Show outdated Hide outdated expression/builtin_time.go
Show outdated Hide outdated expression/builtin_time.go
Show outdated Hide outdated expression/builtin_time.go
}
tm, err := types.ParseDate(str)
if err != nil {
return nil, errors.Trace(err)

This comment has been minimized.

@zz-jason

zz-jason Sep 13, 2017

Member

ditto

@zz-jason
Show outdated Hide outdated expression/integration_test.go
@spongedu

This comment has been minimized.

Show comment
Hide comment
@spongedu

spongedu Sep 14, 2017

Contributor

@XuHuaiyu Take SQL_MODE into consideration and add related tests. PTAL

Contributor

spongedu commented Sep 14, 2017

@XuHuaiyu Take SQL_MODE into consideration and add related tests. PTAL

Show outdated Hide outdated mysql/const.go
Show outdated Hide outdated mysql/const.go
@XuHuaiyu

This comment has been minimized.

Show comment
Hide comment
@XuHuaiyu

XuHuaiyu Sep 14, 2017

Contributor

LGTM

Contributor

XuHuaiyu commented Sep 14, 2017

LGTM

@XuHuaiyu

This comment has been minimized.

Show comment
Hide comment
@XuHuaiyu

XuHuaiyu Sep 14, 2017

Contributor

/ok-to-test
/run-all-test

Contributor

XuHuaiyu commented Sep 14, 2017

/ok-to-test
/run-all-test

@zz-jason

This comment has been minimized.

Show comment
Hide comment
@zz-jason

zz-jason Sep 15, 2017

Member

/run-unit-test

Member

zz-jason commented Sep 15, 2017

/run-unit-test

@zz-jason

LGTM

@zz-jason zz-jason merged commit 4f07681 into pingcap:master Sep 15, 2017

10 checks passed

ci/circleci Your tests passed on CircleCI!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
jenkins-ci-tidb/build Jenkins job succeeded.
Details
jenkins-ci-tidb/common-test Jenkins job succeeded.
Details
jenkins-ci-tidb/integration-common-test Jenkins job succeeded.
Details
jenkins-ci-tidb/integration-ddl-test Jenkins job succeeded.
Details
jenkins-ci-tidb/mybatis-test Jenkins job succeeded.
Details
jenkins-ci-tidb/sqllogic-test Jenkins job succeeded.
Details
jenkins-ci-tidb/unit-test Jenkins job succeeded.
Details
license/cla Contributor License Agreement is signed.
Details

@spongedu spongedu deleted the spongedu:issue_4307 branch Sep 15, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment