From 9513c433ad1262d81d715775184637d49f067320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=9E=B8?= Date: Tue, 17 Dec 2024 10:19:11 +0800 Subject: [PATCH] And parentheses after not operator if right is SQLQueryExpr --- .../sql/visitor/SQLASTOutputVisitor.java | 14 +++++----- core/src/test/resources/bvt/parser/odps/0.txt | 27 +++++++++++++++++++ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java b/core/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java index 2eed446d09..97dccba5bc 100644 --- a/core/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java +++ b/core/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java @@ -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(')'); } diff --git a/core/src/test/resources/bvt/parser/odps/0.txt b/core/src/test/resources/bvt/parser/odps/0.txt index 93341b4f52..1b924e9b54 100644 --- a/core/src/test/resources/bvt/parser/odps/0.txt +++ b/core/src/test/resources/bvt/parser/odps/0.txt @@ -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)