|
| 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.nereids.exceptions.AnalysisException; |
| 21 | +import org.apache.doris.nereids.trees.expressions.StatementScopeIdGenerator; |
| 22 | +import org.apache.doris.nereids.util.MemoPatternMatchSupported; |
| 23 | +import org.apache.doris.nereids.util.PlanChecker; |
| 24 | +import org.apache.doris.utframe.TestWithFeService; |
| 25 | + |
| 26 | +import com.google.common.collect.ImmutableList; |
| 27 | +import org.junit.jupiter.api.Assertions; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +class PullUpProjectUnderApplyTest extends TestWithFeService implements MemoPatternMatchSupported { |
| 33 | + @Override |
| 34 | + protected void runBeforeAll() throws Exception { |
| 35 | + createDatabase("test"); |
| 36 | + connectContext.setDatabase("default_cluster:test"); |
| 37 | + |
| 38 | + createTables( |
| 39 | + "CREATE TABLE IF NOT EXISTS T (\n" |
| 40 | + + " id bigint,\n" |
| 41 | + + " score bigint,\n" |
| 42 | + + " score_int int\n" |
| 43 | + + ")\n" |
| 44 | + + "DUPLICATE KEY(id)\n" |
| 45 | + + "DISTRIBUTED BY HASH(id) BUCKETS 1\n" |
| 46 | + + "PROPERTIES (\n" |
| 47 | + + " \"replication_num\" = \"1\"\n" |
| 48 | + + ")\n" |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected void runBeforeEach() throws Exception { |
| 54 | + StatementScopeIdGenerator.clear(); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void testPullUpProjectUnderApply() { |
| 59 | + List<String> testSql = ImmutableList.of( |
| 60 | + "select * from T as T1 where id = (select max(id) from T as T2 where T1.score = T2.score)", |
| 61 | + "select * from T as T1 where id = (select max(id) + 1 from T as T2 where T1.score = T2.score)" |
| 62 | + ); |
| 63 | + |
| 64 | + testSql.forEach(sql -> |
| 65 | + PlanChecker.from(connectContext) |
| 66 | + .analyze(sql) |
| 67 | + .applyTopDown(new PullUpProjectUnderApply()) |
| 68 | + .printlnTree() |
| 69 | + .matchesNotCheck( |
| 70 | + logicalApply( |
| 71 | + logicalSubQueryAlias(), |
| 72 | + logicalAggregate() |
| 73 | + ) |
| 74 | + ) |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void testScalarTwoColumn() { |
| 80 | + String sql = "select * from T as T1 where id = (select max(id), score from T as T2 where T1.score = T2.score)"; |
| 81 | + Assertions.assertThrows(AnalysisException.class, () -> PlanChecker.from(connectContext) |
| 82 | + .analyze(sql) |
| 83 | + .rewrite() |
| 84 | + ); |
| 85 | + } |
| 86 | +} |
0 commit comments