From 8efdb5c7542611b46df9c277264ae64636705198 Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Fri, 31 Jul 2026 14:55:32 +0000 Subject: [PATCH] Merge pull request #111099 from groeneai/groeneai/fix-111077-row-policy-additional-table-filter-shared-column Fix NOT_FOUND_COLUMN_IN_BLOCK with row policy and additional_table_filters on a shared column --- src/Planner/PlannerJoinTree.cpp | 35 +++++++++++-- ...ional_table_filter_shared_column.reference | 6 +++ ..._additional_table_filter_shared_column.sql | 49 +++++++++++++++++++ 3 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 tests/queries/0_stateless/04613_row_policy_additional_table_filter_shared_column.reference create mode 100644 tests/queries/0_stateless/04613_row_policy_additional_table_filter_shared_column.sql diff --git a/src/Planner/PlannerJoinTree.cpp b/src/Planner/PlannerJoinTree.cpp index e400bfe7edbc..6ad269f2ed21 100644 --- a/src/Planner/PlannerJoinTree.cpp +++ b/src/Planner/PlannerJoinTree.cpp @@ -565,7 +565,8 @@ void updatePrewhereOutputsIfNeeded(SelectQueryInfo & table_expression_query_info std::optional buildRowPolicyFilterIfNeeded(const StoragePtr & storage, SelectQueryInfo & table_expression_query_info, PlannerContextPtr & planner_context, - std::set & used_row_policies) + std::set & used_row_policies, + NameSet required_names_without_filter = {}) { const auto & query_context = planner_context->getQueryContext(); @@ -579,7 +580,11 @@ std::optional 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 buildCustomKeyFilterIfNeeded(const StoragePtr & storage, @@ -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()); diff --git a/tests/queries/0_stateless/04613_row_policy_additional_table_filter_shared_column.reference b/tests/queries/0_stateless/04613_row_policy_additional_table_filter_shared_column.reference new file mode 100644 index 000000000000..a7634830c732 --- /dev/null +++ b/tests/queries/0_stateless/04613_row_policy_additional_table_filter_shared_column.reference @@ -0,0 +1,6 @@ +2 +a 3 +b 4 +2 +1 +1 diff --git a/tests/queries/0_stateless/04613_row_policy_additional_table_filter_shared_column.sql b/tests/queries/0_stateless/04613_row_policy_additional_table_filter_shared_column.sql new file mode 100644 index 000000000000..b9cbc334f133 --- /dev/null +++ b/tests/queries/0_stateless/04613_row_policy_additional_table_filter_shared_column.sql @@ -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;