Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Development Version
-------------------

Nothing yet.
* Recognize JOIN immediately before a parenthesized subquery as a keyword
instead of a function name (issue615).


Release 0.6.0 (Aug 13, 2026)
Expand Down
5 changes: 3 additions & 2 deletions sqlparse/keywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ def find_delimited_spans(text):
# FIXME(atronah): never match,
# because `re.match` doesn't work with look-behind regexp feature
(r'(?<=\.)[A-ZÀ-Ü]\w*', tokens.Name), # .'Name'
# JOIN before a parenthesis is a clause, not a function (issue615).
(r'((LEFT\s+|RIGHT\s+|FULL\s+)?(INNER\s+|OUTER\s+|STRAIGHT\s+)?'
r'|(CROSS\s+|NATURAL\s+)?)?JOIN\b', tokens.Keyword),
(r'[A-ZÀ-Ü]\w*(?=\()', tokens.Name), # side effect: change kw to func
(r'-?0x[\dA-F]+', tokens.Number.Hexadecimal),
(r'-?\d+(\.\d+)?E-?\d+', tokens.Number.Float),
Expand All @@ -171,8 +174,6 @@ def find_delimited_spans(text):
# cannot be preceded by word character or a right bracket --
# otherwise it's probably an array index
(r'(?<![\w\])])(\[[^\]\[]+\])', tokens.Name),
(r'((LEFT\s+|RIGHT\s+|FULL\s+)?(INNER\s+|OUTER\s+|STRAIGHT\s+)?'
r'|(CROSS\s+|NATURAL\s+)?)?JOIN\b', tokens.Keyword),
(r'END(\s+IF|\s+LOOP|\s+WHILE|\s+FOR|\s+CASE)?\b', tokens.Keyword),
(r'IF\s+(NOT\s+)?EXISTS\b', tokens.Keyword),
(r'NOT\s+NULL\b', tokens.Keyword),
Expand Down
32 changes: 32 additions & 0 deletions tests/test_join_parenthesis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pytest

import sqlparse
from sqlparse import sql
from sqlparse import tokens as T
from sqlparse.lexer import tokenize


@pytest.mark.parametrize('join', [
'JOIN', 'join', 'LEFT JOIN', 'LEFT OUTER JOIN', 'CROSS JOIN', 'NATURAL JOIN',
])
def test_join_before_parenthesized_subquery(join):
condition = '' if join in ('CROSS JOIN', 'NATURAL JOIN') else ' on child.parent_id=p.id'
query = f'select * from child {join}(select * from parent) as p{condition};'
statement = sqlparse.parse(query)[0]
significant = [token for token in statement if not token.is_whitespace]

assert significant[4].match(T.Keyword, join.upper())
assert isinstance(significant[3], sql.Identifier)
assert significant[3].get_real_name() == 'child'
assert isinstance(significant[5], sql.Identifier)
assert significant[5].get_alias() == 'p'
assert str(statement) == query


@pytest.mark.parametrize('name', ['joined', 'join_table', 'schema.join', 'schema.JOIN'])
def test_join_function_name_controls(name):
query = f'{name}(1)'
names = [value for ttype, value in tokenize(query) if ttype is T.Name]
assert names == name.split('.')
expected_group = sql.Identifier if '.' in name else sql.Function
assert isinstance(sqlparse.parse(query)[0].tokens[0], expected_group)