|
| 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.rewrite; |
| 19 | + |
| 20 | +import org.apache.doris.catalog.KeysType; |
| 21 | +import org.apache.doris.catalog.OlapTable; |
| 22 | +import org.apache.doris.common.Pair; |
| 23 | +import org.apache.doris.nereids.trees.expressions.Add; |
| 24 | +import org.apache.doris.nereids.trees.expressions.Alias; |
| 25 | +import org.apache.doris.nereids.trees.expressions.NamedExpression; |
| 26 | +import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral; |
| 27 | +import org.apache.doris.nereids.trees.plans.JoinType; |
| 28 | +import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan; |
| 29 | +import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; |
| 30 | +import org.apache.doris.nereids.util.LogicalPlanBuilder; |
| 31 | +import org.apache.doris.nereids.util.MemoPatternMatchSupported; |
| 32 | +import org.apache.doris.nereids.util.PlanChecker; |
| 33 | +import org.apache.doris.nereids.util.PlanConstructor; |
| 34 | +import org.apache.doris.qe.ConnectContext; |
| 35 | + |
| 36 | +import com.google.common.collect.ImmutableList; |
| 37 | +import org.junit.jupiter.api.AfterEach; |
| 38 | +import org.junit.jupiter.api.BeforeEach; |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | + |
| 41 | +class PushDownTopNDistinctThroughJoinTest implements MemoPatternMatchSupported { |
| 42 | + private static final PushDownTopNDistinctThroughJoin RULE = new PushDownTopNDistinctThroughJoin(); |
| 43 | + private static final LogicalOlapScan LEFT_SCAN = PlanConstructor.newLogicalOlapScan(0, "t1", 0); |
| 44 | + private static final LogicalOlapScan RIGHT_SCAN = PlanConstructor.newLogicalOlapScan(1, "t2", 0); |
| 45 | + private ConnectContext connectContext; |
| 46 | + |
| 47 | + @BeforeEach |
| 48 | + void setUp() { |
| 49 | + connectContext = new ConnectContext(); |
| 50 | + connectContext.setThreadLocalInfo(); |
| 51 | + } |
| 52 | + |
| 53 | + @AfterEach |
| 54 | + void tearDown() { |
| 55 | + ConnectContext.remove(); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void pushDirectShapeWhenAccumulatedPrefixDeterminesChildOutput() { |
| 60 | + NamedExpression id = LEFT_SCAN.getOutput().get(0); |
| 61 | + LogicalPlan left = new LogicalPlanBuilder(LEFT_SCAN) |
| 62 | + .projectExprs(ImmutableList.of(id, LEFT_SCAN.getOutput().get(1), |
| 63 | + new Alias(new Add(id, new IntegerLiteral(1)), "id_plus_one"))) |
| 64 | + .build(); |
| 65 | + LogicalPlan plan = new LogicalPlanBuilder(left) |
| 66 | + .join(RIGHT_SCAN, JoinType.LEFT_OUTER_JOIN, Pair.of(0, 0)) |
| 67 | + .distinct(ImmutableList.of(0, 1, 2, 3, 4)) |
| 68 | + .topN(10, 0, ImmutableList.of(0, 1, 3)) |
| 69 | + .build(); |
| 70 | + |
| 71 | + PlanChecker.from(connectContext, plan) |
| 72 | + .applyTopDown(RULE) |
| 73 | + .matchesFromRoot( |
| 74 | + logicalTopN( |
| 75 | + logicalAggregate( |
| 76 | + logicalJoin( |
| 77 | + logicalTopN(logicalAggregate(logicalProject(logicalOlapScan()))) |
| 78 | + .when(topN -> topN.getLimit() == 10), |
| 79 | + logicalOlapScan() |
| 80 | + ) |
| 81 | + ) |
| 82 | + ) |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void pushAllSlotsProjectShapeForNonNullUniquePrefix() { |
| 88 | + LogicalOlapScan uniqueScan = newUniqueScan(2, "unique_not_null", false); |
| 89 | + LogicalPlan join = new LogicalPlanBuilder(uniqueScan) |
| 90 | + .join(RIGHT_SCAN, JoinType.LEFT_OUTER_JOIN, Pair.of(0, 0)) |
| 91 | + .build(); |
| 92 | + LogicalPlan plan = new LogicalPlanBuilder(join) |
| 93 | + .projectExprs(ImmutableList.<NamedExpression>builder().addAll(join.getOutput()).build()) |
| 94 | + .distinct(ImmutableList.of(0, 1, 2, 3)) |
| 95 | + .topN(10, 0, ImmutableList.of(0, 2)) |
| 96 | + .build(); |
| 97 | + |
| 98 | + PlanChecker.from(connectContext, plan) |
| 99 | + .applyTopDown(RULE) |
| 100 | + .matchesFromRoot( |
| 101 | + logicalTopN( |
| 102 | + logicalProject( |
| 103 | + logicalAggregate( |
| 104 | + logicalJoin( |
| 105 | + logicalTopN(logicalAggregate(logicalOlapScan())) |
| 106 | + .when(topN -> topN.getLimit() == 10), |
| 107 | + logicalOlapScan() |
| 108 | + ) |
| 109 | + ) |
| 110 | + ) |
| 111 | + ) |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void rejectNullableUniquePrefix() { |
| 117 | + LogicalOlapScan nullableUniqueScan = newUniqueScan(3, "unique_nullable", true); |
| 118 | + LogicalPlan plan = new LogicalPlanBuilder(nullableUniqueScan) |
| 119 | + .join(RIGHT_SCAN, JoinType.LEFT_OUTER_JOIN, Pair.of(0, 0)) |
| 120 | + .distinct(ImmutableList.of(0, 1, 2, 3)) |
| 121 | + .topN(10, 0, ImmutableList.of(0, 2)) |
| 122 | + .build(); |
| 123 | + |
| 124 | + PlanChecker.from(connectContext, plan) |
| 125 | + .applyTopDown(RULE) |
| 126 | + .matchesFromRoot( |
| 127 | + logicalTopN( |
| 128 | + logicalAggregate( |
| 129 | + logicalJoin(logicalOlapScan(), logicalOlapScan()) |
| 130 | + ) |
| 131 | + ) |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + private LogicalOlapScan newUniqueScan(long tableId, String tableName, boolean keyNullable) { |
| 136 | + OlapTable table = PlanConstructor.newOlapTable(tableId, tableName, 0, KeysType.UNIQUE_KEYS); |
| 137 | + table.getFullSchema().get(0).setIsAllowNull(keyNullable); |
| 138 | + table.getFullSchema().get(1).setIsKey(false); |
| 139 | + return new LogicalOlapScan(PlanConstructor.getNextRelationId(), table, ImmutableList.of("db")); |
| 140 | + } |
| 141 | +} |
0 commit comments