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: rewrite builtin function: DATEDIFF #4212

Merged
merged 4 commits into from
Aug 24, 2017

Conversation

spongedu
Copy link
Contributor

for #4080

@zz-jason zz-jason changed the title expression: rewrite builtin datediff expression: rewrite builtin function: DATEDIFF Aug 16, 2017
@zz-jason zz-jason added the contribution This PR is from a community contributor. label Aug 16, 2017
bf, err := newBaseBuiltinFuncWithTp(args, ctx, tpInt, tpTime, tpTime)
if err != nil {
return nil, errors.Trace(err)
}
Copy link
Member

Choose a reason for hiding this comment

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

set Flen and Decimal of bf.tp

func (b *builtinDateDiffSig) evalInt(row []types.Datum) (int64, bool, error) {
ctx := b.ctx.GetSessionVars().StmtCtx
t1, isNull, err := b.args[0].EvalTime(row, ctx)
if isNull || err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

in mysql:

MySQL > select datediff("2014-12-09", "aaaa");
Field   1:  `datediff("2014-12-09", "aaaa")`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       LONGLONG
Collation:  binary (63)
Length:     7
Max_length: 0
Decimals:   0
Flags:      BINARY NUM


+--------------------------------+
| datediff("2014-12-09", "aaaa") |
+--------------------------------+
|                           NULL |
+--------------------------------+
1 row in set, 1 warning (0.00 sec)

MySQL > show warnings;
Field   1:  `Level`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       VAR_STRING
Collation:  utf8_general_ci (33)
Length:     21
Max_length: 7
Decimals:   31
Flags:      NOT_NULL

Field   2:  `Code`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       LONG
Collation:  binary (63)
Length:     4
Max_length: 4
Decimals:   0
Flags:      NOT_NULL UNSIGNED BINARY NUM

Field   3:  `Message`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       VAR_STRING
Collation:  utf8_general_ci (33)
Length:     1536
Max_length: 32
Decimals:   31
Flags:      NOT_NULL


+---------+------+----------------------------------+
| Level   | Code | Message                          |
+---------+------+----------------------------------+
| Warning | 1292 | Incorrect datetime value: 'aaaa' |
+---------+------+----------------------------------+
1 row in set (0.00 sec)

@@ -963,6 +963,9 @@ func (s *testIntegrationSuite) TestTimeBuiltin(c *C) {
result.Check(testkit.Rows("62966505600 63426672000 63426721412 63426721412"))
result = tk.MustQuery("select to_days(950501), to_days('2007-10-07'), to_days('2007-10-07 00:00:59'), to_days('0000-01-01')")
result.Check(testkit.Rows("728779 733321 733321 1"))
result = tk.MustQuery("select datediff('2007-12-31 23:59:59','2007-12-30'), datediff('2010-11-30 23:59:59', " +
Copy link
Member

Choose a reason for hiding this comment

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

add more tests like datediff("2014-12-09", "aaaa");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -994,5 +994,12 @@ func (s *testPlanSuite) createTestCase4TimeFuncs() []typeInferTestCase {
{"microsecond(c_blob )", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag, 6, 0},
{"microsecond(c_set )", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag, 6, 0},
{"microsecond(c_enum )", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag, 6, 0},

{"datediff(c_char, c_datetime)", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag, 20, 0},
Copy link
Member

Choose a reason for hiding this comment

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

Flen is not correct

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed it's not consistent with mysql. But I think it's reasonable to set it to mysql.MaxIntWidth. I'll take a look how this was set in mysql......

@@ -963,6 +963,9 @@ func (s *testIntegrationSuite) TestTimeBuiltin(c *C) {
result.Check(testkit.Rows("62966505600 63426672000 63426721412 63426721412"))
result = tk.MustQuery("select to_days(950501), to_days('2007-10-07'), to_days('2007-10-07 00:00:59'), to_days('0000-01-01')")
result.Check(testkit.Rows("728779 733321 733321 1"))
result = tk.MustQuery("select datediff('2007-12-31 23:59:59','2007-12-30'), datediff('2010-11-30 23:59:59', " +
Copy link
Contributor

Choose a reason for hiding this comment

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

add case like select datediff('2007-12-31 23:59:59', '23:59:59'); .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@zz-jason
Copy link
Member

@spongedu plz resolve conflicts

@XuHuaiyu
Copy link
Contributor

PTAL @winkyao @zz-jason

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

@XuHuaiyu @winkyao PTAL

@zz-jason zz-jason added the status/LGT1 Indicates that a PR has LGTM 1. label Aug 23, 2017
@spongedu spongedu force-pushed the rewrite_builtin_datediff branch 2 times, most recently from 399dba5 to 71c2db8 Compare August 23, 2017 12:50
@zz-jason
Copy link
Member

@spongedu please resolve conflicts

Copy link
Contributor

@XuHuaiyu XuHuaiyu 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

@@ -1078,6 +1078,9 @@ func (s *testIntegrationSuite) TestTimeBuiltin(c *C) {
result.Check(testkit.Rows("62966505600 63426672000 63426721412 63426721412"))
result = tk.MustQuery("select to_days(950501), to_days('2007-10-07'), to_days('2007-10-07 00:00:59'), to_days('0000-01-01')")
result.Check(testkit.Rows("728779 733321 733321 1"))
result = tk.MustQuery("select datediff('2007-12-31 23:59:59','2007-12-30'), datediff('2010-11-30 23:59:59', " +
Copy link
Contributor

@XuHuaiyu XuHuaiyu Aug 24, 2017

Choose a reason for hiding this comment

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

add test cases like

  1. "0000-01-01", "0001-01-01"
  2. "0001-00-01", "0001-00-01"
  3. "0001-01-00", "0001-01-00"
  4. "2017-01-01", "2017-01-01"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. @XuHuaiyu PTAL

@spongedu spongedu force-pushed the rewrite_builtin_datediff branch 2 times, most recently from eedfb4e to e3504f9 Compare August 24, 2017 06:44
@XuHuaiyu
Copy link
Contributor

Please merge the master, I've split tpTime into tpDatetime and tpTimestamp.

@spongedu
Copy link
Contributor Author

@XuHuaiyu Done

@XuHuaiyu
Copy link
Contributor

please fix ci

@spongedu
Copy link
Contributor Author

@XuHuaiyu Done. PTAL

Copy link
Contributor

@XuHuaiyu XuHuaiyu left a comment

Choose a reason for hiding this comment

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

LGTM

@XuHuaiyu XuHuaiyu merged commit ff57b06 into pingcap:master Aug 24, 2017
@spongedu spongedu deleted the rewrite_builtin_datediff branch August 24, 2017 08:43
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. status/LGT1 Indicates that a PR has LGTM 1.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants