Skip to content

fix: accept trailing comma in from list - #2948

Open
harryfoster2006 wants to merge 1 commit into
JSQLParser:masterfrom
harryfoster2006:fix/trailing-comma-from-list-before-where
Open

harryfoster2006 wants to merge 1 commit 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)

[ LOOKAHEAD(2) lateralViews=LateralViews() ]
[ LOOKAHEAD(2) joins=JoinsList() ]
// trailing comma after the last table: FROM a, b, WHERE ...
[ LOOKAHEAD({ isTrailingFromItemCommaAhead() }) "," ]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will need to guard this by supporting/affected Dialect please.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please provide at least one test where the Validation fails when other or no Dialects are selected.

[ LOOKAHEAD(3) <K_FROM> <K_ONLY> { plainSelect.setUsingOnly(true); } fromItem=FromItem()
[ LOOKAHEAD(2) lateralViews=LateralViews() ]
[ LOOKAHEAD(2) joins=JoinsList() ]
[ LOOKAHEAD({ isTrailingFromItemCommaAhead() }) "," ]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will need to guard this by supporting/affected Dialect please.

}

@Test
void testTrailingCommaInFromListBeforeWhere() throws JSQLParserException {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use parametrized tests.

}

@Test
void testTrailingCommaInFromListOnlyBeforeWhere() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use parametrized tests.

This branch has not been deployed

No deployments
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