@@ -221,7 +221,7 @@ ColumnsDescription StorageMerge::getColumnsDescriptionFromSourceTablesImpl(
221221 size_t table_num = 0 ;
222222 ColumnsDescription res;
223223
224- getFirstTableImpl (query_context, ignore_self, database_name_or_regexp, [&table_num, &access, &res, max_tables_to_look](auto && t)
224+ traverseTablesUntilImpl (query_context, ignore_self, database_name_or_regexp, [&table_num, &access, &res, max_tables_to_look](auto && t)
225225 {
226226 if (!t)
227227 return false ;
@@ -257,13 +257,13 @@ ColumnsDescription StorageMerge::getColumnsDescriptionFromSourceTablesImpl(
257257}
258258
259259template <typename F>
260- StoragePtr StorageMerge::getFirstTable (F && predicate) const
260+ StoragePtr StorageMerge::traverseTablesUntil (F && predicate) const
261261{
262- return getFirstTableImpl (context. lock (), this , database_name_or_regexp, std::forward<F>(predicate));
262+ return traverseTablesUntilImpl ( getContext (), this , database_name_or_regexp, std::forward<F>(predicate));
263263}
264264
265265template <typename F>
266- StoragePtr StorageMerge::getFirstTableImpl (const ContextPtr & query_context, const IStorage * ignore_self, const DatabaseNameOrRegexp & database_name_or_regexp, F && predicate)
266+ StoragePtr StorageMerge::traverseTablesUntilImpl (const ContextPtr & query_context, const IStorage * ignore_self, const DatabaseNameOrRegexp & database_name_or_regexp, F && predicate)
267267{
268268 auto database_table_iterators = database_name_or_regexp.getDatabaseIterators (query_context);
269269
@@ -285,7 +285,7 @@ StoragePtr StorageMerge::getFirstTableImpl(const ContextPtr & query_context, con
285285template <typename F>
286286void StorageMerge::forEachTable (F && func) const
287287{
288- getFirstTable ([&func](const auto & table)
288+ traverseTablesUntil ([&func](const auto & table)
289289 {
290290 func (table);
291291 // / Always continue to the next table.
@@ -295,13 +295,13 @@ void StorageMerge::forEachTable(F && func) const
295295
296296bool StorageMerge::isRemote () const
297297{
298- auto first_remote_table = getFirstTable ([](const StoragePtr & table) { return table && table->isRemote (); });
298+ auto first_remote_table = traverseTablesUntil ([](const StoragePtr & table) { return table && table->isRemote (); });
299299 return first_remote_table != nullptr ;
300300}
301301
302302bool StorageMerge::supportsPrewhere () const
303303{
304- return getFirstTable ([](const auto & table) { return !table->supportsPrewhere (); }) == nullptr ;
304+ return traverseTablesUntil ([](const auto & table) { return !table->supportsPrewhere (); }) == nullptr ;
305305}
306306
307307bool StorageMerge::canMoveConditionsToPrewhere () const
@@ -316,7 +316,7 @@ bool StorageMerge::canMoveConditionsToPrewhere() const
316316 // / NOTE: Type can be different, and in this case, PREWHERE cannot be
317317 // / applied for those columns, but there a separate method to return
318318 // / supported columns for PREWHERE - supportedPrewhereColumns().
319- return getFirstTable ([](const auto & table) { return !table->canMoveConditionsToPrewhere (); }) == nullptr ;
319+ return traverseTablesUntil ([](const auto & table) { return !table->canMoveConditionsToPrewhere (); }) == nullptr ;
320320}
321321
322322std::optional<NameSet> StorageMerge::supportedPrewhereColumns () const
@@ -421,7 +421,7 @@ StorageSnapshotPtr StorageMerge::getStorageSnapshot(const StorageMetadataPtr & m
421421 const auto & access = query_context->getAccess ();
422422
423423 auto virtuals = common_virtuals;
424- if (auto first_table = getFirstTable ([access](auto && table)
424+ if (auto first_table = traverseTablesUntil ([access](auto && table)
425425 {
426426 if (!table)
427427 return false ;
@@ -1325,12 +1325,13 @@ StorageMerge::StorageListWithLocks ReadFromMerge::getSelectedTables(
13251325 ColumnWithTypeAndName (std::make_shared<DataTypeString>(), " _database" ),
13261326 ColumnWithTypeAndName (std::make_shared<DataTypeString>(), " _table" )
13271327 };
1328+ // Extract predicate part, that could be evaluated only with _database and _table columns
13281329 auto table_filter_dag = VirtualColumnUtils::splitFilterDagForAllowedInputs (filter_actions_dag->getOutputs ().at (0 ), &sample_block);
13291330 if (table_filter_dag)
13301331 {
13311332 auto filter_expression = VirtualColumnUtils::buildFilterExpression (std::move (*table_filter_dag), query_context);
13321333 auto filter_column_name = filter_expression->getActionsDAG ().getOutputs ().at (0 )->result_name ;
1333- table_filter = [filter_expression, filter_column_name] (const auto & database_name, const auto & table_name)
1334+ table_filter = [filter= std::move ( filter_expression), column_name= std::move ( filter_column_name) ] (const auto & database_name, const auto & table_name)
13341335 {
13351336 MutableColumnPtr database_column = ColumnString::create ();
13361337 MutableColumnPtr table_column = ColumnString::create ();
@@ -1340,18 +1341,18 @@ StorageMerge::StorageListWithLocks ReadFromMerge::getSelectedTables(
13401341 ColumnWithTypeAndName (std::move (database_column), std::make_shared<DataTypeString>(), " _database" ),
13411342 ColumnWithTypeAndName (std::move (table_column), std::make_shared<DataTypeString>(), " _table" )
13421343 };
1343- filter_expression ->execute (block);
1344+ filter ->execute (block);
13441345 // Valid only when block has exactly one row.
1345- return block.getByName (filter_column_name ).column ->getBool (0 );
1346+ return block.getByName (column_name ).column ->getBool (0 );
13461347 };
13471348 }
13481349 }
13491350
13501351 auto access = query_context->getAccess ();
13511352 for (const auto & iterator : database_table_iterators)
13521353 {
1353- auto all_show = access->isGranted (AccessType::SHOW_TABLES, iterator->databaseName ());
1354- auto all_select = access->isGranted (AccessType::SELECT, iterator->databaseName ());
1354+ auto granted_show_on_all_tables = access->isGranted (AccessType::SHOW_TABLES, iterator->databaseName ());
1355+ auto granted_select_on_all_tables = access->isGranted (AccessType::SELECT, iterator->databaseName ());
13551356 while (iterator->isValid ())
13561357 {
13571358 StoragePtr storage = iterator->table ();
@@ -1360,10 +1361,10 @@ StorageMerge::StorageListWithLocks ReadFromMerge::getSelectedTables(
13601361
13611362 if (storage.get () != storage_merge.get ())
13621363 if (!table_filter || table_filter (iterator->databaseName (), iterator->name ()))
1363- if (all_show || access->isGranted (AccessType::SHOW_TABLES, iterator->databaseName (), iterator->name ()))
1364+ if (granted_show_on_all_tables || access->isGranted (AccessType::SHOW_TABLES, iterator->databaseName (), iterator->name ()))
13641365 {
13651366
1366- if (!all_select )
1367+ if (!granted_select_on_all_tables )
13671368 access->checkAccess (AccessType::SELECT, iterator->databaseName (), iterator->name (), column_names);
13681369
13691370 auto table_lock = storage->lockForShare (query_context->getCurrentQueryId (), settings[Setting::lock_acquire_timeout]);
@@ -1671,7 +1672,7 @@ bool StorageMerge::supportsTrivialCountOptimization(const StorageSnapshotPtr &,
16711672{
16721673 // / Here we actually need storage snapshot of all nested tables.
16731674 // / But to avoid complexity pass nullptr to make more lightweight check in MergeTreeData.
1674- return getFirstTable ([&](const auto & table) { return !table->supportsTrivialCountOptimization (nullptr , ctx); }) == nullptr ;
1675+ return traverseTablesUntil ([&](const auto & table) { return !table->supportsTrivialCountOptimization (nullptr , ctx); }) == nullptr ;
16751676}
16761677
16771678std::optional<UInt64> StorageMerge::totalRows (ContextPtr query_context) const
@@ -1688,7 +1689,7 @@ template <typename F>
16881689std::optional<UInt64> StorageMerge::totalRowsOrBytes (F && func) const
16891690{
16901691 UInt64 total_rows_or_bytes = 0 ;
1691- auto first_table = getFirstTable ([&](const auto & table)
1692+ auto first_table = traverseTablesUntil ([&](const auto & table)
16921693 {
16931694 if (auto rows_or_bytes = func (table))
16941695 {
0 commit comments