|
17 | 17 |
|
18 | 18 | package org.apache.doris.nereids.cost; |
19 | 19 |
|
| 20 | +import org.apache.doris.nereids.PlanContext; |
20 | 21 | import org.apache.doris.nereids.StatementContext; |
21 | 22 | import org.apache.doris.nereids.sqltest.SqlTestBase; |
| 23 | +import org.apache.doris.nereids.trees.expressions.Slot; |
| 24 | +import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateParam; |
| 25 | +import org.apache.doris.nereids.trees.plans.AggMode; |
| 26 | +import org.apache.doris.nereids.trees.plans.AggPhase; |
22 | 27 | import org.apache.doris.nereids.trees.plans.Plan; |
| 28 | +import org.apache.doris.nereids.trees.plans.physical.PhysicalHashAggregate; |
23 | 29 | import org.apache.doris.nereids.trees.plans.physical.PhysicalHashJoin; |
24 | 30 | import org.apache.doris.nereids.util.PlanChecker; |
| 31 | +import org.apache.doris.nereids.util.PlanConstructor; |
25 | 32 | import org.apache.doris.qe.SessionVariable; |
| 33 | +import org.apache.doris.statistics.Statistics; |
| 34 | +import org.apache.doris.statistics.StatisticsBuilder; |
26 | 35 |
|
| 36 | +import com.google.common.collect.ImmutableList; |
27 | 37 | import org.junit.jupiter.api.Assertions; |
28 | 38 | import org.junit.jupiter.api.Test; |
| 39 | +import org.mockito.Mockito; |
| 40 | + |
| 41 | +import java.util.Optional; |
29 | 42 |
|
30 | 43 | class CostModelV1Test extends SqlTestBase { |
31 | 44 |
|
@@ -87,4 +100,41 @@ void testMaterializingCost() { |
87 | 100 | .getBestPlanTree(); |
88 | 101 | p.anyMatch(j -> j instanceof PhysicalHashJoin && ((PhysicalHashJoin<?, ?>) j).getJoinType().isRightJoin()); |
89 | 102 | } |
| 103 | + |
| 104 | + @Test |
| 105 | + void testPartitionedScalarAggregateCostUsesClusterScale() { |
| 106 | + int originBeNumberForTest = connectContext.getSessionVariable().getBeNumberForTest(); |
| 107 | + connectContext.getSessionVariable().setBeNumberForTest(4); |
| 108 | + try { |
| 109 | + Plan child = PlanConstructor.newLogicalOlapScan(101, "partitioned_scalar_agg_t", 0); |
| 110 | + Slot partitionKey = child.getOutput().get(0); |
| 111 | + PhysicalHashAggregate<Plan> aggregate = new PhysicalHashAggregate<Plan>( |
| 112 | + ImmutableList.of(), ImmutableList.of(partitionKey), Optional.of(ImmutableList.of(partitionKey)), |
| 113 | + new AggregateParam(AggPhase.GLOBAL, AggMode.INPUT_TO_RESULT), false, null, false, child); |
| 114 | + PhysicalHashAggregate<Plan> singlePointAggregate = new PhysicalHashAggregate<Plan>( |
| 115 | + ImmutableList.of(), ImmutableList.of(partitionKey), Optional.empty(), |
| 116 | + new AggregateParam(AggPhase.GLOBAL, AggMode.INPUT_TO_RESULT), false, null, false, child); |
| 117 | + PhysicalHashAggregate<Plan> groupByAggregate = new PhysicalHashAggregate<Plan>( |
| 118 | + ImmutableList.of(partitionKey), ImmutableList.of(partitionKey), Optional.empty(), |
| 119 | + new AggregateParam(AggPhase.GLOBAL, AggMode.INPUT_TO_RESULT), false, null, false, child); |
| 120 | + Statistics childStats = new StatisticsBuilder().setRowCount(1000).build(); |
| 121 | + PlanContext context = Mockito.mock(PlanContext.class); |
| 122 | + Mockito.when(context.getChildStatistics(0)).thenReturn(childStats); |
| 123 | + Mockito.when(context.getCostWeight()).thenReturn(new CostWeight(1, 1, 1.5, 1)); |
| 124 | + |
| 125 | + Cost cost = new CostModel(connectContext).visitPhysicalHashAggregate(aggregate, context); |
| 126 | + Cost singlePointCost = new CostModel(connectContext).visitPhysicalHashAggregate(singlePointAggregate, |
| 127 | + context); |
| 128 | + Cost groupByCost = new CostModel(connectContext).visitPhysicalHashAggregate(groupByAggregate, context); |
| 129 | + |
| 130 | + Assertions.assertEquals(250, cost.getCpuCost(), 1e-9); |
| 131 | + Assertions.assertEquals(250, cost.getMemoryCost(), 1e-9); |
| 132 | + Assertions.assertEquals(1000, singlePointCost.getCpuCost(), 1e-9); |
| 133 | + Assertions.assertEquals(1000, singlePointCost.getMemoryCost(), 1e-9); |
| 134 | + Assertions.assertEquals(250, groupByCost.getCpuCost(), 1e-9); |
| 135 | + Assertions.assertEquals(250, groupByCost.getMemoryCost(), 1e-9); |
| 136 | + } finally { |
| 137 | + connectContext.getSessionVariable().setBeNumberForTest(originBeNumberForTest); |
| 138 | + } |
| 139 | + } |
90 | 140 | } |
0 commit comments