diff --git a/dbms/src/Storages/MergeTree/MergeTreeData.cpp b/dbms/src/Storages/MergeTree/MergeTreeData.cpp index 899b3552c8d..8e7146e4bfa 100644 --- a/dbms/src/Storages/MergeTree/MergeTreeData.cpp +++ b/dbms/src/Storages/MergeTree/MergeTreeData.cpp @@ -117,7 +117,8 @@ MergeTreeData::MergeTreeData( initPrimaryKey(); - if (sampling_expression && (!primary_expr_ast || !primary_key_sample.has(sampling_expression->getColumnName()))) + if (sampling_expression && (!primary_expr_ast || !primary_key_sample.has(sampling_expression->getColumnName())) + && !attach && !settings.compatibility_allow_sampling_expression_not_in_primary_key) /// This is for backward compatibility. throw Exception("Sampling expression must be present in the primary key", ErrorCodes::BAD_ARGUMENTS); MergeTreeDataFormatVersion min_format_version(0); @@ -125,7 +126,7 @@ MergeTreeData::MergeTreeData( { try { - String partition_expr_str = "toYYYYMM(" + date_column_name + ")"; + String partition_expr_str = "toYYYYMM(" + backQuoteIfNeed(date_column_name) + ")"; ParserNotEmptyExpressionList parser(/* allow_alias_without_as_keyword = */ false); partition_expr_ast = parseQuery( parser, partition_expr_str.data(), partition_expr_str.data() + partition_expr_str.length(), "partition expression"); diff --git a/dbms/src/Storages/MergeTree/MergeTreeSettings.h b/dbms/src/Storages/MergeTree/MergeTreeSettings.h index bfd2b91dd8d..f91ca880a66 100644 --- a/dbms/src/Storages/MergeTree/MergeTreeSettings.h +++ b/dbms/src/Storages/MergeTree/MergeTreeSettings.h @@ -122,7 +122,15 @@ struct MergeTreeSettings M(SettingUInt64, vertical_merge_algorithm_min_rows_to_activate, 16 * DEFAULT_MERGE_BLOCK_SIZE) \ \ /** Minimal amount of non-PK columns to activate Vertical merge algorithm */ \ - M(SettingUInt64, vertical_merge_algorithm_min_columns_to_activate, 11) + M(SettingUInt64, vertical_merge_algorithm_min_columns_to_activate, 11) \ + \ + /** Compatibility settings */ \ + \ + /** Allow to create a table with sampling expression not in primary key. \ + * This is needed only to temporarily allow to run the server with wrong tables \ + * for backward compatibility. \ + */ \ + M(SettingBool, compatibility_allow_sampling_expression_not_in_primary_key, false) \ /// Settings that should not change after the creation of a table. #define APPLY_FOR_IMMUTABLE_MERGE_TREE_SETTINGS(M) \