From 1a22f3b2acee7699a6a49aa40917841adcdfc7d3 Mon Sep 17 00:00:00 2001 From: xinghuayu007 <1450306854@qq.com> Date: Wed, 2 Sep 2020 10:50:52 +0800 Subject: [PATCH] [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. --- .../main/java/org/apache/doris/analysis/Expr.java | 9 ++++++++- .../org/apache/doris/analysis/SelectStmtTest.java | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) mode change 100644 => 100755 fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java mode change 100644 => 100755 fe/fe-core/src/test/java/org/apache/doris/analysis/SelectStmtTest.java diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java old mode 100644 new mode 100755 index 27170703a8..7f476be7dc --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java @@ -1405,7 +1405,14 @@ abstract public class Expr extends TreeNode 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; } /** diff --git a/fe/fe-core/src/test/java/org/apache/doris/analysis/SelectStmtTest.java b/fe/fe-core/src/test/java/org/apache/doris/analysis/SelectStmtTest.java old mode 100644 new mode 100755 index 278e69d64e..e7f3dd6a4d --- a/fe/fe-core/src/test/java/org/apache/doris/analysis/SelectStmtTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/analysis/SelectStmtTest.java @@ -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" +