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/builtin_time: fix wrong precision for maketime #28068

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

w169q169
Copy link
Contributor

@w169q169 w169q169 commented Sep 15, 2021

What problem does this PR solve?

Issue Number: close #27771

Problem Summary:

What is changed and how it works?

What's Changed:

How it Works:

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

None

@ti-chi-bot
Copy link
Member

[REVIEW NOTIFICATION]

This pull request has not been approved.

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added release-note-none Denotes a PR that doesn't merit a release note. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Sep 15, 2021
@w169q169
Copy link
Contributor Author

/cc @wjhuang2016

@w169q169
Copy link
Contributor Author

/run-check_dev_2

@ti-chi-bot ti-chi-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 21, 2021
Copy link
Member

@wjhuang2016 wjhuang2016 left a comment

Choose a reason for hiding this comment

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

_

@ti-chi-bot ti-chi-bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Sep 22, 2021
Copy link
Member

@wjhuang2016 wjhuang2016 left a comment

Choose a reason for hiding this comment

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

you can use setDecimalAndFlenForTime after #28252 gets merged.

decimal = args[2].GetType().Decimal
if decimal > 6 || decimal == types.UnspecifiedLength {
decimal = 6
case types.ETString:
Copy link
Member

Choose a reason for hiding this comment

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

  bool resolve_type(THD *) override {
    set_data_type_time(MY_MIN(args[2]->decimals, DATETIME_MAX_DECIMALS));
    return false;
  }

I think this can work, we don't need getExpressionFsp().

Copy link
Member

Choose a reason for hiding this comment

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

But be careful with UnspecifiedLength, which is 31 in MySQL but -1 in TiDB

Copy link
Contributor Author

@w169q169 w169q169 Sep 23, 2021

Choose a reason for hiding this comment

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

thanks for your reply, I will continue after your works finished

Copy link
Member

Choose a reason for hiding this comment

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

If the type is ETSTRING, then the decimal should be -1?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can we set the decimal be UnspecifiedLength, if the type is ETSTRING ?

@ti-chi-bot ti-chi-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 26, 2021
@ti-chi-bot ti-chi-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 27, 2021
@sre-bot
Copy link
Contributor

sre-bot commented Sep 27, 2021

Please follow PR Title Format:

  • pkg [, pkg2, pkg3]: what is changed

Or if the count of mainly changed packages are more than 3, use

  • *: what is changed

After you have format title, you can leave a comment /run-check_title to recheck it

@ti-chi-bot ti-chi-bot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Sep 27, 2021
switch tp {
case types.ETString:
decimal = 6
case types.ETInt:
Copy link
Member

Choose a reason for hiding this comment

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

If the type is ETINT, then the args[2].GetType().Decimal should be 0.

Copy link
Member

Choose a reason for hiding this comment

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

So the case ETint can be merged to default.

decimal = args[2].GetType().Decimal
if decimal > 6 || decimal == types.UnspecifiedLength {
decimal = 6
case types.ETString:
Copy link
Member

Choose a reason for hiding this comment

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

If the type is ETSTRING, then the decimal should be -1?

default:
decimal = args[2].GetType().Decimal
Copy link
Member

Choose a reason for hiding this comment

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

Can we use this to handle all the cases?

Copy link
Contributor Author

@w169q169 w169q169 Sep 28, 2021

Choose a reason for hiding this comment

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

I add mores unit tests in expression/typeinfer_test.go to cover all the cases . It seems to be good.

Copy link
Member

Choose a reason for hiding this comment

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

For ETString, the decimal shoule be 31? Then it will be truncated to 6,

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 think set UnspecifiedLength for ETSTRING is more clear, because decimal is 31 in MySQL but -1 in TiDB.Decimal use 31 in ETSTRING is not same as common rules in TIDB.

Copy link
Member

Choose a reason for hiding this comment

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

Anyway, it's covered by L5836. We can set decimal = args[2].GetType().Decimal for ETString.

Copy link
Contributor Author

@w169q169 w169q169 Sep 30, 2021

Choose a reason for hiding this comment

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

Set decimal = args[2].GetType().Decimal for ETString can't cover the situation that the arg is varchar type({maketime(c_int_d, c_int_d, c_char) test in expression/typeinfer_test.go).
In that case, the decimal is zero not the UnspecifiedLength, and we will get zero decimal that is wrong result(expect decimal is 6).
So we have to specialize UnspecifiedLength in decimal for type ETSTRING.

Copy link
Member

Choose a reason for hiding this comment

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

For literal "string", the decimal is 31.

mysql> select "asd";
Field   1:  `asd`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       VAR_STRING
Collation:  utf8mb4_bin (46)
Length:     12
Max_length: 3
Decimals:   31
Flags:      NOT_NULL


+-----+
| asd |
+-----+
| asd |
+-----+
1 row in set (0.01 sec)

Could you find out why it's 0 for the string column?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@wjhuang2016

MySQL [test]> select 'asd';
Field   1:  `asd`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       VAR_STRING
Collation:  utf8mb3_general_ci (33)
Length:     9
Max_length: 3
Decimals:   31
Flags:      NOT_NULL 


+-----+
| asd |
+-----+
| asd |
+-----+
1 row in set (0.001 sec)
MySQL [test]> select a from t_test;
Field   1:  `a`
Catalog:    `def`
Database:   `test`
Table:      `t_test`
Org_table:  `t_test`
Type:       VAR_STRING
Collation:  utf8mb3_general_ci (33)
Length:     75
Max_length: 0
Decimals:   0
Flags:      


0 rows in set (0.002 sec)

type decimal
mysql_string(literal) 31
mysql_varstring 0

mysql and tidb look same from the client side, but it has different implement for decimals in types.

for mysql
mysql change 31(DECIMAL_NOT_SPECIFIED) to 0 while sending column meta info (only for varstring, not for literal string).

https://github.com/mysql/mysql-server/blob/3290a66c89eb1625a7058e0ef732432b6952b435/sql/field.cc#L2061-L2063

type decimal(inner) decimal(column-meta)
mysql_string(literal) 31(DECIMAL_NOT_SPECIFIED) 31
mysql_varstring 31(DECIMAL_NOT_SPECIFIED) 0

for tidb
tidb change -1(types.UnspecifiedLength) to 0 while sending column meta info(only for literal string, not for var string)

tidb/server/driver_tidb.go

Lines 409 to 413 in 5b5020d

if fld.Column.Decimal == types.UnspecifiedLength {
if fld.Column.Tp == mysql.TypeDuration {
ci.Decimal = uint8(types.DefaultFsp)
} else {
ci.Decimal = mysql.NotFixedDec

type decimal(inner) decimal(column-meta)
mysql_string(literal) -1(UnspecifiedLength) 31(NotFixedDec)
mysql_varstring 0 0

in the end
according to the above reasons, use min to check decimal is right for mysql but will get wrong result in tidb.

https://github.com/mysql/mysql-server/blob/3290a66c89eb1625a7058e0ef732432b6952b435/sql/item_timefunc.h#L1557-L1563

@ti-chi-bot ti-chi-bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Sep 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note-none Denotes a PR that doesn't merit a release note. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

maketime function gets wrong precision
4 participants