| 231 | } |
| 232 | |
| 233 | void MergeTreeMetaBase::checkProperties( |
| 234 | const StorageInMemoryMetadata & new_metadata, const StorageInMemoryMetadata & old_metadata, bool attach) const |
| 235 | { |
| 236 | if (!new_metadata.sorting_key.definition_ast) |
| 237 | throw Exception("ORDER BY cannot be empty", ErrorCodes::BAD_ARGUMENTS); |
| 238 | |
| 239 | KeyDescription new_sorting_key = new_metadata.sorting_key; |
| 240 | KeyDescription new_primary_key = new_metadata.primary_key; |
| 241 | KeyDescription new_unique_key = new_metadata.unique_key; |
| 242 | |
| 243 | size_t sorting_key_size = new_sorting_key.column_names.size(); |
| 244 | size_t primary_key_size = new_primary_key.column_names.size(); |
| 245 | if (primary_key_size > sorting_key_size) |
| 246 | throw Exception("Primary key must be a prefix of the sorting key, but its length: " |
| 247 | + toString(primary_key_size) + " is greater than the sorting key length: " + toString(sorting_key_size), |
| 248 | ErrorCodes::BAD_ARGUMENTS); |
| 249 | |
| 250 | NameSet primary_key_columns_set; |
| 251 | |
| 252 | for (size_t i = 0; i < sorting_key_size; ++i) |
| 253 | { |
| 254 | const String & sorting_key_column = new_sorting_key.column_names[i]; |
| 255 | |
| 256 | if (i < primary_key_size) |
| 257 | { |
| 258 | const String & pk_column = new_primary_key.column_names[i]; |
| 259 | if (pk_column != sorting_key_column) |
| 260 | throw Exception("Primary key must be a prefix of the sorting key, but the column in the position " |
| 261 | + toString(i) + " is " + sorting_key_column +", not " + pk_column, |
| 262 | ErrorCodes::BAD_ARGUMENTS); |
| 263 | |
| 264 | // for compatibility with cnch-1.4, which allows dup key in `order by` |
| 265 | // if (!primary_key_columns_set.emplace(pk_column).second) |
| 266 | // throw Exception("Primary key contains duplicate columns", ErrorCodes::BAD_ARGUMENTS); |
| 267 | |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | auto all_columns = new_metadata.columns.getAllPhysical(); |
| 272 | |
| 273 | /// Order by check AST |
| 274 | if (old_metadata.hasSortingKey()) |
| 275 | { |
| 276 | /// This is ALTER, not CREATE/ATTACH TABLE. Let us check that all new columns used in the sorting key |
| 277 | /// expression have just been added (so that the sorting order is guaranteed to be valid with the new key). |
| 278 | |
| 279 | Names new_primary_key_columns = new_primary_key.column_names; |
| 280 | Names new_sorting_key_columns = new_sorting_key.column_names; |
| 281 | |
| 282 | ASTPtr added_key_column_expr_list = std::make_shared<ASTExpressionList>(); |
| 283 | const auto & old_sorting_key_columns = old_metadata.getSortingKeyColumns(); |
| 284 | for (size_t new_i = 0, old_i = 0; new_i < sorting_key_size; ++new_i) |
| 285 | { |
| 286 | if (old_i < old_sorting_key_columns.size()) |
| 287 | { |
| 288 | if (new_sorting_key_columns[new_i] != old_sorting_key_columns[old_i]) |
| 289 | added_key_column_expr_list->children.push_back(new_sorting_key.expression_list_ast->children[new_i]); |
| 290 | else |
nothing calls this directly
no test coverage detected