|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.nereids.properties; |
| 19 | + |
| 20 | +import org.apache.doris.common.Pair; |
| 21 | +import org.apache.doris.nereids.CascadesContext; |
| 22 | +import org.apache.doris.nereids.cost.Cost; |
| 23 | +import org.apache.doris.nereids.cost.CostCalculator; |
| 24 | +import org.apache.doris.nereids.jobs.JobContext; |
| 25 | +import org.apache.doris.nereids.memo.Group; |
| 26 | +import org.apache.doris.nereids.memo.GroupExpression; |
| 27 | +import org.apache.doris.nereids.trees.plans.GroupPlan; |
| 28 | +import org.apache.doris.nereids.trees.plans.Plan; |
| 29 | +import org.apache.doris.nereids.trees.plans.physical.PhysicalFilter; |
| 30 | +import org.apache.doris.nereids.trees.plans.physical.PhysicalLimit; |
| 31 | +import org.apache.doris.nereids.trees.plans.physical.PhysicalProject; |
| 32 | + |
| 33 | +import com.google.common.collect.Lists; |
| 34 | +import com.google.common.collect.Maps; |
| 35 | +import com.google.common.collect.Sets; |
| 36 | +import org.junit.jupiter.api.Assertions; |
| 37 | +import org.junit.jupiter.api.BeforeEach; |
| 38 | +import org.junit.jupiter.api.Test; |
| 39 | +import org.mockito.MockedStatic; |
| 40 | +import org.mockito.Mockito; |
| 41 | + |
| 42 | +import java.util.ArrayList; |
| 43 | +import java.util.BitSet; |
| 44 | +import java.util.List; |
| 45 | +import java.util.Map; |
| 46 | +import java.util.Optional; |
| 47 | + |
| 48 | +public class ChildrenPropertiesRegulatorTest { |
| 49 | + |
| 50 | + private List<GroupExpression> children; |
| 51 | + private JobContext mockedJobContext; |
| 52 | + private List<PhysicalProperties> originOutputChildrenProperties |
| 53 | + = Lists.newArrayList(PhysicalProperties.MUST_SHUFFLE); |
| 54 | + |
| 55 | + @BeforeEach |
| 56 | + public void setUp() { |
| 57 | + Group childGroup = Mockito.mock(Group.class); |
| 58 | + Mockito.when(childGroup.getLogicalProperties()).thenReturn(Mockito.mock(LogicalProperties.class)); |
| 59 | + GroupExpression child = Mockito.mock(GroupExpression.class); |
| 60 | + Mockito.when(child.getOutputProperties(Mockito.any())).thenReturn(PhysicalProperties.MUST_SHUFFLE); |
| 61 | + Mockito.when(child.getOwnerGroup()).thenReturn(childGroup); |
| 62 | + Map<PhysicalProperties, Pair<Cost, List<PhysicalProperties>>> lct = Maps.newHashMap(); |
| 63 | + lct.put(PhysicalProperties.MUST_SHUFFLE, Pair.of(Cost.zero(), Lists.newArrayList())); |
| 64 | + Mockito.when(child.getLowestCostTable()).thenReturn(lct); |
| 65 | + children = Lists.newArrayList(child); |
| 66 | + |
| 67 | + mockedJobContext = Mockito.mock(JobContext.class); |
| 68 | + Mockito.when(mockedJobContext.getCascadesContext()).thenReturn(Mockito.mock(CascadesContext.class)); |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testMustShuffleProjectProjectCanNotMerge() { |
| 74 | + testMustShuffleProject(PhysicalProject.class, DistributionSpecExecutionAny.class, false); |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testMustShuffleProjectProjectCanMerge() { |
| 80 | + testMustShuffleProject(PhysicalProject.class, DistributionSpecMustShuffle.class, true); |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testMustShuffleProjectFilter() { |
| 86 | + testMustShuffleProject(PhysicalFilter.class, DistributionSpecMustShuffle.class, true); |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testMustShuffleProjectLimit() { |
| 92 | + testMustShuffleProject(PhysicalLimit.class, DistributionSpecExecutionAny.class, true); |
| 93 | + } |
| 94 | + |
| 95 | + public void testMustShuffleProject(Class<? extends Plan> childClazz, |
| 96 | + Class<? extends DistributionSpec> distributeClazz, |
| 97 | + boolean canMergeChildProject) { |
| 98 | + try (MockedStatic<CostCalculator> mockedCostCalculator = Mockito.mockStatic(CostCalculator.class)) { |
| 99 | + mockedCostCalculator.when(() -> CostCalculator.calculateCost(Mockito.any(), Mockito.any(), |
| 100 | + Mockito.anyList())).thenReturn(Cost.zero()); |
| 101 | + mockedCostCalculator.when(() -> CostCalculator.addChildCost(Mockito.any(), Mockito.any(), Mockito.any(), |
| 102 | + Mockito.any(), Mockito.anyInt())).thenReturn(Cost.zero()); |
| 103 | + |
| 104 | + // project, cannot merge |
| 105 | + Plan mockedChild = Mockito.mock(childClazz); |
| 106 | + Mockito.when(mockedChild.withGroupExpression(Mockito.any())).thenReturn(mockedChild); |
| 107 | + Group mockedGroup = Mockito.mock(Group.class); |
| 108 | + List<GroupExpression> physicalExpressions = Lists.newArrayList(new GroupExpression(mockedChild)); |
| 109 | + Mockito.when(mockedGroup.getPhysicalExpressions()).thenReturn(physicalExpressions); |
| 110 | + GroupPlan mockedGroupPlan = Mockito.mock(GroupPlan.class); |
| 111 | + Mockito.when(mockedGroupPlan.getGroup()).thenReturn(mockedGroup); |
| 112 | + // let AbstractTreeNode's init happy |
| 113 | + Mockito.when(mockedGroupPlan.getAllChildrenTypes()).thenReturn(new BitSet()); |
| 114 | + PhysicalProject parentPlan = new PhysicalProject<>(Lists.newArrayList(), null, mockedGroupPlan); |
| 115 | + GroupExpression parent = new GroupExpression(parentPlan); |
| 116 | + parentPlan = parentPlan.withGroupExpression(Optional.of(parent)); |
| 117 | + parentPlan = Mockito.spy(parentPlan); |
| 118 | + Mockito.doReturn(canMergeChildProject).when(parentPlan).canMergeChildProjections(Mockito.any()); |
| 119 | + parent = Mockito.spy(parent); |
| 120 | + Mockito.doReturn(parentPlan).when(parent).getPlan(); |
| 121 | + ChildrenPropertiesRegulator regulator = new ChildrenPropertiesRegulator(parent, children, |
| 122 | + new ArrayList<>(originOutputChildrenProperties), null, mockedJobContext); |
| 123 | + PhysicalProperties result = regulator.adjustChildrenProperties().get(0).get(0); |
| 124 | + Assertions.assertInstanceOf(distributeClazz, result.getDistributionSpec()); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testMustShuffleFilterProject() { |
| 130 | + testMustShuffleFilter(PhysicalProject.class); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void testMustShuffleFilterFilter() { |
| 135 | + testMustShuffleFilter(PhysicalFilter.class); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void testMustShuffleFilterLimit() { |
| 140 | + testMustShuffleFilter(PhysicalLimit.class); |
| 141 | + } |
| 142 | + |
| 143 | + private void testMustShuffleFilter(Class<? extends Plan> childClazz) { |
| 144 | + try (MockedStatic<CostCalculator> mockedCostCalculator = Mockito.mockStatic(CostCalculator.class)) { |
| 145 | + mockedCostCalculator.when(() -> CostCalculator.calculateCost(Mockito.any(), Mockito.any(), |
| 146 | + Mockito.anyList())).thenReturn(Cost.zero()); |
| 147 | + mockedCostCalculator.when(() -> CostCalculator.addChildCost(Mockito.any(), Mockito.any(), Mockito.any(), |
| 148 | + Mockito.any(), Mockito.anyInt())).thenReturn(Cost.zero()); |
| 149 | + |
| 150 | + // project, cannot merge |
| 151 | + Plan mockedChild = Mockito.mock(childClazz); |
| 152 | + Mockito.when(mockedChild.withGroupExpression(Mockito.any())).thenReturn(mockedChild); |
| 153 | + Group mockedGroup = Mockito.mock(Group.class); |
| 154 | + List<GroupExpression> physicalExpressions = Lists.newArrayList(new GroupExpression(mockedChild)); |
| 155 | + Mockito.when(mockedGroup.getPhysicalExpressions()).thenReturn(physicalExpressions); |
| 156 | + GroupPlan mockedGroupPlan = Mockito.mock(GroupPlan.class); |
| 157 | + Mockito.when(mockedGroupPlan.getGroup()).thenReturn(mockedGroup); |
| 158 | + // let AbstractTreeNode's init happy |
| 159 | + Mockito.when(mockedGroupPlan.getAllChildrenTypes()).thenReturn(new BitSet()); |
| 160 | + GroupExpression parent = new GroupExpression(new PhysicalFilter<>(Sets.newHashSet(), null, mockedGroupPlan)); |
| 161 | + ChildrenPropertiesRegulator regulator = new ChildrenPropertiesRegulator(parent, children, |
| 162 | + new ArrayList<>(originOutputChildrenProperties), null, mockedJobContext); |
| 163 | + PhysicalProperties result = regulator.adjustChildrenProperties().get(0).get(0); |
| 164 | + Assertions.assertInstanceOf(DistributionSpecExecutionAny.class, result.getDistributionSpec()); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments