-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
[feat](nerieds)Push encode slot #45958
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/DecoupleEncodeDecode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// 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.rules.Rule; | ||
import org.apache.doris.nereids.rules.RuleType; | ||
import org.apache.doris.nereids.trees.expressions.Alias; | ||
import org.apache.doris.nereids.trees.expressions.Expression; | ||
import org.apache.doris.nereids.trees.expressions.NamedExpression; | ||
import org.apache.doris.nereids.trees.expressions.functions.scalar.DecodeAsVarchar; | ||
import org.apache.doris.nereids.trees.expressions.functions.scalar.EncodeString; | ||
import org.apache.doris.nereids.trees.plans.logical.LogicalProject; | ||
import org.apache.doris.qe.ConnectContext; | ||
|
||
import com.google.common.collect.Lists; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* in project: | ||
* decode_as_varchar(encode_as_xxx(v)) => v | ||
*/ | ||
public class DecoupleEncodeDecode extends OneRewriteRuleFactory { | ||
@Override | ||
public Rule build() { | ||
return logicalProject() | ||
.when(topN -> ConnectContext.get() != null | ||
&& ConnectContext.get().getSessionVariable().enableCompressMaterialize) | ||
.then(this::rewrite) | ||
.toRule(RuleType.DECOUPLE_DECODE_ENCODE_SLOT); | ||
} | ||
|
||
private LogicalProject<?> rewrite(LogicalProject<?> project) { | ||
List<NamedExpression> newProjections = Lists.newArrayList(); | ||
boolean hasNewProjections = false; | ||
for (NamedExpression e : project.getProjects()) { | ||
boolean changed = false; | ||
if (e instanceof Alias) { | ||
Alias alias = (Alias) e; | ||
Expression body = alias.child(); | ||
if (body instanceof DecodeAsVarchar && body.child(0) instanceof EncodeString) { | ||
Expression encodeBody = body.child(0).child(0); | ||
newProjections.add((NamedExpression) alias.withChildren(encodeBody)); | ||
changed = true; | ||
} else if (body instanceof EncodeString && body.child(0) instanceof DecodeAsVarchar) { | ||
Expression decodeBody = body.child(0).child(0); | ||
newProjections.add((NamedExpression) alias.withChildren(decodeBody)); | ||
changed = true; | ||
} | ||
} | ||
if (!changed) { | ||
newProjections.add(e); | ||
hasNewProjections = true; | ||
} | ||
} | ||
if (hasNewProjections) { | ||
project = project.withProjects(newProjections); | ||
} | ||
return project; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -586,10 +586,17 @@ boolean comparePlan(Plan plan1, Plan plan2) { | |
isEqual = false; | ||
} | ||
for (int i = 0; isEqual && i < plan2.getOutput().size(); i++) { | ||
NamedExpression expr = ((LogicalProject<?>) plan1).getProjects().get(i); | ||
NamedExpression replacedExpr = (NamedExpression) | ||
expr.rewriteUp(e -> plan1ToPlan2.getOrDefault(e, e)); | ||
if (!replacedExpr.equals(((LogicalProject<?>) plan2).getProjects().get(i))) { | ||
Expression expr1 = ((LogicalProject<?>) plan1).getProjects().get(i); | ||
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. is this a bug fix? what's the scenario the bug can reproduce? 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. yes. tpcds q4. This is double checked by @feiniaofeiafei |
||
Expression expr2 = ((LogicalProject<?>) plan2).getProjects().get(i); | ||
if (expr1 instanceof Alias) { | ||
if (!(expr2 instanceof Alias)) { | ||
return false; | ||
} | ||
expr1 = expr1.child(0); | ||
expr2 = expr2.child(0); | ||
} | ||
Expression replacedExpr = expr1.rewriteUp(e -> plan1ToPlan2.getOrDefault(e, e)); | ||
if (!replacedExpr.equals(expr2)) { | ||
isEqual = false; | ||
} | ||
} | ||
|
106 changes: 106 additions & 0 deletions
106
.../src/main/java/org/apache/doris/nereids/rules/rewrite/PullUpProjectBetweenTopNAndAgg.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// 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.properties.OrderKey; | ||
import org.apache.doris.nereids.rules.Rule; | ||
import org.apache.doris.nereids.rules.RuleType; | ||
import org.apache.doris.nereids.trees.expressions.Alias; | ||
import org.apache.doris.nereids.trees.expressions.Expression; | ||
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.plans.Plan; | ||
import org.apache.doris.nereids.trees.plans.logical.LogicalProject; | ||
import org.apache.doris.nereids.trees.plans.logical.LogicalTopN; | ||
import org.apache.doris.qe.ConnectContext; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* | ||
* try to reduce shuffle cost of topN operator, used to optimize plan after applying Compress_materialize | ||
* | ||
* topn(orderKey=[a]) | ||
* --> project(a+1 as x, a+2 as y, a) | ||
* --> any(output(a)) | ||
* => | ||
* project(a+1 as x, a+2 as y, a) | ||
* --> topn(orderKey=[a]) | ||
* --> any(output(a)) | ||
* | ||
*/ | ||
public class PullUpProjectBetweenTopNAndAgg extends OneRewriteRuleFactory { | ||
public static final Logger LOG = LogManager.getLogger(PullUpProjectBetweenTopNAndAgg.class); | ||
|
||
@Override | ||
public Rule build() { | ||
return logicalTopN(logicalProject(logicalAggregate())) | ||
.when(topN -> ConnectContext.get() != null | ||
&& ConnectContext.get().getSessionVariable().enableCompressMaterialize) | ||
.then(topN -> adjust(topN)).toRule(RuleType.ADJUST_TOPN_PROJECT); | ||
} | ||
|
||
private Plan adjust(LogicalTopN<? extends Plan> topN) { | ||
LogicalProject<Plan> project = (LogicalProject<Plan>) topN.child(); | ||
Set<Slot> projectInputSlots = project.getInputSlots(); | ||
Map<SlotReference, SlotReference> keyAsKey = new HashMap<>(); | ||
for (NamedExpression proj : project.getProjects()) { | ||
if (proj instanceof Alias && ((Alias) proj).child(0) instanceof SlotReference) { | ||
keyAsKey.put((SlotReference) ((Alias) proj).toSlot(), (SlotReference) ((Alias) proj).child()); | ||
} | ||
} | ||
boolean match = true; | ||
List<OrderKey> newOrderKeys = new ArrayList<>(); | ||
for (OrderKey orderKey : topN.getOrderKeys()) { | ||
Expression orderExpr = orderKey.getExpr(); | ||
if (orderExpr instanceof SlotReference) { | ||
if (projectInputSlots.contains(orderExpr)) { | ||
newOrderKeys.add(orderKey); | ||
} else if (keyAsKey.containsKey(orderExpr)) { | ||
newOrderKeys.add(orderKey.withExpression(keyAsKey.get(orderExpr))); | ||
} else { | ||
match = false; | ||
break; | ||
} | ||
} else { | ||
match = false; | ||
break; | ||
} | ||
} | ||
if (match) { | ||
if (project.getProjects().size() >= project.getInputSlots().size()) { | ||
LOG.info("$$$$ before: project.getProjects() = " + project.getProjects()); | ||
LOG.info("$$$$ before: project.getInputSlots() = " + project.getInputSlots()); | ||
LOG.info("$$$$ before: " + topN.treeString()); | ||
topN = topN.withChildren(project.children()).withOrderKeys(newOrderKeys); | ||
project = (LogicalProject<Plan>) project.withChildren(topN); | ||
LOG.info("$$$$ after:" + project.treeString()); | ||
return project; | ||
} | ||
} | ||
return topN; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
inactive in this pr?
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.
REPEAT node has a strict requirement, which is not good. I plan to add rule for repeat and drop that requirement in future