From ae4c83fd14826271e9a28e7b4c080db0c54c710a Mon Sep 17 00:00:00 2001 From: HangyuanLiu <460660596@qq.com> Date: Tue, 13 Oct 2020 19:28:36 +0800 Subject: [PATCH 1/2] fix --- .../doris/analysis/FunctionCallExpr.java | 8 ++++++- .../apache/doris/planner/QueryPlanTest.java | 21 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java index 35873a251c0dba..cc4f496e28a789 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java @@ -334,11 +334,17 @@ private void analyzeBuiltinAggFunction(Analyzer analyzer) throws AnalysisExcepti throw new AnalysisException(fnName.getFunction() + " only used in analytic function"); } else { if (children.size() > 2) { - if (!getChild(2).isConstant()) { + if (!getChild(1).isConstant() || !getChild(2).isConstant()) { throw new AnalysisException( "The default parameter (parameter 3) of LAG must be a constant: " + this.toSql()); } + getChild(1).type = Type.BIGINT; + //To ensure that the correct function is selected,the third parameter + //of the lead function must be consistent with the first parameter + if (!getChild(2).type.matchesType(getChild(0).type) && !getChild(2).type.matchesType(Type.NULL)) { + getChild(2).type = getChild(0).type; + } } return; } diff --git a/fe/fe-core/src/test/java/org/apache/doris/planner/QueryPlanTest.java b/fe/fe-core/src/test/java/org/apache/doris/planner/QueryPlanTest.java index f2b93fba6934dd..ba3e24ce9572f9 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/planner/QueryPlanTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/planner/QueryPlanTest.java @@ -1310,6 +1310,27 @@ public void testAggregateSatisfyOlapTableDistribution() throws Exception { System.out.println(explainString); Assert.assertTrue(explainString.contains("AGGREGATE (update finalize)")); } + + @Test + public void testLeadAndLagFunction() throws Exception { + connectContext.setDatabase("default_cluster:test"); + + String queryStr = "explain select time, lead(query_time, 1, NULL) over () as time2 from test.test1"; + String explainString = UtFrameUtils.getSQLPlanOrErrorMsg(connectContext, queryStr); + Assert.assertTrue(explainString.contains("lead(`query_time`, 1, NULL)")); + + queryStr = "explain select time, lead(query_time, 1, 2) over () as time2 from test.test1"; + explainString = UtFrameUtils.getSQLPlanOrErrorMsg(connectContext, queryStr); + Assert.assertTrue(explainString.contains("lead(`query_time`, 1, 2)")); + + queryStr = "explain select time, lead(time, 1, '2020-01-01 00:00:00') over () as time2 from test.test1"; + explainString = UtFrameUtils.getSQLPlanOrErrorMsg(connectContext, queryStr); + Assert.assertTrue(explainString.contains("lead(`time`, 1, '2020-01-01 00:00:00')")); + + queryStr = "explain select time, lag(query_time, 1, 2) over () as time2 from test.test1"; + explainString = UtFrameUtils.getSQLPlanOrErrorMsg(connectContext, queryStr); + Assert.assertTrue(explainString.contains("lag(`query_time`, 1, 2)")); + } } From 3b76305bbdf3e078d32d1c2807041daec9dd64f5 Mon Sep 17 00:00:00 2001 From: HangyuanLiu <460660596@qq.com> Date: Tue, 13 Oct 2020 19:58:05 +0800 Subject: [PATCH 2/2] fix --- .../java/org/apache/doris/analysis/FunctionCallExpr.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java index cc4f496e28a789..d43178d4146d5e 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java @@ -336,14 +336,11 @@ private void analyzeBuiltinAggFunction(Analyzer analyzer) throws AnalysisExcepti if (children.size() > 2) { if (!getChild(1).isConstant() || !getChild(2).isConstant()) { throw new AnalysisException( - "The default parameter (parameter 3) of LAG must be a constant: " - + this.toSql()); + "The default parameter (parameter 2 or parameter 3) of LEAD/LAG must be a constant: " + this.toSql()); } - getChild(1).type = Type.BIGINT; - //To ensure that the correct function is selected,the third parameter - //of the lead function must be consistent with the first parameter + uncheckedCastChild(Type.BIGINT, 1); if (!getChild(2).type.matchesType(getChild(0).type) && !getChild(2).type.matchesType(Type.NULL)) { - getChild(2).type = getChild(0).type; + uncheckedCastChild(getChild(0).type, 2); } } return;