Skip to content

Commit

Permalink
And parentheses after not operator if right is SQLQueryExpr
Browse files Browse the repository at this point in the history
  • Loading branch information
lingo-xp authored and wenshao committed Dec 17, 2024
1 parent 3a68c95 commit 49f8166
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2306,25 +2306,25 @@ public boolean visit(SQLNotExpr x) {
print0(ucase ? "NOT " : "not ");
SQLExpr expr = x.getExpr();

boolean needQuote = false;
boolean needParentheses = false;

if (expr instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) expr;
needQuote = binaryOpExpr.getOperator().isLogical();
needParentheses = binaryOpExpr.getOperator().isLogical();
if (binaryOpExpr.isParenthesized()) {
needQuote = false;
needParentheses = false;
}
} else if (expr instanceof SQLInListExpr || expr instanceof SQLNotExpr
|| expr instanceof SQLBinaryOpExprGroup) {
needQuote = true;
|| expr instanceof SQLBinaryOpExprGroup || expr instanceof SQLQueryExpr) {
needParentheses = true;
}

if (needQuote) {
if (needParentheses) {
print('(');
}
printExpr(expr, parameterized);

if (needQuote) {
if (needParentheses) {
print(')');
}

Expand Down
27 changes: 27 additions & 0 deletions core/src/test/resources/bvt/parser/odps/0.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
select CASE
WHEN UPPER(a) = 'a'
AND (SELECT b FROM UNNEST() ORDER BY c DESC LIMIT 1) = 'b'
AND NOT (SELECT d FROM UNNEST(e) ORDER BY f DESC LIMIT 1)
THEN 'IS'
ELSE 'OTHERS'
END from g
--------------------
SELECT CASE
WHEN UPPER(a) = 'a'
AND (
SELECT b
FROM UNNEST()
ORDER BY c DESC
LIMIT 1
) = 'b'
AND NOT (
SELECT d
FROM UNNEST(e)
ORDER BY f DESC
LIMIT 1
)
THEN 'IS'
ELSE 'OTHERS'
END
FROM g
------------------------------------------------------------------------------------------------------------------------
select struct(a.b, a.c as c, a.d) from a
--------------------
SELECT STRUCT(a.b, a.c AS c, a.d)
Expand Down

0 comments on commit 49f8166

Please sign in to comment.