MySQL: `--` only starts a comment when followed by whitespace or a control character
Author: notatallshaw-gtsCreated Sep 26, 2026Updated Sep 28, 2026
Before you file an issue
Reproduced on 30.19.0 and on main (8bd768664197fbd0ed86158c0eca955c14f43328). Read and write dialect mysql.
Fully reproducible code snippet
import sqlglot
for sql in ["SELECT 1--1", "SELECT 1-- 1", "SELECT 1 -- x\r+1"]:
print(repr(sql), "->", repr(sqlglot.transpile(sql, read="mysql", write="mysql")[0]))Each input and each output run on MySQL 26.7.0 (same on 8.4.11 and 9.7.2):
| Input | MySQL reads | Result | sqlglot output | Result |
|---|---|---|---|---|
SELECT 1--1 |
1 - -1 |
2 | SELECT 1 /* 1 */ |
1 |
SELECT 1-- 1 |
1, then a comment |
1 | SELECT 1 /* 1 */ |
1 |
SELECT 1 -- x\r+1 |
1, then the comment x\r+1 |
1 | SELECT 1 /* x */ + 1 |
2 |
The second row is correct and shown for contrast. The other two change the query's result:
- Row 1: MySQL starts a comment at
--only when the next character is whitespace or a control character, so1--1is1 - -1. sqlglot treats every--as a comment and drops the rest of the line. The docs givebalance--1as the reason for the rule. - Row 3: a MySQL line comment runs to
\nonly. sqlglot also ends it at\r(tokenizer_core.pyline 899), so+1becomes part of the query.
Official Documentation
- Comments: https://dev.mysql.com/doc/refman/26.7/en/comments.html
- '--' as the Start of a Comment: https://dev.mysql.com/doc/refman/26.7/en/ansi-diff-comments.html
- MySQL lexer,
--check: https://github.com/mysql/mysql-server/blob/mysql-26.7.0/sql/sql_lex.cc#L1520-L1525 - MySQL lexer, comment runs to
\n: https://github.com/mysql/mysql-server/blob/mysql-26.7.0/sql/sql_lex.cc#L1931-L1936
Source: tobymao/sqlglot