Skip to content

Commit 8a0da57

Browse files
authored
Merge pull request #712 from Altinity/feature/cleanup_setting_in_secondary_query
Convert functions with object_storage_cluster setting to cluster functions
2 parents 241c3d8 + 480ca5a commit 8a0da57

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

src/Storages/ObjectStorage/StorageObjectStorageCluster.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
{

src/Storages/extractTableFunctionArgumentsFromSelectQuery.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace DB
1212
{
1313

14-
ASTExpressionList * extractTableFunctionArgumentsFromSelectQuery(ASTPtr & query)
14+
ASTFunction * extractTableFunctionFromSelectQuery(ASTPtr & query)
1515
{
1616
auto * select_query = query->as<ASTSelectQuery>();
1717
if (!select_query || !select_query->tables())
@@ -23,6 +23,14 @@ ASTExpressionList * extractTableFunctionArgumentsFromSelectQuery(ASTPtr & query)
2323
return nullptr;
2424

2525
auto * table_function = table_expression->table_function->as<ASTFunction>();
26+
return table_function;
27+
}
28+
29+
ASTExpressionList * extractTableFunctionArgumentsFromSelectQuery(ASTPtr & query)
30+
{
31+
auto * table_function = extractTableFunctionFromSelectQuery(query);
32+
if (!table_function)
33+
return nullptr;
2634
return table_function->arguments->as<ASTExpressionList>();
2735
}
2836

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#pragma once
22

33
#include <Parsers/IAST_fwd.h>
4+
#include <Parsers/ASTFunction.h>
45
#include <Parsers/ASTExpressionList.h>
56

67
namespace DB
78
{
89

10+
ASTFunction * extractTableFunctionFromSelectQuery(ASTPtr & query);
911
ASTExpressionList * extractTableFunctionArgumentsFromSelectQuery(ASTPtr & query);
1012

1113
}

0 commit comments

Comments
 (0)