phpmyadmin / phpmyadmin/sql-parser

Views with CTEs parsed incorrectly

Open
#639 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
PHP
Stars
485
Forks
119
PR merge metrics
No merged PRs in 30d

Description

Hi, parser has been working great for the most part but I ran into an issue today where I have multiple views with CTEs that get mixed together by the parser. I found that if I put parentheses around each view body definition the issue goes away, but MySQL does not require this.

Here is a minimal test script that demonstrates the issue:
test.php

Output from the test script on my machine follows. I am using phpmyadmin/sql-parser version 5.11.1 installed via composer.

-- SQL A:
CREATE VIEW view1 AS
WITH foo AS (SELECT abc FROM foo_table),
bar AS (SELECT id, xyz FROM bar_table)
SELECT foo.id AS id, foo.abc AS abc, bar.xyz AS xyz
FROM foo LEFT JOIN bar USING (id)
ORDER BY foo.abc, bar.xyz;

CREATE VIEW view2 AS
WITH foo AS (SELECT id, abc FROM foo_table),
bar AS (SELECT id, xyz FROM bar_table)
SELECT foo.id AS id, foo.abc AS abc, bar.xyz AS xyz
FROM foo LEFT JOIN bar USING (id)
ORDER BY foo.abc, bar.xyz;

-- Result A:
-- create VIEW view1
CREATE VIEW view1  AS WITH foo AS (SELECT abc FROM foo_table), bar AS (SELECT id, xyz FROM bar_table) SELECT foo.id AS `id`, foo.abc AS `abc`, bar.xyz AS `xyz` FROM foo LEFT JOIN bar USING (id) ORDER BY foo.abc ASC, bar.xyz ASCCREATE VIEW view2  AS WITH foo AS (SELECT id, abc FROM foo_table), bar AS (SELECT id, xyz FROM bar_table) SELECT foo.id AS `id`, foo.abc AS `abc`, bar.xyz AS `xyz` FROM foo LEFT JOIN bar USING (id) ORDER BY foo.abc ASC, bar.xyz ASC


-- SQL B:
CREATE VIEW view1 AS (
WITH foo AS (SELECT abc FROM foo_table),
bar AS (SELECT id, xyz FROM bar_table)
SELECT foo.id AS id, foo.abc AS abc, bar.xyz AS xyz
FROM foo LEFT JOIN bar USING (id)
ORDER BY foo.abc, bar.xyz);

CREATE VIEW view2 AS (
WITH foo AS (SELECT id, abc FROM foo_table),
bar AS (SELECT id, xyz FROM bar_table)
SELECT foo.id AS id, foo.abc AS abc, bar.xyz AS xyz
FROM foo LEFT JOIN bar USING (id)
ORDER BY foo.abc, bar.xyz);

-- Result B:
-- create VIEW view1
CREATE VIEW view1  AS  (
WITH foo AS (SELECT abc FROM foo_table),
bar AS (SELECT id, xyz FROM bar_table)
SELECT foo.id AS id, foo.abc AS abc, bar.xyz AS xyz
FROM foo LEFT JOIN bar USING (id)
ORDER BY foo.abc, bar.xyz)
-- create VIEW view2
CREATE VIEW view2  AS  (
WITH foo AS (SELECT id, abc FROM foo_table),
bar AS (SELECT id, xyz FROM bar_table)
SELECT foo.id AS id, foo.abc AS abc, bar.xyz AS xyz
FROM foo LEFT JOIN bar USING (id)
ORDER BY foo.abc, bar.xyz)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked test.php script and reproduce the output using phpmyadmin/sql-parser 5.11.1. Trace how multiple CREATE VIEW statements containing CTEs are parsed, then add coverage for the unparenthesized form and verify that each view remains separate and correct without requiring parentheses.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, php, sql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.