Skip to content

Commit 9adcd6e

Browse files
Enmkianton-ru
authored andcommitted
Merge pull request #712 from Altinity/feature/cleanup_setting_in_secondary_query
Convert functions with object_storage_cluster setting to cluster functions
1 parent 00e376d commit 9adcd6e

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

src/Storages/ObjectStorage/StorageObjectStorageCluster.cpp

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

src/Storages/extractTableFunctionFromSelectQuery.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,16 @@ ASTFunction * extractTableFunctionFromSelectQuery(ASTPtr & query)
2020
if (!table_expression->table_function)
2121
return nullptr;
2222

23-
return table_expression->table_function->as<ASTFunction>();
23+
auto * table_function = table_expression->table_function->as<ASTFunction>();
24+
return table_function;
25+
}
26+
27+
ASTExpressionList * extractTableFunctionArgumentsFromSelectQuery(ASTPtr & query)
28+
{
29+
auto * table_function = extractTableFunctionFromSelectQuery(query);
30+
if (!table_function)
31+
return nullptr;
32+
return table_function->arguments->as<ASTExpressionList>();
2433
}
2534

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

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

78
namespace DB
89
{
910

1011
ASTFunction * extractTableFunctionFromSelectQuery(ASTPtr & query);
12+
ASTExpressionList * extractTableFunctionArgumentsFromSelectQuery(ASTPtr & query);
1113

1214
}

0 commit comments

Comments
 (0)