Repository: apache/shardingsphere
Issue: insert with generated keys,when the last column value is null，cause NullPointerException

sharding-jdbc <version>4.0.0-RC1</version>

<sharding:key-generator id="xxxxxx" type="SNOWFLAKE" column="id" />
useGeneratedKeys，when insert sql the last column value is null,cause NullPointerException
when update the last column，success!
exception：

Caused by: java.lang.NullPointerException
	at org.apache.shardingsphere.core.route.type.standard.StandardRoutingEngine.isQualifiedInsertOptimizeResult(StandardRoutingEngine.java:221)
	at org.apache.shardingsphere.core.route.type.standard.StandardRoutingEngine.reviseInsertOptimizeResult(StandardRoutingEngine.java:211)
	at org.apache.shardingsphere.core.route.type.standard.StandardRoutingEngine.routeByShardingConditionsWithCondition(StandardRoutingEngine.java:110)
	at org.apache.shardingsphere.core.route.type.standard.StandardRoutingEngine.routeByShardingConditions(StandardRoutingEngine.java:102)
	at org.apache.shardingsphere.core.route.type.standard.StandardRoutingEngine.getDataNodes(StandardRoutingEngine.java:87)
	at org.apache.shardingsphere.core.route.type.standard.StandardRoutingEngine.route(StandardRoutingEngine.java:69)
	at org.apache.shardingsphere.core.route.router.sharding.ParsingSQLRouter.route(ParsingSQLRouter.java:106)
	at org.apache.shardingsphere.core.route.PreparedStatementRoutingEngine.route(PreparedStatementRoutingEngine.java:66)
	at org.apache.shardingsphere.core.PreparedQueryShardingEngine.route(PreparedQueryShardingEngine.java:60)
	at org.apache.shardingsphere.core.BaseShardingEngine.shard(BaseShardingEngine.java:64)
	at org.apache.shardingsphere.shardingjdbc.jdbc.core.statement.ShardingPreparedStatement.shard(ShardingPreparedStatement.java:224)
	at org.apache.shardingsphere.shardingjdbc.jdbc.core.statement.ShardingPreparedStatement.execute(ShardingPreparedStatement.java:170)
	at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:45)
	at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:73)
	at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:49)
	at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:115)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.apache.ibatis.plugin.Invocation.proceed(Invocation.java:49)
	at cn.uce.core.db.interceptor.BatchUpdateExecutorInterceptor.intercept(BatchUpdateExecutorInterceptor.java:79)
	at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61)
	at com.sun.proxy.$Proxy40.update(Unknown Source)
	at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:75)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63)
	at com.sun.proxy.$Proxy40.update(Unknown Source)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63)
	at com.sun.proxy.$Proxy40.update(Unknown Source)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:170)

Changed files:
sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/SQLRewriteEngineTest.java
sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/engine/encrypt/EncryptInsertOptimizeEngine.java
sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/result/insert/InsertOptimizeResultUnit.java
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/EncryptSQLRewriteEngine.java
sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/result/insert/InsertOptimizeResult.java
sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/result/InsertOptimizeResultTest.java
sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/result/insert/SetAssignmentOptimizeResult.java
sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/result/insert/ColumnValueOptimizeResult.java
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/SQLBuilder.java
sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/engine/sharding/insert/InsertOptimizeEngine.java

diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/engine/encrypt/EncryptInsertOptimizeEngine.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/engine/encrypt/EncryptInsertOptimizeEngine.java
index 886f9cc7424..f9f70c6e5ce 100644
--- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/engine/encrypt/EncryptInsertOptimizeEngine.java
+++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/engine/encrypt/EncryptInsertOptimizeEngine.java
@@ -61,7 +61,7 @@ public final class EncryptInsertOptimizeEngine implements OptimizeEngine {
             SQLExpression[] currentColumnValues = createCurrentColumnValues(each);
             Object[] currentParameters = createCurrentParameters(parametersCount, each);
             parametersCount = parametersCount + each.getParametersCount();
-            insertOptimizeResult.addUnit(currentColumnValues, currentParameters);
+            insertOptimizeResult.addUnit(currentColumnValues, currentParameters, each.getParametersCount());
             if (isNeededToAppendQueryAssistedColumn()) {
                 fillWithQueryAssistedColumn(insertOptimizeResult, insertOptimizeResultIndex);
             }
diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/engine/sharding/insert/InsertOptimizeEngine.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/engine/sharding/insert/InsertOptimizeEngine.java
index dbb95eca66f..78f712f4e9b 100644
--- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/engine/sharding/insert/InsertOptimizeEngine.java
+++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/engine/sharding/insert/InsertOptimizeEngine.java
@@ -77,7 +77,7 @@ public final class InsertOptimizeEngine implements OptimizeEngine {
             Object[] currentParameters = createCurrentParameters(parametersCount, insertValue);
             parametersCount = parametersCount + insertValue.getParametersCount();
             ShardingCondition shardingCondition = createShardingCondition(andConditions.get(i));
-            insertOptimizeResult.addUnit(currentColumnValues, currentParameters);
+            insertOptimizeResult.addUnit(currentColumnValues, currentParameters, insertValue.getParametersCount());
             if (isNeededToAppendGeneratedKey()) {
                 Comparable<?> currentGeneratedKey = generatedKeys.next();
                 fillWithGeneratedKeyName(insertOptimizeResult);
diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/result/insert/ColumnValueOptimizeResult.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/result/insert/ColumnValueOptimizeResult.java
index 3bc8fa7156c..0b2dbbe74c7 100644
--- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/result/insert/ColumnValueOptimizeResult.java
+++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/result/insert/ColumnValueOptimizeResult.java
@@ -29,8 +29,8 @@ import java.util.Collection;
  */
 public final class ColumnValueOptimizeResult extends InsertOptimizeResultUnit {
     
-    public ColumnValueOptimizeResult(final Collection<String> columnNames, final SQLExpression[] values, final Object[] parameters) {
-        super(columnNames, values, parameters);
+    public ColumnValueOptimizeResult(final Collection<String> columnNames, final SQLExpression[] values, final Object[] parameters, final int startIndexOfAppendedParameters) {
+        super(columnNames, values, parameters, startIndexOfAppendedParameters);
     }
     
     @Override
diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/result/insert/InsertOptimizeResult.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/result/insert/InsertOptimizeResult.java
index 4e6c6c92057..ff34c0accf4 100644
--- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/result/insert/InsertOptimizeResult.java
+++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/result/insert/InsertOptimizeResult.java
@@ -49,12 +49,13 @@ public final class InsertOptimizeResult {
      *
      * @param columnValues column values
      * @param columnParameters column parameters
+     * @param startIndexOfAppendedParameters start index of appended parameters
      */
-    public void addUnit(final SQLExpression[] columnValues, final Object[] columnParameters) {
+    public void addUnit(final SQLExpression[] columnValues, final Object[] columnParameters, final int startIndexOfAppendedParameters) {
         if (type == InsertType.VALUES) {
-            this.units.add(new ColumnValueOptimizeResult(columnNames, columnValues, columnParameters));
+            this.units.add(new ColumnValueOptimizeResult(columnNames, columnValues, columnParameters, startIndexOfAppendedParameters));
         } else {
-            this.units.add(new SetAssignmentOptimizeResult(columnNames, columnValues, columnParameters));
+            this.units.add(new SetAssignmentOptimizeResult(columnNames, columnValues, columnParameters, startIndexOfAppendedParameters));
         }
     }
 }
diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/result/insert/InsertOptimizeResultUnit.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/result/insert/InsertOptimizeResultUnit.java
index 6b03e7de6d7..6f5f8ea7c6e 100644
--- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/result/insert/InsertOptimizeResultUnit.java
+++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/result/insert/InsertOptimizeResultUnit.java
@@ -48,6 +48,8 @@ public abstract class InsertOptimizeResultUnit {
     
     private final Object[] parameters;
     
+    private final int startIndexOfAppendedParameters;
+    
     private final List<DataNode> dataNodes = new LinkedList<>();
     
     /**
@@ -56,26 +58,25 @@ public abstract class InsertOptimizeResultUnit {
      * @param sqlExpression SQL expression
      */
     public final void addColumnValue(final SQLExpression sqlExpression) {
-        values[getCurrentIndex(values)] = sqlExpression;
+        values[getCurrentIndex(values, 0)] = sqlExpression;
     }
     
     /**
      * Add column parameter.
      *
-     * @param parameter parameter 
+     * @param parameter parameter
      */
     public final void addColumnParameter(final Object parameter) {
-        parameters[getCurrentIndex(parameters)] = parameter;
+        parameters[getCurrentIndex(parameters, startIndexOfAppendedParameters)] = parameter;
     }
     
-    private int getCurrentIndex(final Object[] array) {
-        int count = 0;
-        for (Object each : array) {
-            if (null != each) {
-                count++;
+    private int getCurrentIndex(final Object[] array, final int startIndex) {
+        for (int i = startIndex; i < array.length; i++) {
+            if (null == array[i]) {
+                return i;
             }
         }
-        return count;
+        throw new ShardingException("Index Out Of Bounds For InsertOptimizeResultUnit.");
     }
     
     /**
diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/result/insert/SetAssignmentOptimizeResult.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/result/insert/SetAssignmentOptimizeResult.java
index bba0a7163fc..7ddf5b26189 100644
--- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/result/insert/SetAssignmentOptimizeResult.java
+++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/result/insert/SetAssignmentOptimizeResult.java
@@ -30,8 +30,8 @@ import java.util.Collection;
  */
 public final class SetAssignmentOptimizeResult extends InsertOptimizeResultUnit {
     
-    public SetAssignmentOptimizeResult(final Collection<String> columnNames, final SQLExpression[] values, final Object[] parameters) {
-        super(columnNames, values, parameters);
+    public SetAssignmentOptimizeResult(final Collection<String> columnNames, final SQLExpression[] values, final Object[] parameters, final int startIndexOfAppendedParameters) {
+        super(columnNames, values, parameters, startIndexOfAppendedParameters);
     }
     
     @Override
diff --git a/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/result/InsertOptimizeResultTest.java b/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/result/InsertOptimizeResultTest.java
index 62b5a867ac0..cc88f872124 100644
--- a/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/result/InsertOptimizeResultTest.java
+++ b/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/result/InsertOptimizeResultTest.java
@@ -39,7 +39,7 @@ public final class InsertOptimizeResultTest {
     public void assertAddUnitWithSet() {
         SQLExpression[] expressions = {new SQLNumberExpression(1), new SQLParameterMarkerExpression(1), new SQLTextExpression("test")};
         Object[] parameters = {"parameter"};
-        setInsertOptimizeResult.addUnit(expressions, parameters);
+        setInsertOptimizeResult.addUnit(expressions, parameters, 1);
         assertThat(setInsertOptimizeResult.getColumnNames().size(), is(3));
         assertThat(setInsertOptimizeResult.getType(), is(InsertType.SET));
         assertThat(setInsertOptimizeResult.getUnits().get(0).getValues(), is(expressions));
@@ -58,7 +58,7 @@ public final class InsertOptimizeResultTest {
     public void assertAddUnitWithValues() {
         SQLExpression[] expressions = {new SQLNumberExpression(1), new SQLParameterMarkerExpression(1), new SQLTextExpression("test")};
         Object[] parameters = {"parameter"};
-        valuesInsertOptimizeResult.addUnit(expressions, parameters);
+        valuesInsertOptimizeResult.addUnit(expressions, parameters, 1);
         assertThat(valuesInsertOptimizeResult.getUnits().get(0).toString(), is("(1, ?, 'test')"));
     }
 }
diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/EncryptSQLRewriteEngine.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/EncryptSQLRewriteEngine.java
index 523021e1f9a..e5f225e2c58 100644
--- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/EncryptSQLRewriteEngine.java
+++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/EncryptSQLRewriteEngine.java
@@ -48,6 +48,7 @@ import org.apache.shardingsphere.core.rewrite.placeholder.EncryptUpdateItemColum
 import org.apache.shardingsphere.core.rewrite.placeholder.EncryptWhereColumnPlaceholder;
 import org.apache.shardingsphere.core.rewrite.placeholder.InsertSetPlaceholder;
 import org.apache.shardingsphere.core.rewrite.placeholder.InsertValuesPlaceholder;
+import org.apache.shardingsphere.core.rewrite.placeholder.SelectItemsPlaceholder;
 import org.apache.shardingsphere.core.rewrite.placeholder.ShardingPlaceholder;
 import org.apache.shardingsphere.core.rule.EncryptRule;
 import org.apache.shardingsphere.spi.encrypt.ShardingEncryptor;
@@ -133,11 +134,11 @@ public final class EncryptSQLRewriteEngine {
         sqlBuilder.appendLiterals(originalSQL.substring(0, sqlTokens.get(0).getStartIndex()));
         for (SQLToken each : sqlTokens) {
             if (each instanceof SelectItemsToken) {
-                appendItemsToken(sqlBuilder, (SelectItemsToken) each, count);
+                appendSelectItemsPlaceholder(sqlBuilder, (SelectItemsToken) each, count);
             } else if (each instanceof InsertValuesToken) {
-                appendInsertValuesToken(sqlBuilder, (InsertValuesToken) each, count, optimizeResult.getInsertOptimizeResult().get());
+                appendInsertValuesPlaceholder(sqlBuilder, (InsertValuesToken) each, count, optimizeResult.getInsertOptimizeResult().get());
             } else if (each instanceof InsertSetToken) {
-                appendInsertSetToken(sqlBuilder, (InsertSetToken) each, count, optimizeResult.getInsertOptimizeResult().get());
+                appendInsertSetPlaceholder(sqlBuilder, (InsertSetToken) each, count, optimizeResult.getInsertOptimizeResult().get());
             } else if (each instanceof EncryptColumnToken) {
                 appendEncryptColumnPlaceholder(sqlBuilder, (EncryptColumnToken) each, count);
             } else if (each instanceof RemoveToken) {
@@ -147,22 +148,22 @@ public final class EncryptSQLRewriteEngine {
         }
     }
     
-    private void appendItemsToken(final SQLBuilder sqlBuilder, final SelectItemsToken selectItemsToken, final int count) {
-        if (!(sqlStatement instanceof InsertStatement)) {
-            return;
-        }
-        for (int i = 0; i < selectItemsToken.getItems().size(); i++) {
-            if (selectItemsToken.isFirstOfItemsSpecial() && 0 == i) {
-                sqlBuilder.appendLiterals(SQLUtil.getOriginalValue(selectItemsToken.getItems().get(i), databaseType));
-            } else {
-                sqlBuilder.appendLiterals(", ");
-                sqlBuilder.appendLiterals(SQLUtil.getOriginalValue(selectItemsToken.getItems().get(i), databaseType));
-            }
+    private void appendSelectItemsPlaceholder(final SQLBuilder sqlBuilder, final SelectItemsToken selectItemsToken, final int count) {
+        if (sqlStatement instanceof InsertStatement) {
+            SelectItemsPlaceholder selectItemsPlaceholder = new SelectItemsPlaceholder(selectItemsToken.isFirstOfItemsSpecial());
+            selectItemsPlaceholder.getItems().addAll(Lists.transform(selectItemsToken.getItems(), new Function<String, String>() {
+        
+                @Override
+                public String apply(final String input) {
+                    return SQLUtil.getOriginalValue(input, databaseType);
+                }
+            }));
+            sqlBuilder.appendPlaceholder(selectItemsPlaceholder);
         }
         appendRest(sqlBuilder, count, getStopIndex(selectItemsToken));
     }
     
-    private void appendInsertValuesToken(final SQLBuilder sqlBuilder, final InsertValuesToken insertValuesToken, final int count, final InsertOptimizeResult insertOptimizeResult) {
+    private void appendInsertValuesPlaceholder(final SQLBuilder sqlBuilder, final InsertValuesToken insertValuesToken, final int count, final InsertOptimizeResult insertOptimizeResult) {
         for (InsertOptimizeResultUnit each : insertOptimizeResult.getUnits()) {
             encryptInsertOptimizeResult(insertOptimizeResult.getColumnNames(), each);
         }
@@ -171,7 +172,7 @@ public final class EncryptSQLRewriteEngine {
         appendRest(sqlBuilder, count, getStopIndex(insertValuesToken));
     }
     
-    private void appendInsertSetToken(final SQLBuilder sqlBuilder, final InsertSetToken insertSetToken, final int count, final InsertOptimizeResult insertOptimizeResult) {
+    private void appendInsertSetPlaceholder(final SQLBuilder sqlBuilder, final InsertSetToken insertSetToken, final int count, final InsertOptimizeResult insertOptimizeResult) {
         for (InsertOptimizeResultUnit each : insertOptimizeResult.getUnits()) {
             encryptInsertOptimizeResult(insertOptimizeResult.getColumnNames(), each);
         }
diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/SQLBuilder.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/SQLBuilder.java
index 8b2717964eb..c7fd883b2cd 100644
--- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/SQLBuilder.java
+++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/SQLBuilder.java
@@ -176,15 +176,15 @@ public final class SQLBuilder {
     private void appendIndexPlaceholder(final IndexPlaceholder placeholder, final String actualTableName, final StringBuilder stringBuilder) {
         stringBuilder.append(placeholder.getLogicIndexName());
         if (!Strings.isNullOrEmpty(actualTableName)) {
-            if ('`' == stringBuilder.charAt(stringBuilder.length() - 1)) {
-        		stringBuilder.deleteCharAt(stringBuilder.length() - 1);
+            if ('`' == stringBuilder.charAt(stringBuilder.length() - 1)) { 
+                stringBuilder.deleteCharAt(stringBuilder.length() - 1);
                 stringBuilder.append("_");
                 stringBuilder.append(actualTableName);
-                stringBuilder.append("`");
-        	} else {
-        		stringBuilder.append("_");
-                stringBuilder.append(actualTableName);
-        	}
+                stringBuilder.append("`"); 
+            } else { 
+                stringBuilder.append("_");
+                stringBuilder.append(actualTableName); 
+            }
         }
     }
     
diff --git a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/SQLRewriteEngineTest.java b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/SQLRewriteEngineTest.java
index a40730a942e..0d6c1779d1a 100644
--- a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/SQLRewriteEngineTest.java
+++ b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/SQLRewriteEngineTest.java
@@ -186,7 +186,7 @@ public final class SQLRewriteEngineTest {
         InsertOptimizeResult insertOptimizeResult = new InsertOptimizeResult(InsertType.VALUES, Arrays.asList("name", "age", "id"));
         Object[] parameters = {"x", 1, 1};
         SQLExpression[] sqlExpressions = {new SQLParameterMarkerExpression(0), new SQLParameterMarkerExpression(1), new SQLParameterMarkerExpression(2)};
-        insertOptimizeResult.addUnit(sqlExpressions, parameters);
+        insertOptimizeResult.addUnit(sqlExpressions, parameters, 3);
         insertOptimizeResult.getUnits().get(0).getDataNodes().add(new DataNode("db0.table_1"));
         TableUnit tableUnit = new TableUnit("db0");
         tableUnit.getRoutingTables().add(new RoutingTable("table_x", "table_1"));
@@ -207,7 +207,7 @@ public final class SQLRewriteEngineTest {
         InsertOptimizeResult insertOptimizeResult = new InsertOptimizeResult(InsertType.VALUES, Arrays.asList("name", "id"));
         Object[] parameters = {"Bill", 1};
         SQLExpression[] sqlExpressions = {new SQLParameterMarkerExpression(0), new SQLParameterMarkerExpression(1)};
-        insertOptimizeResult.addUnit(sqlExpressions, parameters);
+        insertOptimizeResult.addUnit(sqlExpressions, parameters, 2);
         insertOptimizeResult.getUnits().get(0).getDataNodes().add(new DataNode("db0.table_1"));
         TableUnit tableUnit = new TableUnit("db0");
         tableUnit.getRoutingTables().add(new RoutingTable("table_x", "table_1"));
@@ -226,7 +226,7 @@ public final class SQLRewriteEngineTest {
         insertStatement.addSQLToken(new InsertValuesToken(21, 32));
         InsertOptimizeResult insertOptimizeResult = new InsertOptimizeResult(InsertType.VALUES, Arrays.asList("name", "id"));
         SQLExpression[] sqlExpressions = {new SQLNumberExpression(10), new SQLNumberExpression(1)};
-        insertOptimizeResult.addUnit(sqlExpressions, new Object[0]);
+        insertOptimizeResult.addUnit(sqlExpressions, new Object[0], 0);
         insertOptimizeResult.getUnits().get(0).getDataNodes().add(new DataNode("db0.table_1"));
         TableUnit tableUnit = new TableUnit("db0");
         tableUnit.getRoutingTables().add(new RoutingTable("table_x", "table_1"));
@@ -248,7 +248,7 @@ public final class SQLRewriteEngineTest {
         insertStatement.addSQLToken(new InsertValuesToken(21, 35));
         InsertOptimizeResult insertOptimizeResult = new InsertOptimizeResult(InsertType.VALUES, Arrays.asList("name", "id"));
         SQLExpression[] sqlExpressions = {new SQLNumberExpression(10), new SQLNumberExpression(1)};
-        insertOptimizeResult.addUnit(sqlExpressions, new Object[0]);
+        insertOptimizeResult.addUnit(sqlExpressions, new Object[0], 0);
         insertOptimizeResult.getUnits().get(0).getDataNodes().add(new DataNode("db0.table_1"));
         TableUnit tableUnit = new TableUnit("db0");
         tableUnit.getRoutingTables().add(new RoutingTable("table_x", "table_1"));
@@ -268,7 +268,7 @@ public final class SQLRewriteEngineTest {
         InsertOptimizeResult insertOptimizeResult = new InsertOptimizeResult(InsertType.VALUES, Arrays.asList("name", "id"));
         SQLExpression[] sqlExpressions = {new SQLParameterMarkerExpression(0), new SQLParameterMarkerExpression(1)};
         Object[] parameters = {"x", 1};
-        insertOptimizeResult.addUnit(sqlExpressions, parameters);
+        insertOptimizeResult.addUnit(sqlExpressions, parameters, 2);
         insertOptimizeResult.getUnits().get(0).getDataNodes().add(new DataNode("db0.table_1"));
         TableUnit tableUnit = new TableUnit("db0");
         tableUnit.getRoutingTables().add(new RoutingTable("table_x", "table_1"));