tuichenchuxin commented on code in PR #18006:
URL: https://github.com/apache/shardingsphere/pull/18006#discussion_r884370002


##########
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidator.java:
##########
@@ -35,11 +45,56 @@ public final class ShardingCreateViewStatementValidator 
extends ShardingDDLState
     @Override
     public void preValidate(final ShardingRule shardingRule, final 
SQLStatementContext<CreateViewStatement> sqlStatementContext,
                             final List<Object> parameters, final 
ShardingSphereDatabase database) {
-        // TODO prevent if has need merge select.
+        Optional<SelectStatement> selectStatement = 
sqlStatementContext.getSqlStatement().getSelect();
+        if (selectStatement.isPresent()) {
+            TableExtractor extractor = new TableExtractor();
+            extractor.extractTablesFromSelect(selectStatement.get());
+            Collection<SimpleTableSegment> tableSegments = 
extractor.getRewriteTables();
+            for (SimpleTableSegment each : tableSegments) {
+                String logicTable = 
each.getTableName().getIdentifier().getValue();
+                if (shardingRule.isShardingTable(logicTable)) {
+                    validateShardingTableAndViewIsBind(shardingRule, 
sqlStatementContext.getSqlStatement().getView().getTableName().getIdentifier().getValue(),
 logicTable);
+                }
+            }
+        }
     }
     
     @Override
     public void postValidate(final ShardingRule shardingRule, final 
SQLStatementContext<CreateViewStatement> sqlStatementContext, final 
List<Object> parameters,
                              final ShardingSphereDatabase database, final 
ConfigurationProperties props, final RouteContext routeContext) {
+        Optional<SelectStatement> selectStatement = 
sqlStatementContext.getSqlStatement().getSelect();
+        selectStatement.ifPresent(statement -> validateNeedsMerge(statement, 
routeContext));
+    }
+    
+    private void validateShardingTableAndViewIsBind(final ShardingRule 
shardingRule, final String logicViewName, final String logicTable) {
+        Collection<String> bindTables = Arrays.asList(logicTable, 
logicViewName);
+        if (shardingRule.isShardingTable(logicTable) && 
!shardingRule.isAllBindingTables(bindTables)) {
+            throw new ShardingSphereException("View name has to bind to 
sharding tables!");
+        }
+    }
+    
+    private void validateNeedsMerge(final SelectStatement selectStatement, 
final RouteContext routeContext) {
+        if (routeContext.getRouteUnits().size() <= 1) {
+            return;
+        }
+        if (hasGroupBy(selectStatement) || hasAggregation(selectStatement) || 
hasDistinct(selectStatement) || hasLimit(selectStatement)) {
+            throw new ShardingSphereException("Can not support creating view 
which has query that needs to be merged!");
+        }
+    }
+    
+    private boolean hasGroupBy(final SelectStatement selectStatement) {
+        return 
selectStatement.getGroupBy().map(GroupBySegment::getGroupByItems).filter(each 
-> !each.isEmpty()).isPresent();
+    }
+    
+    private boolean hasAggregation(final SelectStatement selectStatement) {
+        return 
selectStatement.getProjections().getProjections().stream().anyMatch(each -> 
each instanceof AggregationProjectionSegment);

Review Comment:
   Thank you. Done.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to