@@ -291,7 +291,56 @@ void StorageObjectStorageCluster::updateQueryToSendIfNeeded(
291291    }
292292
293293    if  (cluster_name_in_settings || !endsWith (table_function->name , " Cluster"  ))
294+     {
294295        configuration->addStructureAndFormatToArgsIfNeeded (args, structure, configuration->format , context, /* with_structure=*/ true );
296+ 
297+         // / Convert to old-stype *Cluster table function.
298+         // / This allows to use old clickhouse versions in cluster.
299+         static  std::unordered_map<std::string, std::string> function_to_cluster_function = {
300+             {" s3"  , " s3Cluster"  },
301+             {" azureBlobStorage"  , " azureBlobStorageCluster"  },
302+             {" hdfs"  , " hdfsCluster"  },
303+             {" iceberg"  , " icebergS3Cluster"  },
304+             {" icebergS3"  , " icebergS3Cluster"  },
305+             {" icebergAzure"  , " icebergAzureCluster"  },
306+             {" icebergHDFS"  , " icebergHDFSCluster"  },
307+             {" deltaLake"  , " deltaLakeCluster"  },
308+             {" hudi"  , " hudiCluster"  },
309+         };
310+ 
311+         auto  p = function_to_cluster_function.find (table_function->name );
312+         if  (p == function_to_cluster_function.end ())
313+         {
314+             throw  Exception (
315+                 ErrorCodes::LOGICAL_ERROR,
316+                 " Can't find cluster name for table function {}"  ,
317+                 table_function->name );
318+         }
319+ 
320+         table_function->name  = p->second ;
321+ 
322+         auto  cluster_name = getClusterName (context);
323+         auto  cluster_name_arg = std::make_shared<ASTLiteral>(cluster_name);
324+         args.insert (args.begin (), cluster_name_arg);
325+ 
326+         auto  * select_query = query->as <ASTSelectQuery>();
327+         if  (!select_query)
328+             throw  Exception (
329+                 ErrorCodes::LOGICAL_ERROR,
330+                 " Expected SELECT query from table function {}"  ,
331+                 configuration->getEngineName ());
332+ 
333+         auto  settings = select_query->settings ();
334+         if  (settings)
335+         {
336+             auto  & settings_ast = settings->as <ASTSetQuery &>();
337+             if  (settings_ast.changes .removeSetting (" object_storage_cluster"  ) && settings_ast.changes .empty ())
338+             {
339+                 select_query->setExpression (ASTSelectQuery::Expression::SETTINGS, {});
340+             }
341+             // / No throw if not found - `object_storage_cluster` can be global setting.
342+         }
343+     }
295344    else 
296345    {
297346        ASTPtr cluster_name_arg = args.front ();
0 commit comments