Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,13 @@ 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());
"The default parameter (parameter 2 or parameter 3) of LEAD/LAG must be a constant: " + this.toSql());
}
uncheckedCastChild(Type.BIGINT, 1);
if (!getChild(2).type.matchesType(getChild(0).type) && !getChild(2).type.matchesType(Type.NULL)) {
uncheckedCastChild(getChild(0).type, 2);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should call uncheckCastTo to change child's type.

}
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)"));
}
}