Skip to content

Commit 953429e

Browse files
authored
[fix](function) fix last_value get wrong result when have order by clause (#9247)
1 parent e0c7900 commit 953429e

5 files changed

Lines changed: 127 additions & 93 deletions

File tree

‎be/src/vec/aggregate_functions/aggregate_function_window.h‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,12 @@ struct CopiedValue : public Value {
183183
std::string _copied_value;
184184
};
185185

186-
template <typename T, bool is_nullable, bool is_string, typename StoreType = Value>
186+
template <typename T, bool result_is_nullable, bool is_string, typename StoreType = Value>
187187
struct LeadAndLagData {
188188
public:
189189
bool has_init() const { return _is_init; }
190190

191-
static constexpr bool nullable = is_nullable;
191+
static constexpr bool nullable = result_is_nullable;
192192

193193
void set_null_if_need() {
194194
if (!_has_value) {
@@ -204,7 +204,7 @@ struct LeadAndLagData {
204204
}
205205

206206
void insert_result_into(IColumn& to) const {
207-
if constexpr (is_nullable) {
207+
if constexpr (result_is_nullable) {
208208
if (_data_value.is_null()) {
209209
auto& col = assert_cast<ColumnNullable&>(to);
210210
col.insert_default();
@@ -231,7 +231,7 @@ struct LeadAndLagData {
231231
}
232232

233233
void set_value(const IColumn** columns, int64_t pos) {
234-
if constexpr (is_nullable) {
234+
if (is_column_nullable(*columns[0])) {
235235
const auto* nullable_column = assert_cast<const ColumnNullable*>(columns[0]);
236236
if (nullable_column && nullable_column->is_null_at(pos)) {
237237
_data_value.set_null(true);
@@ -501,7 +501,7 @@ class WindowFunctionData final
501501
};
502502

503503
template <template <typename> class AggregateFunctionTemplate, template <typename> class Data,
504-
bool is_nullable, bool is_copy = false>
504+
bool result_is_nullable, bool is_copy = false>
505505
static IAggregateFunction* create_function_single_value(const String& name,
506506
const DataTypes& argument_types,
507507
const Array& parameters) {
@@ -515,23 +515,24 @@ static IAggregateFunction* create_function_single_value(const String& name,
515515
#define DISPATCH(TYPE) \
516516
if (which.idx == TypeIndex::TYPE) \
517517
return new AggregateFunctionTemplate< \
518-
Data<LeadAndLagData<TYPE, is_nullable, false, StoreType>>>(argument_types);
518+
Data<LeadAndLagData<TYPE, result_is_nullable, false, StoreType>>>(argument_types);
519519
FOR_NUMERIC_TYPES(DISPATCH)
520520
#undef DISPATCH
521521

522522
if (which.is_decimal()) {
523523
return new AggregateFunctionTemplate<
524-
Data<LeadAndLagData<Int128, is_nullable, false, StoreType>>>(argument_types);
524+
Data<LeadAndLagData<Int128, result_is_nullable, false, StoreType>>>(argument_types);
525525
}
526526
if (which.is_date_or_datetime()) {
527527
return new AggregateFunctionTemplate<
528-
Data<LeadAndLagData<Int64, is_nullable, false, StoreType>>>(argument_types);
528+
Data<LeadAndLagData<Int64, result_is_nullable, false, StoreType>>>(argument_types);
529529
}
530530
if (which.is_string_or_fixed_string()) {
531531
return new AggregateFunctionTemplate<
532-
Data<LeadAndLagData<StringRef, is_nullable, true, StoreType>>>(argument_types);
532+
Data<LeadAndLagData<StringRef, result_is_nullable, true, StoreType>>>(
533+
argument_types);
533534
}
534-
DCHECK(false) << "with unknowed type, failed in create_aggregate_function_leadlag";
535+
DCHECK(false) << "with unknowed type, failed in create_aggregate_function_" << name;
535536
return nullptr;
536537
}
537538

‎be/test/vec/function/function_bitmap_test.cpp‎

Lines changed: 55 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -26,157 +26,135 @@ TEST(function_bitmap_test, function_bitmap_min_test) {
2626
std::string func_name = "bitmap_min";
2727
InputTypeSet input_types = {TypeIndex::BitMap};
2828

29-
auto bitmap1 = new BitmapValue(1);
30-
auto bitmap2 = new BitmapValue(std::vector<uint64_t>({1, 9999999}));
31-
auto empty_bitmap = new BitmapValue();
32-
DataSet data_set = {{{bitmap1}, (int64_t)1},
33-
{{bitmap2}, (int64_t)1},
34-
{{empty_bitmap}, Null()},
29+
BitmapValue bitmap1(1);
30+
BitmapValue bitmap2({1, 9999999});
31+
BitmapValue empty_bitmap;
32+
DataSet data_set = {{{&bitmap1}, (int64_t)1},
33+
{{&bitmap2}, (int64_t)1},
34+
{{&empty_bitmap}, Null()},
3535
{{Null()}, Null()}};
3636

3737
check_function<DataTypeInt64, true>(func_name, input_types, data_set);
38-
delete bitmap1;
39-
delete bitmap2;
40-
delete empty_bitmap;
4138
}
4239
TEST(function_bitmap_test, function_bitmap_max_test) {
4340
std::string func_name = "bitmap_max";
4441
InputTypeSet input_types = {TypeIndex::BitMap};
4542

46-
auto bitmap1 = new BitmapValue(1);
47-
auto bitmap2 = new BitmapValue(std::vector<uint64_t>({1, 9999999}));
48-
auto empty_bitmap = new BitmapValue();
49-
DataSet data_set = {{{bitmap1}, (int64_t)1},
50-
{{bitmap2}, (int64_t)9999999},
51-
{{empty_bitmap}, Null()},
43+
BitmapValue bitmap1(1);
44+
BitmapValue bitmap2({1, 9999999});
45+
BitmapValue empty_bitmap;
46+
DataSet data_set = {{{&bitmap1}, (int64_t)1},
47+
{{&bitmap2}, (int64_t)9999999},
48+
{{&empty_bitmap}, Null()},
5249
{{Null()}, Null()}};
5350

5451
check_function<DataTypeInt64, true>(func_name, input_types, data_set);
55-
delete bitmap1;
56-
delete bitmap2;
57-
delete empty_bitmap;
5852
}
5953

6054
TEST(function_bitmap_test, function_bitmap_to_string_test) {
6155
std::string func_name = "bitmap_to_string";
6256
InputTypeSet input_types = {TypeIndex::BitMap};
6357

64-
auto bitmap1 = new BitmapValue(1);
65-
auto bitmap2 = new BitmapValue(std::vector<uint64_t>({1, 9999999}));
66-
auto empty_bitmap = new BitmapValue();
67-
DataSet data_set = {{{bitmap1}, std::string("1")},
68-
{{bitmap2}, std::string("1,9999999")},
69-
{{empty_bitmap}, std::string("")},
58+
BitmapValue bitmap1(1);
59+
BitmapValue bitmap2({1, 9999999});
60+
BitmapValue empty_bitmap;
61+
DataSet data_set = {{{&bitmap1}, std::string("1")},
62+
{{&bitmap2}, std::string("1,9999999")},
63+
{{&empty_bitmap}, std::string("")},
7064
{{Null()}, Null()}};
7165

7266
check_function<DataTypeString, true>(func_name, input_types, data_set);
73-
delete bitmap1;
74-
delete bitmap2;
75-
delete empty_bitmap;
7667
}
7768

7869
TEST(function_bitmap_test, function_bitmap_and_count) {
7970
std::string func_name = "bitmap_and_count";
8071
InputTypeSet input_types = {TypeIndex::BitMap, TypeIndex::BitMap};
81-
auto bitmap1 = new BitmapValue(std::vector<uint64_t>({1, 2, 3}));
82-
auto bitmap2 = new BitmapValue(std::vector<uint64_t>({3, 4, 5}));
83-
auto empty_bitmap = new BitmapValue();
84-
DataSet data_set = {{{bitmap1, empty_bitmap}, (int64_t)0},
85-
{{bitmap1, bitmap1}, (int64_t)3},
86-
{{bitmap1, bitmap2}, (int64_t)1}};
72+
BitmapValue bitmap1({1, 2, 3});
73+
BitmapValue bitmap2({3, 4, 5});
74+
BitmapValue empty_bitmap;
75+
DataSet data_set = {{{&bitmap1, &empty_bitmap}, (int64_t)0},
76+
{{&bitmap1, &bitmap1}, (int64_t)3},
77+
{{&bitmap1, &bitmap2}, (int64_t)1}};
8778

8879
check_function<DataTypeInt64, true>(func_name, input_types, data_set);
89-
delete bitmap1;
90-
delete bitmap2;
91-
delete empty_bitmap;
9280

9381
{
9482
InputTypeSet input_types = {TypeIndex::BitMap, TypeIndex::BitMap, TypeIndex::BitMap};
9583
BitmapValue bitmap1({33, 1, 2019});
9684
BitmapValue bitmap2({0, 33, std::numeric_limits<uint64_t>::min()});
9785
BitmapValue bitmap3({33, 5, std::numeric_limits<uint64_t>::max()});
98-
auto empty_bitmap = new BitmapValue(); //test empty
86+
BitmapValue empty_bitmap; //test empty
9987

100-
DataSet data_set = {{{&bitmap1, &bitmap2, empty_bitmap}, (int64_t)0},
88+
DataSet data_set = {{{&bitmap1, &bitmap2, &empty_bitmap}, (int64_t)0},
10189
{{&bitmap1, &bitmap2, &bitmap3}, (int64_t)1}, //33
10290
{{&bitmap1, &bitmap2, Null()}, Null()},
10391
{{&bitmap1, &bitmap3, &bitmap3}, (int64_t)1}}; //33
10492

10593
check_function<DataTypeInt64, true>(func_name, input_types, data_set);
106-
delete empty_bitmap;
10794
}
10895
}
10996

11097
TEST(function_bitmap_test, function_bitmap_or_count) {
11198
std::string func_name = "bitmap_or_count";
11299
InputTypeSet input_types = {TypeIndex::BitMap, TypeIndex::BitMap};
113-
auto bitmap1 = new BitmapValue(std::vector<uint64_t>({1, 2, 3}));
114-
auto bitmap2 = new BitmapValue(std::vector<uint64_t>({1, 2, 3, 4}));
115-
auto bitmap3 = new BitmapValue(std::vector<uint64_t>({2, 3}));
116-
auto empty_bitmap = new BitmapValue();
117-
DataSet data_set = {{{bitmap1, empty_bitmap}, (int64_t)3},
118-
{{bitmap2, bitmap3}, (int64_t)4},
119-
{{bitmap1, bitmap3}, (int64_t)3}};
100+
101+
BitmapValue bitmap1({1, 2, 3});
102+
BitmapValue bitmap2({1, 2, 3, 4});
103+
BitmapValue bitmap3({2, 3});
104+
BitmapValue empty_bitmap;
105+
DataSet data_set = {{{&bitmap1, &empty_bitmap}, (int64_t)3},
106+
{{&bitmap2, &bitmap3}, (int64_t)4},
107+
{{&bitmap1, &bitmap3}, (int64_t)3}};
120108

121109
check_function<DataTypeInt64, true>(func_name, input_types, data_set);
122-
delete bitmap1;
123-
delete bitmap2;
124-
delete bitmap3;
125-
delete empty_bitmap;
126110

127111
{
128112
InputTypeSet input_types = {TypeIndex::BitMap, TypeIndex::BitMap, TypeIndex::BitMap};
129113
BitmapValue bitmap1({1024, 1, 2019});
130114
BitmapValue bitmap2({0, 33, std::numeric_limits<uint64_t>::min()});
131115
BitmapValue bitmap3({33, 5, std::numeric_limits<uint64_t>::max()}); //18446744073709551615
132-
auto empty_bitmap = new BitmapValue(); //test empty
116+
BitmapValue empty_bitmap; //test empty
133117

134-
DataSet data_set = {{{&bitmap1, &bitmap2, empty_bitmap}, (int64_t)5}, //0,1,33,1024,2019
118+
DataSet data_set = {{{&bitmap1, &bitmap2, &empty_bitmap}, (int64_t)5}, //0,1,33,1024,2019
135119
{{&bitmap1, &bitmap2, &bitmap3},
136120
(int64_t)7}, //0,1,5,33,1024,2019,18446744073709551615
137-
{{&bitmap1, empty_bitmap, Null()}, Null()},
121+
{{&bitmap1, &empty_bitmap, Null()}, Null()},
138122
{{&bitmap1, &bitmap3, &bitmap3},
139123
(int64_t)6}}; //1,5,33,1024,2019,18446744073709551615
140124

141125
check_function<DataTypeInt64, true>(func_name, input_types, data_set);
142-
delete empty_bitmap;
143126
}
144127
}
145128

146129
TEST(function_bitmap_test, function_bitmap_xor_count) {
147130
std::string func_name = "bitmap_xor_count";
148131
InputTypeSet input_types = {TypeIndex::BitMap, TypeIndex::BitMap};
149-
auto bitmap1 = new BitmapValue(std::vector<uint64_t>({1, 2, 3}));
150-
auto bitmap2 = new BitmapValue(std::vector<uint64_t>({1, 2, 3, 4}));
151-
auto bitmap3 = new BitmapValue(std::vector<uint64_t>({2, 3}));
152-
auto bitmap4 = new BitmapValue(std::vector<uint64_t>({1, 2, 6}));
153-
auto empty_bitmap = new BitmapValue();
154-
DataSet data_set = {{{bitmap1, empty_bitmap}, (int64_t)3},
155-
{{bitmap2, bitmap3}, (int64_t)2},
156-
{{bitmap1, bitmap4}, (int64_t)2}};
132+
133+
BitmapValue bitmap1({1, 2, 3});
134+
BitmapValue bitmap2({1, 2, 3, 4});
135+
BitmapValue bitmap3({2, 3});
136+
BitmapValue bitmap4({1, 2, 6});
137+
BitmapValue empty_bitmap;
138+
DataSet data_set = {{{&bitmap1, &empty_bitmap}, (int64_t)3},
139+
{{&bitmap2, &bitmap3}, (int64_t)2},
140+
{{&bitmap1, &bitmap4}, (int64_t)2}};
157141

158142
check_function<DataTypeInt64, true>(func_name, input_types, data_set);
159-
delete bitmap1;
160-
delete bitmap2;
161-
delete bitmap3;
162-
delete bitmap4;
163-
delete empty_bitmap;
164143

165144
{
166145
InputTypeSet input_types = {TypeIndex::BitMap, TypeIndex::BitMap, TypeIndex::BitMap};
167146
BitmapValue bitmap1({1024, 1, 2019});
168147
BitmapValue bitmap2({0, 33, std::numeric_limits<uint64_t>::min()});
169148
BitmapValue bitmap3({33, 5, std::numeric_limits<uint64_t>::max()});
170-
auto empty_bitmap = new BitmapValue(); //test empty
149+
BitmapValue empty_bitmap; //test empty
171150

172151
DataSet data_set = {
173-
{{&bitmap1, &bitmap2, empty_bitmap}, (int64_t)5}, //0,1,33,1024,2019
152+
{{&bitmap1, &bitmap2, &empty_bitmap}, (int64_t)5}, //0,1,33,1024,2019
174153
{{&bitmap1, &bitmap2, &bitmap3}, (int64_t)6}, //0,1,5,1024,2019,18446744073709551615
175-
{{&bitmap1, empty_bitmap, Null()}, Null()},
154+
{{&bitmap1, &empty_bitmap, Null()}, Null()},
176155
{{&bitmap1, &bitmap3, &bitmap3}, (int64_t)3}}; //1,1024,2019
177156

178157
check_function<DataTypeInt64, true>(func_name, input_types, data_set);
179-
delete empty_bitmap;
180158
}
181159
}
182160

@@ -186,15 +164,14 @@ TEST(function_bitmap_test, function_bitmap_and_not_count) {
186164
BitmapValue bitmap1({1, 2, 3});
187165
BitmapValue bitmap2({3, 4, std::numeric_limits<uint64_t>::min()});
188166
BitmapValue bitmap3({33, 5, std::numeric_limits<uint64_t>::max()});
189-
auto empty_bitmap = new BitmapValue();
167+
BitmapValue empty_bitmap;
190168

191-
DataSet data_set = {{{&bitmap1, empty_bitmap}, (int64_t)3}, //1,2,3
169+
DataSet data_set = {{{&bitmap1, &empty_bitmap}, (int64_t)3}, //1,2,3
192170
{{&bitmap2, Null()}, Null()},
193171
{{&bitmap2, &bitmap3}, (int64_t)3}, //0,3,4
194172
{{&bitmap1, &bitmap2}, (int64_t)2}}; //1,2
195173

196174
check_function<DataTypeInt64, true>(func_name, input_types, data_set);
197-
delete empty_bitmap;
198175
}
199176
TEST(function_bitmap_test, function_bitmap_has_all) {
200177
std::string func_name = "bitmap_has_all";
@@ -207,18 +184,16 @@ TEST(function_bitmap_test, function_bitmap_has_all) {
207184
BitmapValue bitmap3 = BitmapValue({0, 1, 2});
208185
BitmapValue bitmap4 = BitmapValue({0, 1, 2, std::numeric_limits<uint64_t>::max()});
209186
BitmapValue bitmap5 = BitmapValue({0, 1, 2});
210-
auto empty_bitmap1 = new BitmapValue();
211-
auto empty_bitmap2 = new BitmapValue();
187+
BitmapValue empty_bitmap1;
188+
BitmapValue empty_bitmap2;
212189

213190
DataSet data_set = {{{&bitmap1, &bitmap2}, uint8(true)},
214-
{{empty_bitmap1, empty_bitmap2}, uint8(true)},
191+
{{&empty_bitmap1, &empty_bitmap2}, uint8(true)},
215192
{{&bitmap3, &bitmap4}, uint8(false)},
216193
{{&bitmap4, &bitmap5}, uint8(true)},
217-
{{Null(), empty_bitmap1}, Null()}};
194+
{{Null(), &empty_bitmap1}, Null()}};
218195

219196
check_function<DataTypeUInt8, true>(func_name, input_types, data_set);
220-
delete empty_bitmap1;
221-
delete empty_bitmap2;
222197
}
223198

224199
} // namespace doris::vectorized

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -710,9 +710,8 @@ private void standardize(Analyzer analyzer) throws AnalysisException {
710710
resetWindow = true;
711711
}
712712

713-
// Change first_value/last_value RANGE windows to ROWS
714-
if ((analyticFnName.getFunction().equalsIgnoreCase(FIRSTVALUE)
715-
|| analyticFnName.getFunction().equalsIgnoreCase(LASTVALUE))
713+
// Change first_value RANGE windows to ROWS
714+
if ((analyticFnName.getFunction().equalsIgnoreCase(FIRSTVALUE))
716715
&& window != null
717716
&& window.getType() == AnalyticWindow.Type.RANGE) {
718717
window = new AnalyticWindow(AnalyticWindow.Type.ROWS, window.getLeftBoundary(),
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- This file is automatically generated. You should know what you did if you want to edit this
2+
-- !select_default --
3+
21 04-21-11 1 1
4+
22 04-22-10-21 0 1
5+
22 04-22-10-21 1 1
6+
23 04-23-10 1 1
7+
24 02-24-10-21 1 1
8+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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_last_value_window") {
19+
def tableName = "state"
20+
21+
22+
sql """ DROP TABLE IF EXISTS ${tableName} """
23+
sql """
24+
CREATE TABLE ${tableName} (
25+
`myday` INT,
26+
`time` VARCHAR(40) NOT NULL,
27+
`state` INT
28+
) ENGINE=OLAP
29+
DUPLICATE KEY(`myday`,time,state)
30+
COMMENT "OLAP"
31+
DISTRIBUTED BY HASH(`myday`) BUCKETS 2
32+
PROPERTIES (
33+
"replication_num" = "1",
34+
"in_memory" = "false",
35+
"storage_format" = "V2"
36+
);
37+
"""
38+
39+
sql """ INSERT INTO ${tableName} VALUES
40+
(21,"04-21-11",1),
41+
(22,"04-22-10-21",0),
42+
(22,"04-22-10-21",1),
43+
(23,"04-23-10",1),
44+
(24,"02-24-10-21",1); """
45+
46+
// not_vectorized
47+
sql """ set enable_vectorized_engine = false; """
48+
49+
qt_select_default """ select *,last_value(state) over(partition by myday order by time) from ${tableName}; """
50+
51+
}

0 commit comments

Comments
 (0)