Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect result of arrayElement / map[] on empty value #59594

Merged
merged 3 commits into from Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/Functions/array/arrayElement.cpp
Expand Up @@ -670,8 +670,7 @@ struct ArrayElementStringImpl
ColumnArray::Offset current_offset = 0;
/// get the total result bytes at first, and reduce the cost of result_data.resize.
size_t total_result_bytes = 0;
ColumnString::Chars zero_buf(1);
zero_buf.push_back(0);
ColumnString::Chars zero_buf(16, '\0'); /// Needs 15 extra bytes for memcpySmallAllowReadWriteOverflow15
std::vector<std::pair<const ColumnString::Char *, UInt64>> selected_bufs;
selected_bufs.reserve(size);
for (size_t i = 0; i < size; ++i)
Expand Down Expand Up @@ -737,8 +736,7 @@ struct ArrayElementStringImpl
size_t size = offsets.size();
result_offsets.resize(size);

ColumnString::Chars zero_buf(1);
zero_buf.push_back(0);
ColumnString::Chars zero_buf(16, '\0'); /// Needs 15 extra bytes for memcpySmallAllowReadWriteOverflow15
ColumnArray::Offset current_offset = 0;
/// get the total result bytes at first, and reduce the cost of result_data.resize.
size_t total_result_bytes = 0;
Expand Down
7 changes: 7 additions & 0 deletions tests/queries/0_stateless/02983_empty_map.reference
@@ -0,0 +1,7 @@
-- { echoOn }
SELECT f1, f2['2'], count() FROM t1 GROUP BY 1,2 order by 1,2;
1 1000111
SELECT f1, f3['2'], count() FROM t1 GROUP BY 1,2 order by 1,2;
1 1000111
SELECT f1, f4[2], count() FROM t1 GROUP BY 1,2 order by 1,2;
1 0 1000111
21 changes: 21 additions & 0 deletions tests/queries/0_stateless/02983_empty_map.sql
@@ -0,0 +1,21 @@
--https://github.com/ClickHouse/ClickHouse/issues/59402
CREATE TABLE t1
(
f1 Int32,
f2 Map(LowCardinality(String),LowCardinality(String)),
f3 Map(String,String),
f4 Map(Int32,Int32)
)
ENGINE=Memory AS
SELECT 1 as f1,
map(number%2,number%10) as f2,
f2 as f3,
f2 as f4
from numbers(1000111);

SET max_block_size=10;

-- { echoOn }
SELECT f1, f2['2'], count() FROM t1 GROUP BY 1,2 order by 1,2;
SELECT f1, f3['2'], count() FROM t1 GROUP BY 1,2 order by 1,2;
SELECT f1, f4[2], count() FROM t1 GROUP BY 1,2 order by 1,2;