vitess icon indicating copy to clipboard operation
vitess copied to clipboard

Bug Report: User defined variables lose binary encoding information

Open dbussink opened this issue 4 months ago • 0 comments

Overview of the Issue

When using user defined variables in a session, information is lost when the value is passed down the MySQL when interpolated.

In the reproduction below, it can be seen that the collation for the @pt variable is returned as utf8mb4_0900_ai_ci. What is happening here is that the bind variables are sent down to vttablet and there interpolated, but then the literal string is used without any annotation.

So MySQL ends up running select collation('foo', rand();, which means the collation information is lost. We need to make sure that the variable is escaped as a proper binary string.

Reproduction Steps

mysql> set @p2 = 'foo';
Query OK, 0 rows affected (0.01 sec)

mysql> select @p2;
Field   1:  `@p2`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       VAR_STRING
Collation:  utf8mb4_0900_ai_ci (255)
Length:     0
Max_length: 3
Decimals:   0
Flags:      


+------+
| @p2  |
+------+
| foo  |
+------+
1 row in set (0.00 sec)

mysql> select collation(@p2), rand();
Field   1:  `collation(@p2)`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       VAR_STRING
Collation:  utf8mb4_0900_ai_ci (255)
Length:     256
Max_length: 18
Decimals:   31
Flags:      

Field   2:  `rand()`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       DOUBLE
Collation:  binary (63)
Length:     23
Max_length: 17
Decimals:   31
Flags:      NOT_NULL BINARY NUM 


+--------------------+-------------------+
| collation(@p2)     | rand()            |
+--------------------+-------------------+
| utf8mb4_0900_ai_ci | 0.048512788534642 |
+--------------------+-------------------+
1 row in set (0.00 sec)

Binary Version

-

Operating System and Environment details

-

Log Fragments

-

dbussink avatar Oct 17 '24 12:10 dbussink