Skip to content

Commit

Permalink
优化cast array语法的解析逻辑 alibaba#5894
Browse files Browse the repository at this point in the history
优化cast array语法的解析逻辑 alibaba#5894
  • Loading branch information
lizongbo committed May 9, 2024
1 parent e08e92e commit d4ce0fa
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class SQLArrayDataType extends SQLObjectImpl implements SQLDataType {

private DbType dbType;
private SQLDataType componentType;
private boolean usedForCast;
private List<SQLExpr> arguments = new ArrayList<SQLExpr>();

public SQLArrayDataType(SQLDataType componentType) {
Expand Down Expand Up @@ -55,6 +56,14 @@ public void setWithTimeZone(Boolean value) {
throw new UnsupportedOperationException();
}

public boolean isUsedForCast() {
return usedForCast;
}

public void setUsedForCast(boolean usedForCast) {
this.usedForCast = usedForCast;
}

@Override
public boolean isWithLocalTimeZone() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,13 @@ public SQLExpr primary() {
accept(Token.AS);
cast.setDataType(
parseDataType(false));
if (cast.getDataType() instanceof SQLArrayDataType) {
SQLArrayDataType arrayDataType = (SQLArrayDataType) cast.getDataType();
if (arrayDataType.getDbType() == null) {
arrayDataType.setDbType(dbType);
}
arrayDataType.setUsedForCast(true);
}
accept(Token.RPAREN);

sqlExpr = cast;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9499,7 +9499,10 @@ public boolean visit(SQLArrayDataType x) {
print(']');
} else {
SQLDataType componentType = x.getComponentType();
if (componentType != null) {
if (x.isUsedForCast() && componentType != null && componentType.getArguments() != null && componentType.getArguments().size() > 0) {
componentType.accept(this);
print0(ucase ? " ARRAY" : " array");
} else if (componentType != null) {
print0(ucase ? "ARRAY<" : "array<");
componentType.accept(this);
print('>');
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/5894>Issue来源</a>
* @see <a href="https://dev.mysql.com/doc/refman/8.0/en/cast-functions.html#function_cast">CAST(expr AS type [ARRAY])</a>
*/
public class Issue5894 {

@Test
public void test_parse_aschararray() {
for (DbType dbType : new DbType[]{DbType.mysql}) {
for (String sql : new String[]{
"alter table db1.rs_push_mall_data add key idx_bill_no_json((CAST(bill_no_json AS CHAR(50) ARRAY)));",
}) {
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
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());
SQLParseAssertUtil.assertParseSql(sql, dbType);
}
}
}
}

0 comments on commit d4ce0fa

Please sign in to comment.