Skip to content

Commit 955ed4a

Browse files
Merge pull request ClickHouse#90047 from ClickHouse/backport/25.8/89680
Backport ClickHouse#89680 to 25.8: Prevent false positive error on empty LowCardinality indexes
2 parents fb58e47 + b0069bf commit 955ed4a

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/Columns/ColumnLowCardinality.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ static void checkPositionsAreLimited(const IColumn & positions, UInt64 limit)
240240

241241
const auto & data = column_ptr->getData();
242242
size_t num_rows = data.size();
243+
if (num_rows == 0)
244+
return true;
243245
UInt64 max_position = 0;
244246
for (size_t i = 0; i < num_rows; ++i)
245247
max_position = std::max<UInt64>(max_position, data[i]);

src/Columns/tests/gtest_low_cardinality.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,25 @@ TEST(ColumnLowCardinality, Clone)
6262
ASSERT_TRUE(assert_cast<const ColumnLowCardinality &>(*nullable_column).nestedIsNullable());
6363
ASSERT_FALSE(assert_cast<const ColumnLowCardinality &>(*column).nestedIsNullable());
6464
}
65+
66+
TEST(ColumnLowCardinality, EmptyDictionaryEmptyIndexes)
67+
{
68+
/// Test edge case: empty dictionary (size=0) with empty indexes (num_rows=0)
69+
/// This should not throw an error, as empty indexes are always valid
70+
/// Regression test for bug where check was: if (max_position >= limit)
71+
/// When num_rows=0, max_position stays 0, and with limit=0, this incorrectly threw
72+
73+
auto data_type = std::make_shared<DataTypeUInt32>();
74+
auto low_cardinality_type = std::make_shared<DataTypeLowCardinality>(data_type);
75+
auto column = low_cardinality_type->createColumn();
76+
auto & lc_column = assert_cast<ColumnLowCardinality &>(*column);
77+
78+
// Create empty keys and indexes columns
79+
auto empty_keys = ColumnUInt32::create();
80+
auto empty_indexes = ColumnUInt8::create();
81+
82+
// This should NOT throw an exception
83+
ASSERT_NO_THROW(lc_column.insertRangeFromDictionaryEncodedColumn(*empty_keys, *empty_indexes));
84+
85+
ASSERT_EQ(column->size(), 0);
86+
}

0 commit comments

Comments
 (0)