[Enhancement] reduce trace rule log (backport #62834) (#62904)

Signed-off-by: Seaven <seaven_7@qq.com>
Co-authored-by: Seaven <seaven_7@qq.com>
This commit is contained in:
mergify[bot] 2025-09-22 16:02:04 +08:00 committed by GitHub
parent bf1bd75627
commit 5f1aa6a870
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 5 deletions

View File

@ -19,6 +19,7 @@ import com.starrocks.common.profile.Tracers;
import com.starrocks.qe.ConnectContext;
import com.starrocks.sql.ast.QueryStatement;
import com.starrocks.sql.common.DebugRelationTracer;
import com.starrocks.sql.optimizer.operator.pattern.Pattern;
import com.starrocks.sql.optimizer.rule.Rule;
import org.slf4j.helpers.MessageFormatter;
@ -171,26 +172,40 @@ public class OptimizerTraceUtil {
args -> String.format("[TRACE QUERY %s] RULE %s exhausted \n", ctx.getQueryId(), rule));
}
private static int calculateRuleDepth(Pattern pattern) {
if (pattern == null) {
return 0;
}
int maxChildDepth = 0;
for (Pattern child : pattern.children()) {
int childDepth = calculateRuleDepth(child);
maxChildDepth = Math.max(maxChildDepth, childDepth);
}
return maxChildDepth + 1;
}
public static void logApplyRuleBefore(OptimizerContext ctx, Rule rule,
OptExpression oldExpression) {
Tracers.log(Tracers.Module.OPTIMIZER, args -> {
StringBuilder sb = new StringBuilder();
int ruleDepth = calculateRuleDepth(rule.getPattern());
sb.append(String.format("[TRACE QUERY %s] APPLY RULE %s\n", ctx.getQueryId(), rule));
sb.append("Original Expression:\n").append(oldExpression.debugString());
sb.append("Original Expression:\n").append(oldExpression.debugString(ruleDepth + 1));
return sb.toString();
});
}
public static void logApplyRuleAfter(List<OptExpression> newExpressions) {
public static void logApplyRuleAfter(Rule rule, List<OptExpression> newExpressions) {
Tracers.log(Tracers.Module.OPTIMIZER, args -> {
StringBuilder sb = new StringBuilder();
sb.append("\nNew Expression:");
if (newExpressions.isEmpty()) {
sb.append("Empty");
} else {
int ruleDepth = calculateRuleDepth(rule.getPattern());
sb.append("\n");
for (int i = 0; i < newExpressions.size(); i++) {
sb.append(i).append(":").append(newExpressions.get(i).debugString());
sb.append(i).append(":").append(newExpressions.get(i).debugString(ruleDepth + 1));
}
}
sb.append("\n");

View File

@ -100,7 +100,7 @@ public class ApplyRuleTask extends OptimizerTask {
}
newExpressions.addAll(targetExpressions);
OptimizerTraceUtil.logApplyRuleAfter(targetExpressions);
OptimizerTraceUtil.logApplyRuleAfter(rule, targetExpressions);
extractExpr = binder.next();
}

View File

@ -103,7 +103,7 @@ public class RewriteTreeTask extends OptimizerTask {
}
Preconditions.checkState(result.size() <= 1, "Rewrite rule should provide at most 1 expression");
OptimizerTraceUtil.logApplyRuleAfter(result);
OptimizerTraceUtil.logApplyRuleAfter(rule, result);
if (result.isEmpty()) {
continue;