Skip to content

Commit e532ab7

Browse files
committed
merge: 0.5.10 change log
1 parent 1f12ce2 commit e532ab7

File tree

106 files changed

+4653
-497
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+4653
-497
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ deploy.sh
4646
/fastmodel-parser/src/test/resources/lsp/keyword.txt
4747
/fastmodel-transform/fastmodel-transform-adbmysql/src/main/antlr4/com/aliyun/fastmodel/transform/adbmysql/parser/gen/AdbMysqlLexer.java
4848
/fastmodel-transform/fastmodel-transform-mysql/src/main/antlr4/com/aliyun/fastmodel/transform/mysql/parser/gen/MySqlLexer.java
49+
gen

CHANGELOG

+18
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ Transformer
5959
向下兼容(compatibility)
6060
• 暂无
6161

62+
过期功能(deprecations)
63+
64+
安全漏洞修复(vulnerability)
65+
66+
67+
## 0.5.10
68+
新功能(new features)
69+
Core
70+
• 新增支持Column的Default表达式
71+
72+
73+
Transformer
74+
• Hologres建表支持Default表达式
75+
• StarRocks建表支持分区表达式
76+
77+
向下兼容(compatibility)
78+
• 暂无
79+
6280
过期功能(deprecations)
6381
6482
安全漏洞修复(vulnerability)

