github-actions[bot] commented on code in PR #61813:
URL: https://github.com/apache/doris/pull/61813#discussion_r2999843486
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/CaseWhen.java:
##########
@@ -99,63 +134,70 @@ public boolean nullable() {
@Override
public String toString() {
StringBuilder output = new StringBuilder("CASE");
- for (Expression child : children()) {
- if (child instanceof WhenClause) {
- output.append(child.toString());
- } else {
- output.append(" ELSE ").append(child.toString());
- }
+ value.ifPresent(v -> output.append(" ").append(v.toString()));
+ for (WhenClause whenClause : whenClauses) {
+ output.append(whenClause.toString());
}
+ defaultValue.ifPresent(dv -> output.append(" ELSE
").append(dv.toString()));
output.append(" END");
return output.toString();
}
@Override
public String toDigest() {
StringBuilder sb = new StringBuilder("CASE");
- for (Expression child : children()) {
- if (child instanceof WhenClause) {
- sb.append(child.toDigest());
- } else {
- sb.append(" ELSE ").append(child.toDigest());
- }
+ value.ifPresent(v -> sb.append(" ").append(v.toDigest()));
+ for (WhenClause whenClause : whenClauses) {
+ sb.append(whenClause.toDigest());
}
+ defaultValue.ifPresent(dv -> sb.append(" ELSE
").append(dv.toDigest()));
sb.append(" END");
return sb.toString();
}
@Override
public String computeToSql() throws UnboundException {
StringBuilder output = new StringBuilder("CASE");
- for (Expression child : children()) {
- if (child instanceof WhenClause) {
- output.append(child.toSql());
- } else {
- output.append(" ELSE ").append(child.toSql());
- }
+ value.ifPresent(v -> output.append(" ").append(v.toSql()));
+ for (WhenClause whenClause : whenClauses) {
+ output.append(whenClause.toSql());
}
+ defaultValue.ifPresent(dv -> output.append(" ELSE
").append(dv.toSql()));
output.append(" END");
return output.toString();
}
@Override
public CaseWhen withChildren(List<Expression> children) {
Preconditions.checkArgument(!children.isEmpty(), "case when should has
at least 1 child");
+ int i = 0;
+ Expression value = null;
+ // First non-WhenClause child before any WhenClause is the simple case
value
Review Comment:
**Suggestion (non-blocking):** The heuristic of "first non-WhenClause child
= value" works correctly today because the simple-case value is always consumed
by `ExpressionAnalyzer` before any downstream rule calls `withChildren()`.
Consider adding a brief comment here explaining this invariant (e.g., `// Note:
value is always consumed during analysis phase; post-analysis, the first child
is always a WhenClause`). This would help future maintainers understand why the
heuristic is safe.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]