|
18 | 18 | package org.apache.doris.nereids.jobs;
|
19 | 19 |
|
20 | 20 | import org.apache.doris.nereids.exceptions.AnalysisException;
|
| 21 | +import org.apache.doris.nereids.memo.CopyInResult; |
| 22 | +import org.apache.doris.nereids.memo.Group; |
21 | 23 | import org.apache.doris.nereids.memo.GroupExpression;
|
22 | 24 | import org.apache.doris.nereids.rules.Rule;
|
23 | 25 | import org.apache.doris.nereids.rules.RuleSet;
|
| 26 | +import org.apache.doris.nereids.trees.plans.Plan; |
| 27 | +import org.apache.doris.qe.ConnectContext; |
| 28 | + |
| 29 | +import com.google.common.base.Preconditions; |
| 30 | +import org.apache.logging.log4j.LogManager; |
| 31 | +import org.apache.logging.log4j.Logger; |
24 | 32 |
|
25 | 33 | import java.util.List;
|
26 | 34 | import java.util.Objects;
|
| 35 | +import java.util.Optional; |
27 | 36 | import java.util.stream.Collectors;
|
28 | 37 |
|
29 | 38 | /**
|
30 | 39 | * Abstract class for all job using for analyze and optimize query plan in Nereids.
|
31 | 40 | */
|
32 | 41 | public abstract class Job {
|
| 42 | + public final Logger logger = LogManager.getLogger(getClass()); |
| 43 | + |
33 | 44 | protected JobType type;
|
34 | 45 | protected JobContext context;
|
35 | 46 | protected boolean once;
|
| 47 | + protected final boolean enableTrace; |
36 | 48 |
|
37 | 49 | public Job(JobType type, JobContext context) {
|
38 |
| - this.type = type; |
39 |
| - this.context = context; |
40 |
| - this.once = true; |
| 50 | + this(type, context, true); |
41 | 51 | }
|
42 | 52 |
|
| 53 | + /** job full parameter constructor */ |
43 | 54 | public Job(JobType type, JobContext context, boolean once) {
|
44 | 55 | this.type = type;
|
45 | 56 | this.context = context;
|
46 | 57 | this.once = once;
|
| 58 | + ConnectContext connectContext = ConnectContext.get(); |
| 59 | + this.enableTrace = connectContext == null |
| 60 | + ? false |
| 61 | + : connectContext.getSessionVariable().isEnableNereidsTrace(); |
47 | 62 | }
|
48 | 63 |
|
49 | 64 | public void pushJob(Job job) {
|
@@ -73,4 +88,48 @@ public List<Rule> getValidRules(GroupExpression groupExpression,
|
73 | 88 | }
|
74 | 89 |
|
75 | 90 | public abstract void execute() throws AnalysisException;
|
| 91 | + |
| 92 | + protected Optional<CopyInResult> invokeRewriteRuleWithTrace(Rule rule, Plan before, Group targetGroup) { |
| 93 | + context.onInvokeRule(rule.getRuleType()); |
| 94 | + |
| 95 | + String traceBefore = enableTrace ? getTraceLog(rule) : null; |
| 96 | + |
| 97 | + List<Plan> afters = rule.transform(before, context.getCascadesContext()); |
| 98 | + Preconditions.checkArgument(afters.size() == 1); |
| 99 | + Plan after = afters.get(0); |
| 100 | + |
| 101 | + if (after != before) { |
| 102 | + CopyInResult result = context.getCascadesContext() |
| 103 | + .getMemo() |
| 104 | + .copyIn(after, targetGroup, true); |
| 105 | + |
| 106 | + if (result.generateNewExpression && enableTrace) { |
| 107 | + String traceAfter = getTraceLog(rule); |
| 108 | + printTraceLog(rule, traceBefore, traceAfter); |
| 109 | + } |
| 110 | + |
| 111 | + return Optional.of(result); |
| 112 | + } |
| 113 | + |
| 114 | + return Optional.empty(); |
| 115 | + } |
| 116 | + |
| 117 | + protected String getTraceLog(Rule rule) { |
| 118 | + if (rule.isRewrite()) { |
| 119 | + return context.getCascadesContext() |
| 120 | + .getMemo() |
| 121 | + .copyOut(false) |
| 122 | + .treeString(); |
| 123 | + } else { |
| 124 | + return context.getCascadesContext() |
| 125 | + .getMemo() |
| 126 | + .getRoot() |
| 127 | + .treeString(); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + protected void printTraceLog(Rule rule, String traceBefore, String traceAfter) { |
| 132 | + logger.info("========== {} {} ==========\nbefore:\n{}\n\nafter:\n{}\n", |
| 133 | + getClass().getSimpleName(), rule.getRuleType(), traceBefore, traceAfter); |
| 134 | + } |
76 | 135 | }
|
0 commit comments