Merge pull request #68860 from ClickHouse/backport/24.6/68681

Backport #68681 to 24.6: Fix ColumnVariant permutation
This commit is contained in:
Kruglov Pavel 2024-08-29 13:32:44 +02:00 committed by GitHub
commit 2519ed380e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 3 deletions

View File

@ -930,7 +930,7 @@ ColumnPtr ColumnVariant::index(const IColumn & indexes, size_t limit) const
{
/// If we have only NULLs, index will take no effect, just return resized column.
if (hasOnlyNulls())
return cloneResized(limit);
return cloneResized(limit == 0 ? indexes.size(): limit);
/// Optimization when we have only one non empty variant and no NULLs.
/// In this case local_discriminators column is filled with identical values and offsets column
@ -985,10 +985,18 @@ ColumnPtr ColumnVariant::indexImpl(const PaddedPODArray<Type> & indexes, size_t
Columns new_variants;
new_variants.reserve(num_variants);
for (size_t i = 0; i != num_variants; ++i)
{
/// Check if no values from this variant were selected.
if (nested_perms[i].empty())
{
new_variants.emplace_back(variants[i]->cloneEmpty());
}
else
{
size_t nested_limit = nested_perms[i].size() == variants[i]->size() ? 0 : nested_perms[i].size();
new_variants.emplace_back(variants[i]->permute(nested_perms[i], nested_limit));
}
}
/// We cannot use new_offsets column as an offset column, because it became invalid after variants permutation.
/// New offsets column will be created in constructor.

View File

@ -0,0 +1,8 @@
SET allow_experimental_variant_type = 1;
drop table if exists test;
create table test (id UInt32, data Variant(String), version UInt64) engine=ReplacingMergeTree(version) order by id;
insert into test values (1, NULL, 1), (2, 'bar', 1), (3, 'bar', 1);
insert into test select id, 'baz' as _data, version+1 as _version from test where id=2;
select * from test final WHERE data is not null format Null;
drop table test;