Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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.hint;

/**
* rule hint.
*/
public class UseCboRuleHint extends Hint {

private final boolean isNotUseCboRule;

public UseCboRuleHint(String hintName, boolean isNotUseCboRule) {
super(hintName);
this.isNotUseCboRule = isNotUseCboRule;
}

public boolean isNotUseCboRule() {
return isNotUseCboRule;
}

@Override
public String getExplainString() {
StringBuilder out = new StringBuilder();
if (isNotUseCboRule) {
out.append("no_use_");
} else {
out.append("use_");
}
out.append(getHintName());
return out.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,11 @@ public class Rewriter extends AbstractBatchJobExecutor {
),

topic("Eager aggregation",
topDown(
costBased(topDown(
new PushDownAggThroughJoinOneSide(),
new PushDownAggThroughJoin()
),
custom(RuleType.PUSH_DOWN_DISTINCT_THROUGH_JOIN, PushDownDistinctThroughJoin::new)
)),
costBased(custom(RuleType.PUSH_DOWN_DISTINCT_THROUGH_JOIN, PushDownDistinctThroughJoin::new))
),

// this rule should invoke after infer predicate and push down distinct, and before push down limit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,23 @@
import org.apache.doris.common.Pair;
import org.apache.doris.nereids.CascadesContext;
import org.apache.doris.nereids.cost.Cost;
import org.apache.doris.nereids.hint.Hint;
import org.apache.doris.nereids.hint.UseCboRuleHint;
import org.apache.doris.nereids.jobs.JobContext;
import org.apache.doris.nereids.jobs.executor.Optimizer;
import org.apache.doris.nereids.jobs.executor.Rewriter;
import org.apache.doris.nereids.memo.GroupExpression;
import org.apache.doris.nereids.rules.Rule;
import org.apache.doris.nereids.rules.RuleType;
import org.apache.doris.nereids.trees.plans.logical.LogicalCTEAnchor;
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
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.Arrays;
import java.util.List;
import java.util.Optional;

Expand All @@ -50,6 +57,12 @@ public CostBasedRewriteJob(List<RewriteJob> rewriteJobs) {

@Override
public void execute(JobContext jobContext) {
// checkHint.first means whether it use hint and checkHint.second means what kind of hint it used
Pair<Boolean, Hint> checkHint = checkRuleHint();
// this means it no_use_cbo_rule(xxx) hint
if (checkHint.first && checkHint.second == null) {
return;
}
CascadesContext currentCtx = jobContext.getCascadesContext();
CascadesContext skipCboRuleCtx = CascadesContext.newCurrentTreeContext(currentCtx);
CascadesContext applyCboRuleCtx = CascadesContext.newCurrentTreeContext(currentCtx);
Expand All @@ -70,12 +83,73 @@ public void execute(JobContext jobContext) {
rewriteJobs, currentCtx.getRewritePlan());
return;
}
if (checkHint.first) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if no need to rewrite, return at the beginning of execute to avoid useless rewrite for better perf

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have add one judgement before applying cbo rewrite rule

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it not enough, should skip very begining rbo too

checkHint.second.setStatus(Hint.HintStatus.SUCCESS);
if (((UseCboRuleHint) checkHint.second).isNotUseCboRule()) {
currentCtx.setRewritePlan(applyCboRuleCtx.getRewritePlan());
}
return;
}
// If the candidate applied cbo rule is better, replace the original plan with it.
if (appliedCboRuleCost.get().first.getValue() < skipCboRuleCost.get().first.getValue()) {
currentCtx.setRewritePlan(applyCboRuleCtx.getRewritePlan());
}
}

/**
* check if we have use rule hint or no use rule hint
* return an optional object which checkHint.first means whether it use hint
* and checkHint.second means what kind of hint it used
* example, when we use *+ no_use_cbo_rule(xxx) * the optional would be (true, false)
* which means it use hint and the hint forbid this kind of rule
*/
private Pair<Boolean, Hint> checkRuleHint() {
Pair<Boolean, Hint> checkResult = Pair.of(false, null);
if (rewriteJobs.get(0) instanceof RootPlanTreeRewriteJob) {
for (Rule rule : ((RootPlanTreeRewriteJob) rewriteJobs.get(0)).getRules()) {
checkResult = checkRuleHintWithHintName(rule.getRuleType());
if (checkResult.first) {
return checkResult;
}
}
}
if (rewriteJobs.get(0) instanceof CustomRewriteJob) {
checkResult = checkRuleHintWithHintName(((CustomRewriteJob) rewriteJobs.get(0)).getRuleType());
}
return checkResult;
}

/**
* for these rules we need use_cbo_rule hint to enable it, otherwise it would be close by default
*/
private static boolean checkBlackList(RuleType ruleType) {
List<RuleType> ruleWhiteList = new ArrayList<>(Arrays.asList(
RuleType.PUSH_DOWN_AGG_THROUGH_JOIN,
RuleType.PUSH_DOWN_AGG_THROUGH_JOIN_ONE_SIDE,
RuleType.PUSH_DOWN_DISTINCT_THROUGH_JOIN));
if (!ruleWhiteList.isEmpty() && ruleWhiteList.contains(ruleType)) {
return true;
}
return false;
}

/**
* main mechanism of checkRuleHint
* return an optional object which checkHint.first means whether it use hint
* and checkHint.second means what kind of hint it used
*/
private Pair<Boolean, Hint> checkRuleHintWithHintName(RuleType ruleType) {
for (Hint hint : ConnectContext.get().getStatementContext().getHints()) {
if (hint.getHintName().equalsIgnoreCase(ruleType.name())) {
return Pair.of(true, hint);
}
}
if (checkBlackList(ruleType)) {
return Pair.of(true, null);
}
return Pair.of(false, null);
}

@Override
public boolean isOnce() {
// TODO: currently, we do not support execute it more than once.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,9 @@ public void execute(JobContext context) {
public boolean isOnce() {
return false;
}

public RuleType getRuleType() {
return ruleType;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ public Plan getNewestPlan() {
}
}

public List<Rule> getRules() {
return rules;
}

/** use to assemble the rewriting plan */
private static class LinkPlanJob extends Job {
LinkPlanJob parentJob;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@
import org.apache.doris.nereids.properties.SelectHintLeading;
import org.apache.doris.nereids.properties.SelectHintOrdered;
import org.apache.doris.nereids.properties.SelectHintSetVar;
import org.apache.doris.nereids.properties.SelectHintUseCboRule;
import org.apache.doris.nereids.trees.TableSample;
import org.apache.doris.nereids.trees.expressions.Add;
import org.apache.doris.nereids.trees.expressions.And;
Expand Down Expand Up @@ -3126,6 +3127,22 @@ private LogicalPlan withSelectHint(LogicalPlan logicalPlan, SelectHintContext hi
case "ordered":
hints.put(hintName, new SelectHintOrdered(hintName));
break;
case "use_cbo_rule":
List<String> useRuleParameters = new ArrayList<String>();
for (HintAssignmentContext kv : hintStatement.parameters) {
String parameterName = visitIdentifierOrText(kv.key);
useRuleParameters.add(parameterName);
}
hints.put(hintName, new SelectHintUseCboRule(hintName, useRuleParameters, false));
break;
case "no_use_cbo_rule":
List<String> noUseRuleParameters = new ArrayList<String>();
for (HintAssignmentContext kv : hintStatement.parameters) {
String parameterName = visitIdentifierOrText(kv.key);
noUseRuleParameters.add(parameterName);
}
hints.put(hintName, new SelectHintUseCboRule(hintName, noUseRuleParameters, true));
break;
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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.properties;

import java.util.List;

/**
* select hint CostBasedRule.
*/
public class SelectHintUseCboRule extends SelectHint {
private final List<String> parameters;

private final boolean isNotUseCboRule;

public SelectHintUseCboRule(String hintName, List<String> parameters, boolean isNotUseCboRule) {
super(hintName);
this.parameters = parameters;
this.isNotUseCboRule = isNotUseCboRule;
}

public List<String> getParameters() {
return parameters;
}

public boolean isNotUseCboRule() {
return isNotUseCboRule;
}

@Override
public String getHintName() {
return super.getHintName();
}

@Override
public String toString() {
return super.getHintName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import org.apache.doris.nereids.hint.Hint;
import org.apache.doris.nereids.hint.LeadingHint;
import org.apache.doris.nereids.hint.OrderedHint;
import org.apache.doris.nereids.hint.UseCboRuleHint;
import org.apache.doris.nereids.properties.SelectHint;
import org.apache.doris.nereids.properties.SelectHintLeading;
import org.apache.doris.nereids.properties.SelectHintSetVar;
import org.apache.doris.nereids.properties.SelectHintUseCboRule;
import org.apache.doris.nereids.rules.Rule;
import org.apache.doris.nereids.rules.RuleType;
import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory;
Expand Down Expand Up @@ -73,6 +75,8 @@ public Rule build() {
} else if (hintName.equalsIgnoreCase("LEADING")) {
extractLeading((SelectHintLeading) hint.getValue(), ctx.cascadesContext,
ctx.statementContext, selectHintPlan.getHints());
} else if (hintName.equalsIgnoreCase("USE_CBO_RULE")) {
extractRule((SelectHintUseCboRule) hint.getValue(), ctx.statementContext);
} else {
logger.warn("Can not process select hint '{}' and skip it", hint.getKey());
}
Expand Down Expand Up @@ -146,4 +150,12 @@ private void extractLeading(SelectHintLeading selectHint, CascadesContext contex
assert (context != null);
}

private void extractRule(SelectHintUseCboRule selectHint, StatementContext statementContext) {
// rule hint need added to statementContext only cause it's set in all scopes
for (String parameter : selectHint.getParameters()) {
UseCboRuleHint hint = new UseCboRuleHint(parameter, selectHint.isNotUseCboRule());
statementContext.addHint(hint);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,7 @@ public void setEnableLeftZigZag(boolean enableLeftZigZag) {
@VariableMgr.VarAttr(name = DISABLE_NEREIDS_RULES, needForward = true)
private String disableNereidsRules = "";

@VariableMgr.VarAttr(name = ENABLE_NEREIDS_RULES, needForward = true)
@VariableMgr.VarAttr(name = ENABLE_NEREIDS_RULES, needForward = true, varType = VariableAnnotation.REMOVED)
public String enableNereidsRules = "";

@VariableMgr.VarAttr(name = ENABLE_NEW_COST_MODEL, needForward = true)
Expand Down
Loading