xingtanzjr commented on a change in pull request #5291:
URL: https://github.com/apache/iotdb/pull/5291#discussion_r830636789



##########
File path: 
server/src/main/java/org/apache/iotdb/db/mpp/sql/planner/plan/DistributionPlanner.java
##########
@@ -29,7 +39,214 @@ public DistributionPlanner(Analysis analysis, 
LogicalQueryPlan logicalPlan) {
     this.logicalPlan = logicalPlan;
   }
 
+  public PlanNode rewriteSource() {
+    SourceRewriter rewriter = new SourceRewriter();
+    return rewriter.visit(logicalPlan.getRootNode(), new 
DistributionPlanContext());
+  }
+
+  public PlanNode addExchangeNode(PlanNode root) {
+    ExchangeNodeAdder adder = new ExchangeNodeAdder();
+    return adder.visit(root, new NodeGroupContext());
+  }
+
   public DistributedQueryPlan planFragments() {
     return null;
   }
+
+  private class SourceRewriter extends 
SimplePlanNodeRewriter<DistributionPlanContext> {
+
+    //TODO: (xingtanzjr) implement the method visitDeviceMergeNode()
+    public PlanNode visitDeviceMerge(TimeJoinNode node, 
DistributionPlanContext context) {
+      return null;
+    }
+
+    public PlanNode visitTimeJoin(TimeJoinNode node, DistributionPlanContext 
context) {
+      TimeJoinNode root = (TimeJoinNode) node.clone();
+
+      // Step 1: Get all source nodes. For the node which is not source, add 
it as the child of
+      // current TimeJoinNode
+      List<SeriesScanNode> sources = new ArrayList<>();
+      for (PlanNode child : node.getChildren()) {
+        if (child instanceof SeriesScanNode) {
+          // If the child is SeriesScanNode, we need to check whether this 
node should be seperated
+          // into several splits.
+          SeriesScanNode handle = (SeriesScanNode) child;
+          Set<DataRegion> dataDistribution =
+              analysis.getPartitionInfo(handle.getSeriesPath(), 
handle.getTimeFilter());
+          // If the size of dataDistribution is m, this SeriesScanNode should 
be seperated into m
+          // SeriesScanNode.
+          for (DataRegion dataRegion : dataDistribution) {
+            SeriesScanNode split = (SeriesScanNode) handle.clone();
+            split.setDataRegion(dataRegion);
+            sources.add(split);
+          }
+        } else if (child instanceof SeriesAggregateScanNode) {
+          // TODO: (xingtanzjr) We should do the same thing for 
SeriesAggregateScanNode. Consider to
+          // make SeriesAggregateScanNode
+          // and SeriesScanNode to derived from the same parent Class because 
they have similar
+          // process logic in many scenarios
+        } else {
+          // In a general logical query plan, the children of TimeJoinNode 
should only be
+          // SeriesScanNode or SeriesAggregateScanNode
+          // So this branch should not be touched.
+          root.addChild(visit(child, context));
+        }
+      }
+
+      // Step 2: For the source nodes, group them by the DataRegion.
+      Map<DataRegion, List<SeriesScanNode>> sourceGroup = new HashMap<>();
+      sources.forEach(
+          source -> {
+            List<SeriesScanNode> group =
+                sourceGroup.containsKey(source.getDataRegion())
+                    ? sourceGroup.get(source.getDataRegion())
+                    : new ArrayList<>();
+            group.add(source);
+            sourceGroup.put(source.getDataRegion(), group);
+          });

Review comment:
       Great!




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