Skip to content

Missing space before UNION when building CREATE VIEW #655

Description

@predictor2718

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:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions