Thanks Julian for your nice guidance, I logged this bug onto CALCITE-5185 <https://issues.apache.org/jira/browse/CALCITE-5185>, and I will go to any lengths to fix it soon.
Julian Hyde <jhyde.apa...@gmail.com> 于2022年6月7日周二 22:28写道: > 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 <zhuangzixiao...@gmail.com> > 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? >