Skip to content

Commit

Permalink
持续优化逻辑和测试用例
Browse files Browse the repository at this point in the history
还有70多个单测没搞定
  • Loading branch information
lizongbo committed Apr 29, 2024
1 parent 1bf7ba0 commit b1e2036
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 49 deletions.
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 @@ -27,9 +27,6 @@ public class SQLUnaryExpr extends SQLExprImpl implements Serializable, SQLReplac
private SQLExpr expr;
private SQLUnaryOperator operator;

private boolean parenthesized;


public SQLUnaryExpr() {
}

Expand All @@ -48,14 +45,6 @@ public SQLUnaryExpr clone() {
return x;
}

public boolean isParenthesized() {
return parenthesized;
}

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

public SQLUnaryOperator getOperator() {
return operator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,8 @@ public SQLExpr primary() {

sqlExpr = listExpr;
}

if (sqlExpr instanceof SQLBinaryOpExpr) {
((SQLBinaryOpExpr) sqlExpr).setParenthesized(true);
}
if (sqlExpr instanceof SQLUnaryExpr) {
((SQLUnaryExpr) 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
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,9 @@ public boolean visit(SQLBetweenExpr x) {
final SQLExpr testExpr = x.getTestExpr();
final SQLExpr beginExpr = x.getBeginExpr();
final SQLExpr endExpr = x.getEndExpr();
if (x.isParenthesized()) {
print('(');
}

boolean quote = false;
if (testExpr instanceof SQLBinaryOpExpr) {
Expand Down Expand Up @@ -633,7 +636,9 @@ public boolean visit(SQLBetweenExpr x) {
if (x.getHint() != null) {
x.getHint().accept(this);
}

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

Expand Down Expand Up @@ -1018,7 +1023,7 @@ private void visitorBinaryRight(SQLBinaryOpExpr x) {
indentCount--;
}
} else if (SQLBinaryOperator.Equality.priority >= op.priority
&& (right instanceof SQLInListExpr || right instanceof SQLBetweenExpr || right instanceof SQLNotExpr)) {
&& (right instanceof SQLInListExpr || right instanceof SQLNotExpr)) {
indentCount++;
print('(');
printExpr(right, parameterized);
Expand Down Expand Up @@ -4021,7 +4026,7 @@ public boolean visit(SQLUnionQuery x) {
@Override
public boolean visit(SQLUnaryExpr x) {
SQLUnaryOperator operator = x.getOperator();
if(x.isParenthesized()){
if (x.isParenthesized()) {
print('(');
}
print0(operator.name);
Expand Down Expand Up @@ -4059,7 +4064,7 @@ public boolean visit(SQLUnaryExpr x) {
} else {
expr.accept(this);
}
if(x.isParenthesized()){
if (x.isParenthesized()) {
print(')');
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public void test_for_parameterize() throws Exception {
assertEquals("SELECT p.id AS \"id\", p.rule_id AS \"ruleId\", p.name AS \"name\", p.param_type AS \"type\", p.default_value AS \"defaultValue\"\n" +
"\t, p.description AS \"description\"\n" +
"FROM rules_parameters p\n" +
"WHERE p.rule_id = ?", psql);
"WHERE (p.rule_id = ?)", psql);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ public void test_for_parameterize() throws Exception {
"\t, `ktv_resource`.`CONSUME_ID`, `ktv_resource`.`GROUP_ID`, `ktv_resource`.`BUSINESS_ID`, `ktv_resource`.`rule`, `ktv_resource`.`market_place`\n" +
"\t, `ktv_resource`.`VERSION`\n" +
"FROM ktv_resource `ktv_resource`\n" +
"WHERE `ktv_resource`.`KTV_ID` = ?\n" +
"\tAND `ktv_resource`.`STATUS` = ?\n" +
"\tAND `ktv_resource`.`START_TIME` <= ?\n" +
"\tAND `ktv_resource`.`END_TIME` >= ?\n" +
"\tAND `ktv_resource`.`seller_id` IN (?)\n" +
"\tAND (`ktv_resource`.`AVAILABLE_COUNT` IS NULL\n" +
"\t\tOR `ktv_resource`.`AVAILABLE_COUNT` > ?\n" +
"\t\tOR `ktv_resource`.`AVAILABLE_COUNT` = ?)\n" +
"LIMIT ?, ?", psql);
"WHERE ((`ktv_resource`.`KTV_ID` = ?)\n"
+ "\tAND (`ktv_resource`.`STATUS` = ?)\n"
+ "\tAND (`ktv_resource`.`START_TIME` <= ?)\n"
+ "\tAND (`ktv_resource`.`END_TIME` >= ?)\n"
+ "\tAND `ktv_resource`.`seller_id` IN (?)\n"
+ "\tAND (`ktv_resource`.`AVAILABLE_COUNT` IS NULL\n"
+ "\t\tOR (`ktv_resource`.`AVAILABLE_COUNT` > ?)\n"
+ "\t\tOR (`ktv_resource`.`AVAILABLE_COUNT` = ?)))\n"
+ "LIMIT ?, ?", psql);

String rsql = SQLUtils.format(psql, JdbcConstants.MYSQL, parameters);
System.out.println(rsql);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.alibaba.druid.bvt.sql.mysql.param;

import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.fastjson2.JSON;
import com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils;
import com.alibaba.druid.sql.visitor.VisitorFeature;
Expand All @@ -25,7 +27,9 @@ public void test_in() throws Exception {

public void test_between() throws Exception {
String sql = "select ((0='x6') & 31) ^ (76 NOT BETWEEN 3 AND 4) ;";

MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
statementList.toString();
List<Object> params = new ArrayList<Object>();
String psql = ParameterizedOutputVisitorUtils.parameterize(sql, JdbcConstants.MYSQL, params, VisitorFeature.OutputParameterizedUnMergeShardingTable);
assertEquals("SELECT ((? = ?) & ?) ^ (? NOT BETWEEN ? AND ?);", psql);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void test_for_parameterize() throws Exception {
"\tAND `buyer_resource`.`START_TIME` <= '2017-10-16 23:34:28.519'\n" +
"\tAND `buyer_resource`.`END_TIME` >= '2017-10-16 23:34:28.519'\n" +
"\tAND `buyer_resource`.`seller_id` = 2933220011\n" +
"\tAND (`buyer_resource`.`AVAILABLE_COUNT` = 0 OR `buyer_resource`.`AVAILABLE_COUNT` = -1)\n" +
"\tAND ((`buyer_resource`.`AVAILABLE_COUNT` = 0 OR `buyer_resource`.`AVAILABLE_COUNT` = -1))\n" +
"LIMIT 0, 20", formattedSql);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,23 @@
public class MySqlSelectTest_106_hints extends MysqlTest {
public void test_0() throws Exception {
String sql = "\n" +
"select sum(CASE WHEN a.purchase_times=1 THEN 1 else 0 END ) oneCustomersNum, sum(CASE WHEN a.purchase_times=1 THEN a.payment else 0 END ) onceMoney, sum(CASE WHEN a.purchase_times=1 THEN a.interval_day else 0 END ) oneIntervalDay, sum(CASE WHEN a.purchase_times=2 THEN 1 else 0 END ) twoCustomersNum, sum(CASE WHEN a.purchase_times=2 THEN a.payment else 0 END ) twoMoney, sum(CASE WHEN a.purchase_times=2 THEN a.interval_day else 0 END ) twoIntervalDay, sum(CASE WHEN a.purchase_times=3 THEN 1 else 0 END ) threeCustomersNum, sum(CASE WHEN a.purchase_times=3 THEN a.payment else 0 END ) threeMoney, sum(CASE WHEN a.purchase_times=3 THEN a.interval_day else 0 END ) threeIntervalDay, sum(CASE WHEN a.purchase_times=4 THEN 1 else 0 END ) fourCustomersNum, sum(CASE WHEN a.purchase_times=4 THEN a.payment else 0 END ) fourMoney, sum(CASE WHEN a.purchase_times=4 THEN a.interval_day else 0 END ) fourIntervalDay, sum(CASE WHEN a.purchase_times=5 THEN 1 else 0 END ) fiveCustomersNum, sum(CASE WHEN a.purchase_times=5 THEN a.payment else 0 END ) fiveMoney, sum(CASE WHEN a.purchase_times=5 THEN a.interval_day else 0 END ) fiveIntervalDay from t_buyer_day a force index (sellerId_during) WHERE a.sellerId = 3234284498 and a.pay_trades>0 and ( a.during = str_to_date('2018-01-10', '%Y-%m-%d') );";
"select sum(CASE WHEN a.purchase_times=1 THEN 1 else 0 END ) oneCustomersNum,"
+ " sum(CASE WHEN a.purchase_times=1 THEN a.payment else 0 END ) onceMoney, "
+ "sum(CASE WHEN a.purchase_times=1 THEN a.interval_day else 0 END ) oneIntervalDay, "
+ "sum(CASE WHEN a.purchase_times=2 THEN 1 else 0 END ) twoCustomersNum, "
+ "sum(CASE WHEN a.purchase_times=2 THEN a.payment else 0 END ) twoMoney,"
+ " sum(CASE WHEN a.purchase_times=2 THEN a.interval_day else 0 END ) twoIntervalDay, "
+ "sum(CASE WHEN a.purchase_times=3 THEN 1 else 0 END ) threeCustomersNum, "
+ "sum(CASE WHEN a.purchase_times=3 THEN a.payment else 0 END ) threeMoney, "
+ "sum(CASE WHEN a.purchase_times=3 THEN a.interval_day else 0 END ) threeIntervalDay, "
+ "sum(CASE WHEN a.purchase_times=4 THEN 1 else 0 END ) fourCustomersNum, "
+ "sum(CASE WHEN a.purchase_times=4 THEN a.payment else 0 END ) fourMoney, "
+ "sum(CASE WHEN a.purchase_times=4 THEN a.interval_day else 0 END ) fourIntervalDay,"
+ " sum(CASE WHEN a.purchase_times=5 THEN 1 else 0 END ) fiveCustomersNum, "
+ "sum(CASE WHEN a.purchase_times=5 THEN a.payment else 0 END ) fiveMoney, "
+ "sum(CASE WHEN a.purchase_times=5 THEN a.interval_day else 0 END ) fiveIntervalDay "
+ "from t_buyer_day a force index (sellerId_during) WHERE a.sellerId = 3234284498 "
+ "and a.pay_trades>0 and ( a.during = str_to_date('2018-01-10', '%Y-%m-%d') );";

MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
Expand Down Expand Up @@ -98,7 +114,7 @@ public void test_0() throws Exception {
"FROM t_buyer_day a FORCE INDEX (sellerId_during)\n" +
"WHERE a.sellerId = 3234284498\n" +
"\tAND a.pay_trades > 0\n" +
"\tAND a.during = str_to_date('2018-01-10', '%Y-%m-%d');", stmt.toString());
"\tAND (a.during = str_to_date('2018-01-10', '%Y-%m-%d'));", stmt.toString());
}

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

assertEquals(1, statementList.size());

assertEquals("SELECT SQL_SMALL_RESULT NULL IS NOT false\n" +
assertEquals("SELECT SQL_SMALL_RESULT (NULL IS NOT false)\n" +
"FROM corona_select_multi_db_one_tb layer_0_left_tb\n" +
"\tRIGHT JOIN corona_select_one_db_multi_tb layer_0_right_tb ON layer_0_right_tb.smallint_test = layer_0_right_tb.date_test\n" +
"WHERE layer_0_right_tb.time_test = 'x6' NOT BETWEEN 96 AND layer_0_right_tb.bigint_test;", stmt.toString());

assertEquals("SELECT SQL_SMALL_RESULT NULL IS NOT ?\n" +
assertEquals("SELECT SQL_SMALL_RESULT (NULL IS NOT ?)\n" +
"FROM corona_select_multi_db_one_tb layer_0_left_tb\n" +
"\tRIGHT JOIN corona_select_one_db_multi_tb layer_0_right_tb ON layer_0_right_tb.smallint_test = layer_0_right_tb.date_test\n" +
"WHERE layer_0_right_tb.time_test = ? NOT BETWEEN ? AND layer_0_right_tb.bigint_test;"
Expand Down

0 comments on commit b1e2036

Please sign in to comment.