Skip to content

Read a # comment with no space after it as a comment - #2481

Merged
rolandwalker merged 2 commits into
dbcli:mainfrom
goranai:fix/hash-comment-without-space
Nov 29, 2026
Merged

rolandwalker merged 2 commits into
dbcli:mainfrom
goranai:fix/hash-comment-without-space

Conversation

@goranai

@goranai goranai commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #2480

MySQL reads # followed by anything as a comment to the end of the line. sqlparse only does this when a space follows the #. So #drop table t; drop table u; was cut at the ;, the commented-out drop table u was sent to the server, and #note before a DELETE hid it from the destructive warning.

The change is one new module, mycli/packages/utils/sqlparse_utils.py. use_mysql_hash_comments() swaps three patterns on the shared sqlparse lexer:

  • the single line comment (--|# )... becomes (--|#)...
  • the name \w[$#\w]* becomes \w[$\w]*
  • the operator [+/@#%^&|^-]+ becomes [+/@%^&|^-]+

The last two stop a # from running on into a name or an operator, so a#c;d and 1/#c;d read as a comment too. delimiter_command.py, batch_utils.py and sql_utils.py call it when they load, next to the MAX_GROUPING_DEPTH lines that already change sqlparse for the whole process.

This is a change to sqlparse's shared lexer inside mycli, so I want to say it plainly. Only mycli's own code uses sqlparse in this process, and all of mycli's SQL is MySQL. sqlparse has an open request for this (andialbrecht/sqlparse#512), but a fix in mycli does not wait for a sqlparse release. The patterns are matched by their exact text in sqlparse 0.6.1. If a sqlparse upgrade changes one of them, a new test fails, so the fix cannot stop working without anyone noticing.

What changes for a user:

  • mycli --batch, /source, -e and stdin keep #comment;with semicolon inside one statement. Real schema files such as urlooker's sql/schema.sql went from 23 statements to 9, with no comment-only statements.
  • is_destructive(), need_completion_refresh() and need_completion_reset() see the statement after a #note line.
  • Strings, backtick names and /* */ comments that contain # are unchanged.
  • A ; after #word on a system, pager or tee line no longer ends the statement, for example system echo a#b; echo c. That line is one statement now, as it is when the # has a space after it.
  • A lone CR still ends a # comment, as it already does for -- and # . This is not changed here. A --batch file never has a lone CR, because Python reads it as a newline.

Tests: new cases in test_delimitercommand.py, test_batch_utils.py and test_sql_utils.py fail on main (10 failed, 258 passed in those three files) and pass with the fix (268 passed). The full suite is 101 failed, 3492 passed, 133 skipped, 7 xfailed on Windows with the change, and 101 failed, 3475 passed on main. The failing set is the same (no polars or llm package, no pty or pwd).

I also compared the splitter with the sqlglot MySQL tokenizer on 24000 random inputs from two generators. 16407 could be compared. The change fixes 1833 wrong splits from main and adds 2. One added case has #>, where sqlglot reads an operator but MySQL starts a comment, so the change is right there. The other has a -- with no space after it, which this change does not touch. With a lone CR in the inputs (24000 inputs) the change fixes 1681 wrong splits and adds 27, and 24 of those have a lone CR.

Validated with Claude Opus 5.5.

Checklist

  • I added this contribution to the changelog.md file.
  • I added my name to the mycli/AUTHORS.txt file (or it's already there).
  • To lint and format the code, I ran
    uv run ruff check && uv run ruff format && uv run mypy --install-types .

@rolandwalker rolandwalker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Great! Too bad we can't upstream a fix, since sqlparse recently stopped development.

@rolandwalker
rolandwalker merged commit 3807e8c into dbcli:main Nov 29, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A # comment with no space after the # is split at its semicolons

2 participants