[
https://issues.apache.org/jira/browse/CALCITE-5013?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17513768#comment-17513768
]
Jiajun Xie commented on CALCITE-5013:
-------------------------------------
Sorry, I don't know. LIMIT is not a {*}SqlOperator{*}, it is belong to
{*}SqlSelectOperator{*}. Should I set precedence for *SqlSelectOperator* with
LIMIT?
In addition, "a * (b + c)" is right because "(b + c)" is *SqlBasicCall* and
*SqlCall#unparse* consider precedence.
{code:java}
if (leftPrec > operator.getLeftPrec()
|| (operator.getRightPrec() <= rightPrec && (rightPrec != 0))
|| writer.isAlwaysUseParentheses() && isA(SqlKind.EXPRESSION)) {
final SqlWriter.Frame frame = writer.startList("(", ")");
dialect.unparseCall(writer, this, 0, 0);
writer.endList(frame);
} {code}
But {*}SqlSelectOperator#{*}{*}unparse{*} and ** {*}SqlSelect#{*}{*}unparse{*}
did not consider precedence. **
Not only do I need to set precedence for *SqlSelectOperator* with LIMIT, but I
also need to use it in *SqlSelect#unparse.*
{code:java}
if (leftPrec > this.getOperator().getLeftPrec()
|| (this.getOperator().getRightPrec() <= rightPrec && (rightPrec != 0))
|| writer.isAlwaysUseParentheses() && isA(SqlKind.EXPRESSION)
|| !writer.inQuery()) {
// If this SELECT is the topmost item in a sub-query, introduce a new
// frame. (The topmost item in the sub-query might be a UNION or
// ORDER. In this case, we don't need a wrapper frame.)
final SqlWriter.Frame frame =
writer.startList(SqlWriter.FrameTypeEnum.SUB_QUERY, "(", ")");
writer.getDialect().unparseCall(writer, this, 0, 0);
writer.endList(frame);
} {code}
{*}{*}.Is my understanding correct?
> 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)