[
https://issues.apache.org/jira/browse/CALCITE-5013?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17512610#comment-17512610
]
Julian Hyde commented on CALCITE-5013:
--------------------------------------
Having reviewed this, I think that {{unparseBinaryOperand}} would be
unnecessary if the precedence of LIMIT was working correctly.
Consider "a * (b + c)". The parentheses are necessary because '+' has lower
precedence than '*', just as 'LIMIT' has lower precedence than 'UNION'. We just
need to make sure the the precedence of LIMIT gets into the equation.
Also, {{SqlSelect.unparse}} starts with "if (!writer.inQuery()) {" so I think
this problem is ALMOST solved already.
Also, please add the test case
{code}
// Parentheses are required to keep ORDER and LIMIT on the sub-query.
final String retainLimitQuery = "SELECT \"product_id\" FROM \"product\""
+ "UNION ALL\n"
+ "(SELECT \"product_id\" FROM \"product\" ORDER BY \"product_id\"
LIMIT 2)";
final String retainLimitResult = "SELECT \"product_id\"\n"
+ "FROM \"foodmart\".\"product\"\n"
+ "UNION ALL\n"
+ "(SELECT \"product_id\"\n"
+ "FROM \"foodmart\".\"product\"\n"
+ "ORDER BY \"product_id\"\n"
+ "FETCH NEXT 2 ROWS ONLY)";
sql(retainLimitQuery).ok(retainLimitResult);
{code}
It applies even if {{withRemoveSortInSubQuery}} hasn't been switched off.
> JDBC adapter should retain parentheses if a query in a UNION has a LIMIT
> clause
> -------------------------------------------------------------------------------
>
> Key: CALCITE-5013
> URL: https://issues.apache.org/jira/browse/CALCITE-5013
> Project: Calcite
> Issue Type: Bug
> Components: core
> Reporter: Jiajun Xie
> Priority: Major
>
> The JDBC adapter, when generating SQL, should retain parentheses if one of
> the inputs to a set operator (e.g. UNION) has an LIMIT clause.
> In standard SQL, the operand of UNION should not have LIMIT or ORDER BY. So
> parse will fail in {{SqlParserTetest#testLimitUnion}} and
> {{SqlParserTetest#OrderUnion}}.
> When users use parentheses, most engines allow it. See the discussion:
> CALCITE-1892.
> For simple example, parentheses control the scope of the limit
> {code:java}
> select "product_id" from "product"
> union all
> (select "product_id" from "product" limit 10){code}
> {{unparseBinarySyntax}} will miss parentheses, this change will affect
> semantics
> {code:java}
> SELECT \"product_id\" FROM \"foodmart\".\"product\"
> UNION ALL
> SELECT \"product_id\" FROM \"foodmart\".\"product\"
> FETCH NEXT 10 ROWS ONLY -- Affect semantics{code}
--
This message was sent by Atlassian Jira
(v8.20.1#820001)