nom-sql icon indicating copy to clipboard operation
nom-sql copied to clipboard

With clause parsing support

Open soulstompp opened this issue 4 years ago • 2 comments

This change adds the ability to parse CTEs included with a SELECT anywhere that a nested_selection (compound or otherwise) is allowed. This change addresses https://github.com/ms705/nom-sql/issues/77.

soulstompp avatar Aug 15 '22 21:08 soulstompp

This pull request is built off of the branch in https://github.com/ms705/nom-sql/pull/82. I needed to be able to parse nested compound statements in order to parse the WITH clauses.

soulstompp avatar Aug 15 '22 22:08 soulstompp

I realized that this might need a little more work, since the following query would currently parse gives a syntax error in MySQL 8.0.30:

WITH cte1 AS (SELECT a, b FROM table1) SELECT b, d FROM cte1 UNION ALL WITH cte2 AS (SELECT a, b FROM table1) SELECT b, d FROM cte2;

Since the two withs cannot occur at the same level. For example this is acceptable:

(WITH cte1 AS (SELECT a, b FROM table1) SELECT b, d FROM cte1) UNION ALL (WITH cte2 AS (SELECT a, b FROM table1) SELECT b, d FROM cte2);

And it's not really that the WITH can't come after this UNION since this is syntactically correct: SELECT 1 AS a, 2 AS b UNION (WITH cte1 AS (SELECT 2 AS a, 1 AS b) SELECT a, b FROM cte1);

Considering all of that, I am not sure this is really something the parser should concern itself with.

soulstompp avatar Aug 15 '22 22:08 soulstompp