Merge pull request #71886 from wxybear/fix_with_ties_when_rows_read_less_than_request

Fix: select statements that use 'with ties' might not return enough rows
This commit is contained in:
Anton Popov 2024-11-14 15:34:05 +00:00 committed by GitHub
commit 2ed8e4c8b8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 2 deletions

View File

@ -317,8 +317,9 @@ void LimitTransform::splitChunk(PortsData & data)
length = offset + limit - (rows_read - num_rows) - start;
}
/// check if other rows in current block equals to last one in limit
if (with_ties && length)
/// Check if other rows in current block equals to last one in limit
/// when rows read >= offset + limit.
if (with_ties && offset + limit <= rows_read && length)
{
UInt64 current_row_num = start + length;
previous_row_chunk = makeChunkWithPreviousRow(data.current_chunk, current_row_num - 1);

View File

@ -53,3 +53,5 @@
100
100
100
12
12

View File

@ -35,4 +35,24 @@ select count() from (select number, number < 100 from numbers(2000) order by num
SET max_block_size = 5;
select count() from (select number < 100, number from numbers(2000) order by number < 100 desc limit 10 with ties);
SELECT count() FROM (WITH data AS (
SELECT * FROM numbers(0, 10)
UNION ALL
SELECT * FROM numbers(10, 10)
)
SELECT number div 10 AS ten, number
FROM data
ORDER BY ten
LIMIT 8,6 WITH TIES);
SELECT count() FROM (WITH data AS (
SELECT * FROM numbers(0, 10)
UNION ALL
SELECT * FROM numbers(10, 10)
)
SELECT number div 11 AS eleven, number
FROM data
ORDER BY eleven
LIMIT 8,6 WITH TIES);
DROP TABLE ties;