Skip to content

Commit

Permalink
update comments
Browse files Browse the repository at this point in the history
  • Loading branch information
englefly committed Nov 29, 2024
1 parent c49bb90 commit 0eeb184
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ public LogicalProperties(Supplier<List<Slot>> outputSupplier,
);
}

public LogicalProperties withOutputSupplier(Supplier<List<Slot>> outputSupplier) {
return new LogicalProperties(outputSupplier, dataTraitSupplier);
}

public List<Slot> getOutput() {
return outputSupplier.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.expressions.SlotReference;
import org.apache.doris.nereids.trees.expressions.functions.scalar.EncodeStr;
import org.apache.doris.nereids.trees.expressions.functions.scalar.EncodeString;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.logical.LogicalCatalogRelation;
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
Expand Down Expand Up @@ -78,13 +78,13 @@ private boolean containsEncode(LogicalProject<? extends Plan> project) {
}

private boolean containsEncode(Expression expr) {
return expr instanceof EncodeStr && expr.child(0) instanceof SlotReference;
return expr instanceof EncodeString && expr.child(0) instanceof SlotReference;
}

private List<Alias> collectEncodeAlias(LogicalProject<? extends Plan> project) {
List<Alias> encodeAlias = new ArrayList<>();
project.getProjects().forEach(e -> {
if (e instanceof Alias && e.child(0) instanceof EncodeStr) {
if (e instanceof Alias && e.child(0) instanceof EncodeString) {
encodeAlias.add((Alias) e);
}
});
Expand All @@ -94,15 +94,32 @@ private List<Alias> collectEncodeAlias(LogicalProject<? extends Plan> project) {
private LogicalProject<? extends Plan> pushDownEncodeSlot(LogicalProject<? extends Plan> project) {
List<Alias> encodeAlias = collectEncodeAlias(project);
LogicalProject<? extends Plan> result = (LogicalProject<? extends Plan>)
project.accept(EncodeSlotPushDownVisitor.visitor, encodeAlias);
project.accept(EncodeSlotPushDownVisitor.INSTANCE, encodeAlias);
return result;
}

/**
* push down encode slot
*/
public static class EncodeSlotPushDownVisitor extends PlanVisitor<Plan, List<Alias>> {
public static EncodeSlotPushDownVisitor visitor = new EncodeSlotPushDownVisitor();
public static EncodeSlotPushDownVisitor INSTANCE = new EncodeSlotPushDownVisitor();

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()) {
if (replaceMap.containsKey(expr) && replaceMap.get(expr) instanceof NamedExpression) {
newProjections.add((NamedExpression) replaceMap.get(expr));
changed = true;
} else {
newProjections.add(expr);
}
}
if (changed) {
return project.withProjects(newProjections);
}
return project;
}

@Override
public Plan visit(Plan plan, List<Alias> encodeAlias) {
Expand All @@ -111,11 +128,11 @@ public Plan visit(Plan plan, List<Alias> encodeAlias) {
// slot1 -> slot2
Map<Expression, Slot> replaceMap = new HashMap<>();
List<Set<Slot>> byPassSlots = plan.children().stream()
.map(this::getByPassSlot)
.map(this::getPassThroughSlots)
.collect(Collectors.toList());
Map<Plan, List<Alias>> toBePushed = new HashMap<>();
for (Alias alias : encodeAlias) {
EncodeStr encode = (EncodeStr) alias.child();
EncodeString encode = (EncodeString) alias.child();
Expression strExpr = encode.child();
if (strExpr instanceof SlotReference) {
for (int i = 0; i < byPassSlots.size(); i++) {
Expand All @@ -134,7 +151,9 @@ public Plan visit(Plan plan, List<Alias> encodeAlias) {
// 1. rewrite project's expressions: project(slot2),
// 2. push encode_as_int(slot1) as slot2 down to project.child()
// rewrite expressions
plan = plan.replaceExpressions(replaceMap);
if (plan instanceof LogicalProject) {
plan = replaceProjectsEncodeSlot((LogicalProject)plan, replaceMap);
}
// rewrite children
ImmutableList.Builder<Plan> newChildren = ImmutableList.builderWithExpectedSize(plan.arity());
boolean hasNewChildren = false;
Expand Down Expand Up @@ -170,8 +189,8 @@ public Plan visit(Plan plan, List<Alias> encodeAlias) {
return plan;
}

private Set<Slot> getByPassSlot(Plan plan) {
Set<Slot> outputSlots = Sets.newHashSet(plan.getOutput());
private Set<Slot> getPassThroughSlots(Plan plan) {
Set<Slot> outputSlots = Sets.newHashSet(plan.getOutputSet());
outputSlots.removeAll(plan.getInputSlots());
return outputSlots;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.expressions.functions.scalar.DecodeAsVarchar;
import org.apache.doris.nereids.trees.expressions.functions.scalar.EncodeStr;
import org.apache.doris.nereids.trees.expressions.functions.scalar.EncodeString;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
import org.apache.doris.nereids.trees.plans.logical.LogicalLimit;
Expand Down Expand Up @@ -149,7 +149,7 @@ private static Set<Expression> eliminateDecodeAndEncode(Set<Expression> expressi
}

private static Expression eliminateDecodeAndEncode(Expression expression) {
if (expression instanceof DecodeAsVarchar && expression.child(0) instanceof EncodeStr) {
if (expression instanceof DecodeAsVarchar && expression.child(0) instanceof EncodeString) {
return expression.child(0).child(0);
}
boolean hasNewChild = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/**
* ScalarFunction 'EncodeAsBigInt'.
*/
public class EncodeAsBigInt extends EncodeStr
public class EncodeAsBigInt extends EncodeString
implements ExplicitlyCastableSignature, PropagateNullable {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/**
* ScalarFunction 'EncodeAsInt'.
*/
public class EncodeAsInt extends EncodeStr
public class EncodeAsInt extends EncodeString
implements ExplicitlyCastableSignature, PropagateNullable {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/**
* ScalarFunction 'EncodeAsLargeInt'.
*/
public class EncodeAsLargeInt extends EncodeStr
public class EncodeAsLargeInt extends EncodeString
implements ExplicitlyCastableSignature, PropagateNullable {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/**
* ScalarFunction 'CompressAsSmallInt'.
*/
public class EncodeAsSmallInt extends EncodeStr
public class EncodeAsSmallInt extends EncodeString
implements ExplicitlyCastableSignature, PropagateNullable {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
/**
* Encode_as_XXXInt
*/
public abstract class EncodeStr extends ScalarFunction implements UnaryExpression {
public abstract class EncodeString extends ScalarFunction implements UnaryExpression {
/**
* constructor with 1 argument.
*/
public EncodeStr(String name, Expression arg0) {
public EncodeString(String name, Expression arg0) {
super(name, arg0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,4 @@ default String getGroupIdAsString() {
default String getGroupIdWithPrefix() {
return "@" + getGroupIdAsString();
}

default Plan replaceExpressions(Map<? extends Expression, ? extends Expression> replaceMap) {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -278,22 +278,4 @@ public void computeFd(DataTrait.Builder builder) {
}
}
}

@Override
public Plan replaceExpressions(Map<? extends Expression, ? extends Expression> replaceMap) {
List<NamedExpression> newProjections = new ArrayList<>();
boolean changed = false;
for (NamedExpression expr : getProjects()) {
if (replaceMap.containsKey(expr) && replaceMap.get(expr) instanceof NamedExpression) {
newProjections.add((NamedExpression) replaceMap.get(expr));
changed = true;
} else {
newProjections.add(expr);
}
}
if (changed) {
return this.withProjects(newProjections);
}
return this;
}
}

0 comments on commit 0eeb184

Please sign in to comment.