From 4c570778b2e1c06e8bfcb3c48ae5baeff908fcdf Mon Sep 17 00:00:00 2001 From: ali-tny Date: Sat, 26 Sep 2020 16:29:40 +1000 Subject: Add postgres WINDOW keyword Postgres allows statements of the form: ```sql SELECT col_1, col_2, SUM(col_3) OVER w FROM x WINDOW w AS (PARTITION BY col_1 ORDER BY col_2) ``` where the window is defined once at the end of the query (see https://www.postgresql.org/docs/9.5/sql-select.html). This change adds WINDOW as a postgres keyword, preventing queries like the above being misparsed, with table name and WINDOW being grouped into an single identifier . --- sqlparse/keywords.py | 1 + tests/test_tokenize.py | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/sqlparse/keywords.py b/sqlparse/keywords.py index 68ced92..8c872fd 100644 --- a/sqlparse/keywords.py +++ b/sqlparse/keywords.py @@ -833,6 +833,7 @@ KEYWORDS_ORACLE = { # PostgreSQL Syntax KEYWORDS_PLPGSQL = { + 'WINDOW': tokens.Keyword, 'PARTITION': tokens.Keyword, 'OVER': tokens.Keyword, 'PERFORM': tokens.Keyword, diff --git a/tests/test_tokenize.py b/tests/test_tokenize.py index 0d7f878..af0ba16 100644 --- a/tests/test_tokenize.py +++ b/tests/test_tokenize.py @@ -202,6 +202,12 @@ def test_parse_order_by(): assert p.tokens[0].ttype is T.Keyword +def test_parse_window_as(): + p = sqlparse.parse('WINDOW w AS')[0] + assert len(p.tokens) == 5 + assert p.tokens[0].ttype is T.Keyword + + @pytest.mark.parametrize('s', ( "LIKE", "ILIKE", "NOT LIKE", "NOT ILIKE", "NOT LIKE", "NOT ILIKE", -- cgit v1.2.1