|
| 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.rules.analysis; |
| 19 | + |
| 20 | +import org.apache.doris.nereids.trees.expressions.Alias; |
| 21 | +import org.apache.doris.nereids.trees.expressions.Slot; |
| 22 | +import org.apache.doris.nereids.trees.expressions.StatementScopeIdGenerator; |
| 23 | +import org.apache.doris.nereids.trees.expressions.functions.agg.Count; |
| 24 | +import org.apache.doris.nereids.trees.plans.Plan; |
| 25 | +import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate; |
| 26 | +import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan; |
| 27 | +import org.apache.doris.nereids.util.AggregateUtils; |
| 28 | +import org.apache.doris.nereids.util.MemoPatternMatchSupported; |
| 29 | +import org.apache.doris.nereids.util.MemoTestUtils; |
| 30 | +import org.apache.doris.nereids.util.PlanChecker; |
| 31 | +import org.apache.doris.nereids.util.PlanConstructor; |
| 32 | + |
| 33 | +import com.google.common.collect.ImmutableList; |
| 34 | +import com.google.common.collect.ImmutableSet; |
| 35 | +import org.junit.jupiter.api.Assertions; |
| 36 | +import org.junit.jupiter.api.Test; |
| 37 | + |
| 38 | +public class NormalizeMultiColumnDistinctCountTest implements MemoPatternMatchSupported { |
| 39 | + @Test |
| 40 | + public void testNormalizeDifferentArgumentOrder() { |
| 41 | + Plan student = new LogicalOlapScan(StatementScopeIdGenerator.newRelationId(), PlanConstructor.student, |
| 42 | + ImmutableList.of()); |
| 43 | + Slot id = student.getOutput().get(0); |
| 44 | + Slot name = student.getOutput().get(2); |
| 45 | + Alias countIdName = new Alias(new Count(true, id, name), "count_id_name"); |
| 46 | + Alias countNameId = new Alias(new Count(true, name, id), "count_name_id"); |
| 47 | + LogicalAggregate<Plan> root = new LogicalAggregate<>(ImmutableList.of(), |
| 48 | + ImmutableList.of(countIdName, countNameId), student); |
| 49 | + |
| 50 | + PlanChecker.from(MemoTestUtils.createConnectContext(), root) |
| 51 | + .applyTopDown(new NormalizeAggregate()) |
| 52 | + .matchesFromRoot( |
| 53 | + logicalProject( |
| 54 | + logicalAggregate( |
| 55 | + logicalProject( |
| 56 | + logicalOlapScan() |
| 57 | + ) |
| 58 | + ).when(aggregate -> aggregate.getAggregateFunctions().size() == 1) |
| 59 | + .when(aggregate -> aggregate.getOutputExpressions().size() == 1) |
| 60 | + ).when(project -> project.getProjects().size() == 2) |
| 61 | + .when(project -> project.getProjects().get(0).getExprId() |
| 62 | + .equals(countIdName.getExprId())) |
| 63 | + .when(project -> project.getProjects().get(1).getExprId() |
| 64 | + .equals(countNameId.getExprId())) |
| 65 | + .when(project -> project.getProjects().get(0).getInputSlots() |
| 66 | + .equals(project.getProjects().get(1).getInputSlots())) |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testKeepDifferentDistinctArguments() { |
| 72 | + Plan student = new LogicalOlapScan(StatementScopeIdGenerator.newRelationId(), PlanConstructor.student, |
| 73 | + ImmutableList.of()); |
| 74 | + Slot id = student.getOutput().get(0); |
| 75 | + Slot gender = student.getOutput().get(1); |
| 76 | + Slot name = student.getOutput().get(2); |
| 77 | + LogicalAggregate<Plan> root = new LogicalAggregate<>(ImmutableList.of(), |
| 78 | + ImmutableList.of( |
| 79 | + new Alias(new Count(true, id, name), "count_id_name"), |
| 80 | + new Alias(new Count(true, id, gender), "count_id_gender")), |
| 81 | + student); |
| 82 | + |
| 83 | + PlanChecker.from(MemoTestUtils.createConnectContext(), root) |
| 84 | + .applyTopDown(new NormalizeAggregate()) |
| 85 | + .matches( |
| 86 | + logicalJoin( |
| 87 | + logicalAggregate() |
| 88 | + .when(aggregate -> aggregate.getAggregateFunctions().size() == 1), |
| 89 | + logicalAggregate() |
| 90 | + .when(aggregate -> aggregate.getAggregateFunctions().size() == 1) |
| 91 | + ) |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testRemoveDuplicateDistinctArguments() { |
| 97 | + Plan student = new LogicalOlapScan(StatementScopeIdGenerator.newRelationId(), PlanConstructor.student, |
| 98 | + ImmutableList.of()); |
| 99 | + Slot id = student.getOutput().get(0); |
| 100 | + Slot name = student.getOutput().get(2); |
| 101 | + Count count = new Count(true, id, id, name); |
| 102 | + |
| 103 | + Count countIf = (Count) AggregateUtils.countDistinctMultiExprToCountIf(count); |
| 104 | + Assertions.assertEquals(ImmutableSet.of(id, name), countIf.getInputSlots()); |
| 105 | + |
| 106 | + LogicalAggregate<Plan> root = new LogicalAggregate<>(ImmutableList.of(), |
| 107 | + ImmutableList.of( |
| 108 | + new Alias(count, "count_id_id_name"), |
| 109 | + new Alias(new Count(true, name, id), "count_name_id")), |
| 110 | + student); |
| 111 | + PlanChecker.from(MemoTestUtils.createConnectContext(), root) |
| 112 | + .applyTopDown(new NormalizeAggregate()) |
| 113 | + .matchesFromRoot( |
| 114 | + logicalProject( |
| 115 | + logicalAggregate( |
| 116 | + logicalProject( |
| 117 | + logicalOlapScan() |
| 118 | + ) |
| 119 | + ).when(aggregate -> aggregate.getAggregateFunctions().size() == 1) |
| 120 | + .when(aggregate -> aggregate.getAggregateFunctions().iterator().next() |
| 121 | + .arity() == 2) |
| 122 | + ) |
| 123 | + ); |
| 124 | + } |
| 125 | +} |
0 commit comments