dolthub / dolt

Dolt – Git for Data
Apache License 2.0
17.41k stars 488 forks source link

Backtick escaping doesn't work for variables #8010

Closed arvidfm closed 1 month ago

arvidfm commented 1 month ago

MySQL allows for escaping variable names with backticks using the following syntax:

> SET @user_var = 42;
> SELECT @@GLOBAL.`innodb_autoinc_lock_mode`, @@`innodb_autoinc_lock_mode`, @`user_var`;
+-------------------------------------+------------------------------+-------------+
| @@GLOBAL.`innodb_autoinc_lock_mode` | @@`innodb_autoinc_lock_mode` | @`user_var` |
+-------------------------------------+------------------------------+-------------+
|                                   1 |                            1 |          42 |
+-------------------------------------+------------------------------+-------------+

Dolt, however, appears to interpret the backticks as part of the variable name:

> SET @user_var = 42;
> SELECT @@GLOBAL.`innodb_autoinc_lock_mode`, @@`innodb_autoinc_lock_mode`, @`user_var`;
Unknown system variable '@@global.`innodb_autoinc_lock_mode`'

Not much of a problem in practice as you can simply remove the backticks, but it can cause issues if using a query builder that automatically escapes idents, or if you for whatever godforsaken reason need to include spaces in your variable name:

> SET @`my variable` = 42;
Error parsing SQL: 
syntax error at position 19 near 'my variable'
SET @`my variable` = 42
jycor commented 1 month ago

Hey @arvidfm, fix is in vitess repo (our parsing code) and making its way to dolt. Expect to see a release with the fix later this week.

With these new changes, we should be a more lenient than MySQL for backticks now. In addition to the explams you provided above, you can

tmp/main*> set @abc.`def ghi` = 100;
tmp/main*> select @abc.`def ghi`;
+--------------+
| @abc.def ghi |
+--------------+
| 100          |
+--------------+
1 row in set (0.00 sec)

in dolt, while that would result in syntax errors in MySQL, so that's useful I guess.