Skip to content

Commit cdd1b6d

Browse files
authored
[fix](function) fix lag/lead function return invalid data (apache#9076)
1 parent 9e13be4 commit cdd1b6d

4 files changed

Lines changed: 67 additions & 9 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/analysis/AnalyticExpr.java‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -479,13 +479,17 @@ public void analyzeImpl(Analyzer analyzer) throws AnalysisException {
479479

480480
standardize(analyzer);
481481

482-
// min/max is not currently supported on sliding windows (i.e. start bound is not
483-
// unbounded).
484-
if (window != null && isMinMax(fn) &&
485-
window.getLeftBoundary().getType() != BoundaryType.UNBOUNDED_PRECEDING) {
486-
throw new AnalysisException(
487-
"'" + getFnCall().toSql() + "' is only supported with an "
488-
+ "UNBOUNDED PRECEDING start bound.");
482+
// But in Vectorized mode, after calculate a window, will be call reset() to reset state,
483+
// And then restarted calculate next new window;
484+
if (!VectorizedUtil.isVectorized()) {
485+
// min/max is not currently supported on sliding windows (i.e. start bound is not
486+
// unbounded).
487+
if (window != null && isMinMax(fn) &&
488+
window.getLeftBoundary().getType() != BoundaryType.UNBOUNDED_PRECEDING) {
489+
throw new AnalysisException(
490+
"'" + getFnCall().toSql() + "' is only supported with an "
491+
+ "UNBOUNDED PRECEDING start bound.");
492+
}
489493
}
490494

491495
setChildren();

‎fe/fe-core/src/main/java/org/apache/doris/catalog/FunctionSet.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2427,13 +2427,13 @@ private void initAggregateBuiltins() {
24272427
"lag", Lists.newArrayList(t, Type.BIGINT, t), t, t,
24282428
prefix + OFFSET_FN_INIT_SYMBOL.get(t),
24292429
prefix + OFFSET_FN_UPDATE_SYMBOL.get(t),
2430-
null, null, null));
2430+
null, t.isStringType() ? stringValGetValue : null, null));
24312431

24322432
addBuiltin(AggregateFunction.createAnalyticBuiltin(
24332433
"lead", Lists.newArrayList(t, Type.BIGINT, t), t, t,
24342434
prefix + OFFSET_FN_INIT_SYMBOL.get(t),
24352435
prefix + OFFSET_FN_UPDATE_SYMBOL.get(t),
2436-
null, null, null));
2436+
null, t.isStringType() ? stringValGetValue : null, null));
24372437
//vec
24382438
addBuiltin(AggregateFunction.createAnalyticBuiltin(
24392439
"lag", Lists.newArrayList(t, Type.BIGINT, t), t, t,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- This file is automatically generated. You should know what you did if you want to edit this
2+
-- !select_default --
3+
/wyyt-image/2021/11/13/595345040188712460.jpg
4+
/wyyt-image/2022/04/13/1434607674511761493.jpg
5+
/wyyt-image/2022/04/13/1434607674511761493.jpg /wyyt-image/2022/04/13/1434607674511761493.jpg
6+
7+
-- !select_default2 --
8+
/wyyt-image/2021/11/13/595345040188712460.jpg
9+
/wyyt-image/2022/04/13/1434607674511761493.jpg /wyyt-image/2022/04/13/1434607674511761493.jpg
10+
/wyyt-image/2022/04/13/1434607674511761493.jpg
11+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
suite("test_lag_lead_window") {
19+
def tableName = "wftest"
20+
21+
22+
sql """ DROP TABLE IF EXISTS ${tableName} """
23+
sql """
24+
CREATE TABLE ${tableName} ( `aa` varchar(10) NULL COMMENT "", `bb` text NULL COMMENT "", `cc` text NULL COMMENT "" )
25+
ENGINE=OLAP UNIQUE KEY(`aa`) DISTRIBUTED BY HASH(`aa`) BUCKETS 3
26+
PROPERTIES ( "replication_allocation" = "tag.location.default: 1", "in_memory" = "false", "storage_format" = "V2" );
27+
"""
28+
29+
sql """ INSERT INTO ${tableName} VALUES
30+
('a','aa','/wyyt-image/2021/11/13/595345040188712460.jpg'),
31+
('b','aa','/wyyt-image/2022/04/13/1434607674511761493.jpg'),
32+
('c','cc','/wyyt-image/2022/04/13/1434607674511761493.jpg') """
33+
34+
// not_vectorized
35+
sql """ set enable_vectorized_engine = false """
36+
37+
qt_select_default """ select min(t.cc) over(PARTITION by t.cc order by t.aa) ,
38+
lag(t.cc,1,'') over (PARTITION by t.cc order by t.aa) as l1 from ${tableName} t; """
39+
40+
qt_select_default2 """ select min(t.cc) over(PARTITION by t.cc order by t.aa) ,
41+
lead(t.cc,1,'') over (PARTITION by t.cc order by t.aa) as l1 from ${tableName} t; """
42+
43+
}

0 commit comments

Comments
 (0)