fastmodel-common/src/main/java/com/aliyun/fastmodel/common/parser/ParserHelper.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,14 @@ public static NodeLocation getLocation(TerminalNode terminalNode) {
124124
// ******** Helper ************
125125

126126
public static <T> Optional<T> visitIfPresent(AbstractParseTreeVisitor visitor, ParseTree context,
127-
Class<T> clazz) {
127+
Class<T> clazz) {
128128
return Optional.ofNullable(context)
129129
.map(visitor::visit)
130130
.map(clazz::cast);
131131
}
132132

133133
public static <T> List<T> visit(AbstractParseTreeVisitor visitor, List<? extends ParseTree> contexts,
134-
Class<T> clazz) {
134+
Class<T> clazz) {
135135
return contexts.stream()
136136
.map(visitor::visit)
137137
.filter(Objects::nonNull)

fastmodel-common/src/main/java/com/aliyun/fastmodel/common/utils/StripUtils.java

+8-29
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@
1616

1717
package com.aliyun.fastmodel.common.utils;
1818

19+
import com.aliyun.fastmodel.core.tree.util.StringLiteralUtil;
1920
import org.apache.commons.lang3.StringUtils;
2021

22+
import static com.aliyun.fastmodel.core.tree.util.StringLiteralUtil.DOUBLE_QUOTE;
23+
import static com.aliyun.fastmodel.core.tree.util.StringLiteralUtil.PREFIX;
24+
import static com.aliyun.fastmodel.core.tree.util.StringLiteralUtil.SINGLE;
25+
2126
/**
2227
* 去除字符串两边的引号内容
2328
*
@@ -27,9 +32,6 @@
2732
public class StripUtils {
2833

2934
public static final String SUFFIX = ";";
30-
public static final String SINGLE = "'";
31-
public static final String PREFIX = "`";
32-
public static final String DOUBLE_QUOTE = "\"";
3335

3436
/**
3537
* 将字符串坐下strip
@@ -38,42 +40,19 @@ public class StripUtils {
3840
* @return 处理后的字符串
3941
*/
4042
public static String strip(String src) {
41-
String prefix = SINGLE;
42-
if (src.startsWith(prefix)) {
43-
return stripOne(src, prefix);
44-
}
45-
prefix = DOUBLE_QUOTE;
46-
if (src.startsWith(prefix)) {
47-
return stripOne(src, prefix);
48-
}
49-
prefix = PREFIX;
50-
if (src.startsWith(prefix)) {
51-
return stripOne(src, prefix);
52-
}
53-
return src;
43+
return StringLiteralUtil.strip(src);
5444
}
5545

5646
/**
5747
* 将双引号去掉
48+
*
5849
* @param src
5950
* @return 去除之后的双引号
6051
*/
61-
public static String removeDoubleStrip(String src){
52+
public static String removeDoubleStrip(String src) {
6253
return StringUtils.remove(src, DOUBLE_QUOTE);
6354
}
6455

65-
private static String unquote(String value) {
66-
return value.substring(1, value.length() - 1)
67-
.replace("''", "'");
68-
}
69-
70-
private static String stripOne(String src, String prefix) {
71-
if (src.startsWith(prefix) && src.endsWith(prefix)) {
72-
return unquote(src);
73-
}
74-
return src;
75-
}
76-
7756
/**
7857
* 给string增加单引号
7958
*

fastmodel-compare/src/main/java/com/aliyun/fastmodel/compare/impl/table/ColumnCompare.java

+56-12
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@
2626
import java.util.function.BiFunction;
2727
import java.util.stream.Collectors;
2828

29-
import com.aliyun.fastmodel.compare.util.CompareUtil;
3029
import com.aliyun.fastmodel.compare.impl.table.column.OrderColumnManager;
30+
import com.aliyun.fastmodel.compare.util.CompareUtil;
3131
import com.aliyun.fastmodel.core.tree.BaseStatement;
3232
import com.aliyun.fastmodel.core.tree.Comment;
3333
import com.aliyun.fastmodel.core.tree.Property;
3434
import com.aliyun.fastmodel.core.tree.QualifiedName;
35+
import com.aliyun.fastmodel.core.tree.expr.BaseExpression;
3536
import com.aliyun.fastmodel.core.tree.expr.Identifier;
3637
import com.aliyun.fastmodel.core.tree.statement.constants.ColumnCategory;
3738
import com.aliyun.fastmodel.core.tree.statement.constants.ColumnPropertyDefaultKey;
@@ -108,11 +109,11 @@ public List<BaseStatement> compareTableElement(CreateTable before, CreateTable a
108109
}
109110

110111
protected Builder<BaseStatement> incrementCompare(QualifiedName qualifiedName,
111-
List<ColumnDefinition> beforeColumnDefines,
112-
List<ColumnDefinition> afterColumnDefines,
113-
BiFunction<QualifiedName, ColumnDefinition, BaseStatement> addDrop,
114-
BiFunction<QualifiedName, List<ColumnDefinition>,
115-
List<BaseStatement>> addCols) {
112+
List<ColumnDefinition> beforeColumnDefines,
113+
List<ColumnDefinition> afterColumnDefines,
114+
BiFunction<QualifiedName, ColumnDefinition, BaseStatement> addDrop,
115+
BiFunction<QualifiedName, List<ColumnDefinition>,
116+
List<BaseStatement>> addCols) {
116117
Builder<BaseStatement> builder = ImmutableList.builder();
117118
//如果两边的都有的话内容
118119
List<ColumnDefinition> afterCopy = Lists.newArrayList(afterColumnDefines);
@@ -199,18 +200,20 @@ private Map<String, ColumnDefinition> uuidMap(List<ColumnDefinition> beforeColum
199200
* @return
200201
*/
201202
private ChangeCol compare(QualifiedName qualifiedName, ColumnDefinition beforeDefine,
202-
ColumnDefinition afterDefine) {
203+
ColumnDefinition afterDefine) {
203204
boolean changeComment = isChangeColumnComment(beforeDefine, afterDefine);
204205
boolean changeAliased = !Objects.equals(beforeDefine.getAliasedName(), afterDefine.getAliasedName());
205206
boolean changePrimary = false;
206-
boolean changeColumn = !StringUtils.equalsIgnoreCase(beforeDefine.getColName().getValue(),
207+
boolean changeName = !StringUtils.equalsIgnoreCase(beforeDefine.getColName().getValue(),
207208
afterDefine.getColName().getValue());
208209
boolean changeNotNull = false;
209210
boolean changeType = !Objects.equals(beforeDefine.getDataType(), afterDefine.getDataType());
210211
boolean changeCategory = isChangeColumnCategory(beforeDefine, afterDefine);
211212
boolean changeDim = isChangeDim(beforeDefine, afterDefine);
212213
boolean changeRefIndicators = isChangeRefIndicaotrs(beforeDefine, afterDefine);
214+
boolean changeDefaultValue = isChangeDefaultValue(beforeDefine, afterDefine);
213215
List<Property> changeProperties = changeProperties(beforeDefine, afterDefine);
216+
boolean changeColumnProperties = !changeProperties.isEmpty();
214217
Comment comment = null;
215218
if (changeComment) {
216219
if (afterDefine.getComment() == null) {
@@ -241,9 +244,42 @@ private ChangeCol compare(QualifiedName qualifiedName, ColumnDefinition beforeDe
241244
changeNotNull = true;
242245
}
243246
}
247+
ChangeCol.ChangeState changeState = new ChangeCol.ChangeState();
248+
if (changeName) {
249+
changeState.setChangeName();
250+
}
251+
if (changeAliased) {
252+
changeState.setChangeAlias();
253+
}
254+
if (changePrimary) {
255+
changeState.setChangePrimary();
256+
}
257+
if (changeNotNull) {
258+
changeState.setChangeNotNull();
259+
}
260+
if (changeType) {
261+
changeState.setChangeDataType();
262+
}
263+
if (changeComment) {
264+
changeState.setChangeComment();
265+
}
266+
if (changeDefaultValue) {
267+
changeState.setChangeDefaultValue();
268+
}
269+
if (changeColumnProperties) {
270+
changeState.setChangeColumnProperties();
271+
}
272+
if (changeCategory) {
273+
changeState.setChangeCategory();
274+
}
275+
if (changeDim) {
276+
changeState.setChangeDim();
277+
}
278+
if (changeRefIndicators) {
279+
changeState.setChangeRefIndicators();
280+
}
244281
//如果都没有发生修改,那么直接返回null
245-
if (!changeComment && !changeAliased && !changePrimary && !changeNotNull && !changeColumn && !changeType
246-
&& !changeCategory && changeProperties.isEmpty() && !changeDim && !changeRefIndicators) {
282+
if (changeState.nothingChange()) {
247283
return null;
248284
}
249285
if (changeCategory) {
@@ -258,8 +294,16 @@ private ChangeCol compare(QualifiedName qualifiedName, ColumnDefinition beforeDe
258294
if (changeRefIndicators) {
259295
builder.refIndicators(afterDefine.getRefIndicators());
260296
}
261-
ChangeCol changeCol = new ChangeCol(qualifiedName, beforeDefine.getColName(), builder.build());
262-
return changeCol;
297+
if (changeDefaultValue) {
298+
builder.defaultValue(afterDefine.getDefaultValue());
299+
}
300+
return new ChangeCol(qualifiedName, beforeDefine.getColName(), builder.build(), Optional.of(changeState));
301+
}
302+
303+
private boolean isChangeDefaultValue(ColumnDefinition beforeDefine, ColumnDefinition afterDefine) {
304+
BaseExpression defaultValue = beforeDefine.getDefaultValue();
305+
BaseExpression afterDefaultValue = afterDefine.getDefaultValue();
306+
return !Objects.equals(defaultValue, afterDefaultValue);
263307
}
264308

265309
private boolean isChangeDim(ColumnDefinition beforeDefine, ColumnDefinition afterDefine) {

fastmodel-compare/src/test/java/com/aliyun/fastmodel/compare/table/ColumnCompareTest.java

+77-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import com.aliyun.fastmodel.compare.impl.table.ColumnCompare;
2424
import com.aliyun.fastmodel.core.tree.BaseStatement;
25+
import com.aliyun.fastmodel.core.tree.Comment;
2526
import com.aliyun.fastmodel.core.tree.Property;
2627
import com.aliyun.fastmodel.core.tree.QualifiedName;
2728
import com.aliyun.fastmodel.core.tree.datatype.BaseDataType;
@@ -30,8 +31,11 @@
3031
import com.aliyun.fastmodel.core.tree.datatype.GenericDataType;
3132
import com.aliyun.fastmodel.core.tree.datatype.TypeParameter;
3233
import com.aliyun.fastmodel.core.tree.expr.Identifier;
34+
import com.aliyun.fastmodel.core.tree.expr.literal.StringLiteral;
3335
import com.aliyun.fastmodel.core.tree.statement.constants.ColumnCategory;
3436
import com.aliyun.fastmodel.core.tree.statement.constants.ColumnPropertyDefaultKey;
37+
import com.aliyun.fastmodel.core.tree.statement.table.ChangeCol;
38+
import com.aliyun.fastmodel.core.tree.statement.table.ChangeCol.ChangeType;
3539
import com.aliyun.fastmodel.core.tree.statement.table.ColumnDefinition;
3640
import com.aliyun.fastmodel.core.tree.statement.table.CreateTable;
3741
import com.aliyun.fastmodel.core.tree.util.DataTypeUtil;
@@ -41,6 +45,8 @@
4145
import org.junit.Test;
4246

4347
import static org.junit.Assert.assertEquals;
48+
import static org.junit.Assert.assertFalse;
49+
import static org.junit.Assert.assertTrue;
4450

4551
/**
4652
* ColumnCompareTest
@@ -163,7 +169,7 @@ public void compareOrder() {
163169

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

169175
@Test
@@ -194,7 +200,6 @@ public void testCompareNode2() {
194200
.columns(beforeColumns)
195201
.tableName(QualifiedName.of("dim_shop")).build();
196202

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

212-
213217
private static ColumnDefinition getColumn(String colName) {
214218
return ColumnDefinition.builder().colName(new Identifier(colName))
215219
.dataType(DataTypeUtil.simpleType(DataTypeEnums.BIGINT))
@@ -344,5 +348,75 @@ public void testComparePropertiesBeforeNull() {
344348
assertEquals(1, baseStatementList.size());
345349
String join = Joiner.on("\n").join(baseStatementList);
346350
assertEquals(join, "ALTER TABLE dim_shop CHANGE COLUMN c1 c1 BIGINT WITH ('code_table'='a')");
351+
BaseStatement baseStatement = baseStatementList.get(0);
352+
ChangeCol changeCol = (ChangeCol)baseStatement;
353+
boolean change = changeCol.change(ChangeType.COLUMN_PROPERTIES);
354+
assertTrue(change);
355+
change = changeCol.change(ChangeType.COMMENT);
356+
assertFalse(change);
357+
}
358+
359+
@Test
360+
public void testCompareDefaultValue() {
361+
ColumnDefinition columnDefinition = ColumnDefinition.builder()
362+
.colName(new Identifier("c1"))
363+
.defaultValue(new StringLiteral("abc"))
364+
.dataType(DataTypeUtil.simpleType(DataTypeEnums.BIGINT))
365+
.build();
366+
ColumnDefinition columnDefinition2 = ColumnDefinition.builder()
367+
.colName(new Identifier("c1"))
368+
.dataType(DataTypeUtil.simpleType(DataTypeEnums.BIGINT))
369+
.defaultValue(new StringLiteral("bcd"))
370+
.build();
371+
CreateTable before = CreateTable.builder()
372+
.columns(ImmutableList.of(columnDefinition))
373+
.tableName(QualifiedName.of("dim_shop")).build();
374+
375+
CreateTable after = CreateTable.builder()
376+
.columns(ImmutableList.of(columnDefinition2))
377+
.tableName(QualifiedName.of("dim_shop")).build();
378+
379+
List<BaseStatement> baseStatementList = columnCompare.compareTableElement(before, after);
380+
assertEquals(1, baseStatementList.size());
381+
String join = Joiner.on("\n").join(baseStatementList);
382+
assertEquals(join, "ALTER TABLE dim_shop CHANGE COLUMN c1 c1 BIGINT DEFAULT 'bcd'");
383+
BaseStatement baseStatement = baseStatementList.get(0);
384+
assertTrue(baseStatement instanceof ChangeCol);
385+
ChangeCol changeCol = (ChangeCol)baseStatement;
386+
boolean change = changeCol.change(ChangeType.DEFAULT_VALUE);
387+
assertTrue(change);
388+
change = changeCol.change(ChangeType.COMMENT);
389+
assertFalse(change);
390+
}
391+
392+
@Test
393+
public void testCompareComment() {
394+
ColumnDefinition columnDefinition = ColumnDefinition.builder()
395+
.colName(new Identifier("c1"))
396+
.comment(new Comment("abc"))
397+
.dataType(DataTypeUtil.simpleType(DataTypeEnums.BIGINT))
398+
.build();
399+
ColumnDefinition columnDefinition2 = ColumnDefinition.builder()
400+
.colName(new Identifier("c1"))
401+
.comment(new Comment("bcd"))
402+
.dataType(DataTypeUtil.simpleType(DataTypeEnums.BIGINT))
403+
.build();
404+
CreateTable before = CreateTable.builder()
405+
.columns(ImmutableList.of(columnDefinition))
406+
.tableName(QualifiedName.of("dim_shop")).build();
407+
408+
CreateTable after = CreateTable.builder()
409+
.columns(ImmutableList.of(columnDefinition2))
410+
.tableName(QualifiedName.of("dim_shop")).build();
411+
412+
List<BaseStatement> baseStatementList = columnCompare.compareTableElement(before, after);
413+
assertEquals(1, baseStatementList.size());
414+
String join = Joiner.on("\n").join(baseStatementList);
415+
assertEquals(join, "ALTER TABLE dim_shop CHANGE COLUMN c1 c1 BIGINT COMMENT 'bcd'");
416+
BaseStatement baseStatement = baseStatementList.get(0);
417+
assertTrue(baseStatement instanceof ChangeCol);
418+
ChangeCol changeCol = (ChangeCol)baseStatement;
419+
boolean change = changeCol.change(ChangeType.COMMENT);
420+
assertTrue(change);
347421
}
348422
}

fastmodel-core/src/main/java/com/aliyun/fastmodel/core/formatter/ExpressionVisitor.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public class ExpressionVisitor extends AstVisitor<String, Void> {
107107

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

111112
@Override
112113
public String visitExpression(BaseExpression expression, Void context) {
@@ -687,7 +688,7 @@ public String visitCurrentDate(CurrentDate currentDate, Void context) {
687688

688689
@Override
689690
public String visitCurrentTimestamp(CurrentTimestamp currentTimestamp, Void context) {
690-
return "CURRENT_TIMESTAMP";
691+
return CURRENT_TIMESTAMP;
691692
}
692693

693694
@Override

0 commit comments

Comments
 (0)