[BUG](view) fix can't create view with lambda function (#23942)

before the lambda function Expr not implement toSqlImpl() function.
so it's call parent function, which is not suit for lambda function.
and will be have error when create view.
This commit is contained in:
zhangstar333
2023-09-11 10:04:00 +08:00
committed by GitHub
parent 0896aefce3
commit cd13f9e8c6
9 changed files with 292 additions and 6 deletions

View File

@ -1845,6 +1845,11 @@ public abstract class Expr extends TreeNode<Expr> implements ParseNode, Cloneabl
if (slot.getColumnName() != null && slot.getColumnName().equals(colName)) {
return true;
}
} else if (this instanceof ColumnRefExpr) {
ColumnRefExpr slot = (ColumnRefExpr) this;
if (slot.getName() != null && slot.getName().equals(colName)) {
return true;
}
}
return false;
}

View File

@ -263,4 +263,37 @@ public class LambdaFunctionCallExpr extends FunctionCallExpr {
msg.node_type = TExprNodeType.LAMBDA_FUNCTION_CALL_EXPR;
}
}
@Override
public String toSqlImpl() {
StringBuilder sb = new StringBuilder();
sb.append(getFnName().getFunction());
sb.append("(");
int childSize = children.size();
Expr lastExpr = getChild(childSize - 1);
// eg: select array_map(x->x>10, k1) from table,
// but we need analyze each param, so change the function like this in parser
// array_map(x->x>10, k1) ---> array_map(k1, x->x>10),
// so maybe the lambda expr is the end position. and need this check.
boolean lastIsLambdaExpr = (lastExpr instanceof LambdaFunctionExpr);
if (lastIsLambdaExpr) {
sb.append(lastExpr.toSql());
sb.append(", ");
}
for (int i = 0; i < childSize - 1; ++i) {
sb.append(getChild(i).toSql());
if (i != childSize - 2) {
sb.append(", ");
}
}
// and some functions is only implement as a normal array function;
// but also want use as lambda function, select array_sortby(x->x,['b','a','c']);
// so we convert to: array_sortby(array('b', 'a', 'c'), array_map(x -> `x`, array('b', 'a', 'c')))
if (lastIsLambdaExpr == false) {
sb.append(", ");
sb.append(lastExpr.toSql());
}
sb.append(")");
return sb.toString();
}
}

View File

@ -115,7 +115,20 @@ public class LambdaFunctionExpr extends Expr {
@Override
protected String toSqlImpl() {
return String.format("%s -> %s", names.toString(), getChild(0).toSql());
String nameStr = "";
Expr lambdaExpr = slotExpr.get(0);
int exprSize = names.size();
for (int i = 0; i < exprSize; ++i) {
nameStr = nameStr + names.get(i);
if (i != exprSize - 1) {
nameStr = nameStr + ",";
}
}
if (exprSize > 1) {
nameStr = "(" + nameStr + ")";
}
String res = String.format("%s -> %s", nameStr, lambdaExpr.toSql());
return res;
}
@Override