-
Notifications
You must be signed in to change notification settings - Fork 699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
token source position tracking #794
Open
dylanscott
wants to merge
3
commits into
andialbrecht:master
Choose a base branch
from
hex-inc:dscott/token-position-tracking
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
from typing import List, Optional, Tuple, Type, Union | ||
|
||
from sqlparse import parse | ||
from sqlparse.sql import Identifier, IdentifierList, Statement, Token | ||
from sqlparse.tokens import ( | ||
CTE, | ||
DML, | ||
Comparison, | ||
Keyword, | ||
Number, | ||
_TokenType as TokenType, | ||
) | ||
|
||
|
||
def itertokens(token: Token): | ||
yield token | ||
if token.is_group: | ||
for child in token.tokens: | ||
yield from itertokens(child) | ||
|
||
|
||
# allow matching by Token subclass or ttype | ||
TokenClassOrType = Union[TokenType, Type[Token]] | ||
|
||
|
||
def parsed_tokens_with_sources( | ||
sql: str, types: Tuple[TokenClassOrType, ...] | ||
) -> List[Tuple[TokenClassOrType, str, str]]: | ||
# given a query, parses it, iterates over all the tokens it contains, and | ||
# for each token that matches `types`, returns a tuple of the matched token | ||
# type, the token's value, and the source of the token retrieved by slicing | ||
# into the original query using the token's `pos` and `length` attributes | ||
|
||
def matches(token: Token) -> Optional[TokenClassOrType]: | ||
for class_or_type in types: | ||
if isinstance(class_or_type, TokenType): | ||
if token.ttype is class_or_type: | ||
return class_or_type | ||
elif isinstance(token, class_or_type): | ||
return class_or_type | ||
|
||
def get_source(token: Token) -> str: | ||
return sql[token.pos : token.pos + token.length] | ||
|
||
statements = parse(sql) | ||
return [ | ||
(match, token.value, get_source(token)) | ||
for statement in statements | ||
for token in itertokens(statement) | ||
if (match := matches(token)) | ||
] | ||
|
||
|
||
def test_simple_query(): | ||
assert parsed_tokens_with_sources( | ||
"select * from foo;", (DML, Identifier, Keyword, Statement) | ||
) == [ | ||
(Statement, "select * from foo;", "select * from foo;"), | ||
(DML, "select", "select"), | ||
(Keyword, "from", "from"), | ||
(Identifier, "foo", "foo"), | ||
] | ||
|
||
|
||
def test_multiple_statements(): | ||
stmt1 = "select * from foo;" | ||
stmt2 = "\nselect *\nfrom bar;" | ||
assert parsed_tokens_with_sources( | ||
stmt1 + stmt2, (DML, Identifier, Keyword, Statement) | ||
) == [ | ||
(Statement, stmt1, stmt1), | ||
(DML, "select", "select"), | ||
(Keyword, "from", "from"), | ||
(Identifier, "foo", "foo"), | ||
(Statement, stmt2, stmt2), | ||
(DML, "select", "select"), | ||
(Keyword, "from", "from"), | ||
(Identifier, "bar", "bar"), | ||
] | ||
|
||
|
||
def test_subselect(): | ||
stmt = """select a0, b0, c0, d0, e0 from | ||
(select * from dual) q0 where 1=1 and 2=2""" | ||
assert parsed_tokens_with_sources( | ||
stmt, | ||
( | ||
DML, | ||
Comparison, | ||
Identifier, | ||
IdentifierList, | ||
Keyword, | ||
Number.Integer, | ||
Statement, | ||
), | ||
) == [ | ||
(Statement, stmt, stmt), | ||
(DML, "select", "select"), | ||
(IdentifierList, "a0, b0, c0, d0, e0", "a0, b0, c0, d0, e0"), | ||
(Identifier, "a0", "a0"), | ||
(Identifier, "b0", "b0"), | ||
(Identifier, "c0", "c0"), | ||
(Identifier, "d0", "d0"), | ||
(Identifier, "e0", "e0"), | ||
(Keyword, "from", "from"), | ||
(Identifier, "(select * from dual) q0", "(select * from dual) q0"), | ||
(DML, "select", "select"), | ||
(Keyword, "from", "from"), | ||
(Identifier, "dual", "dual"), | ||
(Identifier, "q0", "q0"), | ||
(Keyword, "where", "where"), | ||
(Number.Integer, "1", "1"), | ||
(Comparison, "=", "="), | ||
(Number.Integer, "1", "1"), | ||
(Keyword, "and", "and"), | ||
(Number.Integer, "2", "2"), | ||
(Comparison, "=", "="), | ||
(Number.Integer, "2", "2"), | ||
] | ||
|
||
|
||
def test_cte(): | ||
stmt = """WITH foo AS (SELECT 1, 2, 3) | ||
SELECT * FROM foo;""" | ||
assert parsed_tokens_with_sources( | ||
stmt, (CTE, DML, Identifier, Keyword, Statement) | ||
) == [ | ||
(Statement, stmt, stmt), | ||
(CTE, "WITH", "WITH"), | ||
(Identifier, "foo AS (SELECT 1, 2, 3)", "foo AS (SELECT 1, 2, 3)"), | ||
(Keyword, "AS", "AS"), | ||
(DML, "SELECT", "SELECT"), | ||
(DML, "SELECT", "SELECT"), | ||
(Keyword, "FROM", "FROM"), | ||
(Identifier, "foo", "foo"), | ||
] |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I initially had the
Token
constructor do ahasattr(self, 'pos')
check before assigning, which I didn't love either. But that approach broke one test wheredeepcopy
was called on a parsed statement, whereas this approach is robust to that case.It seemed worth making these properties dynamic given that
TokenList
exposes methods for inserting tokens