You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2020/01/06 01:55:04 UTC

[GitHub] [incubator-shardingsphere] KomachiSion commented on a change in pull request #3868: Part of the shard key in multiple sharding policies can be null

KomachiSion commented on a change in pull request #3868: Part of the shard key in multiple sharding policies can be null
URL: https://github.com/apache/incubator-shardingsphere/pull/3868#discussion_r363133756
 
 

 ##########
 File path: sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/condition/engine/InsertClauseShardingConditionEngine.java
 ##########
 @@ -84,27 +84,39 @@ private ShardingCondition createShardingCondition(final String tableName, final
         for (ExpressionSegment each : insertValueContext.getValueExpressions()) {
             String columnName = columnNames.next();
             if (shardingRule.isShardingColumn(columnName, tableName)) {
-                if (each instanceof SimpleExpressionSegment) {
-                    result.getRouteValues().add(new ListRouteValue<>(columnName, tableName, Collections.singletonList(getRouteValue((SimpleExpressionSegment) each, parameters))));
-                } else if (ExpressionConditionUtils.isNowExpression(each)) {
-                    result.getRouteValues().add(new ListRouteValue<>(columnName, tableName, Collections.singletonList(timeService.getTime())));
+                Comparable<?> routeValue = getRouteValue(each, parameters, timeService);
+                if (null != routeValue) {
+                    result.getRouteValues().add(new ListRouteValue<>(columnName, tableName, Collections.singletonList(routeValue)));
                 }
             }
         }
         return result;
     }
-    
+
+    private Comparable<?> getRouteValue(final ExpressionSegment expressionSegment, final List<Object> parameters, final SPITimeService timeService) {
+        Comparable<?> routeValue = null;
+        if (expressionSegment instanceof SimpleExpressionSegment) {
+            routeValue = getRouteValue((SimpleExpressionSegment) expressionSegment, parameters);
+        } else if (ExpressionConditionUtils.isNowExpression(expressionSegment)) {
+            routeValue = timeService.getTime();
+        }
+        return routeValue;
+    }
+
     private Comparable<?> getRouteValue(final SimpleExpressionSegment expressionSegment, final List<Object> parameters) {
         Object result;
         if (expressionSegment instanceof ParameterMarkerExpressionSegment) {
             result = parameters.get(((ParameterMarkerExpressionSegment) expressionSegment).getParameterMarkerIndex());
         } else {
             result = ((LiteralExpressionSegment) expressionSegment).getLiterals();
         }
+        if (null == result) {
+            return null;
+        }
         Preconditions.checkArgument(result instanceof Comparable, "Sharding value must implements Comparable.");
         return (Comparable) result;
     }
-    
+
 
 Review comment:
   Please do not change the indents

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services