@@ -272,6 +272,55 @@ void StorageObjectStorageCluster::updateQueryToSendIfNeeded(
272272 if (cluster_name_in_settings)
273273 {
274274 configuration->addStructureAndFormatToArgsIfNeeded (args, structure, configuration->format , context, /* with_structure=*/ true );
275+
276+ // / Convert to old-stype *Cluster table function.
277+ // / This allows to use old clickhouse versions in cluster.
278+ static std::unordered_map<std::string, std::string> function_to_cluster_function = {
279+ {" s3" , " s3Cluster" },
280+ {" azureBlobStorage" , " azureBlobStorageCluster" },
281+ {" hdfs" , " hdfsCluster" },
282+ {" iceberg" , " icebergS3Cluster" },
283+ {" icebergS3" , " icebergS3Cluster" },
284+ {" icebergAzure" , " icebergAzureCluster" },
285+ {" icebergHDFS" , " icebergHDFSCluster" },
286+ {" deltaLake" , " deltaLakeCluster" },
287+ {" hudi" , " hudiCluster" },
288+ };
289+
290+ ASTFunction * table_function = extractTableFunctionFromSelectQuery (query);
291+
292+ auto p = function_to_cluster_function.find (table_function->name );
293+ if (p == function_to_cluster_function.end ())
294+ {
295+ throw Exception (
296+ ErrorCodes::LOGICAL_ERROR,
297+ " Can't find cluster name for table function {}" ,
298+ table_function->name );
299+ }
300+
301+ table_function->name = p->second ;
302+
303+ auto cluster_name = getClusterName (context);
304+ auto cluster_name_arg = std::make_shared<ASTLiteral>(cluster_name);
305+ args.insert (args.begin (), cluster_name_arg);
306+
307+ auto * select_query = query->as <ASTSelectQuery>();
308+ if (!select_query)
309+ throw Exception (
310+ ErrorCodes::LOGICAL_ERROR,
311+ " Expected SELECT query from table function {}" ,
312+ configuration->getEngineName ());
313+
314+ auto settings = select_query->settings ();
315+ if (settings)
316+ {
317+ auto & settings_ast = settings->as <ASTSetQuery &>();
318+ if (settings_ast.changes .removeSetting (" object_storage_cluster" ) && settings_ast.changes .empty ())
319+ {
320+ select_query->setExpression (ASTSelectQuery::Expression::SETTINGS, {});
321+ }
322+ // / No throw if not found - `object_storage_cluster` can be global setting.
323+ }
275324 }
276325 else
277326 {
0 commit comments