From f0754a6b6540e37c8aa270975194101024d4b2f8 Mon Sep 17 00:00:00 2001 From: Andi Albrecht Date: Sat, 1 Oct 2016 14:07:52 +0200 Subject: [PATCH] Handle operator grouping after identifying typecasts (fixes #297). --- CHANGELOG | 1 + sqlparse/engine/grouping.py | 2 +- tests/test_grouping.py | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index a3a223bf..c2431018 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -11,6 +11,7 @@ Bug Fixes * Fix parsing of incomplete AS (issue284, by vmuriart). * Fix parsing of Oracle names containing dollars (issue291). * Fix parsing of UNION ALL (issue294). +* Fix grouping of identifiers containing typecasts (issue297). Internal Changes diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py index bae3e247..5fa39094 100644 --- a/sqlparse/engine/grouping.py +++ b/sqlparse/engine/grouping.py @@ -343,9 +343,9 @@ def group(stmt): group_period, group_arrays, group_identifier, - group_operator, group_order, group_typecasts, + group_operator, group_as, group_aliased, group_assignment, diff --git a/tests/test_grouping.py b/tests/test_grouping.py index 655ef0f6..20151a10 100644 --- a/tests/test_grouping.py +++ b/tests/test_grouping.py @@ -64,6 +64,20 @@ def test_grouping_identifiers(): assert identifiers[0].get_alias() == "col" +@pytest.mark.parametrize('s', [ + 'foo, bar', + 'sum(a), sum(b)', + 'sum(a) as x, b as y', + 'sum(a)::integer, b', + 'sum(a)/count(b) as x, y', + 'sum(a)::integer as x, y', + 'sum(a)::integer/count(b) as x, y', # issue297 +]) +def test_group_identifier_list(s): + parsed = sqlparse.parse(s)[0] + assert isinstance(parsed.tokens[0], sql.IdentifierList) + + def test_grouping_identifier_wildcard(): p = sqlparse.parse('a.*, b.id')[0] assert isinstance(p.tokens[0], sql.IdentifierList)