Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/Planner/PlannerJoinTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -979,8 +979,11 @@ void prepareBuildQueryPlanForTableExpression(const QueryTreeNodePtr & table_expr
if (table_node)
{
const auto & column_names_with_aliases = table_expression_data.getSelectedColumnsNames();
columns_names_allowed_to_select = checkAccessRights(
table_node->getStorage(), table_node->getStorageID(), table_node->getStorageSnapshot(), column_names_with_aliases, query_context);
const auto is_access_already_checked
= select_query_options.ignore_access_check && select_query_options.only_analyze && !column_names_with_aliases.empty();
if (!is_access_already_checked)
columns_names_allowed_to_select = checkAccessRights(
table_node->getStorage(), table_node->getStorageID(), table_node->getStorageSnapshot(), column_names_with_aliases, query_context);

checkAccessRightsForColumnsResolvedAway(*table_node, table_expression_data, query_context);
}
Expand Down Expand Up @@ -3171,7 +3174,7 @@ JoinTreeQueryPlan buildQueryPlanForTableExpression(TableExpressionNodePtr table_
/// Trivial count already emits the column with the aggregate's action-node name
/// (see `applyTrivialCountIfPossible`), so the structure matches the expected
/// header by construction and we can skip the recursive planner entirely.
SelectQueryOptions analyze_query_options = SelectQueryOptions(till_stage).analyze();
SelectQueryOptions analyze_query_options = SelectQueryOptions(till_stage).analyze().ignoreAccessCheck();
Planner planner(select_query_info.query_tree,
analyze_query_options,
select_query_info.planner_context);
Expand Down
115 changes: 113 additions & 2 deletions src/Storages/IStorageCluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,20 @@
#include <Parsers/ASTIdentifier.h>
#include <Parsers/ASTLiteral.h>
#include <Parsers/ASTSelectQuery.h>
#include <Parsers/ASTSetQuery.h>
#include <Parsers/ASTSelectWithUnionQuery.h>
#include <Parsers/ASTSubquery.h>
#include <Parsers/ASTTablesInSelectQuery.h>
#include <Parsers/ASTSetQuery.h>
#include <Parsers/stripQuerySettings.h>
#include <Parsers/ASTWithAlias.h>

#include <Common/ProfileEvents.h>
#include <Access/Common/RowPolicyDefs.h>
#include <Access/EnabledRowPolicies.h>
#include <Parsers/makeASTForLogicalFunction.h>
#include <Parsers/ExpressionListParsers.h>
#include <Parsers/parseQuery.h>

#include <algorithm>
#include <memory>
Expand All @@ -75,6 +82,10 @@ namespace DB
namespace Setting
{
extern const SettingsBool allow_experimental_analyzer;
extern const SettingsMap additional_table_filters;
extern const SettingsUInt64 max_parser_backtracks;
extern const SettingsUInt64 max_parser_depth;
extern const SettingsUInt64 max_query_size;
extern const SettingsBool async_query_sending_for_remote;
extern const SettingsBool async_socket_for_remote;
extern const SettingsBool skip_unavailable_shards;
Expand Down Expand Up @@ -648,6 +659,102 @@ void IStorageCluster::readFromRemoteInitiator(
query_plan, column_names, new_storage_snapshot, modified_query_info, storage_and_context.context, processed_stage, max_block_size, num_streams);
}

namespace
{

ContextPtr applyRowPolicyToQueryToSend(ASTPtr & query_to_send, const StorageID & storage_id, const ContextPtr & context)
{
if (!storage_id.hasDatabase())
return context;

const auto row_policy_filter
= context->getRowPolicyFilter(storage_id.getDatabaseName(), storage_id.getTableName(), RowPolicyFilterType::SELECT_FILTER);
if (!row_policy_filter || row_policy_filter->isAlwaysTrue())
return context;

query_to_send = query_to_send->clone();

auto * select_query = query_to_send->as<ASTSelectQuery>();
if (!select_query)
{
if (auto * union_query = query_to_send->as<ASTSelectWithUnionQuery>();
union_query && union_query->list_of_selects && union_query->list_of_selects->children.size() == 1)
{
select_query = union_query->list_of_selects->children[0]->as<ASTSelectQuery>();
}
}

if (!select_query || select_query->hasJoin())
throw Exception(
ErrorCodes::NOT_IMPLEMENTED,
"Row policies on {} cannot be applied to a query sent to cluster nodes that is not a single SELECT without JOIN",
storage_id.getNameForLogs());

auto * table_function = getTableExpression(*select_query, 0)->table_function.get();
auto * table_function_with_alias = dynamic_cast<ASTWithAlias *>(table_function);
if (!table_function_with_alias)
throw Exception(
ErrorCodes::LOGICAL_ERROR,
"Expected a table function in the query sent to cluster nodes for {}, got '{}'",
storage_id.getNameForLogs(),
query_to_send->formatForErrorMessage());

auto table_alias = table_function_with_alias->tryGetAlias();
if (table_alias.empty())
{
table_alias = "__row_policy_table";
table_function_with_alias->setAlias(table_alias);
}

const auto * function = table_function->as<ASTFunction>();
const auto worker_table_name = fmt::format("_table_function.{}", function ? function->name : String{});

auto new_settings = Settings{context->getSettingsRef()};
auto conditions = ASTs{row_policy_filter->expression->clone()};
auto filters = Map{};
filters.push_back(Tuple{table_alias, String{}});
for (const auto & filter : new_settings[Setting::additional_table_filters].value)
{
const auto & tuple = filter.safeGet<Tuple>();
const auto & key = tuple.at(0).safeGet<String>();
if (key != table_alias && key != worker_table_name)
{
filters.push_back(filter);
continue;
}

const auto & client_filter = tuple.at(1).safeGet<String>();
auto parser = ParserExpression{};
conditions.push_back(parseQuery(
parser,
client_filter.data(),
client_filter.data() + client_filter.size(),
"additional filter",
new_settings[Setting::max_query_size],
new_settings[Setting::max_parser_depth],
new_settings[Setting::max_parser_backtracks]));
}
filters.front() = Tuple{table_alias, makeASTForLogicalAnd(std::move(conditions))->formatWithSecretsOneLine()};
new_settings[Setting::additional_table_filters] = filters;

if (auto * settings_ast = select_query->settings() ? select_query->settings()->as<ASTSetQuery>() : nullptr)
{
auto has_assignment = false;
while (settings_ast->changes.removeSetting("additional_table_filters"))
has_assignment = true;
if (std::erase(settings_ast->default_settings, "additional_table_filters"))
has_assignment = true;
if (has_assignment)
settings_ast->changes.setSetting("additional_table_filters", Field{filters});
}

auto new_context = Context::createCopy(context);
new_context->setSettings(new_settings);
return new_context;
}

}

/// The code executes on initiator
void IStorageCluster::read(
QueryPlan & query_plan,
Expand Down Expand Up @@ -782,6 +889,8 @@ void IStorageCluster::read(
updateQueryToSendIfNeeded(
query_to_send, storage_snapshot, context, /*make_cluster_function=*/true, /*add_structure_and_format=*/true);

const auto worker_context = applyRowPolicyToQueryToSend(query_to_send, getStorageID(), context);

/// In case the current node is not supposed to initiate the clustered query
/// Sends this query to a remote initiator using the `remote` table function
if (settings[Setting::object_storage_remote_initiator])
Expand All @@ -805,7 +914,7 @@ void IStorageCluster::read(
column_names,
storage_snapshot,
query_info,
context,
worker_context,
processed_stage,
max_block_size,
num_streams,
Expand Down Expand Up @@ -846,7 +955,7 @@ void IStorageCluster::read(
column_names,
query_info,
storage_snapshot,
context,
worker_context,
sample_block,
std::move(this_ptr),
std::move(query_to_send),
Expand Down Expand Up @@ -1455,6 +1564,8 @@ ContextPtr ReadFromCluster::updateSettings(const Settings & settings)

auto new_context = Context::createCopy(context);
new_context->setSettings(new_settings);
if (!cluster->getSecret().empty())
new_context->setInitialUserName({});
return new_context;
}

Expand Down
13 changes: 13 additions & 0 deletions tests/integration/test_database_iceberg/configs/cluster_swarm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<clickhouse>
<remote_servers>
<cluster_swarm>
<secret>swarm_secret</secret>
<shard>
<replica>
<host>node2</host>
<port>9000</port>
</replica>
</shard>
</cluster_swarm>
</remote_servers>
</clickhouse>
Loading
Loading