Skip to content

fix: accept trailing comma in from list - #2948

Merged
manticore-projects merged 4 commits into
JSQLParser:masterfrom
harryfoster2006:fix/trailing-comma-from-list-before-where
Nov 22, 2026
Merged

manticore-projects merged 4 commits into
JSQLParser:masterfrom
harryfoster2006:fix/trailing-comma-from-list-before-where

Conversation

@harryfoster2006

Copy link
Copy Markdown
Contributor

Snowflake accepts a trailing comma after the last table of a comma-separated FROM list. This isn't in the Snowflake documentation, but running these by hand on Snowflake works and we have seen real cases:

SELECT
    o.order_id,
    c.customer_name,
FROM orders o,
     customers c,
WHERE o.customer_id = c.customer_id;
SELECT 1 FROM (SELECT 1 AS a) UG, (SELECT 1 AS b) G, WHERE UG.a = G.b

Current behaviour: JSqlParser already accepts the select-list comma but fails on the , before WHERE.

Fix

PlainSelect now takes one optional , after FromItem, LateralViews and JoinsList, for both FROM and FROM ONLY.
Scope: a new semantic lookahead, isTrailingFromItemCommaAhead(), only matches when the next token is , and the one after it is WHERE, so it doesn't change how any other comma is parsed.

Result: the comma is dropped rather than kept as an empty join.

  • SELECT * FROM a, b, WHERE 1 = 1 has a single join b and deparses to SELECT * FROM a, b WHERE 1 = 1
  • SELECT * FROM a, WHERE 1 = 1 has no joins at all

Tests:

  • a single table, several tables, and a mix of explicit joins and comma joins
  • FROM ONLY
  • the comma inside a subquery, a CTE and an IN (SELECT ...)
  • parse failures for a trailing comma before GROUP BY, before ORDER BY, at the end of the statement, or doubled (, , WHERE)

@harryfoster2006

Copy link
Copy Markdown
Contributor Author

Thanks for the review, I've guarded it on Dialect.SNOWFLAKE. It got me a bit curious to see if other dialects allow this. I found that DuckDB also accepts this (and is actually documented https://duckdb.org/docs/2.0/sql/dialect/friendly_sql#trailing-commas and more: before GROUP BY/ORDER BY and at the end of the statement), but there's no DuckDB dialect to guard on and I think i'll keep this PR to be snowflake only.

Happy to follow up separately if you'd like it supported. I haven't seen anything for the other dialects but I might test it just to make sure

@manticore-projects
manticore-projects merged commit 443adc2 into JSQLParser:master Nov 22, 2026
10 checks passed
@manticore-projects

Copy link
Copy Markdown
Contributor

Thank you much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants