Skip to content

Commit

Permalink
持续调整单测和优化逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongbo committed Apr 24, 2024
1 parent b6653b6 commit e89e14c
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class SQLUnaryExpr extends SQLExprImpl implements Serializable, SQLReplac
private SQLExpr expr;
private SQLUnaryOperator operator;

private boolean parenthesized;


public SQLUnaryExpr() {
}

Expand All @@ -41,9 +44,18 @@ public SQLUnaryExpr clone() {
x.setExpr(expr.clone());
}
x.operator = operator;
x.parenthesized = parenthesized;
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 @@ -113,6 +113,11 @@ public SQLExpr expr() {
parenthesized = false;
}
}
if (expr instanceof SQLUnaryExpr) {
if (((SQLUnaryExpr) expr).isParenthesized()) {
parenthesized = false;
}
}
Lexer.SavePoint mark = lexer.mark();
Token token = lexer.token;
if (token == Token.COMMA) {
Expand All @@ -137,6 +142,9 @@ public SQLExpr 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 @@ -391,6 +399,9 @@ public SQLExpr primary() {
if (sqlExpr instanceof SQLBinaryOpExpr) {
((SQLBinaryOpExpr) sqlExpr).setParenthesized(true);
}
if (sqlExpr instanceof SQLUnaryExpr) {
((SQLUnaryExpr) sqlExpr).setParenthesized(true);
}

if ((lexer.token == Token.UNION || lexer.token == Token.MINUS || lexer.token == Token.EXCEPT)
&& sqlExpr instanceof SQLQueryExpr) {
Expand Down Expand Up @@ -854,7 +865,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
Original file line number Diff line number Diff line change
Expand Up @@ -4021,6 +4021,9 @@ public boolean visit(SQLUnionQuery x) {
@Override
public boolean visit(SQLUnaryExpr x) {
SQLUnaryOperator operator = x.getOperator();
if(x.isParenthesized()){
print('(');
}
print0(operator.name);
SQLExpr expr = x.getExpr();

Expand Down Expand Up @@ -4056,6 +4059,9 @@ public boolean visit(SQLUnaryExpr x) {
} else {
expr.accept(this);
}
if(x.isParenthesized()){
print(')');
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void test_0() throws Exception {
" UNION\n" +
"\n" +
" SELECT coalesce(a.user_id, b.user_id) AS user_id\n" +
" FROM user_info_online a LEFT JOIN user_info_offline b ON a.user_id = b.user_id\n" +
" FROM user_info_online a LEFT JOIN user_info_offline b ON (a.user_id = b.user_id)\n" +
" WHERE ((a.create_time > '2018-01-01 00:00:00') OR\n" +
" (a.create_time IS NULL AND b.create_time > '2018-01-01 00:00:00')\n" +
" )\n" +
Expand All @@ -62,10 +62,10 @@ public void test_0() throws Exception {
"\tUNION\n" +
"\tSELECT coalesce(a.user_id, b.user_id) AS user_id\n" +
"\tFROM user_info_online a\n" +
"\t\tLEFT JOIN user_info_offline b ON a.user_id = b.user_id\n" +
"\tWHERE a.create_time > '2018-01-01 00:00:00'\n" +
"\t\tLEFT JOIN user_info_offline b ON (a.user_id = b.user_id)\n" +
"\tWHERE ((a.create_time > '2018-01-01 00:00:00')\n" +
"\t\tOR (a.create_time IS NULL\n" +
"\t\t\tAND b.create_time > '2018-01-01 00:00:00')\n" +
"\t\t\tAND b.create_time > '2018-01-01 00:00:00'))\n" +
")", //
stmt.toString());

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

SQLStatement stmt = statementList.get(0);

assertEquals("SELECT CAST(`calcs`.`date0` AS TIMESTAMP) + CAST(TRUNCATE(-`calcs`.`num4`, 0) AS INTEGER) * INTERVAL '1' DAY + CAST(TRUNCATE((`calcs`.`num4` - CAST(TRUNCATE(`calcs`.`num4`, 0) AS INTEGER)) * -24, 0) AS INTEGER) * INTERVAL '1' HOUR + CAST(TRUNCATE((`calcs`.`num4` * 24 - CAST(TRUNCATE(`calcs`.`num4` * 24, 0) AS INTEGER)) * -60, 0) AS INTEGER) * INTERVAL '1' MINUTE + CAST(TRUNCATE((`calcs`.`num4` * 24 * 60 - CAST(TRUNCATE(`calcs`.`num4` * 24 * 60, 0) AS INTEGER)) * -60, 0) AS INTEGER) * INTERVAL '1' SECOND AS `TEMP(Test)(2923065813)(0)`\n" +
assertEquals("SELECT (CAST(`calcs`.`date0` AS TIMESTAMP) + CAST(TRUNCATE(-`calcs`.`num4`, 0) AS INTEGER) * INTERVAL '1' DAY + CAST(TRUNCATE((`calcs`.`num4` - CAST(TRUNCATE(`calcs`.`num4`, 0) AS INTEGER)) * -24, 0) AS INTEGER) * INTERVAL '1' HOUR + CAST(TRUNCATE((`calcs`.`num4` * 24 - CAST(TRUNCATE(`calcs`.`num4` * 24, 0) AS INTEGER)) * -60, 0) AS INTEGER) * INTERVAL '1' MINUTE + CAST(TRUNCATE((`calcs`.`num4` * 24 * 60 - CAST(TRUNCATE(`calcs`.`num4` * 24 * 60, 0) AS INTEGER)) * -60, 0) AS INTEGER) * INTERVAL '1' SECOND) AS `TEMP(Test)(2923065813)(0)`\n" +
"FROM `calcs`\n" +
"GROUP BY 1", stmt.toString());

assertEquals("select cast(`calcs`.`date0` as TIMESTAMP) + cast(TRUNCATE(-`calcs`.`num4`, 0) as INTEGER) * interval '1' day + cast(TRUNCATE((`calcs`.`num4` - cast(TRUNCATE(`calcs`.`num4`, 0) as INTEGER)) * -24, 0) as INTEGER) * interval '1' hour + cast(TRUNCATE((`calcs`.`num4` * 24 - cast(TRUNCATE(`calcs`.`num4` * 24, 0) as INTEGER)) * -60, 0) as INTEGER) * interval '1' minute + cast(TRUNCATE((`calcs`.`num4` * 24 * 60 - cast(TRUNCATE(`calcs`.`num4` * 24 * 60, 0) as INTEGER)) * -60, 0) as INTEGER) * interval '1' second as `TEMP(Test)(2923065813)(0)`\n" +
assertEquals("select (cast(`calcs`.`date0` as TIMESTAMP) + cast(TRUNCATE(-`calcs`.`num4`, 0) as INTEGER) * interval '1' day + cast(TRUNCATE((`calcs`.`num4` - cast(TRUNCATE(`calcs`.`num4`, 0) as INTEGER)) * -24, 0) as INTEGER) * interval '1' hour + cast(TRUNCATE((`calcs`.`num4` * 24 - cast(TRUNCATE(`calcs`.`num4` * 24, 0) as INTEGER)) * -60, 0) as INTEGER) * interval '1' minute + cast(TRUNCATE((`calcs`.`num4` * 24 * 60 - cast(TRUNCATE(`calcs`.`num4` * 24 * 60, 0) as INTEGER)) * -60, 0) as INTEGER) * interval '1' second) as `TEMP(Test)(2923065813)(0)`\n" +
"from `calcs`\n" +
"group by 1", stmt.clone().toLowerCaseString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void test_0() throws Exception {
"WHERE NOT `CUSTOMER`.`custkey` IN (\n" +
"\tSELECT `CUSTKEY`\n" +
"\tFROM \"ORDERS\"\n" +
"\tWHERE `CUSTKEY` > 100\n" +
"\tWHERE (`CUSTKEY` > 100)\n" +
")\n" +
"GROUP BY `NAME`\n" +
"ORDER BY `MAXKEY` ASC\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,15 +373,15 @@ public void test_0() throws Exception {
"\t, sszx + sszxfy + sszxk + ss_sjzxf + ss_sjgzzxf AS 装修回款金额\n" +
"\t, ROUND((sszx + sszxfy + sszxk + ss_sjzxf + ss_sjgzzxf) / decoration_amount * 100, 2) AS 装修回款比例\n" +
"\t, str_to_date(act1_date.actual_received_date, '%Y-%m-%d') AS 最后回款时间\n" +
"\t, ROUND(CASE \n" +
"\t, ROUND((CASE \n" +
"\t\tWHEN tran.decoration_merge_flag = 1\n" +
"\t\t\tAND decoration_moneymanage = 1\n" +
"\t\tTHEN tran.deal_price_with_decoration / (tran.sta_price + IFNULL(tran.decoration_sta_price, 0))\n" +
"\t\tWHEN tran.decoration_merge_flag = 1\n" +
"\t\t\tAND decoration_moneymanage = 0\n" +
"\t\tTHEN (tran.deal_price - decoration_amount) / tran.sta_price\n" +
"\t\tWHEN tran.decoration_merge_flag = 0 THEN tran.deal_price / tran.sta_price\n" +
"\tEND * 100, 2) AS 最终折扣\n" +
"\tEND * 100), 2) AS 最终折扣\n" +
"\t, payment.name AS 付款方式, sdd_item.NAME AS 付款方式类型, depayment.name AS 装修付款方式, paymentplan.ysdj AS 应收定金, actualReceiMoney.ssdj AS 实收定金\n" +
"\t, paymentplan.yssq AS 应收首期, actualReceiMoney.sssq AS 实收首期, paymentplan.yslk AS 应收楼款, actualReceiMoney.sslk AS 实收楼款, paymentplan.ysaj AS 应收按揭\n" +
"\t, actualReceiMoney.ssaj AS 实收按揭, paymentplan.ysbc AS 应收面积差款, actualReceiMoney.ssbc AS 实收面积差款, paymentplan.ysdsfy AS 应收代收费用, actualReceiMoney.ssdsfy AS 实收代收费用\n" +
Expand Down Expand Up @@ -634,7 +634,7 @@ public void test_0() throws Exception {
"\t\t\tEND) AS ysdsfy\n" +
"\t\t\t, SUM(plan_amount_total) AS ystotalMoney\n" +
"\t\t\t, SUM(CASE \n" +
"\t\t\t\tWHEN fund_type_code IN ('FIFT02', 'FIFT01') THEN plan_amount_total - received_amount_total\n" +
"\t\t\t\tWHEN fund_type_code IN ('FIFT02', 'FIFT01') THEN (plan_amount_total - received_amount_total)\n" +
"\t\t\t\tELSE 0\n" +
"\t\t\tEND) AS whkAmount\n" +
"\t\tFROM midea_sd_payment_plan pp\n" +
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 ALL +(+(+col1))\n" +
assertEquals("SELECT ALL ++(+col1)\n" +
"FROM random_aggregates_23_tab2 cor0\n" +
"WHERE NOT +(+col2) + CAST(NULL AS SIGNED) IS NOT NULL", stmt.toString());
"WHERE NOT (++col2) + CAST(NULL AS SIGNED) IS NOT NULL", stmt.toString());

System.out.println(stmt.toString());
}
Expand All @@ -50,7 +50,8 @@ public void test_1() throws Exception {
}

public void test_2() throws Exception {
String sql = "SELECT + 81 * 58 + - 33 DIV + CASE 58 WHEN - 15 + + 31 THEN + 95 WHEN - CAST( - NULLIF ( + 49, - 18 ) AS SIGNED ) + + 33 THEN - - 23 + + 54 ELSE 53 END DIV - + 77 DIV 49 AS col1 ";
String sql = "SELECT + 81 * 58 + - 33 DIV + CASE 58 WHEN - 15 + + 31 THEN + 95 WHEN - CAST( - NULLIF ( + 49, - 18 ) AS SIGNED ) + + 33"
+ " THEN - - 23 + + 54 ELSE 53 END DIV - + 77 DIV 49 AS col1 ";

SQLStatement stmt = SQLUtils
.parseSingleStatement(sql, DbType.mysql);
Expand All @@ -59,7 +60,7 @@ public void test_2() throws Exception {
"\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 @@ -76,9 +77,9 @@ 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 -(+88) THEN +28\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 All @@ -97,7 +98,7 @@ public void test_4() throws Exception {
"FROM (\n" +
"\tSELECT 1 AS col0, 2 AS col1, 3 AS col2\n" +
") x\n" +
"WHERE NOT (-(+col2)) * +(+col0) = col0", stmt.toString());
"WHERE NOT (-(+col2) * ++col0) = col0", stmt.toString());
}

public void test_5() throws Exception {
Expand Down

0 comments on commit e89e14c

Please sign in to comment.