diff options
| -rw-r--r-- | CHANGES | 2 | ||||
| -rw-r--r-- | sqlparse/engine/grouping.py | 11 | ||||
| -rw-r--r-- | tests/test_regressions.py | 10 |
3 files changed, 20 insertions, 3 deletions
@@ -5,6 +5,8 @@ Bug Fixes * Fix Python 3 compatibility of sqlformat script (by Piet Delport). * Fix parsing of SQL statements that contain binary data (by Alexey Malyshev). + * Fix a bug where keywords were identified as aliased identifiers in + invalid SQL statements. Enhancements * Improve parsing speed when SQL contains CLOBs or BLOBs (issue86). diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py index 0b64d21..0f39dd2 100644 --- a/sqlparse/engine/grouping.py +++ b/sqlparse/engine/grouping.py @@ -15,7 +15,7 @@ def _group_left_right(tlist, ttype, value, cls, check_right=lambda t: True, check_left=lambda t: True, include_semicolon=False): - [_group_left_right(sgroup, ttype, value, cls, check_right, + [_group_left_right(sgroup, ttype, value, cls, check_right, check_left, include_semicolon) for sgroup in tlist.get_sublists() if not isinstance(sgroup, cls)] idx = 0 @@ -26,7 +26,7 @@ def _group_left_right(tlist, ttype, value, cls, if right is None or not check_right(right): token = tlist.token_next_match(tlist.token_index(token) + 1, ttype, value) - elif left is None or not check_right(left): + elif left is None or not check_left(left): token = tlist.token_next_match(tlist.token_index(token) + 1, ttype, value) else: @@ -105,8 +105,13 @@ def group_as(tlist): # Currently limited to DML/DDL. Maybe additional more non SQL reserved # keywords should appear here (see issue8). return not token.ttype in (T.DML, T.DDL) + + def _left_valid(token): + return token.ttype is not T.Keyword + _group_left_right(tlist, T.Keyword, 'AS', sql.Identifier, - check_right=_right_valid) + check_right=_right_valid, + check_left=_left_valid) def group_assignment(tlist): diff --git a/tests/test_regressions.py b/tests/test_regressions.py index 24fb10f..94d644f 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -178,3 +178,13 @@ def test_parse_sql_with_binary(): if sys.version_info < (3,): tformatted = tformatted.decode('unicode-escape') assert formatted == tformatted + + +def test_dont_alias_keywords(): + # The _group_left_right function had a bug where the check for the + # left side wasn't handled correctly. In one case this resulted in + # a keyword turning into an identifier. + p = sqlparse.parse('FROM AS foo')[0] + assert len(p.tokens) == 5 + assert p.tokens[0].ttype is T.Keyword + assert p.tokens[2].ttype is T.Keyword |
