Update the implementaion of nth_value

Make nth_value for nullable values for out of frame rows, as the same
fashion as lagInFrame just does.
This commit is contained in:
ryzuo 2021-07-23 00:34:08 +08:00
parent 94d4fb9dfc
commit 4d36d54c81
3 changed files with 40 additions and 23 deletions

View File

@ -1631,16 +1631,9 @@ struct WindowFunctionNthValue final : public WindowFunction
else else
{ {
// Offset is inside the frame. // Offset is inside the frame.
auto srcColumnPtr = transform->blockAt(target_row).input_columns[workspace.argument_column_indices[0]]; to.insertFrom(*transform->blockAt(target_row).input_columns[
// If the original column type is Nullable(from DDL) workspace.argument_column_indices[0]],
if (srcColumnPtr->getDataType() == TypeIndex::Nullable) target_row.row);
{
to.insertFrom(*srcColumnPtr, target_row.row);
}
else
{
assert_cast<ColumnNullable&>(to).insertFromNotNullable(*srcColumnPtr, target_row.row);
}
} }
} }
}; };

View File

@ -1106,9 +1106,9 @@ from numbers(10)
window w as (order by number) window w as (order by number)
order by number order by number
; ;
0 0 \N \N \N 0 0 0 0 0
1 0 1 \N \N 1 0 1 0 0
2 0 1 2 \N 2 0 1 2 0
3 0 1 2 3 3 0 1 2 3
4 0 1 2 3 4 0 1 2 3
5 0 1 2 3 5 0 1 2 3
@ -1127,16 +1127,30 @@ from numbers(10)
window w as (order by number range between 1 preceding and 1 following) window w as (order by number range between 1 preceding and 1 following)
order by number order by number
; ;
0 0 1 \N \N 0 0 1 0 0
1 0 1 2 \N 1 0 1 2 0
2 1 2 3 \N 2 1 2 3 0
3 2 3 4 \N 3 2 3 4 0
4 3 4 5 \N 4 3 4 5 0
5 4 5 6 \N 5 4 5 6 0
6 5 6 7 \N 6 5 6 7 0
7 6 7 8 \N 7 6 7 8 0
8 7 8 9 \N 8 7 8 9 0
9 8 9 \N \N 9 8 9 0 0
-- to make nth_value return null for out-of-frame rows, cast the argument to
-- Nullable; otherwise, it returns default values.
SELECT
number,
nth_value(toNullable(number), 1) OVER w as firstValue,
nth_value(toNullable(number), 3) OVER w as thridValue
FROM numbers(5)
WINDOW w AS (ORDER BY number ASC)
;
0 0 \N
1 0 \N
2 0 2
3 0 2
4 0 2
-- In this case, we had a problem with PartialSortingTransform returning zero-row -- In this case, we had a problem with PartialSortingTransform returning zero-row
-- chunks for input chunks w/o columns. -- chunks for input chunks w/o columns.
select count() over () from numbers(4) where number < 2; select count() over () from numbers(4) where number < 2;

View File

@ -427,6 +427,16 @@ window w as (order by number range between 1 preceding and 1 following)
order by number order by number
; ;
-- to make nth_value return null for out-of-frame rows, cast the argument to
-- Nullable; otherwise, it returns default values.
SELECT
number,
nth_value(toNullable(number), 1) OVER w as firstValue,
nth_value(toNullable(number), 3) OVER w as thridValue
FROM numbers(5)
WINDOW w AS (ORDER BY number ASC)
;
-- In this case, we had a problem with PartialSortingTransform returning zero-row -- In this case, we had a problem with PartialSortingTransform returning zero-row
-- chunks for input chunks w/o columns. -- chunks for input chunks w/o columns.
select count() over () from numbers(4) where number < 2; select count() over () from numbers(4) where number < 2;