#8443·sqlglot

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

python
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, so 1--1 is 1 - -1. sqlglot treats every -- as a comment and drops the rest of the line. The docs give balance--1 as the reason for the rule.
  • Row 3: a MySQL line comment runs to \n only. sqlglot also ends it at \r (tokenizer_core.py line 899), so +1 becomes part of the query.

Official Documentation