sqlglot icon indicating copy to clipboard operation
sqlglot copied to clipboard

ClickHouse: cannot parse NATURAL joins

Open princejain756 opened this issue 1 month ago • 0 comments

SELECT 1 FROM a NATURAL JOIN b fails to parse for ClickHouse with Unexpected token at NATURAL, even though ClickHouse supports NATURAL [INNER | LEFT | RIGHT | FULL] [OUTER] JOIN and sqlglot's own ClickHouse generator emits exactly that spelling for transpiled input:

>>> sqlglot.transpile("SELECT 1 FROM a NATURAL JOIN b", read="postgres", write="clickhouse")
['SELECT 1 FROM a NATURAL JOIN b']
>>> sqlglot.parse_one("SELECT 1 FROM a NATURAL JOIN b", read="clickhouse")
ParseError: Invalid expression / Unexpected token. Line 1, Col: 23.

Cause: ClickHouse.Parser._parse_join_parts matches GLOBAL, then strictness/side/kind slots, but never NATURAL. Consuming NATURAL into the method slot (and keeping the method-to-global_ move for a literal GLOBAL only) fixes it while matching the AST shape other dialects produce.

I have the fix plus identity tests for all six NATURAL spellings on branch fix-clickhouse-natural-join in my fork (verified against GLOBAL ANY, ASOF, ARRAY, PASTE joins). Happy to open the PR if/when PR creation is possible for me; opening the issue regardless since the bug stands on its own.

AI-assisted report/fix (SWE-2).

princejain756 avatar Oct 22 '26 12:10 princejain756