JackieTien97 commented on a change in pull request #5291: URL: https://github.com/apache/iotdb/pull/5291#discussion_r830626521
########## File path: server/src/main/java/org/apache/iotdb/db/mpp/common/DataRegion.java ########## @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iotdb.db.mpp.common; + +/** + * This class is used to represent the data partition info including the DataRegionId and physical + * node IP address + */ +// TODO: (xingtanzjr) This class should be substituted with the class defined in Consensus level +public class DataRegion { + private Integer dataRegionId; Review comment: `int` will be better, but it's fine to keep it as this way as it will be replaced with the class defined in Consensus level later ########## File path: server/src/main/java/org/apache/iotdb/db/mpp/sql/planner/plan/node/PlanNodeAllocator.java ########## @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iotdb.db.mpp.sql.planner.plan.node; + +public class PlanNodeAllocator { Review comment: ```suggestion public class PlanNodeIdAllocator { ``` ########## File path: server/src/main/java/org/apache/iotdb/db/mpp/execution/InstanceState.java ########## @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.iotdb.db.mpp.execution; + +import java.util.Set; +import java.util.stream.Stream; + +import static com.google.common.collect.ImmutableSet.toImmutableSet; + +public enum InstanceState { Review comment: Aha, I've already add `FragmentInstanceState` in this package, you can safely delete this class. ########## 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: ```suggestion Map<DataRegion, List<SeriesScanNode>> sourceGroup = sources.stream().collect(Collectors.groupingBy(SeriesScanNode::getDataRegion)); ``` ########## File path: server/src/main/java/org/apache/iotdb/db/mpp/sql/planner/plan/node/source/SeriesScanNode.java ########## @@ -19,14 +19,17 @@ package org.apache.iotdb.db.mpp.sql.planner.plan.node.source; import org.apache.iotdb.db.metadata.path.PartialPath; -import org.apache.iotdb.db.mpp.common.OrderBy; +import org.apache.iotdb.db.mpp.common.DataRegion; import org.apache.iotdb.db.mpp.sql.planner.plan.node.PlanNode; +import org.apache.iotdb.db.mpp.sql.planner.plan.node.PlanNodeAllocator; import org.apache.iotdb.db.mpp.sql.planner.plan.node.PlanNodeId; import org.apache.iotdb.db.mpp.sql.planner.plan.node.PlanVisitor; +import org.apache.iotdb.db.sql.statement.component.OrderBy; import org.apache.iotdb.tsfile.read.filter.basic.Filter; import com.google.common.collect.ImmutableList; +import javax.xml.crypto.Data; Review comment: ```suggestion ``` ########## 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); + }); + + // Step 3: For the source nodes which belong to same data region, add a TimeJoinNode for them + // and make the + // new TimeJoinNode as the child of current TimeJoinNode + sourceGroup.forEach( + (dataRegion, seriesScanNodes) -> { + if (seriesScanNodes.size() == 1) { + root.addChild(seriesScanNodes.get(0)); + } else { + // We clone a TimeJoinNode from root to make the params to be consistent + TimeJoinNode parentOfGroup = (TimeJoinNode) root.clone(); + seriesScanNodes.forEach(parentOfGroup::addChild); + root.addChild(parentOfGroup); + } + }); Review comment: What if all `seriesScanNodes.size()` > 1, we will have a duplicated TimeJoinNode. We may simply choose the first entry of `sourceGroup` to generate the root `TimeJoinNode` and then add others as its children. -- 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]
