[
https://issues.apache.org/jira/browse/CALCITE-5013?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17513794#comment-17513794
]
Julian Hyde commented on CALCITE-5013:
--------------------------------------
I can't tell you how the code would look unless I write the code. My intuition
is that precedence is involved because in "select x from t union select y from
t2 limit 5" the "union" keyword is "pulling to the left" and the "limit"
keyword is "pulling to the right", and who will win the tug-of-war is
determined by precedence. The fact that limit is "inside" a {{SqlOperator}} is
a mere implementation detail. Also, precedence is involved in every call to
{{unparse}} because {{unparse}} has arguments {{left}} and {{{}right{}}}, so I
would approach this by running a test case in a debugger and finding the
particular call where the left/right precedence values look wrong.
> 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)