[SQL][Function] Validate the param of rand function in compile step (#4439)

The param of rand() function should be literal, but current compiler ignore to
validate the literal param of rand function, it is validated in execution step.
This PR make it validated in compile step, and make it more earlier to find the usage error of rand() function.
This commit is contained in:
xinghuayu007
2020-09-02 10:50:52 +08:00
committed by GitHub
parent 498b06fbe2
commit 1a22f3b2ac
2 changed files with 23 additions and 1 deletions

View File

@ -1405,7 +1405,14 @@ abstract public class Expr extends TreeNode<Expr> implements ParseNode, Cloneabl
throws AnalysisException {
FunctionName fnName = new FunctionName(name);
Function searchDesc = new Function(fnName, argTypes, Type.INVALID, false);
return Catalog.getCurrentCatalog().getFunction(searchDesc, mode);
Function f = Catalog.getCurrentCatalog().getFunction(searchDesc, mode);
if (f != null && fnName.getFunction().equalsIgnoreCase("rand")) {
if (this.children.size() == 1
&& !(this.children.get(0) instanceof LiteralExpr)) {
throw new AnalysisException("The param of rand function must be literal");
}
}
return f;
}
/**

View File

@ -379,6 +379,21 @@ public class SelectStmtTest {
dorisAssert.query(sql).explainQuery();
}
@Test
public void testRandFunction() throws Exception {
String sql = "select rand(db1.tbl1.k1) from db1.tbl1;";
try {
dorisAssert.query(sql).explainQuery();
Assert.fail("The param of rand function must be literal");
} catch (AnalysisException e) {
System.out.println(e.getMessage());
}
sql = "select rand(1234) from db1.tbl1;";
dorisAssert.query(sql).explainQuery();
sql = "select rand() from db1.tbl1;";
dorisAssert.query(sql).explainQuery();
}
@Test
public void testVarcharToLongSupport() throws Exception {
String sql = "select count(*)\n" +