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

core: Fix confusing behaviors of FROM_BASE64 function in WHERE clause #28229

Merged
merged 17 commits into from
Sep 27, 2021

Conversation

zhaoxugang
Copy link
Contributor

@zhaoxugang zhaoxugang commented Sep 21, 2021

  1. core: Fix confusing behaviors of FROM_BASE64 function in WHERE clause

What problem does this PR solve?

Issue Number: close #28154

Problem Summary:

What is changed and how it works?

Proposal: xxx

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

Coorercing the function `FROM_BASE64()` to a boolean value could return invalid results.

@ti-chi-bot
Copy link
Member

ti-chi-bot commented Sep 21, 2021

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • morgo
  • xhebox

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 size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Sep 21, 2021
@morgo
Copy link
Contributor

morgo commented Sep 21, 2021

MySQL 5.7 and MySQL 8.0 behave differently here:

mysql [localhost:5731] {msandbox} (test) > select * from t where from_base64('');
Empty set (0.00 sec)

mysql [localhost:5731] {msandbox} (test) > update t set a = 'def' where from_base64('');
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql [localhost:5731] {msandbox} (test) > select * from t where from_base64('invalidbase64');
Empty set (0.00 sec)

mysql [localhost:5731] {msandbox} (test) > update t set a = 'hig' where from_base64('invalidbase64');
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql [localhost:5731] {msandbox} (test) > select * from t where from_base64('test');
Empty set (0.00 sec)

mysql [localhost:5731] {msandbox} (test) > update t set a = 'xyz' where from_base64('test');
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql [localhost:5731] {msandbox} (test) > select * from t;
+------+
| a    |
+------+
| abc  |
+------+
1 row in set (0.01 sec)

Versus:

mysql [localhost:8024] {root} (test) > insert into t values('abc');
Query OK, 1 row affected (0.00 sec)

mysql [localhost:8024] {root} (test) > select * from t where from_base64('');
Empty set (0.00 sec)

mysql [localhost:8024] {root} (test) > update t set a = 'def' where from_base64('');
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql [localhost:8024] {root} (test) > select * from t where from_base64('invalidbase64');
Empty set (0.00 sec)

mysql [localhost:8024] {root} (test) > update t set a = 'hig' where from_base64('invalidbase64');
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql [localhost:8024] {root} (test) > select * from t where from_base64('test');
Empty set, 1 warning (0.00 sec) <-- the warning is a truncated value

mysql [localhost:8024] {root} (test) > update t set a = 'xyz' where from_base64('test');
ERROR 1292 (22007): Truncated incorrect DOUBLE value: '\xB5\xEB-'
mysql [localhost:8024] {root} (test) > select * from t;
+------+
| a    |
+------+
| abc  |
+------+
1 row in set (0.00 sec)

@zhaoxugang
Copy link
Contributor Author

MySQL 5.7 and MySQL 8.0 behave differently here:

mysql [localhost:5731] {msandbox} (test) > select * from t where from_base64('');
Empty set (0.00 sec)

mysql [localhost:5731] {msandbox} (test) > update t set a = 'def' where from_base64('');
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql [localhost:5731] {msandbox} (test) > select * from t where from_base64('invalidbase64');
Empty set (0.00 sec)

mysql [localhost:5731] {msandbox} (test) > update t set a = 'hig' where from_base64('invalidbase64');
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql [localhost:5731] {msandbox} (test) > select * from t where from_base64('test');
Empty set (0.00 sec)

mysql [localhost:5731] {msandbox} (test) > update t set a = 'xyz' where from_base64('test');
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql [localhost:5731] {msandbox} (test) > select * from t;
+------+
| a    |
+------+
| abc  |
+------+
1 row in set (0.01 sec)

Versus:

mysql [localhost:8024] {root} (test) > insert into t values('abc');
Query OK, 1 row affected (0.00 sec)

mysql [localhost:8024] {root} (test) > select * from t where from_base64('');
Empty set (0.00 sec)

mysql [localhost:8024] {root} (test) > update t set a = 'def' where from_base64('');
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql [localhost:8024] {root} (test) > select * from t where from_base64('invalidbase64');
Empty set (0.00 sec)

mysql [localhost:8024] {root} (test) > update t set a = 'hig' where from_base64('invalidbase64');
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql [localhost:8024] {root} (test) > select * from t where from_base64('test');
Empty set, 1 warning (0.00 sec) <-- the warning is a truncated value

mysql [localhost:8024] {root} (test) > update t set a = 'xyz' where from_base64('test');
ERROR 1292 (22007): Truncated incorrect DOUBLE value: '\xB5\xEB-'
mysql [localhost:8024] {root} (test) > select * from t;
+------+
| a    |
+------+
| abc  |
+------+
1 row in set (0.00 sec)

I tried in mysql 8.0.21,following:
mysql> insert into t values('abc');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t where from_base64('');
Empty set (0.00 sec)

mysql> update t set a = 'def' where from_base64('');
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0

mysql> select * from t where from_base64('invalidbase64');
Empty set (0.00 sec)

mysql> update t set a = 'hig' where from_base64('invalidbase64');
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0

mysql> select * from t where from_base64('test');
Empty set (0.00 sec)

mysql> update t set a = 'xyz' where from_base64('test');
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0

mysql> select * from t;
+------+
| a |
+------+
| abc |
+------+
1 row in set (0.01 sec)

mysql> select @@Version;
+-----------+
| @@Version |
+-----------+
| 8.0.21 |
+-----------+
1 row in set (0.00 sec)

@zhaoxugang
Copy link
Contributor Author

/cc @morgo @xhebox

@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 23, 2021
Copy link
Contributor

@morgo morgo left a comment

Choose a reason for hiding this comment

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

LGTM, I've updated the release note

@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Sep 27, 2021
Copy link
Contributor

@xhebox xhebox left a comment

Choose a reason for hiding this comment

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

LGTM

@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Sep 27, 2021
@xhebox
Copy link
Contributor

xhebox commented Sep 27, 2021

/merge

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: 85a9953

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Sep 27, 2021
@@ -987,7 +987,10 @@ func (b *PlanBuilder) buildSelection(ctx context.Context, p LogicalPlan, where a
for _, item := range cnfItems {
if con, ok := item.(*expression.Constant); ok && con.DeferredExpr == nil && con.ParamMarker == nil {
ret, _, err := expression.EvalBool(b.ctx, expression.CNFExprs{con}, chunk.Row{})
if err != nil || ret {
if err != nil {
return nil, errors.Trace(err)
Copy link
Contributor

@xiongjiwei xiongjiwei Sep 28, 2021

Choose a reason for hiding this comment

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

return error here may cause problems

update t set a = 'b' where ' ';

in tidb, it returns error, in MySQL it is ok.

ref #27954

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

+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.

IMO the main question is that when convert err is present we should return err or return empty set?if return err maybe effect the user of tidb if return empty will confuse with actual return empty set.I need some advices
@xiongjiwei @yuqi1129 @morgo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note size/M Denotes a PR that changes 30-99 lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Confusing behaviors of FROM_BASE64 function in WHERE clause
6 participants