Fix BETWEEN with leading-dot decimals being mis-lexed (issue601)#861
Open
AmadNaseem wants to merge 1 commit into
Open
Fix BETWEEN with leading-dot decimals being mis-lexed (issue601)#861AmadNaseem wants to merge 1 commit into
AmadNaseem wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes a lexer edge case where a keyword immediately preceding a leading-dot float (e.g. BETWEEN .03) could be mis-tokenized as a Name due to the qualified-identifier regex, which then breaks downstream grouping.
Changes:
- Tighten the qualified-name lookahead in
SQL_REGEXto ignore.when it begins a numeric literal (.(\d...)). - Add a regression test for
between .03 and .06tokenization (issue601). - Document the fix in
CHANGELOG.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
sqlparse/keywords.py |
Refines the qualified-name regex to avoid consuming keywords before leading-dot floats. |
tests/test_regressions.py |
Adds a regression test ensuring between/and remain Keyword tokens with leading-dot floats. |
CHANGELOG |
Adds a bug-fix entry describing the corrected lexing behavior (issue601). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+474
to
+480
| tokens = [t for t in p.flatten() if not t.is_whitespace] | ||
| assert tokens[1].ttype is T.Keyword | ||
| assert tokens[1].value == 'between' | ||
| assert tokens[2].ttype is T.Number.Float | ||
| assert tokens[3].ttype is T.Keyword | ||
| assert tokens[3].value == 'and' | ||
| assert tokens[4].ttype is T.Number.Float |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #601.
sqlparsemis-lexesBETWEEN/ANDwhen the range bounds are decimals written without a leading zero:The same query with
0.03/0.06parses fine, which points at the leading dot.Cause: the qualified-name rule in
SQL_REGEX(sqlparse/keywords.py) is[A-ZÀ-Ü]\w*(?=\s*\.)— it matches a bareword followed by a., intended forschema.name. But it also fires onbetweeninbetween .03, mistaking the decimal point for a member-access period, so the keyword is swallowed as a name.Fix: narrow the lookahead to
[A-ZÀ-Ü]\w*(?=\s*\.(?!\d)), so the.only counts as member access when it is not followed by a digit. This is safe because SQL identifiers cannot start with a digit, soname.<digit>is never valid member access. Qualified names still work:schema.table,db.schema.table,t.col,t.*,a.b.c.dall verified unchanged..03/.06were already matched by the float rule; they just weren't reached before because the name rule consumed the keyword.Added a regression test in
tests/test_regressions.pyand aCHANGELOGentry. Full test suite passes (488 passed).