Skip to content

Commit

Permalink
When add duration field to UDT check whether this UDT is used in some…
Browse files Browse the repository at this point in the history
… clustering key

Having values of the duration type is not allowed for clustering
columns, because duration can't be ordered. This is correctly validated
when creating a table but do not validated when we alter the type.

Fixes #12913

Closes #16022
  • Loading branch information
alezzqz authored and denesb committed Nov 14, 2023
1 parent 4968f50 commit bd73536
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cql3/statements/alter_type_statement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ user_type alter_type_statement::add_or_alter::do_add(data_dictionary::database d
throw exceptions::invalid_request_exception(format("Cannot add new field to type {}: maximum number of fields reached", _name));
}

if (_field_type->is_duration()) {
auto&& ks = db.find_keyspace(keyspace());
for (auto&& schema : ks.metadata()->cf_meta_data() | boost::adaptors::map_values) {
for (auto&& column : schema->clustering_key_columns()) {
if (column.type->references_user_type(_name.get_keyspace(), _name.get_user_type_name())) {
throw exceptions::invalid_request_exception(format("Cannot add new field to type {} because it is used in the clustering key column {} of table {}.{} where durations are not allowed",
_name.to_cql_string(), column.name_as_text(), schema->ks_name(), schema->cf_name()));
}
}
}
}

std::vector<bytes> new_names(to_update->field_names());
new_names.push_back(_field_name->name());
std::vector<data_type> new_types(to_update->field_types());
Expand Down
6 changes: 6 additions & 0 deletions test/boost/user_types_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ SEASTAR_TEST_CASE(test_invalid_user_type_statements) {
"Cannot drop user type ks.ut6 as it is still used by user type ut7");
e.execute_cql("drop type ut7").discard_result().get();
e.execute_cql("drop type ut6").discard_result().get();

// cannot add duration field to UDT used in clustering keys (issue #12913)
e.execute_cql("create type ut8 (a int, b int)").discard_result().get();
e.execute_cql("create table cf4 (pk int, ck frozen<ut8>, primary key(pk, ck))").discard_result().get();
REQUIRE_INVALID(e, "alter type ut8 add d duration",
"Cannot add new field to type ks.ut8 because it is used in the clustering key column ck of table ks.cf4 where durations are not allowed");
});
}

Expand Down

0 comments on commit bd73536

Please sign in to comment.