Skip to content

Commit

Permalink
继续优化解析括号的逻辑
Browse files Browse the repository at this point in the history
继续优化解析括号的逻辑,单测还剩58个不成功
  • Loading branch information
lizongbo committed May 1, 2024
1 parent db8447c commit a703f00
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 40 deletions.
12 changes: 10 additions & 2 deletions core/src/main/java/com/alibaba/druid/sql/parser/SQLExprParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,24 @@ public SQLExpr expr() {

boolean parenthesized = (lexer.token == Token.LPAREN);
SQLExpr expr = primary();
if (expr instanceof SQLBinaryOpExpr) {
if (parenthesized && expr instanceof SQLBinaryOpExpr) {
if (((SQLBinaryOpExpr) expr).isParenthesized()) {
parenthesized = false;
}
}
if (expr instanceof SQLUnaryExpr) {
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 SQLIdentifierExpr) {
parenthesized = false;
((SQLIdentifierExpr) expr).setParenthesized(true);
}
Lexer.SavePoint mark = lexer.mark();
Token token = lexer.token;
if (token == Token.COMMA) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,6 @@ public boolean visitInternal(SQLBinaryOpExpr x) {
boolean isRoot = parent instanceof SQLSelectQueryBlock;
boolean relational = operator == SQLBinaryOperator.BooleanAnd
|| operator == SQLBinaryOperator.BooleanOr;

if (isRoot && relational) {
this.indentCount++;
}
Expand Down Expand Up @@ -995,10 +994,9 @@ private void visitorBinaryRight(SQLBinaryOpExpr x) {
&& rightOp.isLogical()
&& op.isLogical()
)) {
if (rightRational) {
if (rightRational) { //这里需要验证
this.indentCount++;
}

//print('(');
printExpr(binaryRight, parameterized);
//print(')');
Expand Down Expand Up @@ -1148,9 +1146,9 @@ private void visitBinaryLeft(SQLExpr left, SQLBinaryOperator op) {
quote = false;
}
if (quote) {
print('(');
//print('(');
printExpr(left);
print(')');
//print(')');
} else {
printExpr(left);
}
Expand Down Expand Up @@ -1225,6 +1223,9 @@ protected final void printExpr(SQLExpr x, boolean parameterized) {
}

public boolean visit(SQLCaseExpr x) {
if (x.isParenthesized()) {
print('(');
}
this.indentCount++;
print0(ucase ? "CASE " : "case ");

Expand Down Expand Up @@ -1256,7 +1257,9 @@ public boolean visit(SQLCaseExpr x) {
this.indentCount--;
println();
print0(ucase ? "END" : "end");

if (x.isParenthesized()) {
print(')');
}
return false;
}

Expand Down Expand Up @@ -1876,6 +1879,9 @@ protected void printInteger(SQLIntegerExpr x, boolean parameterized) {
}

public boolean visit(SQLMethodInvokeExpr x) {
if (x.isParenthesized()) {
print('(');
}
SQLExpr owner = x.getOwner();
if (owner != null) {
printMethodOwner(owner);
Expand Down Expand Up @@ -1903,6 +1909,9 @@ public boolean visit(SQLMethodInvokeExpr x) {
printFunctionName(function);
}
printMethodParameters(x);
if (x.isParenthesized()) {
print(')');
}
return false;
}

Expand Down Expand Up @@ -2624,7 +2633,6 @@ protected void printWhere(SQLExpr where) {
if (beforeComments != null && !beforeComments.isEmpty() && isPrettyFormat()) {
printlnComments(beforeComments);
}

printExpr(where, parameterized);

List<String> afterComments = where.getAfterCommentsDirect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public void test15() {
String text = SQLUtils.toSQLString(stmtList, JdbcConstants.MYSQL);
Assert.assertEquals("SELECT *\n" +
"FROM SUNTEST\n" +
"WHERE (~ID) = 1;", text);
"WHERE ~ID = 1;", text);


sql = "SELECT * FROM SUNTEST WHERE ~(ID = 1);";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,34 @@

public class MySqlSelectTest_153 extends MysqlTest {
public void test_0() throws Exception {
String sql = "SELECT ((layer_1_column_0)|(NULLIF(NULL,null )))FROM (SELECT NULL is NULL AS layer_1_column_0 FROM corona_select_multi_db_one_tb WHERE 'a' AND 'b') AS layer_0_table WHERE ! ~ 25 IS NULL;";
String sql = "SELECT ((layer_1_column_0)|(NULLIF(NULL,null )))"
+ "FROM (SELECT NULL is NULL AS layer_1_column_0 "
+ "FROM corona_select_multi_db_one_tb WHERE 'a' AND 'b') "
+ "AS layer_0_table "
+ "WHERE ! ~ 25 IS NULL;";
//
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL, SQLParserFeature.TDDLHint);
SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);

System.out.println(stmt.toString());
assertEquals(1, statementList.size());

assertEquals("SELECT layer_1_column_0 | NULLIF(NULL, NULL)\n" +
assertEquals("SELECT (layer_1_column_0 | (NULLIF(NULL, NULL)))\n" +
"FROM (\n" +
"\tSELECT NULL IS NULL AS layer_1_column_0\n" +
"\tFROM corona_select_multi_db_one_tb\n" +
"\tWHERE 'a'\n" +
"\t\tAND 'b'\n" +
") layer_0_table\n" +
"WHERE (!(~25)) IS NULL;", stmt.toString());
"WHERE !~25 IS NULL;", stmt.toString());

assertEquals("SELECT layer_1_column_0 | NULLIF(NULL, NULL)\n" +
assertEquals("SELECT (layer_1_column_0 | (NULLIF(NULL, NULL)))\n" +
"FROM (\n" +
"\tSELECT NULL IS NULL AS layer_1_column_0\n" +
"\tFROM corona_select_multi_db_one_tb\n" +
"\tWHERE ?\n" +
"\t\tAND ?\n" +
") layer_0_table\n" +
"WHERE (!(~?)) IS NULL;"
"WHERE !~? IS NULL;"
, ParameterizedOutputVisitorUtils.parameterize(sql, JdbcConstants.MYSQL, VisitorFeature.OutputParameterizedZeroReplaceNotUseOriginalSql));


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public void test_0() throws Exception {

assertEquals(1, statementList.size());

assertEquals("SELECT 1 IS NULL, ~NULLIF(1, 1)\n" +
assertEquals("SELECT 1 IS NULL, (~(NULLIF(1, 1)))\n" +
"FROM corona_select_one_db_one_tb layer_0_left_tb\n" +
"\tRIGHT JOIN corona_select_multi_db_one_tb layer_0_right_tb ON layer_0_right_tb.tinyint_1bit_test = layer_0_right_tb.decimal_test\n" +
"WHERE 1 + '1' IS NULL != 30 - layer_0_right_tb.time_test NOT IN (layer_0_left_tb.decimal_test, layer_0_right_tb.tinyint_test, layer_0_left_tb.integer_test, RPAD(NULL, 0, layer_0_left_tb.year_test))", stmt.toString());

assertEquals("SELECT ? IS NULL, ~NULLIF(?, ?)\n" +
assertEquals("SELECT ? IS NULL, (~(NULLIF(?, ?)))\n" +
"FROM corona_select_one_db_one_tb layer_0_left_tb\n" +
"\tRIGHT JOIN corona_select_multi_db_one_tb layer_0_right_tb ON layer_0_right_tb.tinyint_1bit_test = layer_0_right_tb.decimal_test\n" +
"WHERE ? + ? IS NULL != ? - layer_0_right_tb.time_test NOT IN (layer_0_left_tb.decimal_test, layer_0_right_tb.tinyint_test, layer_0_left_tb.integer_test, RPAD(NULL, ?, layer_0_left_tb.year_test))"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public void test_2() throws Exception {
SQLStatement stmt = SQLUtils
.parseSingleStatement(sql, DbType.mysql);

assertEquals("SELECT (+81) * 58 + -33 DIV (+CASE 58\n" +
assertEquals("SELECT +81 * 58 + -33 DIV +CASE 58\n" +
"\t\tWHEN -15 + +31 THEN +95\n" +
"\t\tWHEN -CAST(-NULLIF(+49, -18) AS SIGNED) + +33 THEN --23 + +54\n" +
"\t\tELSE 53\n" +
"\tEND) DIV (-+77) DIV 49 AS col1", stmt.toString());
"\tEND DIV -+77 DIV 49 AS col1", stmt.toString());

System.out.println(stmt.toString());
}
Expand All @@ -77,8 +77,8 @@ public void test_3() throws Exception {
.parseSingleStatement(sql, DbType.mysql);

assertEquals("SELECT DISTINCT CASE +27\n" +
"\t\tWHEN (-MIN(+-75)) / -COUNT(*) * (--52) - -COUNT(*) - -36 / ++56 * -24 * -2 THEN 64\n" +
"\t\tWHEN ++MIN(+9) + -76 + COUNT(*) + -15 + +25 + (-+(-+79)) * 28 THEN NULL\n" +
"\t\tWHEN -MIN(+-75) / -COUNT(*) * (--52) - -COUNT(*) - -36 / ++56 * -24 * -2 THEN 64\n" +
"\t\tWHEN ++MIN(+9) + -76 + COUNT(*) + -15 + +25 + -+(-+79) * 28 THEN NULL\n" +
"\t\tWHEN -+88 THEN +28\n" +
"\t\tELSE -89 + +-29\n" +
"\tEND", stmt.toString());
Expand Down Expand Up @@ -118,13 +118,18 @@ public void test_6() throws Exception {
SQLStatement stmt = SQLUtils
.parseSingleStatement(sql, DbType.mysql);

assertEquals("SELECT DISTINCT 81 DIV +73 * -85 DIV +(+50) AS col1", stmt.toString());
assertEquals("SELECT DISTINCT 81 DIV +73 * -85 DIV ++50 AS col1", stmt.toString());

System.out.println(stmt.toString());
}

public void test_7() throws Exception {
String sql = "SELECT + CASE WHEN 33 NOT BETWEEN - + 16 AND ( + COUNT( * ) + COUNT( * ) / - COALESCE ( - 27, ( - MAX( ALL 41 ) ) / 24 * - - 95 - - 80 + - COUNT( * ) * CAST( NULL AS DECIMAL ) / + 76 - - + 74 * - 49 + - - 25 ) * 89 * - NULLIF ( - - SUM( DISTINCT + 57 ), COUNT( * ) ) - 29 - + MAX( - - 43 ) - + + MAX( DISTINCT + 90 ) + CASE - + 26 WHEN NULLIF ( 5, + 58 * MIN( 67 ) + COUNT( * ) * 8 ) * 47 + CAST( NULL AS SIGNED ) THEN 57 WHEN + 45 THEN NULL ELSE CAST( 35 AS SIGNED ) * 56 END * CAST( NULL AS SIGNED ) ) THEN NULL WHEN NOT + ( - 10 ) / 42 IS NULL THEN + 93 ELSE 54 END * + 36";
String sql = "SELECT + CASE WHEN 33 NOT BETWEEN - + 16 AND ( + COUNT( * ) "
+ "+ COUNT( * ) / - COALESCE ( - 27, ( - MAX( ALL 41 ) ) / 24 * - - 95 - - 80 + - COUNT( * ) * CAST( NULL AS DECIMAL ) / + 76 - - + 74 * - 49 + - - 25 ) * 89 * - "
+ "NULLIF ( - - SUM( DISTINCT + 57 ), COUNT( * ) ) - 29 - + MAX( - - 43 ) - + + MAX( DISTINCT + 90 ) + CASE - + 26 "
+ "WHEN NULLIF ( 5, + 58 * MIN( 67 ) + COUNT( * ) * 8 ) * 47 + CAST( NULL AS SIGNED ) "
+ "THEN 57 WHEN + 45 THEN NULL ELSE CAST( 35 AS SIGNED ) * 56 END * CAST( NULL AS SIGNED ) ) "
+ "THEN NULL WHEN NOT + ( - 10 ) / 42 IS NULL THEN + 93 ELSE 54 END * + 36";

SQLStatement stmt = SQLUtils
.parseSingleStatement(sql, DbType.mysql);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void test_0() throws Exception {
"\t, ip, owner, gmt_create\n" +
"FROM resource_instance\n" +
"WHERE type = 16\n" +
"\tAND properties -> '$.idkp' = '1647796581073291'", stmt.toString());
"\tAND (properties -> '$.idkp' = '1647796581073291)'", stmt.toString());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public void test_0() throws Exception {
SQLStatement stmt = SQLUtils
.parseSingleStatement(sql, DbType.mysql);

assertEquals("SELECT CASE \n" +
"\t\tWHEN `test4dmp`.`sum`(1) OVER (PARTITION BY 1 ) = 1 THEN 1\n" +
"\tEND AS `case when sum(1) OVER (PARTITION BY 1 ) =1 then 1 end`\n" +
assertEquals("SELECT (CASE \n" +
"\t\tWHEN (`test4dmp`.`sum`(1) OVER (PARTITION BY 1 ) = 1) THEN 1\n" +
"\tEND) AS `case when sum(1) OVER (PARTITION BY 1 ) =1 then 1 end`\n" +
"FROM test", stmt.toString());
}

Expand All @@ -44,9 +44,9 @@ public void test_1() throws Exception {
SQLStatement stmt = SQLUtils
.parseSingleStatement(sql, DbType.mysql);

assertEquals("SELECT CASE \n" +
"\t\tWHEN `test4dmp`.`sum`(1) = 1 THEN 1\n" +
"\tEND\n" +
assertEquals("SELECT (CASE \n" +
"\t\tWHEN (`test4dmp`.`sum`(1) = 1) THEN 1\n" +
"\tEND)\n" +
"FROM test", stmt.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,21 @@

public class MySqlSelectTest_92 extends MysqlTest {
public void test_0() throws Exception {
String sql = "select * from TABLENAME cfgdatasou0_ where cfgdatasou0_.type=? and cfgdatasou0_.module_name=? and cfgdatasou0_.node_type=? or cfgdatasou0_.type=? and cfgdatasou0_.module_name=? and cfgdatasou0_.node_type=? or cfgdatasou0_.type=? and cfgdatasou0_.module_name=? and cfgdatasou0_.node_type=?";
String sql = "select * from TABLENAME cfgdatasou0_ "
+ "where cfgdatasou0_.type=? and cfgdatasou0_.module_name=? "
+ "and cfgdatasou0_.node_type=? or cfgdatasou0_.type=? "
+ "and cfgdatasou0_.module_name=? and cfgdatasou0_.node_type=? "
+ "or cfgdatasou0_.typeccc=?"
+ " and cfgdatasou0_.module_nameaaa=?"
+ " and cfgdatasou0_.node_typebbb=?";

MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();

assertEquals(1, statementList.size());

SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
System.out.println(stmt.toString());
assertEquals("SELECT *\n" +
"FROM TABLENAME cfgdatasou0_\n" +
"WHERE cfgdatasou0_.type = ?\n" +
Expand All @@ -41,8 +48,8 @@ public void test_0() throws Exception {
"\tOR cfgdatasou0_.type = ?\n" +
"\tAND cfgdatasou0_.module_name = ?\n" +
"\tAND cfgdatasou0_.node_type = ?\n" +
"\tOR cfgdatasou0_.type = ?\n" +
"\tAND cfgdatasou0_.module_name = ?\n" +
"\tAND cfgdatasou0_.node_type = ?", stmt.toString());
"\tOR cfgdatasou0_.typeccc = ?\n" +
"\tAND cfgdatasou0_.module_nameaaa = ?\n" +
"\tAND cfgdatasou0_.node_typebbb = ?", stmt.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ public void test_0() throws Exception {
"\tSELECT /*+ qb_name(\"innerQuery\") */ 1 AS C1\n" +
"\tFROM SYS.\"X$KZSPR\" \"X$KZSPR\"\n" +
"\tWHERE (\"X$KZSPR\".\"INST_ID\" = USERENV('INSTANCE'))\n" +
"\t\tAND (-\"X$KZSPR\".\"KZSPRPRV\" = -45\n" +
"\t\t\tOR -\"X$KZSPR\".\"KZSPRPRV\" = -47\n" +
"\t\t\tOR -\"X$KZSPR\".\"KZSPRPRV\" = -48\n" +
"\t\t\tOR -\"X$KZSPR\".\"KZSPRPRV\" = -49\n" +
"\t\t\tOR -\"X$KZSPR\".\"KZSPRPRV\" = -50)\n" +
"\t\tAND ((-\"X$KZSPR\".\"KZSPRPRV\") = -45\n" +
"\t\t\tOR (-\"X$KZSPR\".\"KZSPRPRV\") = -47\n" +
"\t\t\tOR (-\"X$KZSPR\".\"KZSPRPRV\") = -48\n" +
"\t\t\tOR (-\"X$KZSPR\".\"KZSPRPRV\") = -49\n" +
"\t\t\tOR (-\"X$KZSPR\".\"KZSPRPRV\") = -50)\n" +
") innerQuery", text);
}

Expand Down

0 comments on commit a703f00

Please sign in to comment.