Skip to content

Fix comment marker swallowed by preceding operator run - #878

Open
mayuriphad wants to merge 1 commit into
andialbrecht:masterfrom
mayuriphad:fix-comment-after-operator
Open

Fix comment marker swallowed by preceding operator run#878
mayuriphad wants to merge 1 commit into
andialbrecht:masterfrom
mayuriphad:fix-comment-after-operator

Conversation

@mayuriphad

Copy link
Copy Markdown

Bug

Fixes #722.

sqlparse's operator regex ([+/@#%^&|^-]+ in sqlparse/keywords.py) is
greedy and includes - in its character class. When a -- (or # )
single-line comment marker immediately follows an operator with no
intervening whitespace, e.g. ||--comment, the operator regex consumes the
whole ||-- run as a single Operator token before the comment regex ever
gets a chance to match. The comment is then invisible to the tokenizer.

Repro:

import sqlparse
sql = "myval := oneval || otherval ||--;\ncontinuedvals;\n"
print(sqlparse.format(sql, strip_comments=True))
# strip_comments leaves the "--;" comment in place instead of stripping it

This can also make sqlparse.split() misjudge statement boundaries: a ;
that lives inside an un-recognized "comment" gets treated as a real
statement terminator only in other contexts, but more directly, comment
content that should have been dropped is not.

Fix

Add a negative lookahead to the operator regex so it stops consuming
characters right before a -- or # comment marker, letting the earlier
comment regex handle it as intended.

Test plan

Added regression tests:

  • tests/test_format.py::TestFormat::test_strip_comments_single_no_space_before_operator
  • tests/test_split.py::test_split_comment_directly_after_operator

Both fail on master and pass with the fix. Ran the full test suite
locally:

python -m pytest tests/
508 passed, 2 xfailed, 1 xpassed

Also ran ruff check sqlparse/keywords.py — clean (pre-existing lint
findings elsewhere in the repo are unrelated to this change).

The operator regex [+/@#%^&|^-]+ greedily consumed a directly
adjacent -- (or # ) comment marker, e.g. `||--comment` tokenized as
a single Operator token instead of `||` followed by a Comment.Single
token. This caused strip_comments to leave such comments in place,
and could make sqlparse.split() misjudge statement boundaries when
the comment contained a `;`.

Add a negative lookahead so the operator regex stops before a
comment marker, letting the comment regex handle it as before.

Fixes andialbrecht#722
Copilot AI lite review requested due to automatic review settings August 20, 2026 12:41

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

strip_comments does not strip when "|--" is found.

2 participants