Skip to content

Commit 211c3e5

Browse files
PanternBaoyanhuqing666
authored andcommitted
throw error when increment column doesn't exist and fix additional ; (#1510)
1 parent 8ea7e5a commit 211c3e5

File tree

6 files changed

+40
-62
lines changed

6 files changed

+40
-62
lines changed

src/main/java/com/actiontech/dble/manager/dump/DumpFileContext.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ public void setSchema(String schema) {
4949
this.schema = schema;
5050
}
5151

52-
public void skipCurrentContext() {
53-
this.isSkip = true;
52+
public void setSkipContext(boolean skip) {
53+
this.isSkip = skip;
5454
}
5555

56-
public boolean isSkip() {
56+
public boolean isSkipContext() {
5757
return this.isSkip;
5858
}
5959

@@ -93,6 +93,9 @@ public void setTable(String table) throws DumpException {
9393
if (this.tableConfig == null && this.defaultDataNode == null) {
9494
throw new DumpException("schema " + schema + " has no default node.");
9595
}
96+
if (this.tableConfig != null && this.tableConfig.getParentTC() != null) {
97+
throw new DumpException("can't process child table, skip.");
98+
}
9699
}
97100

98101
public boolean isPushDown() {

src/main/java/com/actiontech/dble/manager/dump/DumpFileExecutor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ public void run() {
7474
if (ServerParse.DDL == type || ServerParse.CREATE_DATABASE == type || ServerParse.USE == (0xff & type)) {
7575
stmt = stmt.replace("/*!", "/*#");
7676
statement = RouteStrategyFactory.getRouteStrategy().parserSQL(stmt);
77+
context.setSkipContext(false);
7778
}
7879
// if ddl is wrong,the following statement is skip.
79-
if (context.isSkip()) {
80+
if (context.isSkipContext()) {
8081
continue;
8182
}
8283
if (ServerParse.INSERT == type && !context.isPushDown()) {
@@ -89,7 +90,7 @@ public void run() {
8990
handler.handle(context, statement);
9091
} catch (DumpException | SQLSyntaxErrorException e) {
9192
String currentStmt = context.getStmt().length() <= 1024 ? context.getStmt() : context.getStmt().substring(0, 1024);
92-
context.skipCurrentContext();
93+
context.setSkipContext(true);
9394
LOGGER.warn("current stmt[" + currentStmt + "] error,because:" + e.getMessage());
9495
context.addError("current stmt[" + currentStmt + "] error,because:" + e.getMessage());
9596
} catch (InterruptedException ie) {
@@ -119,7 +120,7 @@ private boolean preHandle(DumpFileWriter writer, int type, String stmt) throws I
119120
}
120121
// skip view
121122
if ((ServerParse.MYSQL_CMD_COMMENT == type || ServerParse.MYSQL_COMMENT == type) && skipView(stmt)) {
122-
context.skipCurrentContext();
123+
context.setSkipContext(true);
123124
return true;
124125
}
125126
// footer

src/main/java/com/actiontech/dble/manager/dump/handler/CreateTableHandler.java

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import com.actiontech.dble.util.StringUtil;
88
import com.alibaba.druid.sql.SQLUtils;
99
import com.alibaba.druid.sql.ast.SQLStatement;
10-
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
11-
import com.alibaba.druid.sql.ast.expr.SQLNullExpr;
1210
import com.alibaba.druid.sql.ast.statement.SQLCharacterDataType;
1311
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
1412
import com.alibaba.druid.sql.ast.statement.SQLTableElement;
@@ -34,34 +32,28 @@ public boolean preHandle(DumpFileContext context, SQLStatement sqlStatement) thr
3432
checkColumns(context, columns);
3533
// partition column check
3634
if (tableConfig.getPartitionColumn() != null && context.getPartitionColumnIndex() == -1) {
37-
throw new DumpException("table[" + context.getTable() + "] can't find partition column in create.");
35+
throw new DumpException("can't find partition column in create.");
36+
}
37+
// increment column check
38+
if (tableConfig.isAutoIncrement() && context.getIncrementColumnIndex() == -1) {
39+
throw new DumpException("can't find increment column in create.");
3840
}
3941
}
4042
return false;
4143
}
4244

4345
@Override
44-
public void handle(DumpFileContext context, SQLStatement sqlStatement) throws InterruptedException {
46+
public void handle(DumpFileContext context, SQLStatement sqlStatement) throws DumpException, InterruptedException {
4547
boolean isChanged = false;
4648
List<SQLTableElement> columns = ((MySqlCreateTableStatement) sqlStatement).getTableElementList();
4749
TableConfig tableConfig = context.getTableConfig();
48-
if (tableConfig.isAutoIncrement()) {
49-
// add increment column if not exists
50-
if (context.getIncrementColumnIndex() == -1) {
51-
SQLColumnDefinition column = new SQLColumnDefinition();
50+
if (context.getIncrementColumnIndex() != -1) {
51+
// check data type of increment column
52+
SQLColumnDefinition column = (SQLColumnDefinition) columns.get(context.getIncrementColumnIndex());
53+
if (!column.getDataType().getName().equals("bigint")) {
54+
context.addError("data type of increment column isn't bigint, dble replaced it by itself.");
5255
column.setDataType(new SQLCharacterDataType("bigint"));
53-
column.setDefaultExpr(new SQLNullExpr());
54-
column.setName(new SQLIdentifierExpr(tableConfig.getTrueIncrementColumn()));
55-
columns.add(column);
56-
context.setPartitionColumnIndex(columns.size());
5756
isChanged = true;
58-
} else {
59-
SQLColumnDefinition column = (SQLColumnDefinition) columns.get(context.getIncrementColumnIndex());
60-
if (!column.getDataType().getName().equals("bigint")) {
61-
context.addError("data type of increment column isn't bigint, dble replaced it by itself.");
62-
column.setDataType(new SQLCharacterDataType("bigint"));
63-
isChanged = true;
64-
}
6557
}
6658
}
6759

src/main/java/com/actiontech/dble/manager/dump/handler/GlobalTableInsertHandler.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,10 @@ public class GlobalTableInsertHandler extends InsertHandler {
1111

1212
private long time = new Date().getTime();
1313

14-
@Override
15-
public void preProcess(DumpFileContext context) throws InterruptedException {
16-
if (insertHeader == null) {
17-
return;
18-
}
19-
for (String dataNode : context.getTableConfig().getDataNodes()) {
20-
context.getWriter().write(dataNode, insertHeader.toString(), true, false);
21-
}
22-
}
23-
2414
@Override
2515
public void process(DumpFileContext context, SQLInsertStatement.ValuesClause valueClause, boolean isFirst) throws InterruptedException, SQLNonTransientException {
2616
valueClause.addValue(new SQLIntegerExpr(time));
2717
super.process(context, valueClause, isFirst);
2818
}
2919

30-
@Override
31-
public void postProcess(DumpFileContext context) throws InterruptedException {
32-
for (String dataNode : context.getTableConfig().getDataNodes()) {
33-
context.getWriter().write(dataNode, ";", false, false);
34-
}
35-
super.postProcess(context);
36-
}
3720
}

src/main/java/com/actiontech/dble/manager/dump/handler/InsertHandler.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.alibaba.druid.sql.SQLUtils;
1010
import com.alibaba.druid.sql.ast.SQLExpr;
1111
import com.alibaba.druid.sql.ast.SQLStatement;
12-
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
1312
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
1413
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
1514
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement;
@@ -22,7 +21,7 @@ public class InsertHandler extends DefaultHandler {
2221
protected StringBuilder insertHeader;
2322

2423
@Override
25-
public boolean preHandle(DumpFileContext context, SQLStatement sqlStatement) throws DumpException, InterruptedException {
24+
public boolean preHandle(DumpFileContext context, SQLStatement sqlStatement) throws DumpException {
2625
MySqlInsertStatement insert = (MySqlInsertStatement) sqlStatement;
2726
// check columns from insert columns
2827
checkColumns(context, insert.getColumns());
@@ -64,11 +63,6 @@ private void handleIncrementColumn(DumpFileContext context, List<SQLExpr> values
6463

6564
String tableKey = StringUtil.getFullName(context.getSchema(), context.getTable());
6665
long val = SequenceManager.getHandler().nextId(tableKey);
67-
if (incrementIndex == values.size()) {
68-
values.add(new SQLIntegerExpr(val));
69-
return;
70-
}
71-
7266
SQLExpr value = values.get(incrementIndex);
7367
if (!StringUtil.isEmpty(SQLUtils.toMySqlString(value)) && !context.isNeedSkipError()) {
7468
context.addError("For table using global sequence, dble has set increment column values for you.");
@@ -96,22 +90,17 @@ private void checkColumns(DumpFileContext context, List<SQLExpr> columns) throws
9690
partitionColumnIndex = i;
9791
}
9892
}
99-
if (tableConfig.isAutoIncrement() && incrementColumnIndex == -1) {
100-
// add increment column
101-
columns.add(new SQLCharExpr(tableConfig.getTrueIncrementColumn()));
102-
incrementColumnIndex = columns.size();
103-
// if increment column is same with partition column
104-
if (tableConfig.getTrueIncrementColumn().equalsIgnoreCase(tableConfig.getPartitionColumn())) {
105-
partitionColumnIndex = incrementColumnIndex;
106-
}
107-
}
108-
93+
// partition column check
10994
if (tableConfig.getPartitionColumn() != null && partitionColumnIndex == -1) {
11095
throw new DumpException("can't find partition column in insert.");
11196
}
97+
// increment column check
98+
if (tableConfig.isAutoIncrement() && incrementColumnIndex == -1) {
99+
throw new DumpException("can't find increment column in insert.");
100+
}
101+
context.setIncrementColumnIndex(incrementColumnIndex);
102+
context.setPartitionColumnIndex(partitionColumnIndex);
112103
}
113-
context.setIncrementColumnIndex(incrementColumnIndex);
114-
context.setPartitionColumnIndex(partitionColumnIndex);
115104
}
116105

117106
protected String toString(List<SQLExpr> values, boolean isFirst) {
@@ -131,9 +120,18 @@ protected String toString(List<SQLExpr> values, boolean isFirst) {
131120
}
132121

133122
public void preProcess(DumpFileContext context) throws InterruptedException {
123+
if (insertHeader == null) {
124+
return;
125+
}
126+
for (String dataNode : context.getTableConfig().getDataNodes()) {
127+
context.getWriter().write(dataNode, insertHeader.toString(), true, false);
128+
}
134129
}
135130

136131
public void postProcess(DumpFileContext context) throws InterruptedException {
132+
for (String dataNode : context.getTableConfig().getDataNodes()) {
133+
context.getWriter().write(dataNode, ";", false, false);
134+
}
137135
insertHeader = null;
138136
}
139137

src/main/java/com/actiontech/dble/manager/dump/handler/ShardingTableInsertHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class ShardingTableInsertHandler extends InsertHandler {
1818

1919
private Map<String, LongPtr> dataNodes = new HashMap<>(64);
2020

21+
@Override
2122
public void preProcess(DumpFileContext context) {
2223
if (!dataNodes.isEmpty()) {
2324
dataNodes.clear();
@@ -29,7 +30,7 @@ public void postProcess(DumpFileContext context) throws InterruptedException {
2930
for (String dataNode : dataNodes.keySet()) {
3031
context.getWriter().write(dataNode, ";", false, false);
3132
}
32-
super.postProcess(context);
33+
insertHeader = null;
3334
}
3435

3536
@Override

0 commit comments

Comments
 (0)