Skip to content

Commit

Permalink
[Refactor] refactor lambda error msg (#17414)
Browse files Browse the repository at this point in the history
  • Loading branch information
fzhedu committed Feb 10, 2023
1 parent cc3200c commit 832ffb5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
12 changes: 7 additions & 5 deletions fe/fe-core/src/main/java/com/starrocks/analysis/Expr.java
Original file line number Diff line number Diff line change
Expand Up @@ -1335,20 +1335,22 @@ public boolean hasLambdaFunction(Expr expression) {
}
if (num == 1 && (pos == 0 || pos == children.size() - 1)) {
if (children.size() <= 1) {
throw new SemanticException("Lambda functions should work with inputs in high-order functions.");
throw new SemanticException("Lambda functions need array inputs in high-order functions.");
}
return true;
} else if (num > 1) {
throw new SemanticException("A high-order function can have one lambda function.");
throw new SemanticException("A high-order function should have only 1 lambda function, " +
"but there are " + num + " lambda functions.");
} else if (pos > 0 && pos < children.size() - 1) {
throw new SemanticException(
"Lambda functions can only be the first or last argument of any high-order function, " +
"or lambda arguments should be in ().");
"Lambda functions should only be the first or last argument of any high-order function, " +
"or lambda arguments should be in () if there are more than one lambda arguments, " +
"like (x,y)->x+y.");
} else if (num == 0) {
if (expression instanceof FunctionCallExpr) {
String funcName = ((FunctionCallExpr) expression).getFnName().getFunction();
if (funcName.equals(FunctionSet.ARRAY_MAP) || funcName.equals(FunctionSet.TRANSFORM)) {
throw new SemanticException(funcName + " should not without lambda function input.");
throw new SemanticException("There are no lambda functions in high-order function " + funcName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,14 @@ private Expr rewriteHighOrderFunction(Expr expr) {
// only high-order functions can use lambda functions.
void analyzeHighOrderFunction(Visitor visitor, Expr expression, Scope scope) {
if (!isHighOrderFunction(expression)) {
throw new SemanticException("Lambda Functions can only be used in supported high-order functions.");
String funcName = "";
if (expression instanceof FunctionCallExpr) {
funcName = ((FunctionCallExpr) expression).getFnName().getFunction();
} else {
funcName = expression.toString();
}
throw new SemanticException(funcName + " can't use lambda functions, " +
"as it is not a supported high-order function.");
}
int childSize = expression.getChildren().size();
// move the lambda function to the first if it is at the last.
Expand All @@ -187,7 +194,7 @@ void analyzeHighOrderFunction(Visitor visitor, Expr expression, Scope scope) {
expr.setType(Type.ARRAY_INT); // Let it have item type.
}
if (!expr.getType().isArrayType()) {
throw new SemanticException("Lambda inputs should be arrays.");
throw new SemanticException(i + "th lambda input should be arrays.");
}
Type itemType = ((ArrayType) expr.getType()).getItemType();
if (itemType == Type.NULL) { // Since slot_ref with Type.NULL is rewritten to Literal in toThrift(),
Expand Down

0 comments on commit 832ffb5

Please sign in to comment.