diff --git a/dbms/src/Databases/DatabaseLazy.cpp b/dbms/src/Databases/DatabaseLazy.cpp index fad908853eb..344d44b378e 100644 --- a/dbms/src/Databases/DatabaseLazy.cpp +++ b/dbms/src/Databases/DatabaseLazy.cpp @@ -64,6 +64,12 @@ void DatabaseLazy::createTable( if (!endsWith(table->getName(), "Log")) throw Exception("Lazy engine can be used only with *Log tables.", ErrorCodes::UNSUPPORTED_METHOD); DatabaseOnDisk::createTable(*this, context, table_name, table, query); + + /// DatabaseOnDisk::createTable renames file, so we need to get new metadata_modification_time. + std::lock_guard lock(tables_mutex); + auto it = tables_cache.find(table_name); + if (it != tables_cache.end()) + it->second.metadata_modification_time = DatabaseOnDisk::getTableMetadataModificationTime(*this, table_name); } @@ -191,9 +197,7 @@ void DatabaseLazy::attachTable(const String & table_name, const StoragePtr & tab auto [it, inserted] = tables_cache.emplace(std::piecewise_construct, std::forward_as_tuple(table_name), - std::forward_as_tuple(table, - current_time, - DatabaseOnDisk::getTableMetadataModificationTime(*this, table_name))); + std::forward_as_tuple(table, current_time, DatabaseOnDisk::getTableMetadataModificationTime(*this, table_name))); if (!inserted) throw Exception("Table " + getDatabaseName() + "." + table_name + " already exists.", ErrorCodes::TABLE_ALREADY_EXISTS); diff --git a/dbms/src/Storages/System/StorageSystemTables.cpp b/dbms/src/Storages/System/StorageSystemTables.cpp index f2f092bfafd..21b18904edc 100644 --- a/dbms/src/Storages/System/StorageSystemTables.cpp +++ b/dbms/src/Storages/System/StorageSystemTables.cpp @@ -65,9 +65,9 @@ static bool needLockStructure(const DatabasePtr& database, const Block& header) if (database->getEngineName() != "Lazy") return true; - static std::set columns_without_lock = { "database", "name", "metadata_modification_time" }; + static const std::set columns_without_lock = { "database", "name", "metadata_modification_time" }; for (const auto& column : header.getColumnsWithTypeAndName()) { - if (columns_without_lock.find(column.name) != columns_without_lock.end()) { + if (columns_without_lock.find(column.name) == columns_without_lock.end()) { return true; } } diff --git a/dbms/tests/queries/0_stateless/01014_lazy_database_basic.reference b/dbms/tests/queries/0_stateless/01014_lazy_database_basic.reference index f7ed9be8c55..48e37b9ab11 100644 --- a/dbms/tests/queries/0_stateless/01014_lazy_database_basic.reference +++ b/dbms/tests/queries/0_stateless/01014_lazy_database_basic.reference @@ -1,15 +1,15 @@ -testlazy log Log 0 ['/var/lib/clickhouse/data/testlazy/log/'] /var/lib/clickhouse/metadata/testlazy/log.sql 0000-00-00 00:00:00 [] [] CREATE TABLE testlazy.log (`a` UInt64, `b` UInt64) ENGINE = Log Log -testlazy slog StripeLog 0 ['/var/lib/clickhouse/data/testlazy/slog/'] /var/lib/clickhouse/metadata/testlazy/slog.sql 0000-00-00 00:00:00 [] [] CREATE TABLE testlazy.slog (`a` UInt64, `b` UInt64) ENGINE = StripeLog StripeLog -testlazy tlog TinyLog 0 ['/var/lib/clickhouse/data/testlazy/tlog/'] /var/lib/clickhouse/metadata/testlazy/tlog.sql 0000-00-00 00:00:00 [] [] CREATE TABLE testlazy.tlog (`a` UInt64, `b` UInt64) ENGINE = TinyLog TinyLog -testlazy log 0000-00-00 00:00:00 -testlazy slog 0000-00-00 00:00:00 -testlazy tlog 0000-00-00 00:00:00 -testlazy log2 0000-00-00 00:00:00 -testlazy slog 0000-00-00 00:00:00 -testlazy tlog 0000-00-00 00:00:00 -testlazy log2 0000-00-00 00:00:00 -testlazy slog 0000-00-00 00:00:00 -testlazy tlog 0000-00-00 00:00:00 +testlazy log CREATE TABLE testlazy.log (`a` UInt64, `b` UInt64) ENGINE = Log +testlazy slog CREATE TABLE testlazy.slog (`a` UInt64, `b` UInt64) ENGINE = StripeLog +testlazy tlog CREATE TABLE testlazy.tlog (`a` UInt64, `b` UInt64) ENGINE = TinyLog +testlazy log +testlazy slog +testlazy tlog +testlazy log2 +testlazy slog +testlazy tlog +testlazy log2 +testlazy slog +testlazy tlog 1 1 2 2 3 3 diff --git a/dbms/tests/queries/0_stateless/01014_lazy_database_basic.sh b/dbms/tests/queries/0_stateless/01014_lazy_database_basic.sh index a4e09302db5..041948a21eb 100755 --- a/dbms/tests/queries/0_stateless/01014_lazy_database_basic.sh +++ b/dbms/tests/queries/0_stateless/01014_lazy_database_basic.sh @@ -14,13 +14,13 @@ ${CLICKHOUSE_CLIENT} -n -q " sleep 1.5 ${CLICKHOUSE_CLIENT} -q " - SELECT * FROM system.tables WHERE database = 'testlazy'; + SELECT database, name, create_table_query FROM system.tables WHERE database = 'testlazy'; " sleep 1.5 ${CLICKHOUSE_CLIENT} -q " - SELECT database, name, metadata_modification_time FROM system.tables WHERE database = 'testlazy'; + SELECT database, name FROM system.tables WHERE database = 'testlazy'; " sleep 1.5 @@ -28,13 +28,13 @@ sleep 1.5 ${CLICKHOUSE_CLIENT} -n -q " SELECT * FROM testlazy.log LIMIT 0; -- drop testlazy.log from cache RENAME TABLE testlazy.log TO testlazy.log2; - SELECT database, name, metadata_modification_time FROM system.tables WHERE database = 'testlazy'; + SELECT database, name FROM system.tables WHERE database = 'testlazy'; " sleep 1.5 ${CLICKHOUSE_CLIENT} -q " - SELECT database, name, metadata_modification_time FROM system.tables WHERE database = 'testlazy'; + SELECT database, name FROM system.tables WHERE database = 'testlazy'; " sleep 1.5