Skip to content
Merged
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
35 changes: 31 additions & 4 deletions src/Planner/PlannerJoinTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,8 @@ void updatePrewhereOutputsIfNeeded(SelectQueryInfo & table_expression_query_info
std::optional<FilterDAGInfo> buildRowPolicyFilterIfNeeded(const StoragePtr & storage,
SelectQueryInfo & table_expression_query_info,
PlannerContextPtr & planner_context,
std::set<std::string> & used_row_policies)
std::set<std::string> & used_row_policies,
NameSet required_names_without_filter = {})
{
const auto & query_context = planner_context->getQueryContext();

Expand All @@ -579,7 +580,11 @@ std::optional<FilterDAGInfo> buildRowPolicyFilterIfNeeded(const StoragePtr & sto
used_row_policies.emplace(std::move(name));
}

return buildFilterInfo(row_policy_filter->expression, table_expression_query_info.table_expression, planner_context);
return buildFilterInfo(
row_policy_filter->expression,
table_expression_query_info.table_expression,
planner_context,
std::move(required_names_without_filter));
}

std::optional<FilterDAGInfo> buildCustomKeyFilterIfNeeded(const StoragePtr & storage,
Expand Down Expand Up @@ -1012,8 +1017,30 @@ JoinTreeQueryPlan buildQueryPlanForTableExpression(QueryTreeNodePtr table_expres

updatePrewhereOutputsIfNeeded(table_expression_query_info, table_expression_data.getColumnNames(), storage_snapshot);

auto row_policy_filter_info
= buildRowPolicyFilterIfNeeded(storage, table_expression_query_info, planner_context, used_row_policies);
/// The row-level filter runs inside the reading step and must keep any column a later
/// additional_table_filters step (applied on top) still needs, else that column is dropped
/// from the block (#111077). Mirror the columns_needed_by_other_filters pre-collect used for
/// PREWHERE above.
NameSet row_policy_required_names;
if (table_expression_query_info.additional_filter_ast)
{
if (auto additional_filters_info_temp
= buildAdditionalFiltersIfNeeded(table_expression_query_info, prewhere_info, planner_context))
{
for (const auto * input : additional_filters_info_temp->actions.getInputs())
row_policy_required_names.insert(input->result_name);
}
/// buildFilterInfo treats an empty set as "keep all table columns", so seed it with the
/// columns the query already needs before adding the additional-filter columns.
if (!row_policy_required_names.empty())
{
const auto & current_column_names = table_expression_data.getColumnNames();
row_policy_required_names.insert(current_column_names.begin(), current_column_names.end());
}
}

auto row_policy_filter_info = buildRowPolicyFilterIfNeeded(
storage, table_expression_query_info, planner_context, used_row_policies, std::move(row_policy_required_names));
if (row_policy_filter_info)
{
table_expression_data.setRowLevelFilterActions(row_policy_filter_info->actions.clone());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
2
a 3
b 4
2
1
1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
-- https://github.com/ClickHouse/ClickHouse/issues/111077
-- A row policy and additional_table_filters referencing the same unprojected column
-- must both apply: previously the row-level filter (applied inside the reading step)
-- dropped the shared column from the block, so the additional filter failed with
-- NOT_FOUND_COLUMN_IN_BLOCK.

DROP TABLE IF EXISTS t_atf_rp;
DROP ROW POLICY IF EXISTS rp_atf ON t_atf_rp;

CREATE TABLE t_atf_rp (tenant String, k UInt32) ENGINE = MergeTree ORDER BY tuple();
INSERT INTO t_atf_rp VALUES ('x', 1), ('y', 2), ('a', 3), ('b', 4);
CREATE ROW POLICY rp_atf ON t_atf_rp USING tenant != 'x' TO ALL;

-- Shared column `tenant` (policy) also referenced by additional_table_filters.
-- 'x' blocked by policy, 'y' blocked by the additional filter, 'a'/'b' pass.
SELECT count() FROM t_atf_rp WHERE k > 0
SETTINGS additional_table_filters = {'t_atf_rp': 'tenant != ''y'''};

SELECT tenant, k FROM t_atf_rp WHERE k > 0 ORDER BY k
SETTINGS additional_table_filters = {'t_atf_rp': 'tenant != ''y'''};

-- Not specific to automatic PREWHERE conversion.
SELECT count() FROM t_atf_rp WHERE k > 0
SETTINGS additional_table_filters = {'t_atf_rp': 'tenant != ''y'''}, optimize_move_to_prewhere = 0;

-- Additional filter mixing the shared policy column with another unprojected column.
DROP TABLE IF EXISTS t_atf_rp2;
DROP ROW POLICY IF EXISTS rp_atf2 ON t_atf_rp2;
CREATE TABLE t_atf_rp2 (tenant String, region String, k UInt32) ENGINE = MergeTree ORDER BY tuple();
INSERT INTO t_atf_rp2 VALUES ('x', 'eu', 1), ('a', 'eu', 2), ('a', 'us', 3), ('y', 'eu', 4);
CREATE ROW POLICY rp_atf2 ON t_atf_rp2 USING tenant != 'x' TO ALL;
SELECT count() FROM t_atf_rp2 WHERE k > 0
SETTINGS additional_table_filters = {'t_atf_rp2': 'tenant != ''y'' AND region = ''eu'''};

-- Shared column under type wrappers: the row-level filter must still preserve it.
DROP TABLE IF EXISTS t_atf_rp_lc;
DROP ROW POLICY IF EXISTS rp_atf_lc ON t_atf_rp_lc;
CREATE TABLE t_atf_rp_lc (tenant LowCardinality(Nullable(String)), k UInt32) ENGINE = MergeTree ORDER BY tuple();
INSERT INTO t_atf_rp_lc VALUES ('x', 1), ('y', 2), ('a', 3);
CREATE ROW POLICY rp_atf_lc ON t_atf_rp_lc USING tenant != 'x' TO ALL;
SELECT count() FROM t_atf_rp_lc WHERE k > 0
SETTINGS additional_table_filters = {'t_atf_rp_lc': 'tenant != ''y'''};

DROP ROW POLICY rp_atf ON t_atf_rp;
DROP ROW POLICY rp_atf2 ON t_atf_rp2;
DROP ROW POLICY rp_atf_lc ON t_atf_rp_lc;
DROP TABLE t_atf_rp;
DROP TABLE t_atf_rp2;
DROP TABLE t_atf_rp_lc;
Loading