|
| 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.exceptions.AnalysisException; |
| 21 | +import org.apache.doris.nereids.rules.rewrite.PullUpProjectUnderApply; |
| 22 | +import org.apache.doris.nereids.rules.rewrite.UnCorrelatedApplyFilter; |
| 23 | +import org.apache.doris.nereids.trees.expressions.Expression; |
| 24 | +import org.apache.doris.nereids.trees.expressions.Slot; |
| 25 | +import org.apache.doris.nereids.trees.plans.Plan; |
| 26 | +import org.apache.doris.nereids.trees.plans.logical.LogicalApply; |
| 27 | +import org.apache.doris.nereids.util.ExpressionUtils; |
| 28 | +import org.apache.doris.nereids.util.MemoPatternMatchSupported; |
| 29 | +import org.apache.doris.nereids.util.PlanChecker; |
| 30 | +import org.apache.doris.utframe.TestWithFeService; |
| 31 | + |
| 32 | +import org.junit.jupiter.api.Assertions; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | + |
| 35 | +import java.util.List; |
| 36 | +import java.util.Optional; |
| 37 | +import java.util.Set; |
| 38 | +import java.util.stream.Collectors; |
| 39 | + |
| 40 | +public class FillUpQualifyMissingSlotTest extends TestWithFeService implements MemoPatternMatchSupported { |
| 41 | + |
| 42 | + @Override |
| 43 | + protected void runBeforeAll() throws Exception { |
| 44 | + createDatabase("test"); |
| 45 | + connectContext.setDatabase("test"); |
| 46 | + |
| 47 | + createTables( |
| 48 | + "CREATE TABLE test.o (\n" |
| 49 | + + " k INT,\n" |
| 50 | + + " flag INT,\n" |
| 51 | + + " h INT\n" |
| 52 | + + ")\n" |
| 53 | + + "DUPLICATE KEY (k)\n" |
| 54 | + + "DISTRIBUTED BY HASH (k) BUCKETS 3\n" |
| 55 | + + "PROPERTIES(\n" |
| 56 | + + " 'replication_num' = '1'\n" |
| 57 | + + ");", |
| 58 | + "CREATE TABLE test.i (\n" |
| 59 | + + " k INT,\n" |
| 60 | + + " not_grouped INT\n" |
| 61 | + + ")\n" |
| 62 | + + "DUPLICATE KEY (k)\n" |
| 63 | + + "DISTRIBUTED BY HASH (k) BUCKETS 3\n" |
| 64 | + + "PROPERTIES(\n" |
| 65 | + + " 'replication_num' = '1'\n" |
| 66 | + + ");" |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + private LogicalApply findApply(Plan plan) { |
| 71 | + List<LogicalApply> applies = plan.collectToList(LogicalApply.class::isInstance) |
| 72 | + .stream().map(node -> (LogicalApply) node).collect(Collectors.toList()); |
| 73 | + Assertions.assertEquals(1, applies.size(), |
| 74 | + "expected exactly one LogicalApply in plan:\n" + plan.treeString()); |
| 75 | + return applies.get(0); |
| 76 | + } |
| 77 | + |
| 78 | + private Set<Slot> getCorrelationFilterInputSlots(LogicalApply apply) { |
| 79 | + Optional<Expression> filter = apply.getCorrelationFilter(); |
| 80 | + Assertions.assertTrue(filter.isPresent(), |
| 81 | + "apply should record a correlation filter, plan:\n" + apply.treeString()); |
| 82 | + Set<Expression> conjuncts = ExpressionUtils.extractConjunctionToSet(filter.get()); |
| 83 | + return conjuncts.stream().flatMap(e -> e.getInputSlots().stream()).collect(Collectors.toSet()); |
| 84 | + } |
| 85 | + |
| 86 | + private boolean containsSlotNamed(Set<Slot> slots, String name) { |
| 87 | + return slots.stream().anyMatch(s -> s.getName().equals(name)); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * qualify -> having -> agg where both the having and the qualify reference correlated outer |
| 92 | + * columns. The window expression in qualify is extracted into a project above the having during |
| 93 | + * NormalizeAggregate; the having's correlated predicate must be conjoined into the qualify so it |
| 94 | + * stays above that window project and is still collected into the apply during unnesting. |
| 95 | + */ |
| 96 | + @Test |
| 97 | + public void testCorrelatedQualifyAndHaving() { |
| 98 | + String sql = "SELECT o.k\n" |
| 99 | + + "FROM o\n" |
| 100 | + + "WHERE EXISTS (\n" |
| 101 | + + " SELECT i.k\n" |
| 102 | + + " FROM i\n" |
| 103 | + + " GROUP BY i.k\n" |
| 104 | + + " HAVING o.h = 1\n" |
| 105 | + + " QUALIFY row_number() OVER (ORDER BY i.k) = 1 AND o.flag = 1\n" |
| 106 | + + ")\n" |
| 107 | + + "ORDER BY o.k"; |
| 108 | + Plan plan = PlanChecker.from(connectContext) |
| 109 | + .analyze(sql) |
| 110 | + .applyBottomUp(new PullUpProjectUnderApply()) |
| 111 | + .applyBottomUp(new UnCorrelatedApplyFilter()) |
| 112 | + .getPlan(); |
| 113 | + LogicalApply apply = findApply(plan); |
| 114 | + Set<Slot> slots = getCorrelationFilterInputSlots(apply); |
| 115 | + // both the qualify correlation (o.flag) and the having correlation (o.h) must be collected |
| 116 | + Assertions.assertTrue(containsSlotNamed(slots, "flag"), |
| 117 | + "correlation filter should reference o.flag, plan:\n" + apply.treeString()); |
| 118 | + Assertions.assertTrue(containsSlotNamed(slots, "h"), |
| 119 | + "correlation filter should reference o.h, plan:\n" + apply.treeString()); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * qualify -> project where the qualify references a project alias (f) whose producer is a |
| 124 | + * correlated outer column (o.flag). The alias-producer dependency must be resolved so the |
| 125 | + * correlation slot is still collected into the apply even though the window expression in the |
| 126 | + * project blocks filter pushdown. |
| 127 | + */ |
| 128 | + @Test |
| 129 | + public void testCorrelatedQualifyWithAlias() { |
| 130 | + String sql = "SELECT o.k\n" |
| 131 | + + "FROM o\n" |
| 132 | + + "WHERE EXISTS (\n" |
| 133 | + + " SELECT i.k, o.flag AS f, row_number() OVER (ORDER BY i.k) AS rn\n" |
| 134 | + + " FROM i\n" |
| 135 | + + " QUALIFY rn = 1 AND f = 1\n" |
| 136 | + + ")\n" |
| 137 | + + "ORDER BY o.k"; |
| 138 | + Plan plan = PlanChecker.from(connectContext) |
| 139 | + .analyze(sql) |
| 140 | + .applyBottomUp(new PullUpProjectUnderApply()) |
| 141 | + .applyBottomUp(new UnCorrelatedApplyFilter()) |
| 142 | + .getPlan(); |
| 143 | + LogicalApply apply = findApply(plan); |
| 144 | + Set<Slot> slots = getCorrelationFilterInputSlots(apply); |
| 145 | + Assertions.assertTrue(containsSlotNamed(slots, "flag"), |
| 146 | + "correlation filter should reference o.flag (resolved from alias f), plan:\n" |
| 147 | + + apply.treeString()); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * A having predicate that mixes outer correlated slots with aggregate results cannot be |
| 152 | + * moved above the window project (it depends on the aggregate rows), and the window project |
| 153 | + * would prevent subquery unnesting from collecting its correlation. Such a shape must be |
| 154 | + * rejected during analysis instead of silently dropping the predicate. |
| 155 | + */ |
| 156 | + @Test |
| 157 | + public void testMixedCorrelatedHavingRejected() { |
| 158 | + String sql = "SELECT o.k\n" |
| 159 | + + "FROM o\n" |
| 160 | + + "WHERE EXISTS (\n" |
| 161 | + + " SELECT i.k\n" |
| 162 | + + " FROM i\n" |
| 163 | + + " GROUP BY i.k\n" |
| 164 | + + " HAVING o.h = sum(i.k)\n" |
| 165 | + + " QUALIFY row_number() OVER (ORDER BY i.k) = 1 AND o.flag = 1\n" |
| 166 | + + ")\n" |
| 167 | + + "ORDER BY o.k"; |
| 168 | + AnalysisException exception = Assertions.assertThrows(AnalysisException.class, |
| 169 | + () -> PlanChecker.from(connectContext).analyze(sql)); |
| 170 | + Assertions.assertTrue(exception.getMessage().contains("not supported"), |
| 171 | + "unexpected exception message: " + exception.getMessage()); |
| 172 | + } |
| 173 | +} |
0 commit comments