Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化SQLBinaryOpExpr的括号解析逻辑 #5847 #5855

Merged
merged 20 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.X509EncodedKeySpec;

@Deprecated
public class ConfigTools {
@Deprecated
private static final String DEFAULT_PRIVATE_KEY_STRING = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAocbCrurZGbC5GArEHKlAfDSZi7gFBnd4yxOt0rwTqKBFzGyhtQLu5PRKjEiOXVa95aeIIBJ6OhC2f8FjqFUpawIDAQABAkAPejKaBYHrwUqUEEOe8lpnB6lBAsQIUFnQI/vXU4MV+MhIzW0BLVZCiarIQqUXeOhThVWXKFt8GxCykrrUsQ6BAiEA4vMVxEHBovz1di3aozzFvSMdsjTcYRRo82hS5Ru2/OECIQC2fAPoXixVTVY7bNMeuxCP4954ZkXp7fEPDINCjcQDywIgcc8XLkkPcs3Jxk7uYofaXaPbg39wuJpEmzPIxi3k0OECIGubmdpOnin3HuCP/bbjbJLNNoUdGiEmFL5hDI4UdwAdAiEAtcAwbm08bKN7pwwvyqaCBC//VnEWaq39DCzxr+Z2EIk=";
@Deprecated
public static final String DEFAULT_PUBLIC_KEY_STRING = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKHGwq7q2RmwuRgKxBypQHw0mYu4BQZ3eMsTrdK8E6igRcxsobUC7uT0SoxIjl1WveWniCASejoQtn/BY6hVKWsCAwEAAQ==";

public static void main(String[] args) throws Exception {
Expand Down
9 changes: 9 additions & 0 deletions core/src/main/java/com/alibaba/druid/sql/ast/SQLExprImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@
import java.util.List;

public abstract class SQLExprImpl extends SQLObjectImpl implements SQLExpr {
protected boolean parenthesized;

public SQLExprImpl() {
}

public boolean isParenthesized() {
return parenthesized;
}

public void setParenthesized(boolean parenthesized) {
this.parenthesized = parenthesized;
}
public abstract boolean equals(Object o);

public abstract int hashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class SQLBetweenExpr extends SQLExprImpl implements SQLReplaceable, Seria
private boolean not;
public SQLExpr beginExpr;
public SQLExpr endExpr;

public SQLBetweenExpr() {
}

Expand All @@ -44,6 +43,7 @@ public SQLBetweenExpr clone() {
if (endExpr != null) {
x.setEndExpr(endExpr.clone());
}
x.setParenthesized(parenthesized);
return x;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ public class SQLBinaryOpExpr extends SQLExprImpl implements SQLReplaceable, Seri
protected SQLBinaryOperator operator;
protected DbType dbType;

private boolean parenthesized;

// only for parameterized output
protected transient List<SQLObject> mergedList;

Expand Down Expand Up @@ -150,14 +148,6 @@ public void setOperator(SQLBinaryOperator operator) {
this.operator = operator;
}

public boolean isParenthesized() {
return parenthesized;
}

public void setParenthesized(boolean parenthesized) {
this.parenthesized = parenthesized;
}

protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
if (left != null) {
Expand Down Expand Up @@ -241,7 +231,7 @@ public SQLBinaryOpExpr clone() {
if (hint != null) {
x.hint = hint.clone();
}

x.setParenthesized(parenthesized);
return x;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public SQLUnaryExpr clone() {
x.setExpr(expr.clone());
}
x.operator = operator;
x.parenthesized = parenthesized;
return x;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6162,6 +6162,9 @@ private boolean parseAlterSpecification(SQLAlterTableStatement stmt) {
check.setName(constraintSymbol);
}
check.setExpr(this.exprParser.expr());
if (check.getExpr() instanceof SQLExprImpl) {
((SQLExprImpl) check.getExpr()).setParenthesized(true);
}
accept(Token.RPAREN);
boolean enforce = true;
if (lexer.token() == Token.NOT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,11 @@ public boolean visit(OracleDeleteStatement x) {

public boolean visit(OracleIntervalExpr x) {
SQLExpr value = x.getValue();
if (value instanceof SQLLiteralExpr || value instanceof SQLVariantRefExpr) {
if (x.getValue() instanceof SQLLiteralExpr || x.getValue() instanceof SQLVariantRefExpr) {
print0(ucase ? "INTERVAL " : "interval ");
value.accept(this);
print(' ');
} else {
print('(');
value.accept(this);
print0(") ");
}
x.getValue().accept(this);
print(' ');

print0(x.getType().name());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -855,15 +855,11 @@ private void printHints(List<SQLHint> hints) {
}

public boolean visit(OracleIntervalExpr x) {
if (x.getValue() instanceof SQLLiteralExpr) {
if (x.getValue() instanceof SQLLiteralExpr || x.getValue() instanceof SQLVariantRefExpr) {
print0(ucase ? "INTERVAL " : "interval ");
x.getValue().accept(this);
print(' ');
} else {
print('(');
x.getValue().accept(this);
print0(") ");
}
x.getValue().accept(this);
print(' ');

print0(x.getType().name());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,7 @@ public boolean visit(PGTypeCastExpr x) {

if (expr != null) {
if (expr instanceof SQLBinaryOpExpr) {
print('(');
expr.accept(this);
print(')');
} else if (expr instanceof PGTypeCastExpr && dataType.getArguments().isEmpty()) {
dataType.accept(this);
print('(');
Expand Down Expand Up @@ -953,7 +951,7 @@ private void printHints(List<SQLHint> hints) {
}

public boolean visit(OracleIntervalExpr x) {
if (x.getValue() instanceof SQLLiteralExpr) {
if (x.getValue() instanceof SQLLiteralExpr || x.getValue() instanceof SQLVariantRefExpr) {
print0(ucase ? "INTERVAL " : "interval ");
x.getValue().accept(this);
print(' ');
Expand Down
53 changes: 46 additions & 7 deletions core/src/main/java/com/alibaba/druid/sql/parser/SQLExprParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,30 @@ public SQLExpr expr() {
return expr;
}

boolean parenthesized = (lexer.token == Token.LPAREN);
SQLExpr expr = primary();

if (parenthesized && expr instanceof SQLBinaryOpExpr) {
if (((SQLBinaryOpExpr) expr).isParenthesized()) {
parenthesized = false;
}
}
if (parenthesized && expr instanceof SQLCaseExpr) {
parenthesized = false;
((SQLCaseExpr) expr).setParenthesized(true);
}
if (parenthesized && expr instanceof SQLUnaryExpr) {
if (((SQLUnaryExpr) expr).isParenthesized()) {
parenthesized = false;
}
}
if (parenthesized && expr instanceof SQLQueryExpr) {
parenthesized = false;
((SQLQueryExpr) expr).setParenthesized(true);
}
if (parenthesized && expr instanceof SQLIdentifierExpr) {
parenthesized = false;
((SQLIdentifierExpr) expr).setParenthesized(true);
}
Lexer.SavePoint mark = lexer.mark();
Token token = lexer.token;
if (token == Token.COMMA) {
Expand All @@ -128,7 +150,14 @@ public SQLExpr expr() {
return exprRest(expr);
}
} else {
return exprRest(expr);
SQLExpr sqlExpr = exprRest(expr);
if (parenthesized && sqlExpr instanceof SQLBinaryOpExpr) {
((SQLBinaryOpExpr) sqlExpr).setParenthesized(true);
}
if (parenthesized && sqlExpr instanceof SQLUnaryExpr) {
((SQLUnaryExpr) sqlExpr).setParenthesized(true);
}
return sqlExpr;
}
}

Expand Down Expand Up @@ -378,9 +407,8 @@ public SQLExpr primary() {

sqlExpr = listExpr;
}

if (sqlExpr instanceof SQLBinaryOpExpr) {
((SQLBinaryOpExpr) sqlExpr).setParenthesized(true);
if (sqlExpr instanceof SQLExprImpl) {
((SQLExprImpl) sqlExpr).setParenthesized(true);
}

if ((lexer.token == Token.UNION || lexer.token == Token.MINUS || lexer.token == Token.EXCEPT)
Expand Down Expand Up @@ -845,7 +873,12 @@ public SQLExpr primary() {
lexer.nextToken();

SQLExpr notTarget = expr();

if (notTarget instanceof SQLBinaryOpExpr) {
((SQLBinaryOpExpr) notTarget).setParenthesized(true);
}
if (notTarget instanceof SQLUnaryExpr) {
((SQLUnaryExpr) notTarget).setParenthesized(true);
}
accept(Token.RPAREN);
notTarget = bitXorRest(notTarget);
notTarget = multiplicativeRest(notTarget);
Expand Down Expand Up @@ -1356,7 +1389,10 @@ public SQLExpr primary() {
if (beforeComments != null) {
expr.addBeforeComment(beforeComments);
}

if (lexer.hasComment() && lexer.isKeepComments()) {
// @todo 是否保留注释,暂时待定,因为保留的话,有20来个测试用例会失败 by lizongbo
// expr.addAfterComment(lexer.readAndResetComments());
}
return expr;
}

Expand Down Expand Up @@ -5748,6 +5784,9 @@ public SQLCheck parseCheck() {
SQLCheck check = createCheck();
accept(Token.LPAREN);
check.setExpr(this.expr());
if (check.getExpr() instanceof SQLExprImpl) {
((SQLExprImpl) check.getExpr()).setParenthesized(true);
}
accept(Token.RPAREN);
return check;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public static SQLExprParser createExprParser(String sql, DbType dbType, SQLParse
case postgresql:
case greenplum:
case edb:
case gaussdb:
return new PGExprParser(sql, features);
case sqlserver:
case jtds:
Expand Down