[improvement](Nereids) Support to remove sort which is under table sink (#31751) (#32337)

This commit is contained in:
seawinde
2024-03-17 15:45:53 +08:00
committed by GitHub
parent c34f5045c8
commit 47019133c0
7 changed files with 219 additions and 10 deletions

View File

@ -22,6 +22,7 @@ import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
import org.apache.doris.nereids.trees.plans.logical.LogicalSink;
import org.apache.doris.nereids.trees.plans.logical.LogicalSort;
import org.apache.doris.nereids.trees.plans.logical.LogicalTableSink;
import org.apache.doris.nereids.trees.plans.visitor.CustomRewriter;
import org.apache.doris.nereids.trees.plans.visitor.DefaultPlanRewriter;
@ -29,10 +30,10 @@ import java.util.ArrayList;
import java.util.List;
/**
* Eliminate sort that is not directly below result sink
* Eliminate sort that is not directly below result sink, if there is project between result sink and sort,
* the sort will not be eliminated.
* Note we have put limit in sort node so that we don't need to consider limit
*/
public class EliminateSort extends DefaultPlanRewriter<Boolean> implements CustomRewriter {
@Override
public Plan rewriteRoot(Plan plan, JobContext jobContext) {
@ -45,6 +46,7 @@ public class EliminateSort extends DefaultPlanRewriter<Boolean> implements Custo
List<Plan> newChildren = new ArrayList<>();
boolean hasNewChildren = false;
for (Plan child : plan.children()) {
// eliminate sort default
Plan newChild = child.accept(this, true);
if (newChild != child) {
hasNewChildren = true;
@ -64,14 +66,17 @@ public class EliminateSort extends DefaultPlanRewriter<Boolean> implements Custo
@Override
public Plan visitLogicalProject(LogicalProject<? extends Plan> project, Boolean eliminateSort) {
// sometimes there is project between logicalResultSink and sort, should skip eliminate
return skipEliminateSort(project, eliminateSort);
}
@Override
public Plan visitLogicalSink(LogicalSink<? extends Plan> sink, Boolean eliminateSort) {
// 1. table sink: eliminate -> true
// 2. sink -> tablesink -> olaptablesink
return skipEliminateSort(sink, eliminateSort);
public Plan visitLogicalSink(LogicalSink<? extends Plan> logicalSink, Boolean eliminateSort) {
if (logicalSink instanceof LogicalTableSink) {
// eliminate sort
return visit(logicalSink, true);
}
return skipEliminateSort(logicalSink, eliminateSort);
}
private Plan skipEliminateSort(Plan plan, Boolean eliminateSort) {

View File

@ -41,7 +41,7 @@ import java.util.Optional;
/**
* logical olap table sink for insert command
*/
public class LogicalOlapTableSink<CHILD_TYPE extends Plan> extends LogicalSink<CHILD_TYPE>
public class LogicalOlapTableSink<CHILD_TYPE extends Plan> extends LogicalTableSink<CHILD_TYPE>
implements Sink, PropagateFuncDeps {
// bound data sink
private final Database database;

View File

@ -0,0 +1,43 @@
// 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.trees.plans.logical;
import org.apache.doris.nereids.memo.GroupExpression;
import org.apache.doris.nereids.properties.LogicalProperties;
import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.PlanType;
import java.util.List;
import java.util.Optional;
/**
* Logical table sink for all table type sink
*/
public abstract class LogicalTableSink<CHILD_TYPE extends Plan> extends LogicalSink<CHILD_TYPE> {
public LogicalTableSink(PlanType type,
List<NamedExpression> outputExprs, CHILD_TYPE child) {
super(type, outputExprs, child);
}
public LogicalTableSink(PlanType type, List<NamedExpression> outputExprs,
Optional<GroupExpression> groupExpression,
Optional<LogicalProperties> logicalProperties, CHILD_TYPE child) {
super(type, outputExprs, groupExpression, logicalProperties, child);
}
}

View File

@ -50,7 +50,7 @@ import java.util.Set;
/**
* physical olap table sink for insert command
*/
public class PhysicalOlapTableSink<CHILD_TYPE extends Plan> extends PhysicalSink<CHILD_TYPE> implements Sink {
public class PhysicalOlapTableSink<CHILD_TYPE extends Plan> extends PhysicalTableSink<CHILD_TYPE> implements Sink {
private final Database database;
private final OlapTable targetTable;

View File

@ -0,0 +1,45 @@
// 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.trees.plans.physical;
import org.apache.doris.nereids.memo.GroupExpression;
import org.apache.doris.nereids.properties.LogicalProperties;
import org.apache.doris.nereids.properties.PhysicalProperties;
import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.PlanType;
import org.apache.doris.statistics.Statistics;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Optional;
/**
* Physical table sink for all table type sink
*/
public abstract class PhysicalTableSink<CHILD_TYPE extends Plan> extends PhysicalSink<CHILD_TYPE> {
public PhysicalTableSink(PlanType type, List<NamedExpression> outputExprs,
Optional<GroupExpression> groupExpression,
LogicalProperties logicalProperties,
@Nullable PhysicalProperties physicalProperties,
Statistics statistics, CHILD_TYPE child) {
super(type, outputExprs, groupExpression, logicalProperties, physicalProperties, statistics, child);
}
}

View File

@ -25,11 +25,13 @@ import org.apache.doris.nereids.trees.plans.logical.LogicalFileSink;
import org.apache.doris.nereids.trees.plans.logical.LogicalOlapTableSink;
import org.apache.doris.nereids.trees.plans.logical.LogicalResultSink;
import org.apache.doris.nereids.trees.plans.logical.LogicalSink;
import org.apache.doris.nereids.trees.plans.logical.LogicalTableSink;
import org.apache.doris.nereids.trees.plans.physical.PhysicalDeferMaterializeResultSink;
import org.apache.doris.nereids.trees.plans.physical.PhysicalFileSink;
import org.apache.doris.nereids.trees.plans.physical.PhysicalOlapTableSink;
import org.apache.doris.nereids.trees.plans.physical.PhysicalResultSink;
import org.apache.doris.nereids.trees.plans.physical.PhysicalSink;
import org.apache.doris.nereids.trees.plans.physical.PhysicalTableSink;
/**
* sink visitor
@ -64,6 +66,10 @@ public interface SinkVisitor<R, C> {
return visitLogicalSink(fileSink, context);
}
default R visitLogicalTableSink(LogicalTableSink<? extends Plan> logicalTableSink, C context) {
return visitLogicalSink(logicalTableSink, context);
}
default R visitLogicalOlapTableSink(LogicalOlapTableSink<? extends Plan> olapTableSink, C context) {
return visitLogicalSink(olapTableSink, context);
}
@ -85,6 +91,10 @@ public interface SinkVisitor<R, C> {
return visitPhysicalSink(fileSink, context);
}
default R visitPhysicalTableSink(PhysicalTableSink<? extends Plan> physicalTableSink, C context) {
return visitPhysicalSink(physicalTableSink, context);
}
default R visitPhysicalOlapTableSink(PhysicalOlapTableSink<? extends Plan> olapTableSink, C context) {
return visitPhysicalSink(olapTableSink, context);
}