Skip to content

Commit

Permalink
sort-proj
Browse files Browse the repository at this point in the history
  • Loading branch information
englefly committed Nov 29, 2024
1 parent 0eeb184 commit 3daf48f
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.doris.nereids.util.PlanUtils;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import java.util.ArrayList;
Expand Down Expand Up @@ -91,11 +92,24 @@ private List<Alias> collectEncodeAlias(LogicalProject<? extends Plan> project) {
return encodeAlias;
}

private LogicalProject<? extends Plan> rewriteRootProject(LogicalProject<? extends Plan> project,
List<Alias> encodeAlias) {
List<NamedExpression> projections = project.getProjects().stream().map(
e -> encodeAlias.contains(e) ? e.toSlot() : e)
.collect(Collectors.toList());
return project.withProjects(projections);

}

private LogicalProject<? extends Plan> pushDownEncodeSlot(LogicalProject<? extends Plan> project) {
List<Alias> encodeAlias = collectEncodeAlias(project);
LogicalProject<? extends Plan> result = (LogicalProject<? extends Plan>)
project.accept(EncodeSlotPushDownVisitor.INSTANCE, encodeAlias);
return result;
project = rewriteRootProject(project, encodeAlias);
Plan child = project.child();
Plan newChild = child.accept(EncodeSlotPushDownVisitor.INSTANCE, encodeAlias);
if (child.equals(newChild)) {
project = (LogicalProject<? extends Plan>) project.withChildren(newChild);
}
return project;
}

/**
Expand All @@ -104,7 +118,8 @@ private LogicalProject<? extends Plan> pushDownEncodeSlot(LogicalProject<? exten
public static class EncodeSlotPushDownVisitor extends PlanVisitor<Plan, List<Alias>> {
public static EncodeSlotPushDownVisitor INSTANCE = new EncodeSlotPushDownVisitor();

public LogicalProject<Plan> replaceProjectsEncodeSlot(LogicalProject<Plan> project, Map<? extends Expression, ? extends Expression> replaceMap) {
public LogicalProject<Plan> replaceProjectsEncodeSlot(LogicalProject<Plan> project,
Map<? extends Expression, ? extends Expression> replaceMap) {
List<NamedExpression> newProjections = new ArrayList<>();
boolean changed = false;
for (NamedExpression expr : project.getProjects()) {
Expand Down Expand Up @@ -149,7 +164,7 @@ public Plan visit(Plan plan, List<Alias> encodeAlias) {
// rewrite plan according to encode expression
// for example: project(encode_as_int(slot1) as slot2)
// 1. rewrite project's expressions: project(slot2),
// 2. push encode_as_int(slot1) as slot2 down to project.child()
// 2. push "encode_as_int(slot1) as slot2" down to project.child()
// rewrite expressions
if (plan instanceof LogicalProject) {
plan = replaceProjectsEncodeSlot((LogicalProject)plan, replaceMap);
Expand Down Expand Up @@ -191,7 +206,17 @@ public Plan visit(Plan plan, List<Alias> encodeAlias) {

private Set<Slot> getPassThroughSlots(Plan plan) {
Set<Slot> outputSlots = Sets.newHashSet(plan.getOutputSet());
outputSlots.removeAll(plan.getInputSlots());
Set<Slot> keySlots = Sets.newHashSet();
if (plan instanceof LogicalProject) {
for (NamedExpression e : ((LogicalProject<?>) plan).getProjects()) {
if (!(e instanceof SlotReference)) {
keySlots.addAll(e.getInputSlots());
}
}
} else {
keySlots = plan.getInputSlots();
}
outputSlots.removeAll(keySlots);
return outputSlots;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public OlapTable getTable() {
@Override
public String toString() {
return Utils.toSqlString("LogicalOlapScan",
"name", table.getName(),
"qualified", qualifiedName(),
"indexName", getSelectedMaterializedIndexName().orElse("<index_not_selected>"),
"selectedIndexId", selectedIndexId,
"preAgg", preAggStatus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,58 @@ suite("compress_materialize") {
qt_sort "select * from compressSort order by k desc, v";
qt_sort "select * from compressSort order by k desc nulls last";
qt_sort "select * from compressSort order by k desc nulls last, v limit 3";

// push down encode slot
sql """
drop table if exists t1;
CREATE TABLE t1 (
`k1` int NOT NULL,
`v1` char(5) NOT NULL
) ENGINE=OLAP
DUPLICATE KEY(`k1`)
DISTRIBUTED BY HASH(`k1`) BUCKETS 3
PROPERTIES (
"replication_allocation" = "tag.location.default: 1"
);
insert into t1 values (1, "a"), (2, "b");
drop table if exists t2;
CREATE TABLE t2 (
`k2` int NOT NULL,
`v2` char(5) NOT NULL
) ENGINE=OLAP
DUPLICATE KEY(`k2`)
DISTRIBUTED BY HASH(`k2`) BUCKETS 3
PROPERTIES (
"replication_allocation" = "tag.location.default: 1"
);
insert into t2 values (3, "c"), (4, "d");
"""

qt_explain_sort_agg """
explain physical plan
select v1
from (select sum(k1) as k, v1 from t1 group by v1) t
order by v1;
"""

qt_exec_sort_agg """
select v1
from (select sum(k1) as k, v1 from t1 group by v1) t
order by v1;
"""

qt_sort_proj
"""
explain physical plan
select v1
from t1
where k1 > 0
order by v1;
"""

// TODO: select k, v as x from (select k1 as k, v1 as v from t1) A union all (select k2 as k, v2 as v from t2) order by x; -- 下推 union
}

0 comments on commit 3daf48f

Please sign in to comment.