Skip to content

Commit 9245df8

Browse files
Fix the issue that window function returns incorrect result when range type frame end is preceding (#10238)
close #10236 Remove a wrong judgement
1 parent bf8f69c commit 9245df8

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

dbms/src/DataStreams/WindowTransformAction.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -660,10 +660,12 @@ std::tuple<RowNumber, bool> WindowTransformAction::stepToStartForRangeFrame()
660660

661661
std::tuple<RowNumber, bool> WindowTransformAction::stepToEndForRangeFrame()
662662
{
663-
if (!window_description.frame.end_preceding && !partition_ended)
664-
// If we find the frame end and the partition_ended is false.
665-
// Some previous blocks may be dropped, this is an unexpected behaviour.
666-
// So, we shouldn't do anything before the partition_ended is true.
663+
auto end_preceding = window_description.frame.end_preceding;
664+
if ((!end_preceding || (end_preceding && window_description.frame.end_offset == 0)) && !partition_ended)
665+
// Still return false even when end_preceding is true and end_offset == 0, because there may still
666+
// same rows as current_row in the next block. So we cannot stop find the frame
667+
// end until got the partition end(or got the first row that is greater than
668+
// current_row).
667669
return std::make_tuple(RowNumber(), false);
668670

669671
if (window_description.is_desc)

dbms/src/WindowFunctions/tests/gtest_agg.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,4 +738,39 @@ try
738738
}
739739
CATCH
740740

741+
TEST_F(WindowAggFuncTest, issue10236)
742+
try
743+
{
744+
MockWindowFrame frame;
745+
frame.type = tipb::WindowFrameType::Ranges;
746+
frame.start = buildRangeFrameBound(
747+
tipb::WindowBoundType::Preceding,
748+
tipb::RangeCmpDataType::Int,
749+
ORDER_COL_NAME,
750+
false,
751+
static_cast<Int64>(0));
752+
753+
std::vector<tipb::WindowBoundType> window_bound_types{
754+
tipb::WindowBoundType::Preceding,
755+
tipb::WindowBoundType::Following};
756+
757+
for (auto window_bound_type : window_bound_types)
758+
{
759+
frame.end = buildRangeFrameBound(
760+
window_bound_type,
761+
tipb::RangeCmpDataType::Int,
762+
ORDER_COL_NAME,
763+
false,
764+
static_cast<Int64>(0));
765+
766+
executeFunctionAndAssert(
767+
toNullableVec<Int64>(std::vector<std::optional<Int64>>{10, 10, 10, 10, 10, 10, 10, 10, 10, 10}),
768+
Count(value_col),
769+
{toVec<Int64>({0, 0, 0, 0, 0, 0, 0, 0, 0, 0}),
770+
toVec<Int64>({0, 0, 0, 0, 0, 0, 0, 0, 0, 0}),
771+
toVec<Int64>({0, 0, 0, 0, 0, 0, 0, 0, 0, 0})},
772+
frame);
773+
}
774+
}
775+
CATCH
741776
} // namespace DB::tests

0 commit comments

Comments
 (0)