Skip to content

Fix to populate row policies for all select query types in query_log - #101044

Merged
npakeer merged 8 commits into
ClickHouse:masterfrom
npakeer:row_policies_query_log_fix
Apr 10, 2026
Merged

npakeer merged 8 commits into
ClickHouse:masterfrom
npakeer:row_policies_query_log_fix

Conversation

@npakeer

@npakeer npakeer commented Mar 28, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Row policies were not populated for views, sub queries, insert select etc..to query_log. Even though row policies were used in the query planning, they were not propagated from sub palnner to the parent planner for logging. This change fixes this bug by keeping row_policies(only for logging) inside QueryAccessInfo, were planner and sub planners can access and populate.

Resolves #99456

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.4.1.816

@clickhouse-gh

clickhouse-gh Bot commented Mar 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f706dbe]

Summary: ✅


AI Review

Summary

This PR moves row-policy collection into QueryAccessInfo so used_row_policies is filled consistently for nested/planner-driven queries and removes now-redundant interpreter-level extendQueryLogElemImpl hooks. The implementation looks sound, but test coverage is incomplete for set-operation query paths touched by this refactor.

Findings

⚠️ Majors

  • [tests/queries/0_stateless/04061_row_policy_query_log_subqueries.sql:23] Coverage currently validates view, nested subquery, and INSERT SELECT, but does not validate UNION / INTERSECT / EXCEPT, even though this PR removes logging hooks from InterpreterSelectWithUnionQuery and InterpreterSelectIntersectExceptQuery. This leaves changed behavior in those paths unprotected by regression tests.
    • Suggested fix: extend this test (or add a companion test) with set-operation queries and assert system.query_log.used_row_policies includes expected policies.
Tests
  • ⚠️ Add explicit regression checks for UNION / INTERSECT / EXCEPT queries to cover the interpreter paths modified by this PR.
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    • Add regression coverage for UNION / INTERSECT / EXCEPT and verify used_row_policies is logged correctly for those query forms.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 28, 2026
@npakeer
npakeer marked this pull request as ready for review March 30, 2026 14:18
Comment thread src/Interpreters/Context.cpp Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw Exception(ErrorCodes::LOGICAL_ERROR, "Global context cannot have view access info");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I will make this change.

Comment thread src/Interpreters/Context.cpp Outdated
void Context::addUsedRowPolicy(const String & policy_name)
{
if (isGlobalContext())
throw Exception(ErrorCodes::LOGICAL_ERROR, "Global context cannot have query access info");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw Exception(ErrorCodes::LOGICAL_ERROR, "Global context cannot have query access info");
throw Exception(ErrorCodes::LOGICAL_ERROR, "Global context cannot have used row policies info");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made these change in new commits being pushed.

}

void InterpreterSelectIntersectExceptQuery::extendQueryLogElemImpl(QueryLogElement & elem, const ASTPtr & /*ast*/, ContextPtr /*context_*/) const
void InterpreterSelectIntersectExceptQuery::extendQueryLogElemImpl(QueryLogElement & /*elem*/, const ASTPtr & /*ast*/, ContextPtr /*context_*/) const

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This overriden method now has an empty body. Just remove it then.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense. I think the base class IInterpreter.h seems to have empty function which should handle calls here for select queries.

I pushed the changes.

@npakeer

npakeer commented Apr 3, 2026

Copy link
Copy Markdown
Contributor Author

@nikitamikhaylov Thanks for the review! I pushed all the changes. The CI seems okay except for the stress test which has been failing for all PRs. Let me know if you can approve and whether I can go ahead and merge the PR.

@alexey-milovidov

Copy link
Copy Markdown
Member

The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix.

@alexey-milovidov

Copy link
Copy Markdown
Member

The failures of "Flaky check" in "functions_bad_arguments" will be fixed by #101994.

@alexey-milovidov

Copy link
Copy Markdown
Member

The MSan stress test failure (MemorySanitizer: use-of-uninitialized-value, STID 4179-5154 or 4148-3044) is a known pre-existing issue unrelated to this PR. Fix: #102158

{
auto name = row_policy->getFullName().toString();
if (query_context->hasQueryContext())
query_context->getQueryContext()->addUsedRowPolicy(name);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a regression case that covers multiple source tables with different row policies in one query (e.g. JOIN), to confirm used_row_policies is accumulated from all table-expression filters and not only the first encountered source.

SELECT 'Nested subquery';
SELECT * FROM (SELECT * FROM (SELECT * FROM t_row_policy)) ORDER BY x;

-- Test: INSERT SELECT

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding coverage for views, nested subqueries, and INSERT SELECT. Since this PR also removes extendQueryLogElemImpl from InterpreterSelectWithUnionQuery and InterpreterSelectIntersectExceptQuery, please add a regression that verifies used_row_policies for UNION/INTERSECT/EXCEPT queries too.

Without this, the exact paths changed in those interpreters are still untested, so a future regression in set-operation logging could slip through.

@clickhouse-gh

clickhouse-gh Bot commented Apr 10, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.00% 84.00% +0.00%
Functions 90.90% 90.90% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 88.89% (64/72) | lost baseline coverage: 19 line(s) · Uncovered code

Full report · Diff report

@npakeer
npakeer added this pull request to the merge queue Apr 10, 2026
Merged via the queue into ClickHouse:master with commit c72dbb4 Apr 10, 2026
164 checks passed
@npakeer
npakeer deleted the row_policies_query_log_fix branch April 10, 2026 23:16
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 10, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi — this PR may need backporting to 26.3 (LTS), 26.2, 26.1, 25.8 (LTS), but no backport label was found.

Affected code: InterpreterSelectQuery::extendQueryLogElemImpl, InterpreterSelectQueryAnalyzer::extendQueryLogElemImpl, executeQuery.cpp query log population — core code present in all supported branches.

Why: This fixes a user-visible bug where used_row_policies in system.query_log was empty for views, subqueries, and INSERT SELECT. The affected code (InterpreterSelectQuery::extendQueryLogElemImpl, InterpreterSelectQueryAnalyzer::extendQueryLogElemImpl, executeQuery.cpp logging) has existed since before all supported branches. The fix is safe — it only moves where row policy names are stored during query planning.

If this should be backported, consider adding pr-must-backport or a version-specific label (e.g. v26.3-must-backport). Ignore this if backporting is not applicable.

alexey-milovidov added a commit that referenced this pull request Apr 29, 2026
- Fix typo: `behavoir` → `behavior` (also `prevent` → `prevents`).
- Replace placeholder entry for `#100909` with description of the
  `processAndOptimizeTextIndexFunctions` fix when
  `query_plan_merge_expressions = 0`.
- Replace placeholder entry for `#101230` with description of the
  `S3Queue` stale processing nodes cleanup fix.
- Rewrite the row-policies-in-`query_log` entry (`#101044`) for
  clarity, fixing typos (`sub queries`, `etc..to`, `palnner`,
  `were planner`).

PR: #103729

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

system.query_log.used_row_policies is empty for VIEW and subquery queries

5 participants