Skip to content

Commit

Permalink
merge: 0.5.10 change log
Browse files Browse the repository at this point in the history
  • Loading branch information
Willam2004 committed Jun 9, 2024
1 parent 1f12ce2 commit e532ab7
Show file tree
Hide file tree
Showing 106 changed files with 4,653 additions and 497 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ deploy.sh
/fastmodel-parser/src/test/resources/lsp/keyword.txt
/fastmodel-transform/fastmodel-transform-adbmysql/src/main/antlr4/com/aliyun/fastmodel/transform/adbmysql/parser/gen/AdbMysqlLexer.java
/fastmodel-transform/fastmodel-transform-mysql/src/main/antlr4/com/aliyun/fastmodel/transform/mysql/parser/gen/MySqlLexer.java
gen
18 changes: 18 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ Transformer
向下兼容(compatibility)
• 暂无

过期功能(deprecations)
安全漏洞修复(vulnerability)

## 0.5.10
新功能(new features)
Core
• 新增支持Column的Default表达式


Transformer
• Hologres建表支持Default表达式
• StarRocks建表支持分区表达式

向下兼容(compatibility)
• 暂无

过期功能(deprecations)
安全漏洞修复(vulnerability)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ public static NodeLocation getLocation(TerminalNode terminalNode) {
// ******** Helper ************

public static <T> Optional<T> visitIfPresent(AbstractParseTreeVisitor visitor, ParseTree context,
Class<T> clazz) {
Class<T> clazz) {
return Optional.ofNullable(context)
.map(visitor::visit)
.map(clazz::cast);
}

public static <T> List<T> visit(AbstractParseTreeVisitor visitor, List<? extends ParseTree> contexts,
Class<T> clazz) {
Class<T> clazz) {
return contexts.stream()
.map(visitor::visit)
.filter(Objects::nonNull)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@

package com.aliyun.fastmodel.common.utils;

import com.aliyun.fastmodel.core.tree.util.StringLiteralUtil;
import org.apache.commons.lang3.StringUtils;

import static com.aliyun.fastmodel.core.tree.util.StringLiteralUtil.DOUBLE_QUOTE;
import static com.aliyun.fastmodel.core.tree.util.StringLiteralUtil.PREFIX;
import static com.aliyun.fastmodel.core.tree.util.StringLiteralUtil.SINGLE;

/**
* 去除字符串两边的引号内容
*
Expand All @@ -27,9 +32,6 @@
public class StripUtils {

public static final String SUFFIX = ";";
public static final String SINGLE = "'";
public static final String PREFIX = "`";
public static final String DOUBLE_QUOTE = "\"";

/**
* 将字符串坐下strip
Expand All @@ -38,42 +40,19 @@ public class StripUtils {
* @return 处理后的字符串
*/
public static String strip(String src) {
String prefix = SINGLE;
if (src.startsWith(prefix)) {
return stripOne(src, prefix);
}
prefix = DOUBLE_QUOTE;
if (src.startsWith(prefix)) {
return stripOne(src, prefix);
}
prefix = PREFIX;
if (src.startsWith(prefix)) {
return stripOne(src, prefix);
}
return src;
return StringLiteralUtil.strip(src);
}

/**
* 将双引号去掉
*
* @param src
* @return 去除之后的双引号
*/
public static String removeDoubleStrip(String src){
public static String removeDoubleStrip(String src) {
return StringUtils.remove(src, DOUBLE_QUOTE);
}

private static String unquote(String value) {
return value.substring(1, value.length() - 1)
.replace("''", "'");
}

private static String stripOne(String src, String prefix) {
if (src.startsWith(prefix) && src.endsWith(prefix)) {
return unquote(src);
}
return src;
}

/**
* 给string增加单引号
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
import java.util.function.BiFunction;
import java.util.stream.Collectors;

import com.aliyun.fastmodel.compare.util.CompareUtil;
import com.aliyun.fastmodel.compare.impl.table.column.OrderColumnManager;
import com.aliyun.fastmodel.compare.util.CompareUtil;
import com.aliyun.fastmodel.core.tree.BaseStatement;
import com.aliyun.fastmodel.core.tree.Comment;
import com.aliyun.fastmodel.core.tree.Property;
import com.aliyun.fastmodel.core.tree.QualifiedName;
import com.aliyun.fastmodel.core.tree.expr.BaseExpression;
import com.aliyun.fastmodel.core.tree.expr.Identifier;
import com.aliyun.fastmodel.core.tree.statement.constants.ColumnCategory;
import com.aliyun.fastmodel.core.tree.statement.constants.ColumnPropertyDefaultKey;
Expand Down Expand Up @@ -108,11 +109,11 @@ public List<BaseStatement> compareTableElement(CreateTable before, CreateTable a
}

protected Builder<BaseStatement> incrementCompare(QualifiedName qualifiedName,
List<ColumnDefinition> beforeColumnDefines,
List<ColumnDefinition> afterColumnDefines,
BiFunction<QualifiedName, ColumnDefinition, BaseStatement> addDrop,
BiFunction<QualifiedName, List<ColumnDefinition>,
List<BaseStatement>> addCols) {
List<ColumnDefinition> beforeColumnDefines,
List<ColumnDefinition> afterColumnDefines,
BiFunction<QualifiedName, ColumnDefinition, BaseStatement> addDrop,
BiFunction<QualifiedName, List<ColumnDefinition>,
List<BaseStatement>> addCols) {
Builder<BaseStatement> builder = ImmutableList.builder();
//如果两边的都有的话内容
List<ColumnDefinition> afterCopy = Lists.newArrayList(afterColumnDefines);
Expand Down Expand Up @@ -199,18 +200,20 @@ private Map<String, ColumnDefinition> uuidMap(List<ColumnDefinition> beforeColum
* @return
*/
private ChangeCol compare(QualifiedName qualifiedName, ColumnDefinition beforeDefine,
ColumnDefinition afterDefine) {
ColumnDefinition afterDefine) {
boolean changeComment = isChangeColumnComment(beforeDefine, afterDefine);
boolean changeAliased = !Objects.equals(beforeDefine.getAliasedName(), afterDefine.getAliasedName());
boolean changePrimary = false;
boolean changeColumn = !StringUtils.equalsIgnoreCase(beforeDefine.getColName().getValue(),
boolean changeName = !StringUtils.equalsIgnoreCase(beforeDefine.getColName().getValue(),
afterDefine.getColName().getValue());
boolean changeNotNull = false;
boolean changeType = !Objects.equals(beforeDefine.getDataType(), afterDefine.getDataType());
boolean changeCategory = isChangeColumnCategory(beforeDefine, afterDefine);
boolean changeDim = isChangeDim(beforeDefine, afterDefine);
boolean changeRefIndicators = isChangeRefIndicaotrs(beforeDefine, afterDefine);
boolean changeDefaultValue = isChangeDefaultValue(beforeDefine, afterDefine);
List<Property> changeProperties = changeProperties(beforeDefine, afterDefine);
boolean changeColumnProperties = !changeProperties.isEmpty();
Comment comment = null;
if (changeComment) {
if (afterDefine.getComment() == null) {
Expand Down Expand Up @@ -241,9 +244,42 @@ private ChangeCol compare(QualifiedName qualifiedName, ColumnDefinition beforeDe
changeNotNull = true;
}
}
ChangeCol.ChangeState changeState = new ChangeCol.ChangeState();
if (changeName) {
changeState.setChangeName();
}
if (changeAliased) {
changeState.setChangeAlias();
}
if (changePrimary) {
changeState.setChangePrimary();
}
if (changeNotNull) {
changeState.setChangeNotNull();
}
if (changeType) {
changeState.setChangeDataType();
}
if (changeComment) {
changeState.setChangeComment();
}
if (changeDefaultValue) {
changeState.setChangeDefaultValue();
}
if (changeColumnProperties) {
changeState.setChangeColumnProperties();
}
if (changeCategory) {
changeState.setChangeCategory();
}
if (changeDim) {
changeState.setChangeDim();
}
if (changeRefIndicators) {
changeState.setChangeRefIndicators();
}
//如果都没有发生修改,那么直接返回null
if (!changeComment && !changeAliased && !changePrimary && !changeNotNull && !changeColumn && !changeType
&& !changeCategory && changeProperties.isEmpty() && !changeDim && !changeRefIndicators) {
if (changeState.nothingChange()) {
return null;
}
if (changeCategory) {
Expand All @@ -258,8 +294,16 @@ private ChangeCol compare(QualifiedName qualifiedName, ColumnDefinition beforeDe
if (changeRefIndicators) {
builder.refIndicators(afterDefine.getRefIndicators());
}
ChangeCol changeCol = new ChangeCol(qualifiedName, beforeDefine.getColName(), builder.build());
return changeCol;
if (changeDefaultValue) {
builder.defaultValue(afterDefine.getDefaultValue());
}
return new ChangeCol(qualifiedName, beforeDefine.getColName(), builder.build(), Optional.of(changeState));
}

private boolean isChangeDefaultValue(ColumnDefinition beforeDefine, ColumnDefinition afterDefine) {
BaseExpression defaultValue = beforeDefine.getDefaultValue();
BaseExpression afterDefaultValue = afterDefine.getDefaultValue();
return !Objects.equals(defaultValue, afterDefaultValue);
}

private boolean isChangeDim(ColumnDefinition beforeDefine, ColumnDefinition afterDefine) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.aliyun.fastmodel.compare.impl.table.ColumnCompare;
import com.aliyun.fastmodel.core.tree.BaseStatement;
import com.aliyun.fastmodel.core.tree.Comment;
import com.aliyun.fastmodel.core.tree.Property;
import com.aliyun.fastmodel.core.tree.QualifiedName;
import com.aliyun.fastmodel.core.tree.datatype.BaseDataType;
Expand All @@ -30,8 +31,11 @@
import com.aliyun.fastmodel.core.tree.datatype.GenericDataType;
import com.aliyun.fastmodel.core.tree.datatype.TypeParameter;
import com.aliyun.fastmodel.core.tree.expr.Identifier;
import com.aliyun.fastmodel.core.tree.expr.literal.StringLiteral;
import com.aliyun.fastmodel.core.tree.statement.constants.ColumnCategory;
import com.aliyun.fastmodel.core.tree.statement.constants.ColumnPropertyDefaultKey;
import com.aliyun.fastmodel.core.tree.statement.table.ChangeCol;
import com.aliyun.fastmodel.core.tree.statement.table.ChangeCol.ChangeType;
import com.aliyun.fastmodel.core.tree.statement.table.ColumnDefinition;
import com.aliyun.fastmodel.core.tree.statement.table.CreateTable;
import com.aliyun.fastmodel.core.tree.util.DataTypeUtil;
Expand All @@ -41,6 +45,8 @@
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* ColumnCompareTest
Expand Down Expand Up @@ -163,7 +169,7 @@ public void compareOrder() {

List<BaseStatement> baseStatementList = columnCompare.compareTableElement(before, after);
String join = Joiner.on("\n").join(baseStatementList);
assertEquals(join, "ALTER TABLE dim_shop CHANGE COLUMN c2 c2 BIGINT FIRST" );
assertEquals(join, "ALTER TABLE dim_shop CHANGE COLUMN c2 c2 BIGINT FIRST");
}

@Test
Expand Down Expand Up @@ -194,7 +200,6 @@ public void testCompareNode2() {
.columns(beforeColumns)
.tableName(QualifiedName.of("dim_shop")).build();


CreateTable after = CreateTable.builder()
.columns(afterColumns)
.tableName(QualifiedName.of("dim_shop")).build();
Expand All @@ -209,7 +214,6 @@ public void testCompareNode2() {
+ "ALTER TABLE dim_shop CHANGE COLUMN c c BIGINT AFTER d");
}


private static ColumnDefinition getColumn(String colName) {
return ColumnDefinition.builder().colName(new Identifier(colName))
.dataType(DataTypeUtil.simpleType(DataTypeEnums.BIGINT))
Expand Down Expand Up @@ -344,5 +348,75 @@ public void testComparePropertiesBeforeNull() {
assertEquals(1, baseStatementList.size());
String join = Joiner.on("\n").join(baseStatementList);
assertEquals(join, "ALTER TABLE dim_shop CHANGE COLUMN c1 c1 BIGINT WITH ('code_table'='a')");
BaseStatement baseStatement = baseStatementList.get(0);
ChangeCol changeCol = (ChangeCol)baseStatement;
boolean change = changeCol.change(ChangeType.COLUMN_PROPERTIES);
assertTrue(change);
change = changeCol.change(ChangeType.COMMENT);
assertFalse(change);
}

@Test
public void testCompareDefaultValue() {
ColumnDefinition columnDefinition = ColumnDefinition.builder()
.colName(new Identifier("c1"))
.defaultValue(new StringLiteral("abc"))
.dataType(DataTypeUtil.simpleType(DataTypeEnums.BIGINT))
.build();
ColumnDefinition columnDefinition2 = ColumnDefinition.builder()
.colName(new Identifier("c1"))
.dataType(DataTypeUtil.simpleType(DataTypeEnums.BIGINT))
.defaultValue(new StringLiteral("bcd"))
.build();
CreateTable before = CreateTable.builder()
.columns(ImmutableList.of(columnDefinition))
.tableName(QualifiedName.of("dim_shop")).build();

CreateTable after = CreateTable.builder()
.columns(ImmutableList.of(columnDefinition2))
.tableName(QualifiedName.of("dim_shop")).build();

List<BaseStatement> baseStatementList = columnCompare.compareTableElement(before, after);
assertEquals(1, baseStatementList.size());
String join = Joiner.on("\n").join(baseStatementList);
assertEquals(join, "ALTER TABLE dim_shop CHANGE COLUMN c1 c1 BIGINT DEFAULT 'bcd'");
BaseStatement baseStatement = baseStatementList.get(0);
assertTrue(baseStatement instanceof ChangeCol);
ChangeCol changeCol = (ChangeCol)baseStatement;
boolean change = changeCol.change(ChangeType.DEFAULT_VALUE);
assertTrue(change);
change = changeCol.change(ChangeType.COMMENT);
assertFalse(change);
}

@Test
public void testCompareComment() {
ColumnDefinition columnDefinition = ColumnDefinition.builder()
.colName(new Identifier("c1"))
.comment(new Comment("abc"))
.dataType(DataTypeUtil.simpleType(DataTypeEnums.BIGINT))
.build();
ColumnDefinition columnDefinition2 = ColumnDefinition.builder()
.colName(new Identifier("c1"))
.comment(new Comment("bcd"))
.dataType(DataTypeUtil.simpleType(DataTypeEnums.BIGINT))
.build();
CreateTable before = CreateTable.builder()
.columns(ImmutableList.of(columnDefinition))
.tableName(QualifiedName.of("dim_shop")).build();

CreateTable after = CreateTable.builder()
.columns(ImmutableList.of(columnDefinition2))
.tableName(QualifiedName.of("dim_shop")).build();

List<BaseStatement> baseStatementList = columnCompare.compareTableElement(before, after);
assertEquals(1, baseStatementList.size());
String join = Joiner.on("\n").join(baseStatementList);
assertEquals(join, "ALTER TABLE dim_shop CHANGE COLUMN c1 c1 BIGINT COMMENT 'bcd'");
BaseStatement baseStatement = baseStatementList.get(0);
assertTrue(baseStatement instanceof ChangeCol);
ChangeCol changeCol = (ChangeCol)baseStatement;
boolean change = changeCol.change(ChangeType.COMMENT);
assertTrue(change);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public class ExpressionVisitor extends AstVisitor<String, Void> {

private static final ThreadLocal<DecimalFormat> DOUBLE_FORMATTER = ThreadLocal.withInitial(
() -> new DecimalFormat("0.###################E0###", new DecimalFormatSymbols(Locale.US)));
public static final String CURRENT_TIMESTAMP = "CURRENT_TIMESTAMP";

@Override
public String visitExpression(BaseExpression expression, Void context) {
Expand Down Expand Up @@ -687,7 +688,7 @@ public String visitCurrentDate(CurrentDate currentDate, Void context) {

@Override
public String visitCurrentTimestamp(CurrentTimestamp currentTimestamp, Void context) {
return "CURRENT_TIMESTAMP";
return CURRENT_TIMESTAMP;
}

@Override
Expand Down
Loading

0 comments on commit e532ab7

Please sign in to comment.