|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iotdb.db.queryengine.plan.relational.analyzer; |
| 21 | + |
| 22 | +import org.apache.iotdb.db.queryengine.plan.planner.plan.DistributedQueryPlan; |
| 23 | +import org.apache.iotdb.db.queryengine.plan.planner.plan.LogicalQueryPlan; |
| 24 | +import org.apache.iotdb.db.queryengine.plan.planner.plan.node.PlanNode; |
| 25 | +import org.apache.iotdb.db.queryengine.plan.relational.planner.SymbolAllocator; |
| 26 | +import org.apache.iotdb.db.queryengine.plan.relational.planner.TableLogicalPlanner; |
| 27 | +import org.apache.iotdb.db.queryengine.plan.relational.planner.assertions.PlanMatchPattern; |
| 28 | +import org.apache.iotdb.db.queryengine.plan.relational.planner.distribute.TableDistributedPlanner; |
| 29 | +import org.apache.iotdb.db.queryengine.plan.relational.planner.node.AggregationNode; |
| 30 | +import org.apache.iotdb.db.queryengine.plan.relational.planner.node.OutputNode; |
| 31 | + |
| 32 | +import com.google.common.collect.ImmutableList; |
| 33 | +import com.google.common.collect.ImmutableMap; |
| 34 | +import org.junit.Test; |
| 35 | + |
| 36 | +import java.util.Optional; |
| 37 | + |
| 38 | +import static org.apache.iotdb.db.queryengine.plan.relational.analyzer.AnalyzerTest.analyzeSQL; |
| 39 | +import static org.apache.iotdb.db.queryengine.plan.relational.analyzer.TestUtils.DEFAULT_WARNING; |
| 40 | +import static org.apache.iotdb.db.queryengine.plan.relational.analyzer.TestUtils.QUERY_CONTEXT; |
| 41 | +import static org.apache.iotdb.db.queryengine.plan.relational.analyzer.TestUtils.SESSION_INFO; |
| 42 | +import static org.apache.iotdb.db.queryengine.plan.relational.analyzer.TestUtils.TEST_MATADATA; |
| 43 | +import static org.apache.iotdb.db.queryengine.plan.relational.planner.assertions.PlanAssert.assertPlan; |
| 44 | +import static org.apache.iotdb.db.queryengine.plan.relational.planner.assertions.PlanMatchPattern.aggregation; |
| 45 | +import static org.apache.iotdb.db.queryengine.plan.relational.planner.assertions.PlanMatchPattern.aggregationFunction; |
| 46 | +import static org.apache.iotdb.db.queryengine.plan.relational.planner.assertions.PlanMatchPattern.collect; |
| 47 | +import static org.apache.iotdb.db.queryengine.plan.relational.planner.assertions.PlanMatchPattern.exchange; |
| 48 | +import static org.apache.iotdb.db.queryengine.plan.relational.planner.assertions.PlanMatchPattern.output; |
| 49 | +import static org.apache.iotdb.db.queryengine.plan.relational.planner.assertions.PlanMatchPattern.project; |
| 50 | +import static org.apache.iotdb.db.queryengine.plan.relational.planner.assertions.PlanMatchPattern.singleGroupingSet; |
| 51 | +import static org.apache.iotdb.db.queryengine.plan.relational.planner.assertions.PlanMatchPattern.tableScan; |
| 52 | +import static org.apache.iotdb.db.queryengine.plan.relational.planner.assertions.PlanMatchPattern.union; |
| 53 | +import static org.junit.Assert.assertEquals; |
| 54 | + |
| 55 | +public class PushAggregationThroughUnionTest { |
| 56 | + |
| 57 | + @Test |
| 58 | + public void UnionAggregationTest() { |
| 59 | + |
| 60 | + String sql = "select * from t1 union select * from t2"; |
| 61 | + Analysis analysis = analyzeSQL(sql, TEST_MATADATA, QUERY_CONTEXT); |
| 62 | + SymbolAllocator symbolAllocator = new SymbolAllocator(); |
| 63 | + LogicalQueryPlan actualLogicalQueryPlan = |
| 64 | + new TableLogicalPlanner( |
| 65 | + QUERY_CONTEXT, TEST_MATADATA, SESSION_INFO, symbolAllocator, DEFAULT_WARNING) |
| 66 | + .plan(analysis); |
| 67 | + |
| 68 | + // verify the Logical plan `Output - Aggregation - union - 2*tableScan` |
| 69 | + assertPlan( |
| 70 | + actualLogicalQueryPlan, |
| 71 | + output(aggregation(union(tableScan("testdb.t1"), tableScan("testdb.t2"))))); |
| 72 | + |
| 73 | + // verify the Distributed plan `Output - Aggregation - collect - 6*exchange` + |
| 74 | + // 6*(aggregation-project-tableScan)` |
| 75 | + TableDistributedPlanner TableDistributedPlanner = |
| 76 | + new TableDistributedPlanner( |
| 77 | + analysis, symbolAllocator, actualLogicalQueryPlan, TEST_MATADATA, null); |
| 78 | + DistributedQueryPlan actualDistributedQueryPlan = TableDistributedPlanner.plan(); |
| 79 | + assertEquals(7, actualDistributedQueryPlan.getFragments().size()); |
| 80 | + |
| 81 | + PlanMatchPattern expectedPattern = |
| 82 | + output( |
| 83 | + aggregation( |
| 84 | + collect(exchange(), exchange(), exchange(), exchange(), exchange(), exchange()))); |
| 85 | + |
| 86 | + OutputNode outputNode = |
| 87 | + (OutputNode) |
| 88 | + actualDistributedQueryPlan.getFragments().get(0).getPlanNodeTree().getChildren().get(0); |
| 89 | + assertPlan(outputNode, expectedPattern); |
| 90 | + |
| 91 | + for (int i = 1; i < actualDistributedQueryPlan.getFragments().size(); i++) { |
| 92 | + PlanNode planNode = |
| 93 | + actualDistributedQueryPlan.getFragments().get(i).getPlanNodeTree().getChildren().get(0); |
| 94 | + assertPlan(planNode, aggregation(project(tableScan(i <= 3 ? "testdb.t1" : "testdb.t2")))); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void unionAllWithGroupByAggregationTest() { |
| 100 | + |
| 101 | + String sql = |
| 102 | + "SELECT tag1, COUNT(s1), sum(s1) FROM (SELECT tag1, s1 FROM t1 UNION ALL SELECT tag1, s1 FROM t2) GROUP BY tag1"; |
| 103 | + Analysis analysis = analyzeSQL(sql, TEST_MATADATA, QUERY_CONTEXT); |
| 104 | + SymbolAllocator symbolAllocator = new SymbolAllocator(); |
| 105 | + |
| 106 | + LogicalQueryPlan actualLogicalQueryPlan = |
| 107 | + new TableLogicalPlanner( |
| 108 | + QUERY_CONTEXT, TEST_MATADATA, SESSION_INFO, symbolAllocator, DEFAULT_WARNING) |
| 109 | + .plan(analysis); |
| 110 | + |
| 111 | + // verify the Logical plan |
| 112 | + assertPlan( |
| 113 | + actualLogicalQueryPlan, |
| 114 | + output(aggregation(union(tableScan("testdb.t1"), tableScan("testdb.t2"))))); |
| 115 | + |
| 116 | + // verify the Distributed plan |
| 117 | + TableDistributedPlanner tableDistributedPlanner = |
| 118 | + new TableDistributedPlanner( |
| 119 | + analysis, symbolAllocator, actualLogicalQueryPlan, TEST_MATADATA, null); |
| 120 | + DistributedQueryPlan actualDistributedQueryPlan = tableDistributedPlanner.plan(); |
| 121 | + |
| 122 | + assertEquals(7, actualDistributedQueryPlan.getFragments().size()); |
| 123 | + PlanMatchPattern expectedRootPattern = |
| 124 | + output( |
| 125 | + aggregation( |
| 126 | + singleGroupingSet("tag1"), |
| 127 | + ImmutableMap.of( |
| 128 | + Optional.of("count"), |
| 129 | + aggregationFunction("count", ImmutableList.of("count_12")), |
| 130 | + Optional.of("sum"), |
| 131 | + aggregationFunction("sum", ImmutableList.of("sum_13"))), |
| 132 | + Optional.empty(), |
| 133 | + AggregationNode.Step.FINAL, |
| 134 | + collect(exchange(), exchange(), exchange(), exchange(), exchange(), exchange()))); |
| 135 | + OutputNode outputNode = |
| 136 | + (OutputNode) |
| 137 | + actualDistributedQueryPlan.getFragments().get(0).getPlanNodeTree().getChildren().get(0); |
| 138 | + assertPlan(outputNode, expectedRootPattern); |
| 139 | + |
| 140 | + for (int i = 1; i < actualDistributedQueryPlan.getFragments().size(); i++) { |
| 141 | + PlanNode planNode = |
| 142 | + actualDistributedQueryPlan.getFragments().get(i).getPlanNodeTree().getChildren().get(0); |
| 143 | + PlanMatchPattern expectedLeafPattern = |
| 144 | + aggregation(project(tableScan(i <= 3 ? "testdb.t1" : "testdb.t2"))); |
| 145 | + assertPlan(planNode, expectedLeafPattern); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments