diff --git a/src/Storages/MergeTree/MergeTask.cpp b/src/Storages/MergeTree/MergeTask.cpp index 58bffaab34b..02f5525e221 100644 --- a/src/Storages/MergeTree/MergeTask.cpp +++ b/src/Storages/MergeTree/MergeTask.cpp @@ -200,6 +200,20 @@ bool MergeTask::ExecuteAndFinalizeHorizontalPart::prepare() ctx->need_remove_expired_values = false; } + /// Skip fully expired columns manually, since in case of need_remove_expired_values is not set, + /// TTLTransform will not be used, and columns that had been removed by TTL will be added again with default values. + if (!ctx->need_remove_expired_values) + { + for (auto & [column_name, ttl] : global_ctx->new_data_part->ttl_infos.columns_ttl) + { + if (ttl.finished()) + { + global_ctx->new_data_part->expired_columns.insert(column_name); + LOG_TRACE(ctx->log, "Adding expired column {} for {}", column_name, global_ctx->new_data_part->name); + } + } + } + ctx->sum_input_rows_upper_bound = global_ctx->merge_list_element_ptr->total_rows_count; ctx->sum_compressed_bytes_upper_bound = global_ctx->merge_list_element_ptr->total_size_bytes_compressed; global_ctx->chosen_merge_algorithm = chooseMergeAlgorithm(); diff --git a/tests/queries/0_stateless/02335_column_ttl_expired_column_optimization.reference b/tests/queries/0_stateless/02335_column_ttl_expired_column_optimization.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/queries/0_stateless/02335_column_ttl_expired_column_optimization.sh b/tests/queries/0_stateless/02335_column_ttl_expired_column_optimization.sh new file mode 100755 index 00000000000..e2f7209d036 --- /dev/null +++ b/tests/queries/0_stateless/02335_column_ttl_expired_column_optimization.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +# shellcheck source=../shell_config.sh +. "$CUR_DIR"/../shell_config.sh + +$CLICKHOUSE_LOCAL --path "$CLICKHOUSE_TEST_UNIQUE_NAME" -nm -q " + create table ttl_02335 ( + date Date, + key Int, + value String TTL date + interval 1 month + ) + engine=MergeTree + order by key + settings + min_bytes_for_wide_part=0, + min_rows_for_wide_part=0; + + -- all_1_1_0 + -- all_1_1_1 + insert into ttl_02335 values ('2010-01-01', 2010, 'foo'); + -- all_1_1_2 + optimize table ttl_02335 final; + -- all_1_1_3 + optimize table ttl_02335 final; +" + +test -f "$CLICKHOUSE_TEST_UNIQUE_NAME"/data/_local/ttl_02335/all_1_1_3/value.bin && echo "[FAIL] value column should not exist" +exit 0