phpmyadmin / phpmyadmin/sql-parser

Missing space before UNION when building CREATE VIEW

Open Beginner friendly
#655 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

When CreateStatement::build() reconstructs a CREATE VIEW ... AS SELECT ... UNION ...
statement, the output misses the whitespace between the built SELECT and the
trailing body tokens. Result: invalid SQL such as ... WHERE 3 = 3union all (...).

This was previously reported downstream in phpMyAdmin:

  • phpmyadmin/phpmyadmin#19692
  • phpmyadmin/phpmyadmin#18515 (same bug, older)

Reproduction

use PhpMyAdmin\SqlParser\Parser;

$sql = "CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER "
     . "VIEW `v1` AS select 1 AS `a` where 3 = 3 union all (select 2 AS `a`)";

echo (new Parser($sql))->statements[0]->build();

Actual output:

CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1`  AS SELECT 1 AS `a` WHERE 3 = 3union all (select 2 AS `a`)

(note: 3 = 3union all — no space).

Expected: a space before union all, producing valid SQL.

Environment

  • Library version: current master (head f440917e)
  • PHP: 8.4

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 at CreateStatement::build() and reproduce the issue with the SQL example through PhpMyAdmin\SqlParser\Parser. Verify that rebuilding the CREATE VIEW preserves whitespace before the trailing UNION body and produces valid SQL rather than 3union all.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, php
Domain
databases
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.