From 9e31800ebb0481270739595e302d8f298839fc95 Mon Sep 17 00:00:00 2001 From: Vitaly Baranov Date: Tue, 22 Dec 2020 23:16:25 +0300 Subject: [PATCH] Fix filling table system.settings_profile_elements. --- .../StorageSystemSettingsProfileElements.cpp | 168 ++++++++---------- ..._settings_profile_while_assigned.reference | 2 + ...5_drop_settings_profile_while_assigned.sql | 8 + 3 files changed, 82 insertions(+), 96 deletions(-) create mode 100644 tests/queries/0_stateless/01605_drop_settings_profile_while_assigned.reference create mode 100644 tests/queries/0_stateless/01605_drop_settings_profile_while_assigned.sql diff --git a/src/Storages/System/StorageSystemSettingsProfileElements.cpp b/src/Storages/System/StorageSystemSettingsProfileElements.cpp index 6d6df4fe114..cf47416e188 100644 --- a/src/Storages/System/StorageSystemSettingsProfileElements.cpp +++ b/src/Storages/System/StorageSystemSettingsProfileElements.cpp @@ -68,121 +68,97 @@ void StorageSystemSettingsProfileElements::fillData(MutableColumns & res_columns auto add_rows_for_single_element = [&](const String & owner_name, EntityType owner_type, const SettingsProfileElement & element, size_t & index) { - switch (owner_type) + size_t old_num_rows = column_profile_name.size(); + size_t new_num_rows = old_num_rows + 1; + size_t current_index = index++; + + bool inserted_value = false; + if (!element.value.isNull() && !element.setting_name.empty()) { - case EntityType::SETTINGS_PROFILE: - { - column_user_name.insertDefault(); - column_user_name_null_map.push_back(true); - column_role_name.insertDefault(); - column_role_name_null_map.push_back(true); - column_profile_name.insertData(owner_name.data(), owner_name.length()); - column_profile_name_null_map.push_back(false); - break; - } - case EntityType::USER: - { - column_user_name.insertData(owner_name.data(), owner_name.length()); - column_user_name_null_map.push_back(false); - column_profile_name.insertDefault(); - column_profile_name_null_map.push_back(true); - column_role_name.insertDefault(); - column_role_name_null_map.push_back(true); - break; - } - case EntityType::ROLE: - { - column_user_name.insertDefault(); - column_user_name_null_map.push_back(true); - column_role_name.insertData(owner_name.data(), owner_name.length()); - column_role_name_null_map.push_back(false); - column_profile_name.insertDefault(); - column_profile_name_null_map.push_back(true); - break; - } - default: - assert(false); + String str = Settings::valueToStringUtil(element.setting_name, element.value); + column_value.insertData(str.data(), str.length()); + column_value_null_map.push_back(false); + inserted_value = true; } + bool inserted_min = false; + if (!element.min_value.isNull() && !element.setting_name.empty()) + { + String str = Settings::valueToStringUtil(element.setting_name, element.min_value); + column_min.insertData(str.data(), str.length()); + column_min_null_map.push_back(false); + inserted_min = true; + } + + bool inserted_max = false; + if (!element.max_value.isNull() && !element.setting_name.empty()) + { + String str = Settings::valueToStringUtil(element.setting_name, element.max_value); + column_max.insertData(str.data(), str.length()); + column_max_null_map.push_back(false); + inserted_max = true; + } + + bool inserted_readonly = false; + if (element.readonly && !element.setting_name.empty()) + { + column_readonly.push_back(*element.readonly); + column_readonly_null_map.push_back(false); + inserted_readonly = true; + } + + bool inserted_setting_name = false; + if (inserted_value || inserted_min || inserted_max || inserted_readonly) + { + const auto & setting_name = element.setting_name; + column_setting_name.insertData(setting_name.data(), setting_name.size()); + column_setting_name_null_map.push_back(false); + inserted_setting_name = true; + } + + bool inserted_inherit_profile = false; if (element.parent_profile) { auto parent_profile = access_control.tryReadName(*element.parent_profile); if (parent_profile) { - column_index.push_back(index++); - column_setting_name.insertDefault(); - column_setting_name_null_map.push_back(true); - column_value.insertDefault(); - column_value_null_map.push_back(true); - column_min.insertDefault(); - column_min_null_map.push_back(true); - column_max.insertDefault(); - column_max_null_map.push_back(true); - column_readonly.push_back(0); - column_readonly_null_map.push_back(true); const String & parent_profile_str = *parent_profile; column_inherit_profile.insertData(parent_profile_str.data(), parent_profile_str.length()); column_inherit_profile_null_map.push_back(false); + inserted_inherit_profile = true; } } - if (!element.setting_name.empty() - && (!element.value.isNull() || !element.min_value.isNull() || !element.max_value.isNull() || element.readonly)) + if (inserted_setting_name || inserted_inherit_profile) { - const auto & setting_name = element.setting_name; - column_index.push_back(index++); - column_setting_name.insertData(setting_name.data(), setting_name.size()); - column_setting_name_null_map.push_back(false); - - if (element.value.isNull()) + switch (owner_type) { - column_value.insertDefault(); - column_value_null_map.push_back(true); - } - else - { - String str = Settings::valueToStringUtil(setting_name, element.value); - column_value.insertData(str.data(), str.length()); - column_value_null_map.push_back(false); + case EntityType::SETTINGS_PROFILE: + { + column_profile_name.insertData(owner_name.data(), owner_name.length()); + column_profile_name_null_map.push_back(false); + break; + } + case EntityType::USER: + { + column_user_name.insertData(owner_name.data(), owner_name.length()); + column_user_name_null_map.push_back(false); + break; + } + case EntityType::ROLE: + { + column_role_name.insertData(owner_name.data(), owner_name.length()); + column_role_name_null_map.push_back(false); + break; + } + default: + assert(false); } - if (element.min_value.isNull()) - { - column_min.insertDefault(); - column_min_null_map.push_back(true); - } - else - { - String str = Settings::valueToStringUtil(setting_name, element.min_value); - column_min.insertData(str.data(), str.length()); - column_min_null_map.push_back(false); - } + column_index.push_back(current_index); - if (element.max_value.isNull()) - { - column_max.insertDefault(); - column_max_null_map.push_back(true); - } - else - { - String str = Settings::valueToStringUtil(setting_name, element.max_value); - column_max.insertData(str.data(), str.length()); - column_max_null_map.push_back(false); - } - - if (element.readonly) - { - column_readonly.push_back(*element.readonly); - column_readonly_null_map.push_back(false); - } - else - { - column_readonly.push_back(0); - column_readonly_null_map.push_back(true); - } - - column_inherit_profile.insertDefault(); - column_inherit_profile_null_map.push_back(true); + for (auto & res_column : res_columns) + res_column->insertManyDefaults(new_num_rows - res_column->size()); } }; diff --git a/tests/queries/0_stateless/01605_drop_settings_profile_while_assigned.reference b/tests/queries/0_stateless/01605_drop_settings_profile_while_assigned.reference new file mode 100644 index 00000000000..47942812a11 --- /dev/null +++ b/tests/queries/0_stateless/01605_drop_settings_profile_while_assigned.reference @@ -0,0 +1,2 @@ +\N test_01605 \N 0 \N \N \N \N \N test_01605 +PROFILE DROPPED diff --git a/tests/queries/0_stateless/01605_drop_settings_profile_while_assigned.sql b/tests/queries/0_stateless/01605_drop_settings_profile_while_assigned.sql new file mode 100644 index 00000000000..c9205d7fd89 --- /dev/null +++ b/tests/queries/0_stateless/01605_drop_settings_profile_while_assigned.sql @@ -0,0 +1,8 @@ +CREATE USER OR REPLACE 'test_01605'; +CREATE SETTINGS PROFILE OR REPLACE 'test_01605'; +ALTER USER 'test_01605' SETTINGS PROFILE 'test_01605'; +SELECT * FROM system.settings_profile_elements WHERE user_name='test_01605' OR profile_name='test_01605'; +DROP SETTINGS PROFILE 'test_01605'; +SELECT 'PROFILE DROPPED'; +SELECT * FROM system.settings_profile_elements WHERE user_name='test_01605' OR profile_name='test_01605'; +DROP USER 'test_01605';