@@ -84,6 +84,7 @@ extern const int ALTER_OF_COLUMN_IS_FORBIDDEN;
8484extern const int CANNOT_EXTRACT_TABLE_STRUCTURE;
8585extern const int STORAGE_REQUIRES_PARAMETER;
8686extern const int UNKNOWN_TABLE;
87+ extern const int ACCESS_DENIED;
8788extern const int TABLE_IS_READ_ONLY;
8889}
8990
@@ -1770,36 +1771,39 @@ SinkToStoragePtr StorageMerge::write(
17701771 bool async_insert)
17711772{
17721773 const auto & access = context_->getAccess ();
1773- std::optional<StoragePtr> table_storage{};
1774+
17741775 if (table_to_write_auto)
17751776 {
1776- chassert (!database_name_or_regexp.database_is_regexp );
1777- const auto & database = database_name_or_regexp.source_database_name_or_regexp ;
1778- auto allShowGranted = access->isGranted (AccessType::SHOW, database);
1779- forEachTable ([&](const StoragePtr & table)
1777+ table_to_write = std::nullopt ;
1778+ bool any_table_found = false ;
1779+ forEachTableName ([&](const auto & table_name)
17801780 {
1781- if (!allShowGranted && !access->isGranted (AccessType::SHOW_TABLES, database, table->getName ()))
1782- return ;
1783-
1784- if (!table_storage || table->getName () > table_storage.value ()->getName ())
1785- table_storage = table;
1781+ any_table_found = true ;
1782+ if (!table_to_write.has_value () || table_to_write->getFullName () < table_name.getFullName ())
1783+ {
1784+ if (access->isGranted (AccessType::INSERT, table_name.database , table_name.table ))
1785+ table_to_write = table_name;
1786+ }
17861787 });
1787-
1788- if (!table_storage)
1789- throw Exception (ErrorCodes::UNKNOWN_TABLE, " Can't find any table to write for storage {}" , getName ());
1790-
1791- access->checkAccess (AccessType::INSERT, database, table_storage.value ()->getName ());
1788+ if (!table_to_write.has_value ())
1789+ {
1790+ if (any_table_found)
1791+ throw Exception (ErrorCodes::ACCESS_DENIED, " Not allowed to write in any suitable table for storage {}" , getName ());
1792+ else
1793+ throw Exception (ErrorCodes::UNKNOWN_TABLE, " Can't find any table to write for storage {}" , getName ());
1794+ }
17921795 }
17931796 else
17941797 {
1795- if (!table_to_write)
1798+ if (!table_to_write. has_value () )
17961799 throw Exception (ErrorCodes::TABLE_IS_READ_ONLY, " Method write is not allowed in storage {} without described table to write" , getName ());
17971800
1798- access->checkAccess (AccessType::INSERT, table_to_write.value ().database , table_to_write.value ().table );
1799- table_storage = DatabaseCatalog::instance ().getTable (StorageID (table_to_write.value ()), context_);
1801+ access->checkAccess (AccessType::INSERT, table_to_write->database , table_to_write->table );
18001802 }
18011803
1802- return table_storage.value ()->write (query, metadata_snapshot, context_, async_insert);
1804+ auto database = DatabaseCatalog::instance ().getDatabase (table_to_write->database );
1805+ auto table = database->getTable (table_to_write->table , context_);
1806+ return table->write (query, metadata_snapshot, context_, async_insert);
18031807}
18041808
18051809void registerStorageMerge (StorageFactory & factory)
0 commit comments