Skip to content

Commit

Permalink
优化支持sql cast函数的多重括号解析和生成 alibaba#5958
Browse files Browse the repository at this point in the history
优化支持sql cast函数的多重括号解析和生成 alibaba#5958
  • Loading branch information
lizongbo committed Jun 2, 2024
1 parent eda229e commit b219209
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
20 changes: 19 additions & 1 deletion core/src/main/java/com/alibaba/druid/sql/ast/SQLExprImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

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

protected int parenthesizedCount;
public SQLExprImpl() {
}

Expand All @@ -29,7 +29,25 @@ public boolean isParenthesized() {

public void setParenthesized(boolean parenthesized) {
this.parenthesized = parenthesized;
if (parenthesized) {
parenthesizedCount++;
} else {
parenthesizedCount--;
}
}

public int getParenthesizedCount() {
return parenthesizedCount;
}

public void setParenthesizedCount(int parenthesizedCount) {
this.parenthesizedCount = parenthesizedCount;
}

public void increaseParenthesizedCount() {
this.parenthesizedCount++;
}

public abstract boolean equals(Object o);

public abstract int hashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,7 @@ public boolean visit(SQLGetDiagnosticsStatement x) {
}

public boolean visit(SQLCastExpr x) {
tryPrintLparen(x);
if (x.isTry()) {
print0(ucase ? "TRY_CAST(" : "try_cast(");
} else {
Expand All @@ -1297,7 +1298,7 @@ public boolean visit(SQLCastExpr x) {
print0(ucase ? " AS " : " as ");
x.getDataType().accept(this);
print0(")");

tryPrintRparen(x);
return false;
}

Expand Down Expand Up @@ -11839,4 +11840,21 @@ public boolean visit(StarRocksIndexDefinition x) {
}
return false;
}
protected void tryPrintLparen(SQLExprImpl x) {
if (x.isParenthesized()) {
print('(');
}
for (int i = 1; i < x.getParenthesizedCount(); ++i) {
print('(');
}
}

protected void tryPrintRparen(SQLExprImpl x) {
if (x.isParenthesized()) {
print(')');
}
for (int i = 1; i < x.getParenthesizedCount(); ++i) {
print(')');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void test_parse_aschararray() {
List<SQLStatement> statementList = parser.parseStatementList();
assertEquals(1, statementList.size());
assertEquals("ALTER TABLE db1.rs_push_mall_data\n"
+ "\tADD KEY idx_bill_no_json (CAST(bill_no_json AS CHAR(50) ARRAY));", statementList.get(0).toString());
+ "\tADD KEY idx_bill_no_json ((CAST(bill_no_json AS CHAR(50) ARRAY)));", statementList.get(0).toString());
SQLParseAssertUtil.assertParseSql(sql, dbType);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.alibaba.druid.bvt.sql.mysql.issues;

import java.util.List;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.SQLParseAssertUtil;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.parser.SQLParserUtils;
import com.alibaba.druid.sql.parser.SQLStatementParser;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* @author lizongbo
* @see <a href="https://github.com/alibaba/druid/issues/5958" >Issue来源</a>
*/
public class Issue5958 {


@Test
public void test_parse_alter() {
for (DbType dbType : new DbType[]{DbType.mysql}) {
for (String sql : new String[]{
//"select (((a-b))) from aa;",
"alter TABLE test.rs_urge_pickup_config ADD KEY idx_site_id_list2 ((cast(site_id_list as char(10) array)));",
}) {
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
List<SQLStatement> statementList = parser.parseStatementList();
System.out.println(statementList);
assertEquals(1, statementList.size());
SQLParseAssertUtil.assertParseSql(sql, dbType);
}
}
}
}

0 comments on commit b219209

Please sign in to comment.