Yes. The parentheses are required for correctness. Please log a bug. While you’re there, it would be nice if there were a line break after each CTE. (Including if there are multiple CTEs.) Maybe the fix can address that too.
Julian > On Jun 7, 2022, at 05:19, Yanjing Wang <[email protected]> wrote: > > Hi community, > > the test follows > @Test void testWithUnion2() { > // Per the standard WITH ... SELECT ... UNION is valid even without > parens. > final String sql = "with emp2 as (select * from emp union all select * > from emp)\n" > + "select * from emp2\n" > + "union\n" > + "select * from emp2\n"; > final String expected = "WITH `EMP2` AS (SELECT *\n" > + "FROM `EMP`\n" > + "UNION ALL\n" > + "SELECT *\n" > + "FROM `EMP`) (SELECT *\n" > + "FROM `EMP2`\n" > + "UNION\n" > + "SELECT *\n" > + "FROM `EMP2`)"; > sql(sql).ok(expected); > } > > when I change SqlPrettyWriter.config() > .withAlwaysUseParentheses(true) > to > .withAlwaysUseParentheses(false) > > the test produces sql > WITH `EMP2` AS SELECT * > FROM `EMP` > UNION ALL > SELECT * > FROM `EMP` (SELECT * > FROM `EMP2` > UNION > SELECT * > FROM `EMP2`) > > but the sql can't be parsed by calcite. > > I find postgreSQL and BigQuery both need parentheses for the with item > query. > > Should we always force the with item query to be wrapped by parentheses?
