-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[fix](set operation) Use regular child output for set operation rules #64908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package org.apache.doris.nereids.rules.rewrite; | ||
|
|
||
| import org.apache.doris.nereids.trees.expressions.Alias; | ||
| import org.apache.doris.nereids.trees.expressions.ExprId; | ||
| import org.apache.doris.nereids.trees.expressions.NamedExpression; | ||
| import org.apache.doris.nereids.trees.expressions.SlotReference; | ||
| import org.apache.doris.nereids.trees.expressions.StatementScopeIdGenerator; | ||
| import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral; | ||
| import org.apache.doris.nereids.trees.plans.Plan; | ||
| import org.apache.doris.nereids.trees.plans.RelationId; | ||
| import org.apache.doris.nereids.trees.plans.algebra.SetOperation.Qualifier; | ||
| import org.apache.doris.nereids.trees.plans.logical.LogicalOneRowRelation; | ||
| import org.apache.doris.nereids.trees.plans.logical.LogicalUnion; | ||
| import org.apache.doris.nereids.types.IntegerType; | ||
| import org.apache.doris.nereids.util.MemoPatternMatchSupported; | ||
| import org.apache.doris.nereids.util.MemoTestUtils; | ||
| import org.apache.doris.nereids.util.PlanChecker; | ||
| import org.apache.doris.utframe.TestWithFeService; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| class SetOperationOutputMappingTest extends TestWithFeService implements MemoPatternMatchSupported { | ||
|
|
||
| @Override | ||
| protected void runBeforeAll() throws Exception { | ||
| createDatabase("test"); | ||
| useDatabase("test"); | ||
| connectContext.getSessionVariable().setDisableNereidsRules("PRUNE_EMPTY_PARTITION"); | ||
|
|
||
| createTable("CREATE TABLE set_operation_output_mapping_t (\n" | ||
| + " k int NULL,\n" | ||
| + " v int NULL\n" | ||
| + ") ENGINE=OLAP\n" | ||
| + "DISTRIBUTED BY HASH(k) BUCKETS 1\n" | ||
| + "PROPERTIES (\n" | ||
| + " \"replication_allocation\" = \"tag.location.default: 1\"\n" | ||
| + ");"); | ||
| } | ||
|
|
||
| @Override | ||
| protected void runBeforeEach() throws Exception { | ||
| StatementScopeIdGenerator.clear(); | ||
| } | ||
|
|
||
| @Test | ||
| void testMergeOneRowRelationUsesRegularChildOutput() { | ||
| Alias firstProject = new Alias(new ExprId(1), new IntegerLiteral(10), "first_col"); | ||
| Alias secondProject = new Alias(new ExprId(2), new IntegerLiteral(20), "second_col"); | ||
| SlotReference secondProjectSlot = (SlotReference) secondProject.toSlot(); | ||
| LogicalOneRowRelation oneRowRelation = new LogicalOneRowRelation( | ||
| new RelationId(1), ImmutableList.of(firstProject, secondProject)); | ||
|
|
||
| SlotReference unionOutput = new SlotReference(new ExprId(10), "second_col", | ||
| IntegerType.INSTANCE, false, ImmutableList.of()); | ||
| LogicalUnion union = new LogicalUnion(Qualifier.ALL, | ||
| ImmutableList.of(unionOutput), | ||
| ImmutableList.of(ImmutableList.of(secondProjectSlot)), | ||
| ImmutableList.of(), | ||
| false, | ||
| ImmutableList.of(oneRowRelation)); | ||
|
|
||
| Plan rewritten = PlanChecker.from(MemoTestUtils.createConnectContext(), union) | ||
| .applyTopDown(new MergeOneRowRelationIntoUnion()) | ||
| .getPlan(); | ||
|
|
||
| Assertions.assertInstanceOf(LogicalUnion.class, rewritten); | ||
| LogicalUnion rewrittenUnion = (LogicalUnion) rewritten; | ||
| Assertions.assertEquals(0, rewrittenUnion.children().size()); | ||
| Assertions.assertEquals(1, rewrittenUnion.getConstantExprsList().size()); | ||
|
|
||
| List<NamedExpression> constantExprs = rewrittenUnion.getConstantExprsList().get(0); | ||
| Assertions.assertEquals(1, constantExprs.size()); | ||
| Assertions.assertEquals(secondProject.getExprId(), constantExprs.get(0).getExprId()); | ||
| Assertions.assertInstanceOf(IntegerLiteral.class, constantExprs.get(0).child(0)); | ||
| Assertions.assertEquals(20, ((IntegerLiteral) constantExprs.get(0).child(0)).getValue()); | ||
| } | ||
|
|
||
| @Test | ||
| void testPushDownTopNDistinctThroughUnionUsesRegularChildOutput() { | ||
| String sql = "SELECT *\n" | ||
| + "FROM (\n" | ||
| + " SELECT *\n" | ||
| + " FROM (\n" | ||
| + " SELECT k, v, v, ROW_NUMBER() OVER (ORDER BY k DESC)\n" | ||
| + " FROM set_operation_output_mapping_t\n" | ||
| + " ) u1\n" | ||
| + " UNION\n" | ||
| + " SELECT *\n" | ||
| + " FROM (\n" | ||
| + " SELECT k, v, v, ROW_NUMBER() OVER (ORDER BY k DESC)\n" | ||
| + " FROM set_operation_output_mapping_t\n" | ||
| + " ) u2\n" | ||
| + ") u\n" | ||
| + "ORDER BY 1\n" | ||
| + "LIMIT 10"; | ||
|
|
||
| PlanChecker.from(connectContext) | ||
| .analyze(sql) | ||
| .rewrite() | ||
| .matches( | ||
| logicalUnion( | ||
| logicalTopN().when(topN -> topN.getLimit() == 10 && topN.getOffset() == 0), | ||
| logicalTopN().when(topN -> topN.getLimit() == 10 && topN.getOffset() == 0) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void testPushDownLimitDistinctThroughUnionUsesRegularChildOutput() { | ||
| String sql = "SELECT DISTINCT *\n" | ||
| + "FROM (\n" | ||
| + " SELECT *\n" | ||
| + " FROM (\n" | ||
| + " SELECT k, v, v, ROW_NUMBER() OVER (ORDER BY k DESC)\n" | ||
| + " FROM set_operation_output_mapping_t\n" | ||
| + " ) u1\n" | ||
| + " UNION ALL\n" | ||
| + " SELECT *\n" | ||
| + " FROM (\n" | ||
| + " SELECT k, v, v, ROW_NUMBER() OVER (ORDER BY k DESC)\n" | ||
| + " FROM set_operation_output_mapping_t\n" | ||
| + " ) u2\n" | ||
| + ") u\n" | ||
| + "LIMIT 10"; | ||
|
|
||
| PlanChecker.from(connectContext) | ||
| .analyze(sql) | ||
| .rewrite() | ||
| .matches( | ||
| logicalUnion( | ||
| logicalLimit().when(limit -> limit.getLimit() == 10 && limit.getOffset() == 0), | ||
| logicalLimit().when(limit -> limit.getLimit() == 10 && limit.getOffset() == 0) | ||
| ) | ||
| ); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| suite("merge_one_row_relation_into_union") { | ||
| sql "SET enable_nereids_planner=true" | ||
| sql "SET enable_fallback_to_original_planner=false" | ||
|
|
||
| sql """ | ||
| EXPLAIN SHAPE PLAN SELECT v | ||
| FROM ( | ||
| SELECT CAST('unused' AS CHAR(6)) AS k, CAST('alpha' AS CHAR(6)) AS v | ||
| UNION ALL | ||
| SELECT CAST('unused' AS CHAR(6)), CAST('beta' AS CHAR(6)) | ||
| ) u | ||
| GROUP BY v | ||
| """ | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| suite("push_down_limit_distinct_through_union") { | ||
| sql "set parallel_pipeline_task_num=2" | ||
| sql "SET enable_nereids_planner=true" | ||
| sql "SET enable_fallback_to_original_planner=false" | ||
| sql "set runtime_filter_mode=OFF" | ||
| sql "SET disable_join_reorder=true" | ||
| sql "set disable_nereids_rules=PRUNE_EMPTY_PARTITION" | ||
|
|
||
| sql """ | ||
| DROP TABLE IF EXISTS push_down_limit_distinct_union_t; | ||
| """ | ||
|
|
||
| sql """ | ||
| CREATE TABLE IF NOT EXISTS push_down_limit_distinct_union_t ( | ||
| `id` int NULL, | ||
| `score` int NULL | ||
| ) ENGINE = OLAP | ||
| DISTRIBUTED BY HASH(id) BUCKETS 1 | ||
| PROPERTIES ( | ||
| "replication_allocation" = "tag.location.default: 1" | ||
| ); | ||
| """ | ||
|
|
||
| sql """ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make this EXPLAIN a named |
||
| explain shape plan select distinct * | ||
| from ( | ||
| select * | ||
| from ( | ||
| select id, score, score, row_number() over (order by id desc) | ||
| from push_down_limit_distinct_union_t | ||
| ) u1 | ||
| union all | ||
| select * | ||
| from ( | ||
| select id, score, score, row_number() over (order by id desc) | ||
| from push_down_limit_distinct_union_t | ||
| ) u2 | ||
| ) u | ||
| limit 10; | ||
| """ | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,7 +72,26 @@ suite("push_down_top_n_distinct_through_union") { | |
| explain shape plan select * from ((select * from table2 t1 limit 5) union (select * from table2 t2 limit 5)) sub order by id limit 10; | ||
| """ | ||
|
|
||
| sql """ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make this new EXPLAIN a named |
||
| explain shape plan select * | ||
| from ( | ||
| select * | ||
| from ( | ||
| select id, score, score, row_number() over (order by id desc) | ||
| from table2 | ||
| ) u1 | ||
| union | ||
| select * | ||
| from ( | ||
| select id, score, score, row_number() over (order by id desc) | ||
| from table2 | ||
| ) u2 | ||
| ) u | ||
| order by 1 | ||
| limit 10; | ||
| """ | ||
|
|
||
| qt_push_down_topn_union_complex_conditions """ | ||
| explain shape plan select * from (select * from table2 t1 where t1.score > 10 and t1.name = 'Test' union select * from table2 t2 where t2.id < 5 and t2.score < 20) sub order by id limit 10; | ||
| """ | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make this EXPLAIN a named
qt_...check and add the generated.outoutput instead of running it with plainsql.sqlonly executes the EXPLAIN; it does not compare the shape plan, so this regression would still pass if the one-row merge stopped producing the intended shape as long as planning succeeds.