Fix LowCardinality(Nullable) key in hyperrectangle

This commit is contained in:
Amos Bird 2024-04-23 15:11:22 +08:00
parent d731838246
commit d44062edb2
No known key found for this signature in database
GPG Key ID: 80D430DCBECFEDB4
4 changed files with 25 additions and 8 deletions

View File

@ -2250,9 +2250,11 @@ static BoolMask forAnyHyperrectangle(
if (left_bounded && right_bounded)
hyperrectangle[prefix_size] = Range(left_keys[prefix_size], true, right_keys[prefix_size], true);
else if (left_bounded)
hyperrectangle[prefix_size] = Range::createLeftBounded(left_keys[prefix_size], true, data_types[prefix_size]->isNullable());
hyperrectangle[prefix_size]
= Range::createLeftBounded(left_keys[prefix_size], true, isNullableOrLowCardinalityNullable(data_types[prefix_size]));
else if (right_bounded)
hyperrectangle[prefix_size] = Range::createRightBounded(right_keys[prefix_size], true, data_types[prefix_size]->isNullable());
hyperrectangle[prefix_size]
= Range::createRightBounded(right_keys[prefix_size], true, isNullableOrLowCardinalityNullable(data_types[prefix_size]));
return callback(hyperrectangle);
}
@ -2262,13 +2264,15 @@ static BoolMask forAnyHyperrectangle(
if (left_bounded && right_bounded)
hyperrectangle[prefix_size] = Range(left_keys[prefix_size], false, right_keys[prefix_size], false);
else if (left_bounded)
hyperrectangle[prefix_size] = Range::createLeftBounded(left_keys[prefix_size], false, data_types[prefix_size]->isNullable());
hyperrectangle[prefix_size]
= Range::createLeftBounded(left_keys[prefix_size], false, isNullableOrLowCardinalityNullable(data_types[prefix_size]));
else if (right_bounded)
hyperrectangle[prefix_size] = Range::createRightBounded(right_keys[prefix_size], false, data_types[prefix_size]->isNullable());
hyperrectangle[prefix_size]
= Range::createRightBounded(right_keys[prefix_size], false, isNullableOrLowCardinalityNullable(data_types[prefix_size]));
for (size_t i = prefix_size + 1; i < key_size; ++i)
{
if (data_types[i]->isNullable())
if (isNullableOrLowCardinalityNullable(data_types[i]))
hyperrectangle[i] = Range::createWholeUniverse();
else
hyperrectangle[i] = Range::createWholeUniverseWithoutNull();
@ -2324,7 +2328,7 @@ BoolMask KeyCondition::checkInRange(
key_ranges.reserve(used_key_size);
for (size_t i = 0; i < used_key_size; ++i)
{
if (data_types[i]->isNullable())
if (isNullableOrLowCardinalityNullable(data_types[i]))
key_ranges.push_back(Range::createWholeUniverse());
else
key_ranges.push_back(Range::createWholeUniverseWithoutNull());

View File

@ -383,7 +383,8 @@ MergeTreeDataSelectSamplingData MergeTreeDataSelectExecutor::getSampling(
if (has_lower_limit)
{
if (!key_condition.addCondition(
sampling_key.column_names[0], Range::createLeftBounded(lower, true, sampling_key.data_types[0]->isNullable())))
sampling_key.column_names[0],
Range::createLeftBounded(lower, true, isNullableOrLowCardinalityNullable(sampling_key.data_types[0]))))
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Sampling column not in primary key");
ASTPtr args = std::make_shared<ASTExpressionList>();
@ -401,7 +402,8 @@ MergeTreeDataSelectSamplingData MergeTreeDataSelectExecutor::getSampling(
if (has_upper_limit)
{
if (!key_condition.addCondition(
sampling_key.column_names[0], Range::createRightBounded(upper, false, sampling_key.data_types[0]->isNullable())))
sampling_key.column_names[0],
Range::createRightBounded(upper, false, isNullableOrLowCardinalityNullable(sampling_key.data_types[0]))))
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Sampling column not in primary key");
ASTPtr args = std::make_shared<ASTExpressionList>();

View File

@ -0,0 +1,10 @@
DROP TABLE IF EXISTS small;
CREATE TABLE small (`dt` DateTime, `user_email` LowCardinality(Nullable(String)))
ENGINE = MergeTree order by (dt, user_email) settings allow_nullable_key = 1, min_bytes_for_wide_part=0, min_rows_for_wide_part=0;
INSERT INTO small (dt, user_email) SELECT number, if(number % 3 = 2, NULL, number) FROM numbers(1e2);
SELECT SUM(dt::int) FROM small WHERE user_email IS NULL;
DROP TABLE small;