diff --git a/src/Functions/fuzzBits.cpp b/src/Functions/fuzzBits.cpp index 9094b90da30..475c5afbfd5 100644 --- a/src/Functions/fuzzBits.cpp +++ b/src/Functions/fuzzBits.cpp @@ -99,18 +99,40 @@ public: ColumnString::Chars & chars_to = col_to->getChars(); ColumnString::Offsets & offsets_to = col_to->getOffsets(); - chars_to.resize(col_in->getChars().size()); - // TODO: Maybe we can share `col_in->getOffsets()` to `offsets_to.resize` like clever pointers? They are same - offsets_to.resize(input_rows_count); + size_t col_in_rows = col_in->getOffsets().size(); - const auto * ptr_in = col_in->getChars().data(); - auto * ptr_to = chars_to.data(); - fuzzBits(ptr_in, ptr_to, chars_to.size(), inverse_probability); - - for (size_t i = 0; i < input_rows_count; ++i) + if (col_in_rows >= input_rows_count) { - offsets_to[i] = col_in->getOffsets()[i]; - ptr_to[offsets_to[i] - 1] = 0; + chars_to.resize(col_in->getChars().size()); + // TODO: Maybe we can share `col_in->getOffsets()` to `offsets_to.resize` like clever pointers? They are same + offsets_to.resize(input_rows_count); + + const auto * ptr_in = col_in->getChars().data(); + auto * ptr_to = chars_to.data(); + fuzzBits(ptr_in, ptr_to, chars_to.size(), inverse_probability); + + for (size_t i = 0; i < input_rows_count; ++i) + { + offsets_to[i] = col_in->getOffsets()[i]; + ptr_to[offsets_to[i] - 1] = 0; + } + } + else + { + assert(col_in_rows == 1); + chars_to.resize(col_in->getChars().size() * input_rows_count); + offsets_to.resize(input_rows_count); + size_t offset = col_in->getOffsets()[0]; + + const auto * ptr_in = col_in->getChars().data(); + auto * ptr_to = chars_to.data(); + + for (size_t i = 0; i < input_rows_count; ++i) + { + fuzzBits(ptr_in, ptr_to + i * offset, offset, inverse_probability); + offsets_to[i] = (i + 1) * offset; + ptr_to[offsets_to[i] - 1] = 0; + } } return col_to; diff --git a/tests/queries/0_stateless/01585_fuzz_bits_with_bugfix.reference b/tests/queries/0_stateless/01585_fuzz_bits_with_bugfix.reference new file mode 100644 index 00000000000..b7083447e98 --- /dev/null +++ b/tests/queries/0_stateless/01585_fuzz_bits_with_bugfix.reference @@ -0,0 +1,5 @@ +String +String +String +String +String diff --git a/tests/queries/0_stateless/01585_fuzz_bits_with_bugfix.sql b/tests/queries/0_stateless/01585_fuzz_bits_with_bugfix.sql new file mode 100644 index 00000000000..d41f18e2098 --- /dev/null +++ b/tests/queries/0_stateless/01585_fuzz_bits_with_bugfix.sql @@ -0,0 +1,3 @@ +SELECT toTypeName(fuzzBits('stringstring', 0.5)) from numbers(3); + +SELECT toTypeName(fuzzBits('stringstring', 0.5)) from ( SELECT 1 AS x UNION ALL SELECT NULL ) group by x