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: return null when cast to huge binary type #8768

Merged
merged 5 commits into from
Dec 29, 2018

Conversation

eurekaka
Copy link
Contributor

@eurekaka eurekaka commented Dec 21, 2018

What problem does this PR solve?

Before this PR, for this query:

mysql> select cast(1 as binary(4294967295));
ERROR 2013 (HY000): Lost connection to MySQL server during query

and tidb-server is down, because we would allocate about 4GB memory for the result, so it triggers golang runtime panic "out of memory" on my machine, but we don't catch this panic and do recover.

What is changed and how it works?

According to https://dev.mysql.com/doc/refman/5.7/en/cast-functions.html#function_cast, MySQL should also allocate this 4GB memory for binary result, and pad \0 in the tail, but in my practical experiment, it does not. Here is the output of MySQL 5.7.10:

mysql> select cast(1 as binary(4294967295));
+-------------------------------+
| cast(1 as binary(4294967295)) |
+-------------------------------+
| 1                             |
+-------------------------------+
1 row in set (0.00 sec)

mysql> select cast(1 as binary(4294967295)) = '1';
+-------------------------------------+
| cast(1 as binary(4294967295)) = '1' |
+-------------------------------------+
|                                   1 |
+-------------------------------------+
1 row in set (0.00 sec)

In other word, MySQL does not behave as its documentation on this issue.

We have 2 choices to fix this problem:

these 2 choices may lead to different behaviors, I prefer the second one, because if we impose no check on the length and recover when panic occurs, it has risk to trigger panic in other places since binary type is assumed to be less than 255 bytes.

Based on #8768 (comment), change the behavior to be compatible with MySQL 8.0.

Check List

Tests

  • Unit test

Code changes

N/A

Side effects

  • Breaking backward compatibility: as mentioned above, select cast(1 as binary(1024)) is able to be executed successfully before, but unable now. No compatibility breaking now.

Related changes

  • Need to cherry-pick to the release branch

This change is Reviewable

@eurekaka eurekaka added type/bugfix This PR fixes a bug. sig/planner SIG: Planner labels Dec 21, 2018
@eurekaka
Copy link
Contributor Author

/run-all-tests

1 similar comment
@zhouqiang-cl
Copy link
Contributor

/run-all-tests

@eurekaka
Copy link
Contributor Author

@zz-jason @winoros @lamxTyler @XuHuaiyu PTAL

@eurekaka eurekaka requested a review from morgo December 21, 2018 09:27
@eurekaka
Copy link
Contributor Author

/run-all-tests

@eurekaka eurekaka changed the title plan: validate flen of binary target type for cast function expression: return null when cast to huge binary type Dec 24, 2018
@eurekaka
Copy link
Contributor Author

/run-common-test
/run-integration-common-test

@eurekaka
Copy link
Contributor Author

/run-integration-common-test

Copy link
Member

@winoros winoros left a comment

Choose a reason for hiding this comment

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

lgtm

@eurekaka
Copy link
Contributor Author

/rebuild

1 similar comment
@eurekaka
Copy link
Contributor Author

/rebuild

@eurekaka
Copy link
Contributor Author

/run-integration-common-test

@eurekaka
Copy link
Contributor Author

/run-all-tests

@eurekaka
Copy link
Contributor Author

/run-unit-test

@eurekaka
Copy link
Contributor Author

/run-unit-test

@eurekaka
Copy link
Contributor Author

/run-all-tests

@eurekaka eurekaka added status/LGT1 Indicates that a PR has LGTM 1. status/all tests passed labels Dec 25, 2018
@eurekaka
Copy link
Contributor Author

@zz-jason @lamxTyler PTAL

Copy link
Contributor

@alivxxx alivxxx left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/expression status/LGT2 Indicates that a PR has LGTM 2. type/bugfix This PR fixes a bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants